nitiwiki: make summary optional
[nit.git] / contrib / nitiwiki / src / wiki_html.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 # HTML wiki rendering
16 module wiki_html
17
18 import wiki_links
19 import markdown::decorators
20
21 redef class Nitiwiki
22
23 # Render HTML output looking for changes in the markdown sources.
24 redef fun render do
25 super
26 if not root_section.is_dirty and not force_render then return
27 var out_dir = expand_path(config.root_dir, config.out_dir)
28 out_dir.mkdir
29 copy_assets
30 root_section.add_child make_sitemap
31 root_section.render
32 end
33
34 # Copy the asset directory to the (public) output directory.
35 private fun copy_assets do
36 var src = expand_path(config.root_dir, config.assets_dir)
37 var out = expand_path(config.root_dir, config.out_dir)
38 if need_render(src, expand_path(out, config.assets_dir)) then
39 if src.file_exists then sys.system "cp -R {src} {out}"
40 end
41 end
42
43 # Build the wiki sitemap page.
44 private fun make_sitemap: WikiSitemap do
45 var sitemap = new WikiSitemap(self, "sitemap")
46 sitemap.is_dirty = true
47 return sitemap
48 end
49
50 # Markdown processor used for inline element such as titles in TOC.
51 private var inline_processor: MarkdownProcessor is lazy do
52 var proc = new MarkdownProcessor
53 proc.emitter.decorator = new InlineDecorator
54 return proc
55 end
56
57 # Inline markdown (remove h1, p, ... elements).
58 private fun inline_md(md: Writable): Writable do
59 return inline_processor.process(md.write_to_string)
60 end
61 end
62
63 redef class WikiEntry
64 # Get a `<a>` template link to `self`
65 fun tpl_link: Writable do
66 return "<a href=\"{url}\">{title}</a>"
67 end
68 end
69
70 redef class WikiSection
71
72 # Output directory (where to ouput the HTML pages for this section).
73 redef fun out_path: String do
74 if parent == null then
75 return wiki.config.out_dir
76 else
77 return wiki.expand_path(parent.out_path, name)
78 end
79 end
80
81 redef fun render do
82 if not is_dirty and not wiki.force_render then return
83 if is_new then
84 out_full_path.mkdir
85 else
86 sys.system "touch {out_full_path}"
87 end
88 if has_source then
89 wiki.message("Render section {out_path}", 1)
90 copy_files
91 end
92 var index = self.index
93 if index isa WikiSectionIndex then
94 wiki.message("Render auto-index for section {out_path}", 1)
95 index.is_dirty = true
96 add_child index
97 end
98 super
99 end
100
101 # Copy attached files from `src_path` to `out_path`.
102 private fun copy_files do
103 assert has_source
104 var dir = src_full_path.to_s
105 for name in dir.files do
106 if name == wiki.config_filename then continue
107 if name.has_suffix(".md") then continue
108 if has_child(name) then continue
109 var src = wiki.expand_path(dir, name)
110 var out = wiki.expand_path(out_full_path, name)
111 if not wiki.need_render(src, out) then continue
112 sys.system "cp -R {src} {out_full_path}"
113 end
114 end
115
116 redef fun tpl_link do return index.tpl_link
117
118 # Render the section hierarchy as a html tree.
119 #
120 # `limit` is used to specify the max-depth of the tree.
121 #
122 # The generated tree will be something like this:
123 #
124 # ~~~html
125 # <ul>
126 # <li>section 1</li>
127 # <li>section 2
128 # <ul>
129 # <li>section 2.1</li>
130 # <li>section 2.2</li>
131 # </ul>
132 # </li>
133 # </ul>
134 # ~~~
135 fun tpl_tree(limit: Int): Template do
136 return tpl_tree_intern(limit, 1)
137 end
138
139 # Build the template tree for this section recursively.
140 protected fun tpl_tree_intern(limit, count: Int): Template do
141 var out = new Template
142 var index = index
143 out.add "<li>"
144 out.add tpl_link
145 if (limit < 0 or count < limit) and
146 (children.length > 1 or (children.length == 1)) then
147 out.add " <ul>"
148 for child in children.values do
149 if child == index then continue
150 if child isa WikiArticle then
151 out.add "<li>"
152 out.add child.tpl_link
153 out.add "</li>"
154 else if child isa WikiSection and not child.is_hidden then
155 out.add child.tpl_tree_intern(limit, count + 1)
156 end
157 end
158 out.add " </ul>"
159 end
160 out.add "</li>"
161 return out
162 end
163 end
164
165 redef class WikiArticle
166
167 redef fun out_path: String do
168 if parent == null then
169 return wiki.expand_path(wiki.config.out_dir, "{name}.html")
170 else
171 return wiki.expand_path(parent.out_path, "{name}.html")
172 end
173 end
174
175 redef fun render do
176 super
177 if not is_dirty and not wiki.force_render then return
178 wiki.message("Render article {name}", 2)
179 var file = out_full_path
180 file.dirname.mkdir
181 tpl_page.write_to_file file
182 end
183
184
185 # Replace macros in the template by wiki data.
186 private fun tpl_page: TemplateString do
187 var tpl = wiki.load_template(template_file)
188 if tpl.has_macro("TOP_MENU") then
189 tpl.replace("TOP_MENU", tpl_menu)
190 end
191 if tpl.has_macro("HEADER") then
192 tpl.replace("HEADER", tpl_header)
193 end
194 if tpl.has_macro("BODY") then
195 tpl.replace("BODY", tpl_article)
196 end
197 if tpl.has_macro("FOOTER") then
198 tpl.replace("FOOTER", tpl_footer)
199 end
200 return tpl
201 end
202
203 # Generate the HTML header for this article.
204 fun tpl_header: Writable do
205 var file = header_file
206 if not wiki.has_template(file) then return ""
207 return wiki.load_template(file)
208 end
209
210 # Generate the HTML page for this article.
211 fun tpl_article: TplArticle do
212 var article = new TplArticle
213 article.body = content
214 article.breadcrumbs = new TplBreadcrumbs(self)
215 article.sidebar = tpl_sidebar
216 article.sidebar_pos = wiki.config.sidebar
217 return article
218 end
219
220 # Sidebar for this page.
221 var tpl_sidebar: TplSidebar is lazy do
222 var res = new TplSidebar
223 if wiki.config.auto_summary then
224 res.blocks.add tpl_summary
225 end
226 res.blocks.add_all sidebar.blocks
227 return res
228 end
229
230 # Generate the HTML summary for this article.
231 #
232 # Based on `headlines`
233 fun tpl_summary: Writable do
234 var headlines = self.headlines
235 var tpl = new Template
236 tpl.add "<ul class=\"summary list-unstyled\">"
237 var iter = headlines.iterator
238 while iter.is_ok do
239 var hl = iter.item
240 # parse title as markdown
241 var title = wiki.inline_md(hl.title)
242 tpl.add "<li><a href=\"#{hl.id}\">{title}</a>"
243 iter.next
244 if iter.is_ok then
245 if iter.item.level > hl.level then
246 tpl.add "<ul class=\"list-unstyled\">"
247 else if iter.item.level < hl.level then
248 tpl.add "</li>"
249 tpl.add "</ul>"
250 end
251 else
252 tpl.add "</li>"
253 end
254 end
255 tpl.add "</ul>"
256 return tpl
257 end
258
259 # Generate the HTML menu for this article.
260 fun tpl_menu: Writable do
261 var file = menu_file
262 if not wiki.has_template(file) then return ""
263 var tpl = wiki.load_template(file)
264 if tpl.has_macro("MENUS") then
265 var items = new Template
266 for child in wiki.root_section.children.values do
267 if child isa WikiArticle and child.is_index then continue
268 if child isa WikiSection and child.is_hidden then continue
269 items.add "<li"
270 if self == child or self.breadcrumbs.has(child) then
271 items.add " class=\"active\""
272 end
273 items.add ">"
274 items.add child.tpl_link
275 items.add "</li>"
276 end
277 tpl.replace("MENUS", items)
278 end
279 return tpl
280 end
281
282 # Generate the HTML footer for this article.
283 fun tpl_footer: Writable do
284 var file = footer_file
285 if not wiki.has_template(file) then return ""
286 var tpl = wiki.load_template(file)
287 var time = new Tm.gmtime
288 if tpl.has_macro("YEAR") then
289 tpl.replace("YEAR", (time.year + 1900).to_s)
290 end
291 if tpl.has_macro("GEN_TIME") then
292 tpl.replace("GEN_TIME", time.to_s)
293 end
294 return tpl
295 end
296 end
297
298 # A `WikiArticle` that contains the sitemap tree.
299 class WikiSitemap
300 super WikiArticle
301
302 redef fun tpl_article do
303 var article = new TplArticle.with_title("Sitemap")
304 article.body = new TplPageTree(wiki.root_section, -1)
305 return article
306 end
307
308 redef var is_dirty = false
309 end
310
311 # A `WikiArticle` that contains the section index tree.
312 redef class WikiSectionIndex
313
314 redef var is_dirty = false
315
316 redef fun tpl_article do
317 var article = new TplArticle.with_title(section.title)
318 article.body = new TplPageTree(section, -1)
319 article.breadcrumbs = new TplBreadcrumbs(self)
320 return article
321 end
322 end
323
324 # Article HTML output.
325 class TplArticle
326 super Template
327
328 # Article title.
329 var title: nullable Writable = null
330
331 # Article HTML body.
332 var body: nullable Writable = null
333
334 # Sidebar of this article (if any).
335 var sidebar: nullable TplSidebar = null
336
337 # Position of the sidebar.
338 #
339 # See `WikiConfig::sidebar`.
340 var sidebar_pos: String = "left"
341
342 # Breadcrumbs from wiki root to this article.
343 var breadcrumbs: nullable TplBreadcrumbs = null
344
345 # Init `self` with a `title`.
346 init with_title(title: Writable) do
347 self.title = title
348 end
349
350 redef fun rendering do
351 if sidebar_pos == "left" then render_sidebar
352 if sidebar == null then
353 add "<div class=\"col-sm-12 content\">"
354 else
355 add "<div class=\"col-sm-9 content\">"
356 end
357 if body != null then
358 add "<article>"
359 if breadcrumbs != null then
360 add breadcrumbs.as(not null)
361 end
362 if title != null then
363 add "<h1>"
364 add title.as(not null)
365 add "</h1>"
366 end
367 add body.as(not null)
368 add " </article>"
369 end
370 add "</div>"
371 if sidebar_pos == "right" then render_sidebar
372 end
373
374 private fun render_sidebar do
375 if sidebar == null then return
376 add "<div class=\"col-sm-3 sidebar\">"
377 add sidebar.as(not null)
378 add "</div>"
379 end
380 end
381
382 # A collection of HTML blocks displayed on the side of a page.
383 class TplSidebar
384 super Template
385
386 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
387 var blocks = new Array[Writable]
388
389 redef fun rendering do
390 for block in blocks do
391 add "<nav class=\"sideblock\">"
392 add block
393 add "</nav>"
394 end
395 end
396 end
397
398 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
399 class TplBreadcrumbs
400 super Template
401
402 # Bread crumb article.
403 var article: WikiArticle
404
405 redef fun rendering do
406 var path = article.breadcrumbs
407 if path.is_empty or path.length <= 2 and article.is_index then return
408 add "<ol class=\"breadcrumb\">"
409 for entry in path do
410 if entry == path.last then
411 add "<li class=\"active\">"
412 add entry.title
413 add "</li>"
414 else
415 if article.parent == entry and article.is_index then continue
416 add "<li>"
417 add entry.tpl_link
418 add "</li>"
419 end
420 end
421 add "</ol>"
422 end
423 end
424
425 # An HTML tree that show the section pages structure.
426 class TplPageTree
427 super Template
428
429 # Builds the page tree from `root`.
430 var root: WikiSection
431
432 # Limits the tree depth to `max_depth` levels.
433 var max_depth: Int
434
435 redef fun rendering do
436 add "<ul>"
437 add root.tpl_tree(-1)
438 add "</ul>"
439 end
440 end