Method: Doing::WWID#init_doing_file

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

#init_doing_file(path = nil) ⇒ Object

Initializes the doing file.

Parameters:

  • path (String) (defaults to: nil)

    Override path to a doing file, optional



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

def init_doing_file(path = nil)
  @doing_file =  File.expand_path(Doing.setting('doing_file'))

  if path.nil?
    create(@doing_file) unless File.exist?(@doing_file)
    input = IO.read(@doing_file)
    input = input.force_encoding('utf-8') if input.respond_to? :force_encoding
    logger.debug('Read:', "read file #{@doing_file}")
  elsif File.exist?(File.expand_path(path)) && File.file?(File.expand_path(path)) && File.stat(File.expand_path(path)).size.positive?
    @doing_file = File.expand_path(path)
    input = IO.read(File.expand_path(path))
    input = input.force_encoding('utf-8') if input.respond_to? :force_encoding
    logger.debug('Read:', "read file #{File.expand_path(path)}")
  elsif path.length < 256
    @doing_file = File.expand_path(path)
    create(path)
    input = IO.read(File.expand_path(path))
    input = input.force_encoding('utf-8') if input.respond_to? :force_encoding
    logger.debug('Read:', "read file #{File.expand_path(path)}")
  end

  @other_content_top = []
  @other_content_bottom = []

  section = nil
  lines = input.split(/[\n\r]/)

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

    if line =~ /^(\S[\S ]+):\s*(@[\w\-_.]+\s*)*$/
      section = Regexp.last_match(1)
      @content.add_section(Section.new(section, original: line), log: false)
    elsif line =~ /^\s*- (\d{4}-\d\d-\d\d \d\d:\d\d) \| (.*?)(?: +<([a-z0-9]{32})>)? *$/
      if section.nil?
        section = 'Uncategorized'
        @content.add_section(Section.new(section, original: 'Uncategorized:'), log: false)
      end

      date = Regexp.last_match(1).strip
      title = Regexp.last_match(2).strip
      id = Regexp.last_match(3) || nil
      item = Item.new(date, title, section, [], id)
      @content.push(item)
    elsif @content.count.zero?
      # if content[section].items.length - 1 == current
      @other_content_top.push(line)
    elsif line =~ /^\S/
      @other_content_bottom.push(line)
    else
      prev_item = @content.last
      prev_item.note = Note.new unless prev_item.note

      prev_item.note.add(line)
      # end
    end
  end

  Hooks.trigger :post_read, self
  @initial_content = @content.clone
end