Class: ReVIEW::TOCParser
Defined Under Namespace
Classes: Chapter, List, Node, Paragraph, Section
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.chapter_node(chap) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/review/tocparser.rb', line 27
def TOCParser.chapter_node(chap)
toc = TOCParser.parse(chap)
unless toc.size == 1
$stderr.puts "warning: chapter #{toc.join} contains more than 1 chapter"
end
toc.first
end
|
.parse(chap) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/review/tocparser.rb', line 18
def TOCParser.parse(chap)
f = StringIO.new(chap.content, 'r:BOM|utf-8')
stream = Preprocessor::Strip.new(f)
new.parse(stream, chap).map do |root|
root.number = chap.number
root
end
end
|
Instance Method Details
#compile_label(line) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/review/tocparser.rb', line 107
def compile_label(line)
b = ReVIEW::TEXTBuilder.new
dummy_book = ReVIEW::Book::Base.load
dummy_chapter = ReVIEW::Book::Chapter.new(dummy_book, 1, '-', nil, StringIO.new)
dummy_loc = Location.new("", StringIO.new)
b.bind(ReVIEW::Compiler.new(b), dummy_chapter, dummy_loc)
b.compile_inline(line)
end
|
#error!(filename, lineno, msg) ⇒ Object
116
117
118
|
# File 'lib/review/tocparser.rb', line 116
def error!(filename, lineno, msg)
raise "#{filename}:#{lineno}: #{msg}"
end
|
#get_label(line) ⇒ Object
102
103
104
105
|
# File 'lib/review/tocparser.rb', line 102
def get_label(line)
line = line.strip.sub(/\A=+\s*/, '')
compile_label(line)
end
|
#parse(f, chap) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/review/tocparser.rb', line 35
def parse(f, chap)
roots = []
node_stack = []
filename = chap.path
while line = f.gets
case line
when /\A\#@/
;
when /\A\s*\z/
;
when /\A(={2,})[\[\s\{]/
lev = $1.size
error! filename, f.lineno, "section level too deep: #{lev}" if lev > 5
label = get_label(line)
if node_stack.empty?
dummy_chapter = Chapter.new(label, chap)
node_stack.push dummy_chapter
roots.push dummy_chapter
end
next if label =~ /\A\[\//
sec = Section.new(lev, label.gsub(/\A\{.*?\}\s?/, ""))
until node_stack.last.level < sec.level
node_stack.pop
end
node_stack.last.add_child sec
node_stack.push sec
when /\A=[^=]/
label = get_label(line)
node_stack.clear
new_chapter = Chapter.new(label, chap)
node_stack.push new_chapter
roots.push new_chapter
when %r<\A//\w+(?:\[.*?\])*\{\s*\z>
if node_stack.empty?
error! filename, f.lineno, 'list found before section label'
end
node_stack.last.add_child(list = List.new)
beg = f.lineno
list.add line
while line = f.gets
break if %r<\A//\}> =~ line
list.add line
end
error! filename, beg, 'unterminated list' unless line
when %r<\A//\w>
;
else
next if node_stack.empty?
node_stack.last.add_child(par = Paragraph.new(chap))
par.add line
while line = f.gets
break if /\A\s*\z/ =~ line
par.add line
end
end
end
roots
end
|