Method: Doing::WWID#format_input

Defined in:
lib/doing/wwid/editor.rb

#format_input(input) ⇒ Array

Takes a multi-line string and formats it as an entry

Parameters:

  • input (String)

    The string to parse

Returns:

  • (Array)

    [[String]title, [Note]note]

Raises:

  • (EmptyInput)


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
# File 'lib/doing/wwid/editor.rb', line 60

def format_input(input)
  raise EmptyInput, 'No content in entry' if input.nil? || input.strip.empty?

  input_lines = input.split(/[\n\r]+/).delete_if(&:ignore?)
  title = input_lines[0]&.strip
  raise EmptyInput, 'No content in first line' if title.nil? || title.strip.empty?

  date = nil
  iso_rx = /\d{4}-\d\d-\d\d \d\d:\d\d/
  date_rx = /^(?:\s*- )?(?<date>.*?) \| (?=\S)/

  raise EmptyInput, 'No content' if title.sub(/^.*?\| */, '').strip.empty?

  title.expand_date_tags(Doing.setting('date_tags'))

  if title =~ date_rx
    m = title.match(date_rx)
    d = m['date']
    date = if d =~ iso_rx
             Time.parse(d)
           else
             d.chronify(guess: :begin)
           end
    title.sub!(date_rx, '').strip!
  end

  note = Note.new
  note.add(input_lines[1..-1]) if input_lines.length > 1
  # If title line ends in a parenthetical, use that as the note
  if note.empty? && title =~ /\s+\(.*?\)$/
    title.sub!(/\s+\((?<note>.*?)\)$/) do
      m = Regexp.last_match
      note.add(m['note'])
      ''
    end
  end

  note.strip_lines!
  note.compress

  [date, title, note]
end