nitiwiki: implements html generation.
[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 # <ul>
125 # <li>section 1</li>
126 # <li>section 2
127 # <ul>
128 # <li>section 2.1</li>
129 # <li>section 2.2</li>
130 # </ul>
131 # </li>
132 # </ul>
133 fun tpl_tree(limit: Int): Template do
134 return tpl_tree_intern(limit, 1)
135 end
136
137 # Build the template tree for this section recursively.
138 protected fun tpl_tree_intern(limit, count: Int): Template do
139 var out = new Template
140 var index = index
141 out.add "<li>"
142 out.add tpl_link
143 if (limit < 0 or count < limit) and
144 (children.length > 1 or (children.length == 1)) then
145 out.add " <ul>"
146 for child in children.values do
147 if child == index then continue
148 if child isa WikiArticle then
149 out.add "<li>"
150 out.add child.tpl_link
151 out.add "</li>"
152 else if child isa WikiSection and not child.is_hidden then
153 out.add child.tpl_tree_intern(limit, count + 1)
154 end
155 end
156 out.add " </ul>"
157 end
158 out.add "</li>"
159 return out
160 end
161 end
162
163 redef class WikiArticle
164
165 redef fun out_path: String do
166 if parent == null then
167 return wiki.expand_path(wiki.config.out_dir, "{name}.html")
168 else
169 return wiki.expand_path(parent.out_path, "{name}.html")
170 end
171 end
172
173 redef fun url do
174 if parent == null then
175 return wiki.config.root_url.join_path("{name}.html")
176 else
177 return parent.url.join_path("{name}.html")
178 end
179 end
180
181 # Is `self` an index page?
182 #
183 # Checks if `self.name == "index"`.
184 fun is_index: Bool do return name == "index"
185
186 redef fun render do
187 if not is_dirty and not wiki.force_render then return
188 wiki.message("Render article {name}", 2)
189 var file = out_full_path
190 file.dirname.mkdir
191 tpl_page.write_to_file file
192 super
193 end
194
195
196 # Replace macros in the template by wiki data.
197 private fun tpl_page: TemplateString do
198 var tpl = wiki.load_template(template_file)
199 if tpl.has_macro("TOP_MENU") then
200 tpl.replace("TOP_MENU", tpl_menu)
201 end
202 if tpl.has_macro("HEADER") then
203 tpl.replace("HEADER", tpl_header)
204 end
205 if tpl.has_macro("BODY") then
206 tpl.replace("BODY", tpl_article)
207 end
208 if tpl.has_macro("FOOTER") then
209 tpl.replace("FOOTER", tpl_footer)
210 end
211 return tpl
212 end
213
214 # Generate the HTML header for this article.
215 fun tpl_header: Streamable do
216 var file = header_file
217 if not wiki.has_template(file) then return ""
218 return wiki.load_template(file)
219 end
220
221 # Generate the HTML page for this article.
222 fun tpl_article: TplArticle do
223 var article = new TplArticle
224 article.body = content
225 article.breadcrumbs = new TplBreadcrumbs(self)
226 tpl_sidebar.blocks.add tpl_summary
227 article.sidebar = tpl_sidebar
228 return article
229 end
230
231 # Sidebar for this page.
232 var tpl_sidebar = new TplSidebar
233
234 # Generate the HTML summary for this article.
235 #
236 # Based on `headlines`
237 fun tpl_summary: Streamable do
238 var headlines = self.headlines
239 var tpl = new Template
240 tpl.add "<ul class=\"summary list-unstyled\">"
241 var iter = headlines.iterator
242 while iter.is_ok do
243 var hl = iter.item
244 # parse title as markdown
245 var title = hl.title.md_to_html.to_s
246 title = title.substring(3, title.length - 8)
247 tpl.add "<li><a href=\"#{hl.id}\">{title}</a>"
248 iter.next
249 if iter.is_ok then
250 if iter.item.level > hl.level then
251 tpl.add "<ul class=\"list-unstyled\">"
252 else if iter.item.level < hl.level then
253 tpl.add "</li>"
254 tpl.add "</ul>"
255 end
256 else
257 tpl.add "</li>"
258 end
259 end
260 tpl.add "</ul>"
261 return tpl
262 end
263
264 # Generate the HTML menu for this article.
265 fun tpl_menu: Streamable do
266 var file = menu_file
267 if not wiki.has_template(file) then return ""
268 var tpl = wiki.load_template(file)
269 if tpl.has_macro("MENUS") then
270 var items = new Template
271 for child in wiki.root_section.children.values do
272 if child isa WikiArticle and child.is_index then continue
273 if child isa WikiSection and child.is_hidden then continue
274 items.add "<li"
275 if self == child or self.breadcrumbs.has(child) then
276 items.add " class=\"active\""
277 end
278 items.add ">"
279 items.add child.tpl_link
280 items.add "</li>"
281 end
282 tpl.replace("MENUS", items)
283 end
284 return tpl
285 end
286
287 # Generate the HTML footer for this article.
288 fun tpl_footer: Streamable do
289 var file = footer_file
290 if not wiki.has_template(file) then return ""
291 var tpl = wiki.load_template(file)
292 var time = new Tm.gmtime
293 if tpl.has_macro("YEAR") then
294 tpl.replace("YEAR", (time.year + 1900).to_s)
295 end
296 if tpl.has_macro("GEN_TIME") then
297 tpl.replace("GEN_TIME", time.to_s)
298 end
299 return tpl
300 end
301 end
302
303 # A `WikiArticle` that contains the sitemap tree.
304 class WikiSitemap
305 super WikiArticle
306
307 redef fun tpl_article do
308 var article = new TplArticle.with_title("Sitemap")
309 article.body = new TplPageTree(wiki.root_section, -1)
310 return article
311 end
312
313 redef var is_dirty = false
314 end
315
316 # A `WikiArticle` that contains the section index tree.
317 class WikiSectionIndex
318 super WikiArticle
319
320 # The section described by `self`.
321 var section: WikiSection
322
323 init(wiki: Nitiwiki, section: WikiSection) do
324 super(wiki, "index")
325 self.section = section
326 end
327
328 redef var is_dirty = false
329
330 redef fun tpl_article do
331 var article = new TplArticle.with_title(section.title)
332 article.body = new TplPageTree(section, -1)
333 article.breadcrumbs = new TplBreadcrumbs(self)
334 return article
335 end
336 end
337
338 # Article HTML output.
339 class TplArticle
340 super Template
341
342 var title: nullable Streamable = null
343 var body: nullable Streamable = null
344 var sidebar: nullable TplSidebar = null
345 var breadcrumbs: nullable TplBreadcrumbs = null
346
347 init with_title(title: Streamable) do
348 self.title = title
349 end
350
351 redef fun rendering do
352 if sidebar != null then
353 add "<div class=\"col-sm-3 sidebar\">"
354 add sidebar.as(not null)
355 add "</div>"
356 add "<div class=\"col-sm-9 content\">"
357 else
358 add "<div class=\"col-sm-12 content\">"
359 end
360 if body != null then
361 add "<article>"
362 if breadcrumbs != null then
363 add breadcrumbs.as(not null)
364 end
365 if title != null then
366 add "<h1>"
367 add title.as(not null)
368 add "</h1>"
369 end
370 add body.as(not null)
371 add " </article>"
372 end
373 add "</div>"
374 end
375 end
376
377 # A collection of HTML blocks displayed on the side of a page.
378 class TplSidebar
379 super Template
380
381 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
382 var blocks = new Array[Streamable]
383
384 redef fun rendering do
385 for block in blocks do
386 add "<div class=\"sideblock\">"
387 add block
388 add "</div>"
389 end
390 end
391 end
392
393 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
394 class TplBreadcrumbs
395 super Template
396
397 # Bread crumb article.
398 var article: WikiArticle
399
400 redef fun rendering do
401 var path = article.breadcrumbs
402 if path.is_empty or path.length <= 2 and article.is_index then return
403 add "<ol class=\"breadcrumb\">"
404 for entry in path do
405 if entry == path.last then
406 add "<li class=\"active\">"
407 add entry.title
408 add "</li>"
409 else
410 if article.parent == entry and article.is_index then continue
411 add "<li>"
412 add entry.tpl_link
413 add "</li>"
414 end
415 end
416 add "</ol>"
417 end
418 end
419
420 # An HTML tree that show the section pages structure.
421 class TplPageTree
422 super Template
423
424 # Builds the page tree from `root`.
425 var root: WikiSection
426
427 # Limits the tree depth to `max_depth` levels.
428 var max_depth: Int
429
430 redef fun rendering do
431 add "<ul>"
432 add root.tpl_tree(-1)
433 add "</ul>"
434 end
435 end