Class: Doing::Items

Inherits:
Array
  • Object
show all
Defined in:
lib/doing/wwidfile.rb

Instance Method Summary collapse

Methods inherited from Array

#highlight_tags, #log_tags, #to_tags

Instance Method Details

#add_section(title) ⇒ Object

Adds a section.

Parameters:

  • title (String)

    The new section title



56
57
58
59
60
61
62
63
64
# File 'lib/doing/wwidfile.rb', line 56

def add_section(title)
  if section_titles.include?(title.cap_first)
    Doing.logger.debug('Skipped': 'Section already exists')
    return
  end

  @sections << { original: "#{title}:", title: title }
  Doing.logger.info('Added section:', %("#{title.cap_first}"))
end

#from(path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/doing/wwidfile.rb', line 5

def from(path)
  return [] unless path
  path = File.expand_path(path)

  if File.exist?(path) && File.file?(path) && File.stat(path).size.positive?
    input = IO.read(File.expand_path(input))
    input = input.force_encoding('utf-8') if input.respond_to? :force_encoding
  end

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

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

    if line =~ /^(\S[\S ]+):\s*(@\S+\s*)*$/
      section = Regexp.last_match(1)
      @sections << { original: line, title: section }
      current = 0
    elsif line =~ /^\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
    elsif current.zero?
      # if content[section][:items].length - 1 == current
      @other_content_top.push(line)
    elsif line =~ /^\S/
      @other_content_bottom.push(line)
    else
      prev_item = @items[current - 1]
      prev_item.note = Note.new unless prev_item.note

      prev_item.note.add(line)
      # end
    end
  end
  Hooks.trigger :post_read, self
end

#guess_section(frag, guessed: false, suggest: false) ⇒ Object

Attempt to match a string with an existing section

Parameters:

  • frag (String)

    The user-provided string

  • guessed (Boolean) (defaults to: false)

    already guessed and failed



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
# File 'lib/doing/wwidfile.rb', line 72

def guess_section(frag, guessed: false, suggest: false)
  return 'All' if frag =~ /^all$/i
  frag ||= wwid.config['current_section']

  @sections.each { |sect| return sect[:title].cap_first if frag.downcase == sect[:title].downcase }

  section = false
  re = frag.split('').join('.*?')
  sections.each do |sect|
    next unless sect =~ /#{re}/i

    Doing.logger.debug('Section match:', %(Assuming "#{sect}" from "#{frag}"))
    section = sect
    break
  end

  return section if suggest

  unless section || guessed
    alt = WWID.guess_view(frag, guessed: true, suggest: true)
    if alt
      meant_view = WWID.yn("Did you mean `doing view #{alt}`?", default_response: 'n')
      raise Errors::InvalidSection, "Run again with `doing view #{alt}`" if meant_view
    end

    res = WWID.yn("Section #{frag} not found, create it", default_response: 'n')

    if res
      add_section(frag.cap_first)
      WWID.write(@doing_file)
      return frag.cap_first
    end

    raise Errors::InvalidSection, "Unknown section: #{frag}"
  end
  section ? section.cap_first : guessed
end

#section_items(section) ⇒ Object



110
111
112
113
114
115
# File 'lib/doing/wwidfile.rb', line 110

def section_items(section)
  section = guess_section(section)
  return @items if section =~ /all/i

  @items.filter { |i| i.section == section }
end

#section_titlesObject



47
48
49
# File 'lib/doing/wwidfile.rb', line 47

def section_titles
  @sections.map { |s| s[:title] }
end