Module: Toodledo::CommandLine::ParserHelper

Included in:
AddFolderCommand, AddGoalCommand, Client
Defined in:
lib/toodledo/command_line/parser_helper.rb

Overview

Methods to parse a string and identify it as a Toodledo symbol.

Constant Summary collapse

FOLDER_REGEXP =

TODO These regexps are highly repetitive. Refactor

/\*((\w+)|\[(.*?)\])/
GOAL_REGEXP =
/\^((\w+)|\[(.*?)\])/
CONTEXT_REGEXP =
/\@((\w+)|\[(.*?)\])/
PRIORITY_REGEXP =
/!(top|high|medium|low|negative|-1|0|1|2|3)/
DATE_REGEXP =
/\#(([^\[]\S*)|\[(.*?)\])/
TAGS_REGEXP =
/\%((\w+)|\[(.*?)\])/
STAR_REGEXP =
/\*\s+|\*$/
LEVEL_REGEXP =

Note that level must exist at the beginning of the line

/^(life|medium|short)/
REGEXP_LIST =

Don’t include level regexp

[ 
  FOLDER_REGEXP, 
  STAR_REGEXP,
  GOAL_REGEXP,
  CONTEXT_REGEXP, 
  PRIORITY_REGEXP,
  DATE_REGEXP,
  TAGS_REGEXP
]

Instance Method Summary collapse

Instance Method Details

#parse_context(input) ⇒ Object

Parses a context in the format @Context or @[Spaced Context]



42
43
44
45
46
# File 'lib/toodledo/command_line/parser_helper.rb', line 42

def parse_context(input)
  match_data = CONTEXT_REGEXP.match(input)
  return nil if (match_data == nil)    
  return strip_brackets(match_data[1])
end

#parse_date(input) ⇒ Object

Parses a date in the format #[2011-03-17]



63
64
65
66
67
# File 'lib/toodledo/command_line/parser_helper.rb', line 63

def parse_date(input)
  match_data = DATE_REGEXP.match(input)
  return match_data if (match_data == nil)    
  return strip_brackets(match_data[1])
end

#parse_folder(input) ⇒ Object

Parses a folder in the format *Folder or *[Spaced Folder]



49
50
51
52
53
# File 'lib/toodledo/command_line/parser_helper.rb', line 49

def parse_folder(input)
  match_data = FOLDER_REGEXP.match(input)    
  return match_data if (match_data == nil)
  return strip_brackets(match_data[1])
end

#parse_goal(input) ⇒ Object

Parses a goal in the format ^Goal or ^[Spaced Goal]



56
57
58
59
60
# File 'lib/toodledo/command_line/parser_helper.rb', line 56

def parse_goal(input)
  match_data = GOAL_REGEXP.match(input)
  return match_data if (match_data == nil)    
  return strip_brackets(match_data[1])
end

#parse_level(input) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/toodledo/command_line/parser_helper.rb', line 114

def parse_level(input)
  match_data = LEVEL_REGEXP.match(input)
  if (match_data == nil)
    return nil         
  end
  
  p = match_data[1]
  case p
  when 'life'
    return Toodledo::Goal::LIFE_LEVEL
  when 'medium'
    return Toodledo::Goal::MEDIUM_LEVEL
  when 'short'
    return Toodledo::Goal::SHORT_LEVEL
  else
    return nil
  end
end

#parse_priority(input) ⇒ Object

Parses priority in the format !priority (top, high, medium, low, negative) TODO Refactor using data-driven design



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
# File 'lib/toodledo/command_line/parser_helper.rb', line 80

def parse_priority(input)
  match_data = PRIORITY_REGEXP.match(input)
  if (match_data == nil)
    return nil         
  end
  
  p = match_data[1]
  case p
  when 'top'
    return Toodledo::Priority::TOP
  when 'high'
    return Toodledo::Priority::HIGH
  when 'medium'
    return Toodledo::Priority::MEDIUM
  when 'low'
    return Toodledo::Priority::LOW
  when 'negative'
    return Toodledo::Priority::NEGATIVE
  when '-1'
    return Toodledo::Priority::NEGATIVE
  when '0'
    return Toodledo::Priority::LOW
  when '1'
    return Toodledo::Priority::MEDIUM
  when '2'
    return Toodledo::Priority::HIGH
  when '3'
    return Toodledo::Priority::TOP

  else
    return nil
  end
end

#parse_remainder(line) ⇒ Object

Returns the bit after we’ve looked for *Folder, @Context & ^Goal & star



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/toodledo/command_line/parser_helper.rb', line 144

def parse_remainder(line)
  input = line

  # Strip out anything that isn't folder, goal or context.
  for regexp in REGEXP_LIST
    input = input.sub(regexp, '')
  end
    
  input.strip!
    
  return input
end

#parse_star(input) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/toodledo/command_line/parser_helper.rb', line 133

def parse_star(input)
  match_data = STAR_REGEXP.match(input)
  if (match_data == nil)
    return false
  else
    return true
  end
end

#parse_tag(input) ⇒ Object

Parses a list of tags in the format %[tag1 tag2 tag3] TODO Allow %tag1, tag2, tag3 ? (This is the format Toodledo’s Twitter client uses)



71
72
73
74
75
# File 'lib/toodledo/command_line/parser_helper.rb', line 71

def parse_tag(input)
  match_data = TAGS_REGEXP.match(input)
  return match_data if (match_data == nil)    
  return strip_brackets(match_data[1]).split(/\s+/)
end

#strip_brackets(inword) ⇒ Object

Strips a string of [ and ] characters



159
160
161
# File 'lib/toodledo/command_line/parser_helper.rb', line 159

def strip_brackets(inword)    
  return inword.gsub(/\[|\]/, '')
end