Module: Margin::Parser::LineMethods

Included in:
Line
Defined in:
lib/margin/parser.rb

Class Method Summary collapse

Class Method Details

.detect_annotation_only(string) ⇒ Object



48
49
50
# File 'lib/margin/parser.rb', line 48

def detect_annotation_only(string)
  string.match? ANNOTATION_ONLY
end

.detect_offset(string) ⇒ Object

Detect the offset of a raw line



33
34
35
36
# File 'lib/margin/parser.rb', line 33

def detect_offset(string)
  s = StringScanner.new(string)
  s.match? (/\s*/)
end

.detect_task(string) ⇒ Object

Detect if this matches the criteria for a task, Return the status and rest of string if yes, otherwise return false.



41
42
43
44
45
46
# File 'lib/margin/parser.rb', line 41

def detect_task(string)
  return false unless string.match? TASK_START
  done = string[1] != " "
  rest = string.sub(TASK_START,"")
  [done, rest]
end

.extract_annotation_value(string) ⇒ Object

Check if a value is really numeric, return the clean value



88
89
90
91
92
93
94
95
# File 'lib/margin/parser.rb', line 88

def extract_annotation_value(string)
  string = string.strip
  case
  when i = Integer(string, exception: false) then i
  when f = Float(string, exception: false) then f
  else string
  end
end

.extract_annotations(string) ⇒ Object



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
# File 'lib/margin/parser.rb', line 52

def extract_annotations(string)
  value = ""
  current_annotation = ""
  annotations = []
  s = StringScanner.new(string)
  in_annotation = false
  until s.eos?
    c = s.getch
    case
    when c == "["
      in_annotation = true
    when c == "]"
      in_annotation = false
      annotations << structure_annotation(current_annotation)
      current_annotation = ""
    when in_annotation
      current_annotation += c
    else
      value += c
    end
  end
  value = value.strip
  value = nil if value.length == 0
  [value, annotations]
end

.strip_line(string) ⇒ Object

Get the inner value by scanning past all leading and trailing line decorations.



24
25
26
27
28
29
30
# File 'lib/margin/parser.rb', line 24

def strip_line(string)
  s = StringScanner.new(string)
  raw_inner = ""
  s.skip LINE_DECORATION
  raw_inner += s.getch until s.match? (/#{LINE_DECORATION}$/)
  raw_inner
end

.structure_annotation(string) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/margin/parser.rb', line 78

def structure_annotation(string)
  first, last = string.split(":",2)
  if last
    { key: first.strip, value: extract_annotation_value(last) }
  else
    { value: extract_annotation_value(first) }
  end
end