all: add `nitish` tag for code-blocks skipped by nitunit
[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_base
19
20 redef class Nitiwiki
21
22 # Render HTML output looking for changes in the markdown sources.
23 fun render do
24 if not root_section.is_dirty and not force_render then return
25 var out_dir = expand_path(config.root_dir, config.out_dir)
26 out_dir.mkdir
27 copy_assets
28 root_section.add_child make_sitemap
29 root_section.render
30 end
31
32 # Copy the asset directory to the (public) output directory.
33 private fun copy_assets do
34 var src = expand_path(config.root_dir, config.assets_dir)
35 var out = expand_path(config.root_dir, config.out_dir)
36 if need_render(src, expand_path(out, config.assets_dir)) then
37 if src.file_exists then sys.system "cp -R {src} {out}"
38 end
39 end
40
41 # Build the wiki sitemap page.
42 private fun make_sitemap: WikiSitemap do
43 var sitemap = new WikiSitemap(self, "sitemap")
44 sitemap.is_dirty = true
45 return sitemap
46 end
47 end
48
49 redef class WikiEntry
50
51 # Url to `self` once generated.
52 fun url: String do return wiki.config.root_url.join_path(breadcrumbs.join("/"))
53
54 # Get a `<a>` template link to `self`
55 fun tpl_link: Streamable do
56 return "<a href=\"{url}\">{title}</a>"
57 end
58 end
59
60 redef class WikiSection
61
62 # Output directory (where to ouput the HTML pages for this section).
63 redef fun out_path: String do
64 if parent == null then
65 return wiki.config.out_dir
66 else
67 return wiki.expand_path(parent.out_path, name)
68 end
69 end
70
71 redef fun render do
72 if not is_dirty and not wiki.force_render then return
73 if is_new then
74 out_full_path.mkdir
75 else
76 sys.system "touch {out_full_path}"
77 end
78 if has_source then
79 wiki.message("Render section {out_path}", 1)
80 copy_files
81 end
82 var index = self.index
83 if index isa WikiSectionIndex then
84 index.is_dirty = true
85 add_child index
86 end
87 super
88 end
89
90 # Copy attached files from `src_path` to `out_path`.
91 private fun copy_files do
92 assert has_source
93 var dir = src_full_path.to_s
94 for name in dir.files do
95 if name == wiki.config_filename then continue
96 if name.has_suffix(".md") then continue
97 if has_child(name) then continue
98 var src = wiki.expand_path(dir, name)
99 var out = wiki.expand_path(out_full_path, name)
100 if not wiki.need_render(src, out) then continue
101 sys.system "cp -R {src} {out_full_path}"
102 end
103 end
104
105 # The index page for this section.
106 #
107 # If no file `index.md` exists for this section,
108 # a summary is generated using contained articles.
109 fun index: WikiArticle is cached do
110 for child in children.values do
111 if child isa WikiArticle and child.is_index then return child
112 end
113 return new WikiSectionIndex(wiki, self)
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 url do
176 if parent == null then
177 return wiki.config.root_url.join_path("{name}.html")
178 else
179 return parent.url.join_path("{name}.html")
180 end
181 end
182
183 # Is `self` an index page?
184 #
185 # Checks if `self.name == "index"`.
186 fun is_index: Bool do return name == "index"
187
188 redef fun render do
189 if not is_dirty and not wiki.force_render then return
190 wiki.message("Render article {name}", 2)
191 var file = out_full_path
192 file.dirname.mkdir
193 tpl_page.write_to_file file
194 super
195 end
196
197
198 # Replace macros in the template by wiki data.
199 private fun tpl_page: TemplateString do
200 var tpl = wiki.load_template(template_file)
201 if tpl.has_macro("TOP_MENU") then
202 tpl.replace("TOP_MENU", tpl_menu)
203 end
204 if tpl.has_macro("HEADER") then
205 tpl.replace("HEADER", tpl_header)
206 end
207 if tpl.has_macro("BODY") then
208 tpl.replace("BODY", tpl_article)
209 end
210 if tpl.has_macro("FOOTER") then
211 tpl.replace("FOOTER", tpl_footer)
212 end
213 return tpl
214 end
215
216 # Generate the HTML header for this article.
217 fun tpl_header: Streamable do
218 var file = header_file
219 if not wiki.has_template(file) then return ""
220 return wiki.load_template(file)
221 end
222
223 # Generate the HTML page for this article.
224 fun tpl_article: TplArticle do
225 var article = new TplArticle
226 article.body = content
227 article.breadcrumbs = new TplBreadcrumbs(self)
228 tpl_sidebar.blocks.add tpl_summary
229 article.sidebar = tpl_sidebar
230 return article
231 end
232
233 # Sidebar for this page.
234 var tpl_sidebar = new TplSidebar
235
236 # Generate the HTML summary for this article.
237 #
238 # Based on `headlines`
239 fun tpl_summary: Streamable do
240 var headlines = self.headlines
241 var tpl = new Template
242 tpl.add "<ul class=\"summary list-unstyled\">"
243 var iter = headlines.iterator
244 while iter.is_ok do
245 var hl = iter.item
246 # parse title as markdown
247 var title = hl.title.md_to_html.to_s
248 title = title.substring(3, title.length - 8)
249 tpl.add "<li><a href=\"#{hl.id}\">{title}</a>"
250 iter.next
251 if iter.is_ok then
252 if iter.item.level > hl.level then
253 tpl.add "<ul class=\"list-unstyled\">"
254 else if iter.item.level < hl.level then
255 tpl.add "</li>"
256 tpl.add "</ul>"
257 end
258 else
259 tpl.add "</li>"
260 end
261 end
262 tpl.add "</ul>"
263 return tpl
264 end
265
266 # Generate the HTML menu for this article.
267 fun tpl_menu: Streamable do
268 var file = menu_file
269 if not wiki.has_template(file) then return ""
270 var tpl = wiki.load_template(file)
271 if tpl.has_macro("MENUS") then
272 var items = new Template
273 for child in wiki.root_section.children.values do
274 if child isa WikiArticle and child.is_index then continue
275 if child isa WikiSection and child.is_hidden then continue
276 items.add "<li"
277 if self == child or self.breadcrumbs.has(child) then
278 items.add " class=\"active\""
279 end
280 items.add ">"
281 items.add child.tpl_link
282 items.add "</li>"
283 end
284 tpl.replace("MENUS", items)
285 end
286 return tpl
287 end
288
289 # Generate the HTML footer for this article.
290 fun tpl_footer: Streamable do
291 var file = footer_file
292 if not wiki.has_template(file) then return ""
293 var tpl = wiki.load_template(file)
294 var time = new Tm.gmtime
295 if tpl.has_macro("YEAR") then
296 tpl.replace("YEAR", (time.year + 1900).to_s)
297 end
298 if tpl.has_macro("GEN_TIME") then
299 tpl.replace("GEN_TIME", time.to_s)
300 end
301 return tpl
302 end
303 end
304
305 # A `WikiArticle` that contains the sitemap tree.
306 class WikiSitemap
307 super WikiArticle
308
309 redef fun tpl_article do
310 var article = new TplArticle.with_title("Sitemap")
311 article.body = new TplPageTree(wiki.root_section, -1)
312 return article
313 end
314
315 redef var is_dirty = false
316 end
317
318 # A `WikiArticle` that contains the section index tree.
319 class WikiSectionIndex
320 super WikiArticle
321
322 # The section described by `self`.
323 var section: WikiSection
324
325 init(wiki: Nitiwiki, section: WikiSection) do
326 super(wiki, "index")
327 self.section = section
328 end
329
330 redef var is_dirty = false
331
332 redef fun tpl_article do
333 var article = new TplArticle.with_title(section.title)
334 article.body = new TplPageTree(section, -1)
335 article.breadcrumbs = new TplBreadcrumbs(self)
336 return article
337 end
338 end
339
340 # Article HTML output.
341 class TplArticle
342 super Template
343
344 var title: nullable Streamable = null
345 var body: nullable Streamable = null
346 var sidebar: nullable TplSidebar = null
347 var breadcrumbs: nullable TplBreadcrumbs = null
348
349 init with_title(title: Streamable) do
350 self.title = title
351 end
352
353 redef fun rendering do
354 if sidebar != null then
355 add "<div class=\"col-sm-3 sidebar\">"
356 add sidebar.as(not null)
357 add "</div>"
358 add "<div class=\"col-sm-9 content\">"
359 else
360 add "<div class=\"col-sm-12 content\">"
361 end
362 if body != null then
363 add "<article>"
364 if breadcrumbs != null then
365 add breadcrumbs.as(not null)
366 end
367 if title != null then
368 add "<h1>"
369 add title.as(not null)
370 add "</h1>"
371 end
372 add body.as(not null)
373 add " </article>"
374 end
375 add "</div>"
376 end
377 end
378
379 # A collection of HTML blocks displayed on the side of a page.
380 class TplSidebar
381 super Template
382
383 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
384 var blocks = new Array[Streamable]
385
386 redef fun rendering do
387 for block in blocks do
388 add "<div class=\"sideblock\">"
389 add block
390 add "</div>"
391 end
392 end
393 end
394
395 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
396 class TplBreadcrumbs
397 super Template
398
399 # Bread crumb article.
400 var article: WikiArticle
401
402 redef fun rendering do
403 var path = article.breadcrumbs
404 if path.is_empty or path.length <= 2 and article.is_index then return
405 add "<ol class=\"breadcrumb\">"
406 for entry in path do
407 if entry == path.last then
408 add "<li class=\"active\">"
409 add entry.title
410 add "</li>"
411 else
412 if article.parent == entry and article.is_index then continue
413 add "<li>"
414 add entry.tpl_link
415 add "</li>"
416 end
417 end
418 add "</ol>"
419 end
420 end
421
422 # An HTML tree that show the section pages structure.
423 class TplPageTree
424 super Template
425
426 # Builds the page tree from `root`.
427 var root: WikiSection
428
429 # Limits the tree depth to `max_depth` levels.
430 var max_depth: Int
431
432 redef fun rendering do
433 add "<ul>"
434 add root.tpl_tree(-1)
435 add "</ul>"
436 end
437 end