Class: Doing::TemplateExport

Inherits:
Object
  • Object
show all
Includes:
Color, Util
Defined in:
lib/doing/plugins/export/template_export.rb

Overview

Template Export

Constant Summary

Constants included from Color

Color::ATTRIBUTES, Color::ATTRIBUTE_NAMES, Color::ESCAPE_REGEX

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

Methods included from Color

#attributes, coloring?, #rgb, #support?, template, #uncolor

Class Method Details

.render(wwid, items, variables: {}) ⇒ Object



19
20
21
22
23
24
25
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
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
# File 'lib/doing/plugins/export/template_export.rb', line 19

def self.render(wwid, items, variables: {})
  Doing.logger.benchmark(:template_render, :start)
  return if items.nil?

  opt = variables[:options]

  out = ''
  items.each do |item|
    if opt[:highlight] && item.title =~ /@#{Doing.setting('marker_tag')}\b/i
      flag = Doing::Color.send(Doing.setting('marker_color'))
      reset = Doing::Color.reset + Doing::Color.default
    else
      flag = ''
      reset = ''
    end

    placeholders = {}

    if !item.note.empty? && Doing.setting('include_notes')
      note = item.note.map(&:strip).delete_if(&:empty?)
      note.map! { |line| "#{line.sub(/^\t*/, '')}  " }

      if opt[:wrap_width]&.positive?
        width = opt[:wrap_width]
        note.map! do |line|
          line.simple_wrap(width)
          # line.chomp.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
        end
        note.delete_if(&:empty?)
      end
    else
      note = []
    end

    placeholders['id'] = item.id

    placeholders['tags'] = item.tags

    placeholders['date'] = item.date.strftime(opt[:format])

    interval = wwid.get_interval(item, record: true, formatted: false) if opt[:times]
    if interval
      interval = case opt[:interval_format].to_sym
                 when :human
                   interval.time_string(format: :hm)
                 when :text
                   interval.time_string(format: :clock)
                 else
                   interval.time_string(format: opt[:interval_format].to_sym)
                 end
    end

    interval ||= ''
    placeholders['interval'] = interval

    duration = item.duration if opt[:duration]
    if duration
      duration = case opt[:interval_format].to_sym
                 when :human
                   duration.time_string(format: :hm)
                 when :text
                   duration.time_string(format: :clock)
                 else
                   duration.time_string(format: opt[:interval_format].to_sym)
                 end
    end
    duration ||= ''
    placeholders['duration'] = duration

    placeholders['shortdate'] = format('%13s', item.date.relative_date)
    placeholders['section'] = item.section || ''
    placeholders['title'] = item.title
    placeholders['note'] = note
    placeholders['idnote'] = note.empty? ? '' : "\n#{note.map { |l| "\t\t#{l.strip}  " }.join("\n")}"
    placeholders['odnote'] = note.empty? ? '' : "\n#{note.map { |l| "#{l.strip}  " }.join("\n")}"

    chompnote = []
    unless note.empty?
      chompnote = note.map do |l|
        l.gsub(/\n+/, ' ').gsub(/(^\s*|\s*$)/, '').gsub(/\s+/, ' ')
      end
    end
    placeholders['chompnote'] = chompnote.join(' ')

    template = opt[:template].dup
    note_rx = /(?i-m)(?x:^([\s\S]*?)
                (%(?:[io]d|(?:\^[\s\S])?
                (?:(?:[ _t]|[^a-z0-9])?\d+)?
                (?:[\s\S][ _t]?)?)?note)
                ([\s\S]*?)$)/
    template.sub!(note_rx, '\1\3\2')
    output = Doing::TemplateString.new(template,
                                       color: flag,
                                       placeholders: placeholders,
                                       reset: reset,
                                       tags_color: opt[:tags_color],
                                       wrap_width: opt[:wrap_width]).colored

    output.gsub!(/(?<!\\)%(\S)?hr(_under)?/) do
      o = ''
      TTY::Screen.columns.to_i.times do
        char = Regexp.last_match(2).nil? ? '-' : '_'
        char = Regexp.last_match(1).nil? ? char : Regexp.last_match(1)
        o += char
      end
      o
    end
    output.gsub!(/(?<!\\)%n/, "\n")
    output.gsub!(/(?<!\\)%t/, "\t")

    output.gsub!(/\\%/, '%')

    output.highlight_search!(opt[:search]) if opt[:output] =~ /^temp/ && opt[:search] && !opt[:not] && opt[:hilite]

    out += "#{output}\n"
  end

  # Doing.logger.debug('Template Export:', "#{items.count} items output to template #{opt[:output]}")
  if opt[:totals]
    out += wwid.tag_times(format: Doing.setting('timer_format').to_sym,
                          sort_by: opt[:sort_tags],
                          sort_order: opt[:tag_order])
  end
  Doing.logger.benchmark(:template_render, :finish)
  out
end

.settingsObject



13
14
15
16
17
# File 'lib/doing/plugins/export/template_export.rb', line 13

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