Class: Doing::DoingImport

Inherits:
Object show all
Includes:
Util
Defined in:
lib/doing/plugins/import/doing_import.rb

Class Method Summary collapse

Methods included from Util

#args_for_editor, #deep_merge_hashes, #deep_merge_hashes!, #default_editor, #duplicable?, #duplicate_frozen_values, #editor_with_args, #exec_available, #find_default_editor, #first_available_exec, #mergable?, #merge_default_proc, #merge_values, #safe_load_file, #user_home, #write_to_file

Class Method Details

.duplicate?(item) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/doing/plugins/import/doing_import.rb', line 102

def self.duplicate?(item)
  @old_items.each do |oi|
    return true if item.equal?(oi)
  end

  false
end

.import(wwid, path, options: {}) ⇒ Object

Imports a Doing file

Parameters:

  • wwid (WWID)

    WWID object

  • path (String)

    Path to Doing file

  • options (Hash) (defaults to: {})

    Additional Options

Returns:

  • Nothing



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/doing/plugins/import/doing_import.rb', line 26

def self.import(wwid, path, options: {})
  exit_now! 'Path to Doing file required' if path.nil?

  exit_now! 'File not found' unless File.exist?(File.expand_path(path))

  options[:no_overlap] ||= false
  options[:autotag] ||= Doing.auto_tag

  tags = options[:tag] ? options[:tag].split(/[ ,]+/).map { |t| t.sub(/^@?/, '') } : []
  prefix = options[:prefix] || ''

  @old_items = wwid.content.dup

  new_items = read_doing_file(path)

  total = new_items.count

  options[:count] = 0
  new_items = wwid.filter_items(new_items, opt: options)

  skipped = total - new_items.count
  Doing.logger.debug('Skipped:', %(#{skipped} items that didn't match filter criteria)) if skipped.positive?

  imported = []

  new_items.each do |item|
    next if duplicate?(item)

    title = "#{prefix} #{item.title}"
    tags.each do |tag|
      if title =~ /\b#{tag}\b/i
        title.sub!(/\b#{tag}\b/i, "@#{tag}")
      else
        title += " @#{tag}"
      end
    end
    title = wwid.autotag(title) if options[:autotag]
    title.gsub!(/ +/, ' ')
    title.strip!
    section = options[:section] || item.section
    section ||= Doing.setting('current_section')

    new_item = Item.new(item.date, title, section)
    new_item.note = item.note

    is_match = true

    if options[:search]
      is_match = new_item.search(options[:search], case_type: options[:case], negate: options[:not])
    end

    if is_match && options[:date_filter]
      is_match = new_item.date > options[:date_filter][0] && new_item.date < options[:date_filter][1]
      is_match = options[:not] ? !is_match : is_match
    end

    imported.push(new_item) if is_match
  end

  dups = new_items.count - imported.count
  Doing.logger.info('Skipped:', %(#{dups} duplicate items)) if dups.positive?

  imported = wwid.dedup(imported, no_overlap: !options[:overlap])
  overlaps = new_items.count - imported.count - dups
  Doing.logger.debug('Skipped:', "#{overlaps} items with overlapping times") if overlaps.positive?

  imported.each do |item|
    wwid.content.add_section(item.section) unless wwid.content.section?(item.section)
    Hooks.trigger :pre_entry_add, self, item
    wwid.content.push(item)
    Hooks.trigger :post_entry_added, self, item
  end

  Doing.logger.info('Imported:', "#{imported.count} items")
end

.read_doing_file(path) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/doing/plugins/import/doing_import.rb', line 110

def self.read_doing_file(path)
  doing_file = File.expand_path(path)

  return nil unless File.exist?(doing_file) && File.file?(doing_file) && File.stat(doing_file).size.positive?

  input = IO.read(doing_file)
  input = input.force_encoding('utf-8') if input.respond_to? :force_encoding

  lines = input.split(/[\n\r]/)
  current = 0

  items = []
  section = ''

  lines.each do |line|
    next if line =~ /^\s*$/

    case line
    when /^(\S[\S ]+):(\s+@[\w\-_.]+(?= |$))*\s*$/
      section = Regexp.last_match(1)
      current = 0
    when /^\s*- (\d{4}-\d\d-\d\d \d\d:\d\d) \| (.*)/
      date = Regexp.last_match(1).strip
      title = Regexp.last_match(2).strip
      item = Item.new(date, title, section)
      items.push(item)
      current += 1
    when /^\S/
      next
    else
      next if current.zero?

      prev_item = items[current - 1]
      prev_item.note = Note.new unless prev_item.note

      prev_item.note.add(line)
      # end
    end
  end

  items
end

.settingsObject



11
12
13
14
15
# File 'lib/doing/plugins/import/doing_import.rb', line 11

def self.settings
  {
    trigger: 'doing'
  }
end