manual: CI check with nitunit
[nit.git] / lib / markdown2 / markdown_man_rendering.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Manpages rendering of Markdown documents
16 module markdown_man_rendering
17
18 import markdown_rendering
19 import markdown_github
20 import markdown_wikilinks
21
22 # Markdown document renderer to Manpage
23 class ManRenderer
24 super MdRenderer
25
26 # Output under construction
27 private var man: Buffer is noinit
28
29 # Render `node` as Markdown
30 redef fun render(node) do
31 man = new Buffer
32 enter_visit(node)
33 return man.write_to_string
34 end
35
36 redef fun visit(node) do node.render_man(self)
37
38 # Add `string` to `man`
39 fun add(string: String) do
40 man.append(string.replace("-", "\\-"))
41 end
42
43 # Add code that need to be escaped
44 fun add_code(code: String) do
45 add code.replace(" ", "\\ ")
46 end
47
48 # Add a blank line to the output
49 fun add_line do
50 add "\n"
51 end
52 end
53
54 redef class MdNode
55
56 # Render `self` as Manpage format
57 fun render_man(v: ManRenderer) do visit_all(v)
58 end
59
60 # Blocks
61
62 redef class MdBlockQuote
63 redef fun render_man(v) do
64 v.add ".RS"
65 visit_all(v)
66 v.add ".RE"
67 v.add_line
68 end
69 end
70
71 redef class MdCodeBlock
72 redef fun render_man(v) do
73 v.add ".RS\n.nf\n\\f[C]"
74 v.add_line
75
76 var literal = self.literal
77 if literal != null then
78 var lines = literal.split("\n")
79 for i in [0 .. lines.length[ do
80 if i == lines.length - 1 then break
81 var line = lines[i]
82 v.add_code line
83 v.add_line
84 end
85 end
86
87 v.add "\\f[]\n.fi\n.RE"
88 v.add_line
89 end
90 end
91
92 redef class MdHeading
93 redef fun render_man(v) do
94 var level = self.level
95
96 if level == 1 then
97 v.add ".SH "
98 else if level == 2 then
99 v.add ".SS "
100 else if level >= 3 then
101 # We use dictionary (titled paragraph) to simulate a 3rd level (or more)
102 v.add ".TP\n"
103 end
104 visit_all(v)
105 v.add_line
106 end
107 end
108
109 redef class MdUnorderedList
110 redef fun render_man(v) do
111 v.add ".RS"
112 v.add_line
113
114 var node = first_child
115 while node != null do
116 v.add ".IP \\[bu] 3"
117 v.add_line
118 v.enter_visit node
119 v.add_line
120 node = node.next
121 end
122
123 v.add ".RE"
124 v.add_line
125 end
126 end
127
128 redef class MdOrderedList
129 redef fun render_man(v) do
130 v.add ".RS"
131 v.add_line
132
133 var index = start_number
134 var node = first_child
135 while node != null do
136 v.add ".IP \"{index}.\" 3"
137 v.add_line
138 v.enter_visit node
139 v.add_line
140 node = node.next
141 index += 1
142 end
143
144 v.add ".RE"
145 v.add_line
146 end
147 end
148
149 redef class MdParagraph
150 redef fun render_man(v) do
151 var in_list = is_in_list
152 if not in_list then
153 v.add_line
154 end
155 visit_all(v)
156 if not in_list then
157 v.add_line
158 end
159 end
160 end
161
162 redef class MdThematicBreak
163 redef fun render_man(v) do
164 v.add "***"
165 v.add_line
166 end
167 end
168
169 redef class MdHtmlBlock
170 redef fun render_man(v) do
171 v.add_line
172 v.add literal or else ""
173 v.add_line
174 end
175 end
176
177 # Inlines
178
179 redef class MdLineBreak
180 redef fun render_man(v) do
181 v.add_line
182 end
183 end
184
185 redef class MdCode
186 redef fun render_man(v) do
187 v.add "\\f[C]"
188 v.add_code literal
189 v.add "\\f[]"
190 end
191 end
192
193 redef class MdEmphasis
194 redef fun render_man(v) do
195 v.add "\\f[I]"
196 visit_all(v)
197 v.add "\\f[]"
198 end
199 end
200
201 redef class MdStrongEmphasis
202 redef fun render_man(v) do
203 v.add "\\f[B]"
204 visit_all(v)
205 v.add "\\f[]"
206 end
207 end
208
209 redef class MdHtmlInline
210 redef fun render_man(v) do
211 v.add literal
212 end
213 end
214
215 redef class MdLinkOrImage
216 redef fun render_man(v) do
217 var title = self.title
218
219 visit_all(v)
220 v.add " ("
221 v.add destination
222 if title != null and not title.is_empty then
223 v.add " "
224 v.add title
225 end
226 v.add ")"
227 end
228 end
229
230 redef class MdText
231 redef fun render_man(v) do
232 v.add literal
233 end
234 end
235
236 # Github
237
238 redef class MdStrike
239 redef fun render_man(v) do
240 v.add "[STRIKEOUT:"
241 visit_all(v)
242 v.add "]"
243 end
244 end
245
246 # Wikilinks
247
248 redef class MdWikilink
249 redef fun render_man(v) do
250 v.add "("
251 var title = self.title
252 if title != null then
253 v.add "{title} | "
254 end
255 v.add link
256 v.add ")"
257 end
258 end