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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/doing/plugins/export/dayone_export.rb', line 46
def self.render(wwid, items, variables: {})
return unless items.good?
config = Doing.settings
opt = variables[:options]
trigger = opt[:output]
digest = case trigger
when /-days?$/
:day
when /-entries$/
:entries
else
:digest
end
all_items = []
days = {}
flagged = false
tags = []
items.each do |i|
day_flagged = false
date_key = i.date.strftime('%Y-%m-%d')
if String.method_defined? :force_encoding
title = i.title.force_encoding('utf-8').link_urls(format: :markdown)
note = i.note.map { |line| line.force_encoding('utf-8').strip.link_urls(format: :markdown) } if i.note
else
title = i.title.link_urls(format: :markdown)
note = i.note.map { |line| line.strip.link_urls(format: :markdown) } if i.note
end
title = "#{title} @section(#{i.section})" unless variables[:is_single]
tags.concat(i.tag_array).sort!.uniq!
flagged = day_flagged = true if i.tags?(config['marker_tag'])
interval = wwid.get_interval(i, record: true) if i.title =~ /@done\((\d{4}-\d\d-\d\d \d\d:\d\d.*?)\)/ && opt[:times]
interval ||= false
human_time = false
if interval
d, h, m = wwid.get_interval(i, formatted: false).format_time
human_times = []
human_times << format('%<d>d day%<p>s', d: d, p: d == 1 ? '' : 's') if d > 0
human_times << format('%<h>d hour%<p>s', h: h, p: h == 1 ? '' : 's') if h > 0
human_times << format('%<m>d minute%<p>s', m: m, p: m == 1 ? '' : 's') if m > 0
human_time = human_times.join(', ')
end
done = i.tags?('done') ? ' ' : ' '
item = {
date_object: i.date,
date: i.date.strftime('%a %-I:%M%p'),
shortdate: i.date.relative_date,
done: done,
note: note,
section: i.section,
time: interval,
human_time: human_time,
title: title.strip,
starred: day_flagged,
tags: i.tag_array
}
all_items << item
if days.key?(date_key)
days[date_key][:starred] = true if day_flagged
days[date_key][:tags] = days[date_key][:tags].concat(i.tag_array).sort.uniq
days[date_key][:entries].push(item)
else
days[date_key] ||= { tags: [], entries: [], starred: false }
days[date_key][:starred] = true if day_flagged
days[date_key][:tags] = days[date_key][:tags].concat(i.tag_array).sort.uniq
days[date_key][:entries].push(item)
end
end
template = if config['export_templates']['dayone'] && File.exist?(File.expand_path(config['export_templates']['dayone']))
IO.read(File.expand_path(config['export_templates']['dayone']))
else
self.template('dayone')
end
totals = opt[:totals] ? wwid.tag_times(format: :markdown, sort_by: opt[:sort_tags], sort_order: opt[:tag_order]) : ''
case digest
when :day
days.each do |k, hsh|
title = "#{k}: #{variables[:page_title]}"
to_dayone(template: template,
title: title,
items: hsh[:entries],
totals: '',
date: Time.parse(k),
tags: tags,
starred: hsh[:starred])
end
when :entries
entry_template = if config['export_templates']['dayone_entry'] && File.exist?(File.expand_path(config['export_templates']['dayone_entry']))
IO.read(File.expand_path(config['export_templates']['dayone_entry']))
else
self.template('dayone-entry')
end
all_items.each do |item|
to_dayone(template: entry_template,
title: '',
items: [item],
totals: '',
date: item[:date_object],
tags: item[:tags],
starred: item[:starred])
end
else
to_dayone(template: template,
title: variables[:page_title],
items: all_items,
totals: totals,
date: Time.now,
tags: tags,
starred: flagged)
end
@out = ''
end
|