Class: NA::Todo

Inherits:
Object
  • Object
show all
Defined in:
lib/na/todo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Todo

Returns a new instance of Todo.



7
8
9
# File 'lib/na/todo.rb', line 7

def initialize(options = {})
  @files, @actions, @projects = parse(options)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



5
6
7
# File 'lib/na/todo.rb', line 5

def actions
  @actions
end

#filesObject

Returns the value of attribute files.



5
6
7
# File 'lib/na/todo.rb', line 5

def files
  @files
end

#projectsObject

Returns the value of attribute projects.



5
6
7
# File 'lib/na/todo.rb', line 5

def projects
  @projects
end

Instance Method Details

#parse(options) ⇒ Object

Read a todo file and create a list of actions

Parameters:

  • options

    The options

  • depth (Hash)

    a customizable set of options

  • done (Hash)

    a customizable set of options

  • query (Hash)

    a customizable set of options

  • tag (Hash)

    a customizable set of options

  • search (Hash)

    a customizable set of options

  • negate (Hash)

    a customizable set of options

  • regex (Hash)

    a customizable set of options

  • project (Hash)

    a customizable set of options

  • require_na (Hash)

    a customizable set of options

  • file_path (Hash)

    a customizable set of options



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
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
# File 'lib/na/todo.rb', line 29

def parse(options)
  defaults = {
    depth: 1,
    done: false,
    file_path: nil,
    negate: false,
    project: nil,
    query: nil,
    regex: false,
    require_na: true,
    search: nil,
    search_note: true,
    tag: nil
  }

  settings = defaults.merge(options)

  actions = NA::Actions.new
  required = []
  optional = []
  negated = []
  required_tag = []
  optional_tag = []
  negated_tag = []
  projects = []

  NA.notify("Tags: #{settings[:tag]}", debug:true)
  NA.notify("Search: #{settings[:search]}", debug:true)

  settings[:tag]&.each do |t|
    unless t[:tag].nil?
      if settings[:negate]
        optional_tag.push(t) if t[:negate]
        required_tag.push(t) if t[:required] && t[:negate]
        negated_tag.push(t) unless t[:negate]
      else
        optional_tag.push(t) unless t[:negate] || t[:required]
        required_tag.push(t) if t[:required] && !t[:negate]
        negated_tag.push(t) if t[:negate]
      end
    end
  end

  unless settings[:search].nil? || settings[:search].empty?
    if settings[:regex] || settings[:search].is_a?(String)
      if settings[:negate]
        negated.push(settings[:search])
      else
        optional.push(settings[:search])
        required.push(settings[:search])
      end
    else
      settings[:search].each do |t|
        opt, req, neg = parse_search(t, settings[:negate])
        optional.concat(opt)
        required.concat(req)
        negated.concat(neg)
      end
    end
  end

  files = if !settings[:file_path].nil?
            [settings[:file_path]]
          elsif settings[:query].nil?
            NA.find_files(depth: settings[:depth])
          else
            NA.match_working_dir(settings[:query])
          end

  NA.notify("Files: #{files.join(', ')}", debug: true)
  files.each do |file|
    NA.save_working_dir(File.expand_path(file))
    content = file.read_file
    indent_level = 0
    parent = []
    in_yaml = false
    in_action = false
    content.split(/\n/).each.with_index do |line, idx|
      if in_yaml && line !~ /^(---|~~~)\s*$/
        NA.notify("YAML: #{line}", debug: true)
      elsif line =~ /^(---|~~~)\s*$/
        in_yaml = !in_yaml
      elsif line.project? && !in_yaml
        in_action = false
        proj = line.project
        indent = line.indent_level

        if indent.zero? # top level project
          parent = [proj]
        elsif indent <= indent_level # if indent level is same or less, split parent before indent level and append
          parent.slice!(indent, parent.count - indent)
          parent.push(proj)
        else # if indent level is greater, append project to parent
          parent.push(proj)
        end

        projects.push(NA::Project.new(parent.join(':'), indent, idx, idx))

        indent_level = indent
      elsif line.blank?
        in_action = false # Comment out to allow line breaks in of notes, which isn't TaskPaper-compatible
      elsif line.action?
        in_action = false

        action = line.action
        new_action = NA::Action.new(file, File.basename(file, ".#{NA.extension}"), parent.dup, action, idx)

        projects[-1].last_line = idx if projects.count.positive?

        next if line.done? && !settings[:done]

        next if settings[:require_na] && !line.na?

        if settings[:project]
          rx = settings[:project].split(%r{[/:]}).join('.*?/')
          next unless parent.join('/') =~ Regexp.new("#{rx}.*?", Regexp::IGNORECASE)
        end

        has_tag = !optional_tag.empty? || !required_tag.empty? || !negated_tag.empty?
        next if has_tag && !new_action.tags_match?(any: optional_tag,
                                                   all: required_tag,
                                                   none: negated_tag)

        actions.push(new_action)
        in_action = true
      elsif in_action
        actions[-1].note.push(line.strip) if actions.count.positive?
        projects[-1].last_line = idx if projects.count.positive?
      end
    end
    projects = projects.dup
  end

  actions.delete_if do |new_action|
    has_search = !optional.empty? || !required.empty? || !negated.empty?
    has_search && !new_action.search_match?(any: optional,
                                            all: required,
                                            none: negated,
                                            include_note: settings[:search_note])
  end

  [files, actions, projects]
end

#parse_search(tag, negate) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/na/todo.rb', line 173

def parse_search(tag, negate)
  required = []
  optional = []
  negated = []
  new_rx = tag[:token].to_s.wildcard_to_rx

  if negate
    optional.push(new_rx) if tag[:negate]
    required.push(new_rx) if tag[:required] && tag[:negate]
    negated.push(new_rx) unless tag[:negate]
  else
    optional.push(new_rx) unless tag[:negate]
    required.push(new_rx) if tag[:required] && !tag[:negate]
    negated.push(new_rx) if tag[:negate]
  end

  [optional, required, negated]
end