nit: Added link to `CONTRIBUTING.md` from the README
[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.escape_to_sh} {out.escape_to_sh}"
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(context: WikiEntry): Writable do
66 return "<a href=\"{href_from(context)}\">{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.escape_to_sh}"
87 end
88 if has_source then
89 wiki.message("Render section {name} -> {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 {name} -> {out_path}", 1)
95 index.is_dirty = true
96 add_child index
97 end
98 # Hack: Force the rendering of `index` first so that trails are collected
99 # TODO: Add first-pass analysis to collect global information before doing the rendering
100 index.render
101 super
102 end
103
104 # Copy attached files from `src_path` to `out_path`.
105 private fun copy_files do
106 assert has_source
107 var dir = src_full_path.to_s
108 for name in dir.files do
109 if name == wiki.config_filename then continue
110 if name.has_suffix(".md") then continue
111 if has_child(name) then continue
112 var src = wiki.expand_path(dir, name)
113 var out = wiki.expand_path(out_full_path, name)
114 if not wiki.need_render(src, out) then continue
115 sys.system "cp -R -- {src.escape_to_sh} {out_full_path.escape_to_sh}"
116 end
117 end
118
119 redef fun tpl_link(context) do return index.tpl_link(context)
120
121 # Render the section hierarchy as a html tree.
122 #
123 # `limit` is used to specify the max-depth of the tree.
124 #
125 # The generated tree will be something like this:
126 #
127 # ~~~html
128 # <ul>
129 # <li>section 1</li>
130 # <li>section 2
131 # <ul>
132 # <li>section 2.1</li>
133 # <li>section 2.2</li>
134 # </ul>
135 # </li>
136 # </ul>
137 # ~~~
138 fun tpl_tree(limit: Int): Template do
139 return tpl_tree_intern(limit, 1, self)
140 end
141
142 # Build the template tree for this section recursively.
143 protected fun tpl_tree_intern(limit, count: Int, context: WikiEntry): Template do
144 var out = new Template
145 var index = index
146 out.add "<li>"
147 out.add tpl_link(context)
148 if (limit < 0 or count < limit) and
149 (children.length > 1 or (children.length == 1)) then
150 out.add " <ul>"
151 for child in children.values do
152 if child == index then continue
153 if child isa WikiArticle then
154 out.add "<li>"
155 out.add child.tpl_link(context)
156 out.add "</li>"
157 else if child isa WikiSection and not child.is_hidden then
158 out.add child.tpl_tree_intern(limit, count + 1, context)
159 end
160 end
161 out.add " </ul>"
162 end
163 out.add "</li>"
164 return out
165 end
166 end
167
168 redef class WikiArticle
169
170 redef fun out_path: String do
171 if parent == null then
172 return wiki.expand_path(wiki.config.out_dir, "{name}.html")
173 else
174 return wiki.expand_path(parent.out_path, "{name}.html")
175 end
176 end
177
178 redef fun render do
179 super
180 if not is_dirty and not wiki.force_render then return
181 var file = out_full_path
182 wiki.message("Render article {name} -> {file}", 1)
183 file.dirname.mkdir
184 tpl_page.write_to_file file
185 end
186
187
188 # Load a template and resolve page-related macros
189 fun load_template(template_file: String): TemplateString do
190 var tpl = wiki.load_template(template_file)
191 if tpl.has_macro("ROOT_URL") then
192 tpl.replace("ROOT_URL", root_href)
193 end
194 return tpl
195 end
196
197 # Replace macros in the template by wiki data.
198 private fun tpl_page: TemplateString do
199 var tpl = load_template(template_file)
200 if tpl.has_macro("TOP_MENU") then
201 tpl.replace("TOP_MENU", tpl_menu)
202 end
203 if tpl.has_macro("HEADER") then
204 tpl.replace("HEADER", tpl_header)
205 end
206 if tpl.has_macro("BODY") then
207 tpl.replace("BODY", tpl_article)
208 end
209 if tpl.has_macro("FOOTER") then
210 tpl.replace("FOOTER", tpl_footer)
211 end
212 if tpl.has_macro("TRAIL") then
213 tpl.replace("TRAIL", tpl_trail)
214 end
215 return tpl
216 end
217
218 # Generate the HTML header for this article.
219 fun tpl_header: Writable do
220 var file = header_file
221 if not wiki.has_template(file) then return ""
222 return load_template(file)
223 end
224
225 # Generate the HTML page for this article.
226 fun tpl_article: TplArticle do
227 var article = new TplArticle
228 article.body = content
229 if wiki.config.auto_breadcrumbs then
230 article.breadcrumbs = new TplBreadcrumbs(self)
231 end
232 article.sidebar = tpl_sidebar
233 article.sidebar_pos = wiki.config.sidebar
234 return article
235 end
236
237 # Sidebar for this page.
238 var tpl_sidebar: TplSidebar is lazy do
239 var res = new TplSidebar
240 if wiki.config.auto_summary then
241 res.blocks.add tpl_summary
242 end
243 res.blocks.add_all sidebar.blocks
244 return res
245 end
246
247 # Generate the HTML summary for this article.
248 #
249 # Based on `headlines`
250 fun tpl_summary: Writable do
251 var headlines = self.headlines
252 var tpl = new Template
253 tpl.add "<ul class=\"summary list-unstyled\">"
254 var iter = headlines.iterator
255 while iter.is_ok do
256 var hl = iter.item
257 # parse title as markdown
258 var title = wiki.inline_md(hl.title)
259 tpl.add "<li><a href=\"#{hl.id}\">{title}</a>"
260 iter.next
261 if iter.is_ok then
262 if iter.item.level > hl.level then
263 tpl.add "<ul class=\"list-unstyled\">"
264 else if iter.item.level < hl.level then
265 tpl.add "</li>"
266 tpl.add "</ul>"
267 end
268 else
269 tpl.add "</li>"
270 end
271 end
272 tpl.add "</ul>"
273 return tpl
274 end
275
276 # Generate the HTML menu for this article.
277 fun tpl_menu: Writable do
278 var file = menu_file
279 if not wiki.has_template(file) then return ""
280 var tpl = load_template(file)
281 if tpl.has_macro("MENUS") then
282 var items = new Template
283 for child in wiki.root_section.children.values do
284 if child isa WikiArticle and child.is_index then continue
285 if child isa WikiSection and child.is_hidden then continue
286 items.add "<li"
287 if self == child or self.breadcrumbs.has(child) then
288 items.add " class=\"active\""
289 end
290 items.add ">"
291 items.add child.tpl_link(self)
292 items.add "</li>"
293 end
294 tpl.replace("MENUS", items)
295 end
296 return tpl
297 end
298
299 # Generate navigation links for the trail of this article, if any.
300 #
301 # A trail is generated if the article include or is included in a trail.
302 # See `wiki.trails` for details.
303 fun tpl_trail: Writable do
304 if not wiki.trails.has(self) then return ""
305
306 # Get the position of `self` in the trail
307 var flat = wiki.trails.to_a
308 var pos = flat.index_of(self)
309 assert pos >= 0
310
311 var res = new Template
312 res.add "<ul class=\"trail\">"
313 var parent = wiki.trails.parent(self)
314 # Up and prev are disabled on a root
315 if parent != null then
316 if pos > 0 then
317 var target = flat[pos-1]
318 res.add "<li>{target.a_from(self, "prev")}</li>"
319 end
320 res.add "<li>{parent.a_from(self, "up")}</li>"
321 end
322 if pos < flat.length - 1 then
323 var target = flat[pos+1]
324 # Only print the next if it is not a root
325 if target.parent != null then
326 res.add "<li>{target.a_from(self, "next")}</li>"
327 end
328 end
329 res.add "</ul>"
330
331 return res
332 end
333
334 # Generate the HTML footer for this article.
335 fun tpl_footer: Writable do
336 var file = footer_file
337 if not wiki.has_template(file) then return ""
338 var tpl = load_template(file)
339 var time = new Tm.gmtime
340 if tpl.has_macro("YEAR") then
341 tpl.replace("YEAR", (time.year + 1900).to_s)
342 end
343 if tpl.has_macro("GEN_TIME") then
344 tpl.replace("GEN_TIME", time.to_s)
345 end
346 if tpl.has_macro("LAST_CHANGES") then
347 var url = "{wiki.config.last_changes}{src_path or else ""}"
348 tpl.replace("LAST_CHANGES", url)
349 end
350 if tpl.has_macro("EDIT") then
351 var url = "{wiki.config.edit}{src_path or else ""}"
352 tpl.replace("EDIT", url)
353 end
354 return tpl
355 end
356 end
357
358 # A `WikiArticle` that contains the sitemap tree.
359 class WikiSitemap
360 super WikiArticle
361
362 redef fun tpl_article do
363 var article = new TplArticle.with_title("Sitemap")
364 article.body = new TplPageTree(wiki.root_section, -1)
365 return article
366 end
367
368 redef var is_dirty = false
369 end
370
371 # A `WikiArticle` that contains the section index tree.
372 redef class WikiSectionIndex
373
374 redef var is_dirty = false
375
376 redef fun tpl_article do
377 var article = new TplArticle.with_title(section.title)
378 article.body = new TplPageTree(section, -1)
379 article.breadcrumbs = new TplBreadcrumbs(self)
380 return article
381 end
382 end
383
384 # Article HTML output.
385 class TplArticle
386 super Template
387
388 # Article title.
389 var title: nullable Writable = null
390
391 # Article HTML body.
392 var body: nullable Writable = null
393
394 # Sidebar of this article (if any).
395 var sidebar: nullable TplSidebar = null
396
397 # Position of the sidebar.
398 #
399 # See `WikiConfig::sidebar`.
400 var sidebar_pos: String = "left"
401
402 # Breadcrumbs from wiki root to this article.
403 var breadcrumbs: nullable TplBreadcrumbs = null
404
405 # Init `self` with a `title`.
406 init with_title(title: Writable) do
407 self.title = title
408 end
409
410 redef fun rendering do
411 if sidebar_pos == "left" then render_sidebar
412 if sidebar == null then
413 add "<div class=\"col-sm-12 content\">"
414 else
415 add "<div class=\"col-sm-9 content\">"
416 end
417 if body != null then
418 add "<article>"
419 if breadcrumbs != null then
420 add breadcrumbs.as(not null)
421 end
422 if title != null then
423 add "<h1>"
424 add title.as(not null)
425 add "</h1>"
426 end
427 add body.as(not null)
428 add " </article>"
429 end
430 add "</div>"
431 if sidebar_pos == "right" then render_sidebar
432 end
433
434 private fun render_sidebar do
435 if sidebar == null then return
436 add "<div class=\"col-sm-3 sidebar\">"
437 add sidebar.as(not null)
438 add "</div>"
439 end
440 end
441
442 # A collection of HTML blocks displayed on the side of a page.
443 class TplSidebar
444 super Template
445
446 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
447 var blocks = new Array[Writable]
448
449 redef fun rendering do
450 for block in blocks do
451 add "<nav class=\"sideblock\">"
452 add block
453 add "</nav>"
454 end
455 end
456 end
457
458 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
459 class TplBreadcrumbs
460 super Template
461
462 # Bread crumb article.
463 var article: WikiArticle
464
465 redef fun rendering do
466 var path = article.breadcrumbs
467 if path.is_empty or path.length <= 2 and article.is_index then return
468 add "<ol class=\"breadcrumb\">"
469 for entry in path do
470 if entry == path.last then
471 add "<li class=\"active\">"
472 add entry.title
473 add "</li>"
474 else
475 if article.parent == entry and article.is_index then continue
476 add "<li>"
477 add entry.tpl_link(article)
478 add "</li>"
479 end
480 end
481 add "</ol>"
482 end
483 end
484
485 # An HTML tree that show the section pages structure.
486 class TplPageTree
487 super Template
488
489 # Builds the page tree from `root`.
490 var root: WikiSection
491
492 # Limits the tree depth to `max_depth` levels.
493 var max_depth: Int
494
495 redef fun rendering do
496 add "<ul>"
497 add root.tpl_tree(-1)
498 add "</ul>"
499 end
500 end