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.
22 # Render HTML output looking for changes in the markdown sources.
25 if not root_section
.is_dirty
and not force_render
then return
26 var out_dir
= expand_path
(config
.root_dir
, config
.out_dir
)
29 root_section
.add_child make_sitemap
33 # Copy the asset directory to the (public) output directory.
34 private fun copy_assets
do
35 var src
= expand_path
(config
.root_dir
, config
.assets_dir
)
36 var out
= expand_path
(config
.root_dir
, config
.out_dir
)
37 if need_render
(src
, expand_path
(out
, config
.assets_dir
)) then
38 if src
.file_exists
then sys
.system
"cp -R {src} {out}"
42 # Build the wiki sitemap page.
43 private fun make_sitemap
: WikiSitemap do
44 var sitemap
= new WikiSitemap(self, "sitemap")
45 sitemap
.is_dirty
= true
51 # Get a `<a>` template link to `self`
52 fun tpl_link
: Writable do
53 return "<a href=\"{url}\
">{title}</a>"
57 redef class WikiSection
59 # Output directory (where to ouput the HTML pages for this section).
60 redef fun out_path
: String do
61 if parent
== null then
62 return wiki
.config
.out_dir
64 return wiki
.expand_path
(parent
.out_path
, name
)
69 if not is_dirty
and not wiki
.force_render
then return
73 sys
.system
"touch {out_full_path}"
76 wiki
.message
("Render section {out_path}", 1)
79 var index
= self.index
80 if index
isa WikiSectionIndex then
81 wiki
.message
("Render auto-index for section {out_path}", 1)
88 # Copy attached files from `src_path` to `out_path`.
89 private fun copy_files
do
91 var dir
= src_full_path
.to_s
92 for name
in dir
.files
do
93 if name
== wiki
.config_filename
then continue
94 if name
.has_suffix
(".md") then continue
95 if has_child
(name
) then continue
96 var src
= wiki
.expand_path
(dir
, name
)
97 var out
= wiki
.expand_path
(out_full_path
, name
)
98 if not wiki
.need_render
(src
, out
) then continue
99 sys
.system
"cp -R {src} {out_full_path}"
103 redef fun tpl_link
do return index
.tpl_link
105 # Render the section hierarchy as a html tree.
107 # `limit` is used to specify the max-depth of the tree.
109 # The generated tree will be something like this:
116 # <li>section 2.1</li>
117 # <li>section 2.2</li>
122 fun tpl_tree
(limit
: Int): Template do
123 return tpl_tree_intern
(limit
, 1)
126 # Build the template tree for this section recursively.
127 protected fun tpl_tree_intern
(limit
, count
: Int): Template do
128 var out
= new Template
132 if (limit
< 0 or count
< limit
) and
133 (children
.length
> 1 or (children
.length
== 1)) then
135 for child
in children
.values
do
136 if child
== index
then continue
137 if child
isa WikiArticle then
139 out
.add child
.tpl_link
141 else if child
isa WikiSection and not child
.is_hidden
then
142 out
.add child
.tpl_tree_intern
(limit
, count
+ 1)
152 redef class WikiArticle
154 redef fun out_path
: String do
155 if parent
== null then
156 return wiki
.expand_path
(wiki
.config
.out_dir
, "{name}.html")
158 return wiki
.expand_path
(parent
.out_path
, "{name}.html")
164 if not is_dirty
and not wiki
.force_render
then return
165 wiki
.message
("Render article {name}", 2)
166 var file
= out_full_path
168 tpl_page
.write_to_file file
172 # Replace macros in the template by wiki data.
173 private fun tpl_page
: TemplateString do
174 var tpl
= wiki
.load_template
(template_file
)
175 if tpl
.has_macro
("TOP_MENU") then
176 tpl
.replace
("TOP_MENU", tpl_menu
)
178 if tpl
.has_macro
("HEADER") then
179 tpl
.replace
("HEADER", tpl_header
)
181 if tpl
.has_macro
("BODY") then
182 tpl
.replace
("BODY", tpl_article
)
184 if tpl
.has_macro
("FOOTER") then
185 tpl
.replace
("FOOTER", tpl_footer
)
190 # Generate the HTML header for this article.
191 fun tpl_header
: Writable do
192 var file
= header_file
193 if not wiki
.has_template
(file
) then return ""
194 return wiki
.load_template
(file
)
197 # Generate the HTML page for this article.
198 fun tpl_article
: TplArticle do
199 var article
= new TplArticle
200 article
.body
= content
201 article
.breadcrumbs
= new TplBreadcrumbs(self)
202 tpl_sidebar
.blocks
.add tpl_summary
203 article
.sidebar
= tpl_sidebar
207 # Sidebar for this page.
208 var tpl_sidebar
= new TplSidebar
210 # Generate the HTML summary for this article.
212 # Based on `headlines`
213 fun tpl_summary
: Writable do
214 var headlines
= self.headlines
215 var tpl
= new Template
216 tpl
.add
"<ul class=\"summary list-unstyled\
">"
217 var iter
= headlines
.iterator
220 # parse title as markdown
221 var title
= hl
.title
.md_to_html
.to_s
222 title
= title
.substring
(3, title
.length
- 8)
223 tpl
.add
"<li><a href=\"#{hl.id}\">{title}</a>"
226 if iter
.item
.level
> hl
.level
then
227 tpl
.add
"<ul class=\"list-unstyled\
">"
228 else if iter
.item
.level
< hl
.level
then
240 # Generate the HTML menu for this article.
241 fun tpl_menu
: Writable do
243 if not wiki
.has_template
(file
) then return ""
244 var tpl
= wiki
.load_template
(file
)
245 if tpl
.has_macro
("MENUS") then
246 var items
= new Template
247 for child
in wiki
.root_section
.children
.values
do
248 if child
isa WikiArticle and child
.is_index
then continue
249 if child
isa WikiSection and child
.is_hidden
then continue
251 if self == child
or self.breadcrumbs
.has
(child
) then
252 items
.add
" class=\"active\
""
255 items
.add child
.tpl_link
258 tpl
.replace
("MENUS", items
)
263 # Generate the HTML footer for this article.
264 fun tpl_footer
: Writable do
265 var file
= footer_file
266 if not wiki
.has_template
(file
) then return ""
267 var tpl
= wiki
.load_template
(file
)
268 var time
= new Tm.gmtime
269 if tpl
.has_macro
("YEAR") then
270 tpl
.replace
("YEAR", (time
.year
+ 1900).to_s
)
272 if tpl
.has_macro
("GEN_TIME") then
273 tpl
.replace
("GEN_TIME", time
.to_s
)
279 # A `WikiArticle` that contains the sitemap tree.
283 redef fun tpl_article
do
284 var article
= new TplArticle.with_title
("Sitemap")
285 article
.body
= new TplPageTree(wiki
.root_section
, -1)
289 redef var is_dirty
= false
292 # A `WikiArticle` that contains the section index tree.
293 redef class WikiSectionIndex
295 redef var is_dirty
= false
297 redef fun tpl_article
do
298 var article
= new TplArticle.with_title
(section
.title
)
299 article
.body
= new TplPageTree(section
, -1)
300 article
.breadcrumbs
= new TplBreadcrumbs(self)
305 # Article HTML output.
310 var title
: nullable Writable = null
313 var body
: nullable Writable = null
315 # Sidebar of this article (if any).
316 var sidebar
: nullable TplSidebar = null
318 # Breadcrumbs from wiki root to this article.
319 var breadcrumbs
: nullable TplBreadcrumbs = null
321 # Init `self` with a `title`.
322 init with_title
(title
: Writable) do
326 redef fun rendering
do
327 if sidebar
!= null then
328 add
"<div class=\"col-sm-3 sidebar\
">"
329 add sidebar
.as(not null)
331 add
"<div class=\"col-sm-9 content\
">"
333 add
"<div class=\"col-sm-12 content\
">"
337 if breadcrumbs
!= null then
338 add breadcrumbs
.as(not null)
340 if title
!= null then
342 add title
.as(not null)
345 add body
.as(not null)
352 # A collection of HTML blocks displayed on the side of a page.
356 # Blocks are `Stremable` pieces that will be rendered in the sidebar.
357 var blocks
= new Array[Writable]
359 redef fun rendering
do
360 for block
in blocks
do
361 add
"<div class=\"sideblock\
">"
368 # An HTML breadcrumbs that show the path from a `WikiArticle` to the `Nitiwiki` root.
372 # Bread crumb article.
373 var article
: WikiArticle
375 redef fun rendering
do
376 var path
= article
.breadcrumbs
377 if path
.is_empty
or path
.length
<= 2 and article
.is_index
then return
378 add
"<ol class=\"breadcrumb\
">"
380 if entry
== path
.last
then
381 add
"<li class=\"active\
">"
385 if article
.parent
== entry
and article
.is_index
then continue
395 # An HTML tree that show the section pages structure.
399 # Builds the page tree from `root`.
400 var root
: WikiSection
402 # Limits the tree depth to `max_depth` levels.
405 redef fun rendering
do
407 add root
.tpl_tree
(-1)