1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 import markdown
::decorators
23 # Render HTML output looking for changes in the markdown sources.
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
)
30 root_section
.add_child make_sitemap
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}"
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
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
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
)
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>"
70 redef class WikiSection
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
77 return wiki
.expand_path
(parent
.out_path
, name
)
82 if not is_dirty
and not wiki
.force_render
then return
86 sys
.system
"touch -- {out_full_path.escape_to_sh}"
89 wiki
.message
("Render section {name} -> {out_path}", 1)
92 var index
= self.index
93 if index
isa WikiSectionIndex then
94 wiki
.message
("Render auto-index for section {name} -> {out_path}", 1)
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
104 # Copy attached files from `src_path` to `out_path`.
105 private fun copy_files
do
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}"
119 redef fun tpl_link
(context
) do return index
.tpl_link
(context
)
121 # Render the section hierarchy as a html tree.
123 # `limit` is used to specify the max-depth of the tree.
125 # The generated tree will be something like this:
132 # <li>section 2.1</li>
133 # <li>section 2.2</li>
138 fun tpl_tree
(limit
: Int): Template do
139 return tpl_tree_intern
(limit
, 1, self)
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
147 out
.add tpl_link
(context
)
148 if (limit
< 0 or count
< limit
) and
149 (children
.length
> 1 or (children
.length
== 1)) then
151 for child
in children
.values
do
152 if child
== index
then continue
153 if child
isa WikiArticle then
155 out
.add child
.tpl_link
(context
)
157 else if child
isa WikiSection and not child
.is_hidden
then
158 out
.add child
.tpl_tree_intern
(limit
, count
+ 1, context
)
168 redef class WikiArticle
170 redef fun out_path
: String do
171 if parent
== null then
172 return wiki
.expand_path
(wiki
.config
.out_dir
, "{name}.html")
174 return wiki
.expand_path
(parent
.out_path
, "{name}.html")
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)
184 tpl_page
.write_to_file file
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
)
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
)
203 if tpl
.has_macro
("HEADER") then
204 tpl
.replace
("HEADER", tpl_header
)
206 if tpl
.has_macro
("BODY") then
207 tpl
.replace
("BODY", tpl_article
)
209 if tpl
.has_macro
("FOOTER") then
210 tpl
.replace
("FOOTER", tpl_footer
)
212 if tpl
.has_macro
("TRAIL") then
213 tpl
.replace
("TRAIL", tpl_trail
)
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
)
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)
232 article
.sidebar
= tpl_sidebar
233 article
.sidebar_pos
= wiki
.config
.sidebar
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
243 res
.blocks
.add_all sidebar
.blocks
247 # Generate the HTML summary for this article.
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
257 # parse title as markdown
258 var title
= wiki
.inline_md
(hl
.title
)
259 tpl
.add
"<li><a href=\"#{hl.id}\">{title}</a>"
262 if iter
.item
.level
> hl
.level
then
263 tpl
.add
"<ul class=\"list-unstyled\
">"
264 else if iter
.item
.level
< hl
.level
then
276 # Generate the HTML menu for this article.
277 fun tpl_menu
: Writable do
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
287 if self == child
or self.breadcrumbs
.has
(child
) then
288 items
.add
" class=\"active\
""
291 items
.add child
.tpl_link
(self)
294 tpl
.replace
("MENUS", items
)
299 # Generate navigation links for the trail of this article, if any.
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 ""
306 # Get the position of `self` in the trail
307 var flat
= wiki
.trails
.to_a
308 var pos
= flat
.index_of
(self)
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
317 var target
= flat
[pos-1
]
318 res
.add
"<li>{target.a_from(self, "prev")}</li>"
320 res
.add
"<li>{parent.a_from(self, "up")}</li>"
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>"
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
)
343 if tpl
.has_macro
("GEN_TIME") then
344 tpl
.replace
("GEN_TIME", time
.to_s
)
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
)
350 if tpl
.has_macro
("EDIT") then
351 var url
= "{wiki.config.edit}{src_path or else ""}"
352 tpl
.replace
("EDIT", url
)
358 # A `WikiArticle` that contains the sitemap tree.
362 redef fun tpl_article
do
363 var article
= new TplArticle.with_title
("Sitemap")
364 article
.body
= new TplPageTree(wiki
.root_section
, -1)
368 redef var is_dirty
= false
371 # A `WikiArticle` that contains the section index tree.
372 redef class WikiSectionIndex
374 redef var is_dirty
= false
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)
384 # Article HTML output.
389 var title
: nullable Writable = null
392 var body
: nullable Writable = null
394 # Sidebar of this article (if any).
395 var sidebar
: nullable TplSidebar = null
397 # Position of the sidebar.
399 # See `WikiConfig::sidebar`.
400 var sidebar_pos
: String = "left"
402 # Breadcrumbs from wiki root to this article.
403 var breadcrumbs
: nullable TplBreadcrumbs = null
405 # Init `self` with a `title`.
406 init with_title
(title
: Writable) do
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\
">"
415 add
"<div class=\"col-sm-9 content\
">"
419 if breadcrumbs
!= null then
420 add breadcrumbs
.as(not null)
422 if title
!= null then
424 add title
.as(not null)
427 add body
.as(not null)
431 if sidebar_pos
== "right" then render_sidebar
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)
442 # A collection of HTML blocks displayed on the side of a page.
446 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
447 var blocks
= new Array[Writable]
449 redef fun rendering
do
450 for block
in blocks
do
451 add
"<nav class=\"sideblock\
">"
458 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
462 # Bread crumb article.
463 var article
: WikiArticle
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\
">"
470 if entry
== path
.last
then
471 add
"<li class=\"active\
">"
475 if article
.parent
== entry
and article
.is_index
then continue
477 add entry
.tpl_link
(article
)
485 # An HTML tree that show the section pages structure.
489 # Builds the page tree from `root`.
490 var root
: WikiSection
492 # Limits the tree depth to `max_depth` levels.
495 redef fun rendering
do
497 add root
.tpl_tree
(-1)