Module: Todo::Syntax
- Included in:
- Task
- Defined in:
- lib/todo/syntax.rb
Overview
Supports parsing and extracting core syntax fragments from todo.txt lines.
Constant Summary collapse
- CONTEXTS_PATTERN =
The regex used to match contexts.
/(?:\s+|^)@[^\s]+/- PROJECTS_PATTERN =
The regex used to match projects.
/(?:\s+|^)\+[^\s]+/- PRIORITY_PATTERN =
The regex used to match priorities.
/(?:^|\s+)\(([A-Za-z])\)\s+/- CREATED_ON_PATTERN =
The regex used to match creation date.
/(?:^|-\d{2}\s|\)\s)(\d{4}-\d{2}-\d{2})\s/- COMPLETED_FLAG_PATTERN =
/^x\s+/- COMPLETED_ON_PATTERN =
The regex used to match completion date.
/^x\s+(\d{4}-\d{2}-\d{2})\s+/- DUE_ON_PATTERN =
The regex used to match due date.
/(?:due:)(\d{4}-\d{2}-\d{2})(?:\s+|$)/i- TAGS_PATTERN =
The regex used to match generic tags.
/([a-z]+):([A-Za-z0-9_-]+)/i- COMPLETED_FLAG =
'x'.freeze
- SINGLE_SPACE =
' '.freeze
Instance Method Summary collapse
-
#check_completed_flag(line) ⇒ Boolean
Checks whether the given todo item has a completion flag set.
-
#extract_completed_date(line) ⇒ Date
Extracts the completion date for the given todo item.
-
#extract_contexts(line) ⇒ Array<String>
Extract the list of ‘@context` tags out of the task line.
-
#extract_created_on(line) ⇒ Date
Extracts the creation date for the given todo item.
-
#extract_item_text(line) ⇒ String
Extracts the readable text content of a task line, stripping out all the discrete pieces of metadata (priority, dates, completion flag, projects, contexts, etc).
-
#extract_priority(line) ⇒ String
Extracts the priority indicator from the task line.
-
#extract_projects(line) ⇒ Array<String>
Extract the list of ‘+project` tags out of the task line.
-
#extract_tags(line) ⇒ Hash<Symbol,String>
Extracts the collection of tag annotations for the given todo item.
Instance Method Details
#check_completed_flag(line) ⇒ Boolean
Checks whether the given todo item has a completion flag set.
This provides support for ad-hoc handwritten lists where the completed flag is set but there is no completed date.
91 92 93 |
# File 'lib/todo/syntax.rb', line 91 def check_completed_flag(line) line[0] == COMPLETED_FLAG && line[1] == SINGLE_SPACE end |
#extract_completed_date(line) ⇒ Date
Extracts the completion date for the given todo item. Returns nil if a valid date is not found.
72 73 74 75 76 77 78 79 |
# File 'lib/todo/syntax.rb', line 72 def extract_completed_date(line) date = COMPLETED_ON_PATTERN.match(line) begin Date.parse(date[1]) if date rescue ArgumentError return nil # The given date is not valid end end |
#extract_contexts(line) ⇒ Array<String>
Extract the list of ‘@context` tags out of the task line.
108 109 110 |
# File 'lib/todo/syntax.rb', line 108 def extract_contexts(line) line.scan(CONTEXTS_PATTERN).map(&:strip) end |
#extract_created_on(line) ⇒ Date
Extracts the creation date for the given todo item. Returns nil if a valid date is not found.
58 59 60 61 62 63 64 65 |
# File 'lib/todo/syntax.rb', line 58 def extract_created_on(line) date = line.match CREATED_ON_PATTERN begin Date.parse(date[1]) if date rescue ArgumentError return nil # The given date is not valid end end |
#extract_item_text(line) ⇒ String
Extracts the readable text content of a task line, stripping out all the discrete pieces of metadata (priority, dates, completion flag, projects, contexts, etc).
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/todo/syntax.rb', line 33 def extract_item_text(line) line.gsub(COMPLETED_ON_PATTERN, '') .gsub(COMPLETED_FLAG_PATTERN, '') .gsub(PRIORITY_PATTERN, '') .gsub(CREATED_ON_PATTERN, '') .gsub(CONTEXTS_PATTERN, '') .gsub(PROJECTS_PATTERN, '') .gsub(DUE_ON_PATTERN, '') .gsub(TAGS_PATTERN, '') .strip end |
#extract_priority(line) ⇒ String
Extracts the priority indicator from the task line.
49 50 51 |
# File 'lib/todo/syntax.rb', line 49 def extract_priority(line) line.match(PRIORITY_PATTERN)[1] if line =~ PRIORITY_PATTERN end |
#extract_projects(line) ⇒ Array<String>
Extract the list of ‘+project` tags out of the task line.
116 117 118 |
# File 'lib/todo/syntax.rb', line 116 def extract_projects(line) line.scan(PROJECTS_PATTERN).map(&:strip) end |
#extract_tags(line) ⇒ Hash<Symbol,String>
Extracts the collection of tag annotations for the given todo item. Returns an empty hash if no tags are found.
100 101 102 |
# File 'lib/todo/syntax.rb', line 100 def (line) line.scan(TAGS_PATTERN).map { |tag, val| [tag.downcase.to_sym, val] }.to_h end |