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} {out}"
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
: Writable do
66 return "<a href=\"{url}\
">{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}"
89 wiki
.message
("Render section {out_path}", 1)
92 var index
= self.index
93 if index
isa WikiSectionIndex then
94 wiki
.message
("Render auto-index for section {out_path}", 1)
101 # Copy attached files from `src_path` to `out_path`.
102 private fun copy_files
do
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}"
116 redef fun tpl_link
do return index
.tpl_link
118 # Render the section hierarchy as a html tree.
120 # `limit` is used to specify the max-depth of the tree.
122 # The generated tree will be something like this:
129 # <li>section 2.1</li>
130 # <li>section 2.2</li>
135 fun tpl_tree
(limit
: Int): Template do
136 return tpl_tree_intern
(limit
, 1)
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
145 if (limit
< 0 or count
< limit
) and
146 (children
.length
> 1 or (children
.length
== 1)) then
148 for child
in children
.values
do
149 if child
== index
then continue
150 if child
isa WikiArticle then
152 out
.add child
.tpl_link
154 else if child
isa WikiSection and not child
.is_hidden
then
155 out
.add child
.tpl_tree_intern
(limit
, count
+ 1)
165 redef class WikiArticle
167 redef fun out_path
: String do
168 if parent
== null then
169 return wiki
.expand_path
(wiki
.config
.out_dir
, "{name}.html")
171 return wiki
.expand_path
(parent
.out_path
, "{name}.html")
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
181 tpl_page
.write_to_file file
185 # Load a template and resolve page-related macros
186 fun load_template
(template_file
: String): TemplateString do
187 var tpl
= wiki
.load_template
(template_file
)
191 # Replace macros in the template by wiki data.
192 private fun tpl_page
: TemplateString do
193 var tpl
= load_template
(template_file
)
194 if tpl
.has_macro
("TOP_MENU") then
195 tpl
.replace
("TOP_MENU", tpl_menu
)
197 if tpl
.has_macro
("HEADER") then
198 tpl
.replace
("HEADER", tpl_header
)
200 if tpl
.has_macro
("BODY") then
201 tpl
.replace
("BODY", tpl_article
)
203 if tpl
.has_macro
("FOOTER") then
204 tpl
.replace
("FOOTER", tpl_footer
)
209 # Generate the HTML header for this article.
210 fun tpl_header
: Writable do
211 var file
= header_file
212 if not wiki
.has_template
(file
) then return ""
213 return load_template
(file
)
216 # Generate the HTML page for this article.
217 fun tpl_article
: TplArticle do
218 var article
= new TplArticle
219 article
.body
= content
220 if wiki
.config
.auto_breadcrumbs
then
221 article
.breadcrumbs
= new TplBreadcrumbs(self)
223 article
.sidebar
= tpl_sidebar
224 article
.sidebar_pos
= wiki
.config
.sidebar
228 # Sidebar for this page.
229 var tpl_sidebar
: TplSidebar is lazy
do
230 var res
= new TplSidebar
231 if wiki
.config
.auto_summary
then
232 res
.blocks
.add tpl_summary
234 res
.blocks
.add_all sidebar
.blocks
238 # Generate the HTML summary for this article.
240 # Based on `headlines`
241 fun tpl_summary
: Writable do
242 var headlines
= self.headlines
243 var tpl
= new Template
244 tpl
.add
"<ul class=\"summary list-unstyled\
">"
245 var iter
= headlines
.iterator
248 # parse title as markdown
249 var title
= wiki
.inline_md
(hl
.title
)
250 tpl
.add
"<li><a href=\"#{hl.id}\">{title}</a>"
253 if iter
.item
.level
> hl
.level
then
254 tpl
.add
"<ul class=\"list-unstyled\
">"
255 else if iter
.item
.level
< hl
.level
then
267 # Generate the HTML menu for this article.
268 fun tpl_menu
: Writable do
270 if not wiki
.has_template
(file
) then return ""
271 var tpl
= load_template
(file
)
272 if tpl
.has_macro
("MENUS") then
273 var items
= new Template
274 for child
in wiki
.root_section
.children
.values
do
275 if child
isa WikiArticle and child
.is_index
then continue
276 if child
isa WikiSection and child
.is_hidden
then continue
278 if self == child
or self.breadcrumbs
.has
(child
) then
279 items
.add
" class=\"active\
""
282 items
.add child
.tpl_link
285 tpl
.replace
("MENUS", items
)
290 # Generate the HTML footer for this article.
291 fun tpl_footer
: Writable do
292 var file
= footer_file
293 if not wiki
.has_template
(file
) then return ""
294 var tpl
= load_template
(file
)
295 var time
= new Tm.gmtime
296 if tpl
.has_macro
("YEAR") then
297 tpl
.replace
("YEAR", (time
.year
+ 1900).to_s
)
299 if tpl
.has_macro
("GEN_TIME") then
300 tpl
.replace
("GEN_TIME", time
.to_s
)
302 if tpl
.has_macro
("LAST_CHANGES") then
303 var url
= "{wiki.config.last_changes}{src_path or else ""}"
304 tpl
.replace
("LAST_CHANGES", url
)
306 if tpl
.has_macro
("EDIT") then
307 var url
= "{wiki.config.edit}{src_path or else ""}"
308 tpl
.replace
("EDIT", url
)
314 # A `WikiArticle` that contains the sitemap tree.
318 redef fun tpl_article
do
319 var article
= new TplArticle.with_title
("Sitemap")
320 article
.body
= new TplPageTree(wiki
.root_section
, -1)
324 redef var is_dirty
= false
327 # A `WikiArticle` that contains the section index tree.
328 redef class WikiSectionIndex
330 redef var is_dirty
= false
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)
340 # Article HTML output.
345 var title
: nullable Writable = null
348 var body
: nullable Writable = null
350 # Sidebar of this article (if any).
351 var sidebar
: nullable TplSidebar = null
353 # Position of the sidebar.
355 # See `WikiConfig::sidebar`.
356 var sidebar_pos
: String = "left"
358 # Breadcrumbs from wiki root to this article.
359 var breadcrumbs
: nullable TplBreadcrumbs = null
361 # Init `self` with a `title`.
362 init with_title
(title
: Writable) do
366 redef fun rendering
do
367 if sidebar_pos
== "left" then render_sidebar
368 if sidebar
== null then
369 add
"<div class=\"col-sm-12 content\
">"
371 add
"<div class=\"col-sm-9 content\
">"
375 if breadcrumbs
!= null then
376 add breadcrumbs
.as(not null)
378 if title
!= null then
380 add title
.as(not null)
383 add body
.as(not null)
387 if sidebar_pos
== "right" then render_sidebar
390 private fun render_sidebar
do
391 if sidebar
== null then return
392 add
"<div class=\"col-sm-3 sidebar\
">"
393 add sidebar
.as(not null)
398 # A collection of HTML blocks displayed on the side of a page.
402 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
403 var blocks
= new Array[Writable]
405 redef fun rendering
do
406 for block
in blocks
do
407 add
"<nav class=\"sideblock\
">"
414 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
418 # Bread crumb article.
419 var article
: WikiArticle
421 redef fun rendering
do
422 var path
= article
.breadcrumbs
423 if path
.is_empty
or path
.length
<= 2 and article
.is_index
then return
424 add
"<ol class=\"breadcrumb\
">"
426 if entry
== path
.last
then
427 add
"<li class=\"active\
">"
431 if article
.parent
== entry
and article
.is_index
then continue
441 # An HTML tree that show the section pages structure.
445 # Builds the page tree from `root`.
446 var root
: WikiSection
448 # Limits the tree depth to `max_depth` levels.
451 redef fun rendering
do
453 add root
.tpl_tree
(-1)