Class: OrgMode::FileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/org_mode/parser.rb

Constant Summary collapse

RxNodeTitle =
%r{ 
  ^   # beginning of line
  (
    \*+ # multiple stars 
    \s+ # one or more whitespace
    .*  # anything
  )
  $   # untill _end of line
}xs

Class Method Summary collapse

Class Method Details

.parse(buffer) ⇒ Object

Public: parses buffer into nodes and collects there in a OrgMode::File object

Returns OrgMode::File object containing all information of the file.



32
33
34
35
36
37
38
39
# File 'lib/org_mode/parser.rb', line 32

def parse(buffer)
  b, nodes, e  = parse_buffer(buffer)

  parsed_nodes = parse_nodes(nodes)
  root_nodes   = NodeUtils.convert_sequential_nodelist_into_tree(parsed_nodes)

  return File.new(b,root_nodes,e)
end

.parse_buffer(buffer) ⇒ Object



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

def parse_buffer(buffer)
  beginning_of_file, nodes, ending_of_file =
    parse_into_tokens(buffer)
end

.parse_into_tokens(buffer) ⇒ Object

Private: splits buffer into different parts

First part is beginning of file
Second part are the nodetitles in combination
with the content
Third part is the ending of the file

buffer - org mode data

Returns beginning_of_file, nodes, and ending

if beginning is not present and empy string is 
returned. This function will never return nil


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/org_mode/parser.rb', line 65

def parse_into_tokens buffer
  tokens = buffer.split(RxNodeTitle).map(&:rstrip)
  beginning_of_file = tokens.shift || ''

  nodes = []
  while !tokens.empty?
    nodes << Array.new(2) { tokens.shift || '' }
  end

  nodes.map! { |t,c| [t,c[1..-1] || ''] }

  [ beginning_of_file, nodes, "" ]
end

.parse_nodes(nodes) ⇒ Object



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

def parse_nodes(nodes)
  nodes.map do |title,content|
    NodeParser.parse(title,content) 
  end
end