/
docbook_renderer.rb
155 lines (140 loc) · 3.48 KB
/
docbook_renderer.rb
1
2
3
4
5
6
7
8
9
10
11
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
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
101
102
103
104
105
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
148
149
150
151
152
153
154
155
require 'rexml/document'
require 'pp'
class DocbookRenderer < Processor
include Tokenize
include REXML
def process( paragraphs )
@author = 'John Smith'
@title = 'Wealth of Nations'
@chapters = []
@chapter = nil
@section = nil
paragraphs.each do |paragraph|
format( paragraph )
end
@book = Element.new('book')
@book.add_namespace('http://docbook.org/ns/docbook')
@book.add_attribute('version', '5.0')
@book.add_element element_for('title', @title)
@book.add_element element_for('author', @author)
@chapters.each {|ch| @book << ch}
doc = Document.new
decl = XMLDecl.new
decl.version = '1.0'
doc << decl
doc << @book
doc
end
def element_for(type, text)
result = Element.new(type)
result.add_text(text)
result
end
def format( p )
type = p[:type]
text = p.string
return nil if text.empty? and :type != :code
case type
when :title
@title = text
when :author
@author = text
when :chapter
puts "adding chapter #{text}"
@chapter = Element.new('chapter')
@chapters << @chapter
@section = nil
title_element = Element.new('title')
add_body_text(title_element, text)
@chapter.add_element(title_element)
when :section
puts "adding section #{text}"
@section = Element.new('section')
@chapter.add(@section)
title_element = Element.new('title')
add_body_text(title_element, text)
@section.add_element(title_element)
when :body
puts "adding body #{text[0..5]}"
paragraph = Element.new('para')
add_body_text(paragraph, text)
add_content_element(paragraph)
when :code
add_content_element(code_element(type, text))
else
raise "#{type}???"
end
end
def add_content_element(el)
if @section
@section.add_element(el)
elsif @chapter
@chapter.add_element(el)
else
raise "No chapter to add #{el} to"
end
end
def text_element(type, text)
element = [ tag_for(type) ]
add_body_text(element, text)
element
end
def tag_for(type)
case type
when :body
'para'
when :text
'p'
when :author
'h3'
when :section
'h3'
when :chapter
'chapter'
when :sec
'h3'
when :title
'h2'
else
raise "Dont know what to do with #{type}"
end
end
def add_body_text( element, text )
tokens = tokenize_body_text( text )
tokens.each {|token| add_span( token, element ) }
end
def add_span( token, element )
puts "Add span: token: #{token} element: #{element}"
case token[:type]
when :italic
element.add( span_for( token.string, "emphasis" ))
when :code
element.add( span_for( token.string, "code" ))
when :bold
element.add( span_for( token.string, "emphasis" ))
when :normal
element.add_text( token.string )
when :footnote
element.add(footnote_for(token.string))
else
raise "Dont know what to do with #{token}"
end
end
def footnote_for( text )
fn = Element.new('footnote')
fn.add_element(element_for('para', text))
fn
end
def code_element(type, text)
element = Element.new('informalexample')
prog_element = element_for('programlisting', text)
prog_element.add_attribute('xml:space', 'preserve')
element.add_element(prog_element)
element
end
def span_for( text, style )
span = Element.new(style)
span.text = remove_escapes(text)
span
end
end