module Todo::Syntax

Supports parsing and extracting core syntax fragments from todo.txt lines.

Constants

COMPLETED_FLAG
COMPLETED_FLAG_PATTERN
COMPLETED_ON_PATTERN

The regex used to match completion date.

CONTEXTS_PATTERN

The regex used to match contexts.

CREATED_ON_PATTERN

The regex used to match creation date.

DUE_ON_PATTERN

The regex used to match due date.

PRIORITY_PATTERN

The regex used to match priorities.

PROJECTS_PATTERN

The regex used to match projects.

SINGLE_SPACE
TAGS_PATTERN

The regex used to match generic tags.

Public Instance Methods

check_completed_flag(line) click to toggle source

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.

@param line [String] the todo item to be processed @return [Boolean]

# 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) click to toggle source

Extracts the completion date for the given todo item. Returns nil if a valid date is not found.

@param line [String] the todo item to be processed @return [Date] the completed date of the line

# 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) click to toggle source

Extract the list of `@context` tags out of the task line.

@param [String] line Line of text encoding a single task @return [Array<String>] List of context tags

# File lib/todo/syntax.rb, line 108
def extract_contexts(line)
  line.scan(CONTEXTS_PATTERN).map(&:strip)
end
extract_created_on(line) click to toggle source

Extracts the creation date for the given todo item. Returns nil if a valid date is not found.

@param line [String] the todo item to be processed @return [Date] the created date of the line

# 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) click to toggle source

Extracts the readable text content of a task line, stripping out all the discrete pieces of metadata (priority, dates, completion flag, projects, contexts, etc).

@param line [String] the todo item to be processed @return [String] the text content of the item

# 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) click to toggle source

Extracts the priority indicator from the task line.

@param line [String] the todo item to be processed @return [String] the character (from A-Z) representing the priority

# File lib/todo/syntax.rb, line 49
def extract_priority(line)
  line.match(PRIORITY_PATTERN)[1] if line =~ PRIORITY_PATTERN
end
extract_projects(line) click to toggle source

Extract the list of `+project` tags out of the task line.

@param [String] line Line of text encoding a single task @return [Array<String>] List of project tags

# File lib/todo/syntax.rb, line 116
def extract_projects(line)
  line.scan(PROJECTS_PATTERN).map(&:strip)
end
extract_tags(line) click to toggle source

Extracts the collection of tag annotations for the given todo item. Returns an empty hash if no tags are found.

@param line [String] the todo item to be processed @return [Hash<Symbol,String>] the collection of tag annotations

# File lib/todo/syntax.rb, line 100
def extract_tags(line)
  line.scan(TAGS_PATTERN).map { |tag, val| [tag.downcase.to_sym, val] }.to_h
end