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
101
102
103
104
105
106
107
108
109
110
|
# 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 = []
updated = 0
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, item.note, item.id)
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
if wwid.content.find_id(new_item.id)
old_index = wwid.content.index_for_id(new_item.id)
old_item = wwid.content[old_index].clone
wwid.content[old_index] = new_item
Hooks.trigger :post_entry_updated, self, new_item, old_item
updated += 1
else
imported.push(new_item) if is_match
end
end
dups = new_items.count - imported.count
Doing.logger.info('Skipped:', %(#{dups} duplicate items)) if dups.positive?
imported = wwid.dedup(imported, no_overlap: options[:no_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('Updated:', %(#{updated} items))
Doing.logger.info('Imported:', "#{imported.count} items")
end
|