# HTML wiki rendering
module wiki_html
-import wiki_base
+import wiki_links
+import markdown::decorators
redef class Nitiwiki
# Render HTML output looking for changes in the markdown sources.
- fun render do
+ redef fun render do
+ super
if not root_section.is_dirty and not force_render then return
var out_dir = expand_path(config.root_dir, config.out_dir)
out_dir.mkdir
sitemap.is_dirty = true
return sitemap
end
-end
-redef class WikiEntry
+ # Markdown processor used for inline element such as titles in TOC.
+ private var inline_processor: MarkdownProcessor is lazy do
+ var proc = new MarkdownProcessor
+ proc.emitter.decorator = new InlineDecorator
+ return proc
+ end
- # Url to `self` once generated.
- fun url: String do return wiki.config.root_url.join_path(breadcrumbs.join("/"))
+ # Inline markdown (remove h1, p, ... elements).
+ private fun inline_md(md: Writable): Writable do
+ return inline_processor.process(md.write_to_string)
+ end
+end
+redef class WikiEntry
# Get a `<a>` template link to `self`
- fun tpl_link: Streamable do
+ fun tpl_link: Writable do
return "<a href=\"{url}\">{title}</a>"
end
end
end
var index = self.index
if index isa WikiSectionIndex then
+ wiki.message("Render auto-index for section {out_path}", 1)
index.is_dirty = true
add_child index
end
end
end
- # The index page for this section.
- #
- # If no file `index.md` exists for this section,
- # a summary is generated using contained articles.
- fun index: WikiArticle is cached do
- for child in children.values do
- if child isa WikiArticle and child.is_index then return child
- end
- return new WikiSectionIndex(wiki, self)
- end
-
redef fun tpl_link do return index.tpl_link
# Render the section hierarchy as a html tree.
end
end
- redef fun url do
- if parent == null then
- return wiki.config.root_url.join_path("{name}.html")
- else
- return parent.url.join_path("{name}.html")
- end
- end
-
- # Is `self` an index page?
- #
- # Checks if `self.name == "index"`.
- fun is_index: Bool do return name == "index"
-
redef fun render do
+ super
if not is_dirty and not wiki.force_render then return
wiki.message("Render article {name}", 2)
var file = out_full_path
file.dirname.mkdir
tpl_page.write_to_file file
- super
end
end
# Generate the HTML header for this article.
- fun tpl_header: Streamable do
+ fun tpl_header: Writable do
var file = header_file
if not wiki.has_template(file) then return ""
return wiki.load_template(file)
fun tpl_article: TplArticle do
var article = new TplArticle
article.body = content
- article.breadcrumbs = new TplBreadcrumbs(self)
- tpl_sidebar.blocks.add tpl_summary
+ if wiki.config.auto_breadcrumbs then
+ article.breadcrumbs = new TplBreadcrumbs(self)
+ end
article.sidebar = tpl_sidebar
+ article.sidebar_pos = wiki.config.sidebar
return article
end
# Sidebar for this page.
- var tpl_sidebar = new TplSidebar
+ var tpl_sidebar: TplSidebar is lazy do
+ var res = new TplSidebar
+ if wiki.config.auto_summary then
+ res.blocks.add tpl_summary
+ end
+ res.blocks.add_all sidebar.blocks
+ return res
+ end
# Generate the HTML summary for this article.
#
# Based on `headlines`
- fun tpl_summary: Streamable do
+ fun tpl_summary: Writable do
var headlines = self.headlines
var tpl = new Template
tpl.add "<ul class=\"summary list-unstyled\">"
while iter.is_ok do
var hl = iter.item
# parse title as markdown
- var title = hl.title.md_to_html.to_s
- title = title.substring(3, title.length - 8)
+ var title = wiki.inline_md(hl.title)
tpl.add "<li><a href=\"#{hl.id}\">{title}</a>"
iter.next
if iter.is_ok then
end
# Generate the HTML menu for this article.
- fun tpl_menu: Streamable do
+ fun tpl_menu: Writable do
var file = menu_file
if not wiki.has_template(file) then return ""
var tpl = wiki.load_template(file)
end
# Generate the HTML footer for this article.
- fun tpl_footer: Streamable do
+ fun tpl_footer: Writable do
var file = footer_file
if not wiki.has_template(file) then return ""
var tpl = wiki.load_template(file)
if tpl.has_macro("GEN_TIME") then
tpl.replace("GEN_TIME", time.to_s)
end
+ if tpl.has_macro("LAST_CHANGES") then
+ var url = "{wiki.config.last_changes}{src_path or else ""}"
+ tpl.replace("LAST_CHANGES", url)
+ end
+ if tpl.has_macro("EDIT") then
+ var url = "{wiki.config.edit}{src_path or else ""}"
+ tpl.replace("EDIT", url)
+ end
return tpl
end
end
end
# A `WikiArticle` that contains the section index tree.
-class WikiSectionIndex
- super WikiArticle
-
- # The section described by `self`.
- var section: WikiSection
-
- init(wiki: Nitiwiki, section: WikiSection) do
- super(wiki, "index")
- self.section = section
- end
+redef class WikiSectionIndex
redef var is_dirty = false
class TplArticle
super Template
- var title: nullable Streamable = null
- var body: nullable Streamable = null
+ # Article title.
+ var title: nullable Writable = null
+
+ # Article HTML body.
+ var body: nullable Writable = null
+
+ # Sidebar of this article (if any).
var sidebar: nullable TplSidebar = null
+
+ # Position of the sidebar.
+ #
+ # See `WikiConfig::sidebar`.
+ var sidebar_pos: String = "left"
+
+ # Breadcrumbs from wiki root to this article.
var breadcrumbs: nullable TplBreadcrumbs = null
- init with_title(title: Streamable) do
+ # Init `self` with a `title`.
+ init with_title(title: Writable) do
self.title = title
end
redef fun rendering do
- if sidebar != null then
- add "<div class=\"col-sm-3 sidebar\">"
- add sidebar.as(not null)
- add "</div>"
- add "<div class=\"col-sm-9 content\">"
- else
+ if sidebar_pos == "left" then render_sidebar
+ if sidebar == null then
add "<div class=\"col-sm-12 content\">"
+ else
+ add "<div class=\"col-sm-9 content\">"
end
if body != null then
add "<article>"
add " </article>"
end
add "</div>"
+ if sidebar_pos == "right" then render_sidebar
+ end
+
+ private fun render_sidebar do
+ if sidebar == null then return
+ add "<div class=\"col-sm-3 sidebar\">"
+ add sidebar.as(not null)
+ add "</div>"
end
end
super Template
# Blocks are `Stremable` pieces that will be rendered in the sidebar.
- var blocks = new Array[Streamable]
+ var blocks = new Array[Writable]
redef fun rendering do
for block in blocks do
- add "<div class=\"sideblock\">"
+ add "<nav class=\"sideblock\">"
add block
- add "</div>"
+ add "</nav>"
end
end
end