Class: Ntxt::Parser

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

Overview

The parser for Ntxt. Most of this a typical user will not find useful with the exception of Parser.parse.

Instance Method Summary collapse

Instance Method Details

#closeBlockObject



61
62
63
64
# File 'lib/ntxt/parser.rb', line 61

def closeBlock
  @currentBlock.offset = @currentOffset - @currentBlock.start - 1
  @currentBlock = @currentBlock.parent
end

#computeIndentObject

hlevel



57
58
59
# File 'lib/ntxt/parser.rb', line 57

def computeIndent
  /^(\s*).*$/.match(@lines[@currentLine])[1].length
end

#extractTagsObject

Extract all the tags from the given line.

Some tag examples:
  [a tag] [another tag]
  [a tag] [another tag] Not a tag. [a tag]
  No tag on this line.


71
72
73
74
75
76
77
78
# File 'lib/ntxt/parser.rb', line 71

def extractTags()
  line = @lines[@currentLine]
  re = /\[([^\[]+)\]/m
  while line =~ re
    @currentBlock.addTag($~[1])
    line = line.sub(re, '')
  end
end

#hlevelObject

Return an array in which the first element is the indent length and the second element is the contained text. Nil otherwise.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ntxt/parser.rb', line 12

def hlevel()
  line = @lines[@currentLine]

  nextLine = if @lines.length > @currentLine + 1
    @lines[@currentLine+1]
  else
    nil
  end

  case line
  when /^\s*=([^=].*)=\s*$/
    [ 1, $~[1], 1 ]
  when /^\s*==([^=].*)==\s*$/
    [ 2, $~[1], 1 ]
  when /^\s*===([^=].*)===\s*$/
    [ 3, $~[1], 1 ]
  when /^\s*====([^=].*)====\s*$/
    [ 4, $~[1], 1 ]
  when /^\s*=====([^=].*)=====\s*$/
    [ 5, $~[1], 1 ]
  when /^\s*======([^=].*)======\s*$/
    [ 6, $~[1], 1 ]
  when /^\s*#\s*(.*)$/
    [ 1, $~[1], 1 ]
  when /^\s*##\s*(.*)$/
    [ 2, $~[1], 1 ]
  when /^\s*###\s*(.*)$/
    [ 3, $~[1], 1 ]
  when /^\s*####\s*(.*)$/
    [ 4, $~[1], 1 ]
  when /^\s*#####\s*(.*)$/
    [ 5, $~[1], 1 ]
  when /^\s*######\s*(.*)$/
    [ 6, $~[1], 1 ]
  else
    if nextLine && nextLine =~ /\s*==+\s*$/
      [ 1, line, 2 ]
    elsif nextLine && nextLine =~ /\s*--+\s*$/
      [ 2, line, 2 ]
    else
      nil
    end
  end
end

#nextLineObject

parse(ntxtObj)



101
102
103
104
# File 'lib/ntxt/parser.rb', line 101

def nextLine
  @currentOffset += @lines[@currentLine].length + 1
  @currentLine += 1
end

#parse(ntxtObj) ⇒ Object

Parse the given Ntxt ‘s Ntxt#text.

ntxtObj

If this is an Ntxt object, Ntxt#text is parsed. If ntxtObj is not an Ntxt object, it is assumed to be a valid argument for Ntxt.new and a new Ntxt is constructed.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ntxt/parser.rb', line 84

def parse(ntxtObj)

  # If ntxtObj isn't an Ntxt, create it as one.
  ( ntxtObj = Ntxt.new(ntxtObj) ) unless ntxtObj.is_a?( Ntxt )

  rootBlock = Block.new(ntxtObj)

  @lines = ntxtObj.text.split("\n")
  @currentLine = 0
  @currentOffset = 0
  @currentBlock = rootBlock
  
  parseLines()
  
  rootBlock
end

#parseLinesObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ntxt/parser.rb', line 106

def parseLines
  while @currentLine < @lines.length do
    extractTags

    hlvl = hlevel

    if hlvl
      while (not @currentBlock.root? && @currentBlock.header == 0) || @currentBlock.header >= hlvl[0] do
        closeBlock
      end

      @currentBlock = Block.new(
        @currentBlock.ntxt,
        @currentBlock,
        @currentOffset)
      @currentBlock.header = hlvl[0]

      hlvl[2].times { nextLine }

      next
    end

    indent = computeIndent
    while @currentBlock.header == 0 && @currentBlock.indent > indent do
      closeBlock
    end

    if indent > @currentBlock.indent
      @currentBlock = Block.new(
        @currentBlock.ntxt,
        @currentBlock,
        @currentOffset)
      @currentBlock.indent = indent
    end

    nextLine
  end

  while not @currentBlock.root?
    closeBlock
  end
end