X-Git-Url: http://nitlanguage.org diff --git a/contrib/nitiwiki/src/wiki_html.nit b/contrib/nitiwiki/src/wiki_html.nit index 7f213b7..1288d3c 100644 --- a/contrib/nitiwiki/src/wiki_html.nit +++ b/contrib/nitiwiki/src/wiki_html.nit @@ -15,12 +15,14 @@ # 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 @@ -34,7 +36,7 @@ redef class Nitiwiki var src = expand_path(config.root_dir, config.assets_dir) var out = expand_path(config.root_dir, config.out_dir) if need_render(src, expand_path(out, config.assets_dir)) then - if src.file_exists then sys.system "cp -R {src} {out}" + if src.file_exists then sys.system "cp -R -- {src.escape_to_sh} {out.escape_to_sh}" end end @@ -44,16 +46,24 @@ redef class Nitiwiki 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 `` template link to `self` - fun tpl_link: Streamable do - return "{title}" + fun tpl_link(context: WikiEntry): Writable do + return "{title}" end end @@ -73,17 +83,21 @@ redef class WikiSection if is_new then out_full_path.mkdir else - sys.system "touch {out_full_path}" + sys.system "touch -- {out_full_path.escape_to_sh}" end if has_source then - wiki.message("Render section {out_path}", 1) + wiki.message("Render section {name} -> {out_path}", 1) copy_files end var index = self.index if index isa WikiSectionIndex then + wiki.message("Render auto-index for section {name} -> {out_path}", 1) index.is_dirty = true add_child index end + # Hack: Force the rendering of `index` first so that trails are collected + # TODO: Add first-pass analysis to collect global information before doing the rendering + index.render super end @@ -98,22 +112,11 @@ redef class WikiSection var src = wiki.expand_path(dir, name) var out = wiki.expand_path(out_full_path, name) if not wiki.need_render(src, out) then continue - sys.system "cp -R {src} {out_full_path}" + sys.system "cp -R -- {src.escape_to_sh} {out_full_path.escape_to_sh}" 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, "index", self) - end - - redef fun tpl_link do return index.tpl_link + redef fun tpl_link(context) do return index.tpl_link(context) # Render the section hierarchy as a html tree. # @@ -133,15 +136,15 @@ redef class WikiSection # # ~~~ fun tpl_tree(limit: Int): Template do - return tpl_tree_intern(limit, 1) + return tpl_tree_intern(limit, 1, self) end # Build the template tree for this section recursively. - protected fun tpl_tree_intern(limit, count: Int): Template do + protected fun tpl_tree_intern(limit, count: Int, context: WikiEntry): Template do var out = new Template var index = index out.add "
  • " - out.add tpl_link + out.add tpl_link(context) if (limit < 0 or count < limit) and (children.length > 1 or (children.length == 1)) then out.add " " @@ -172,32 +175,31 @@ redef class WikiArticle 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 + wiki.message("Render article {name} -> {file}", 1) file.dirname.mkdir tpl_page.write_to_file file - super end + # Load a template and resolve page-related macros + fun load_template(template_file: String): TemplateString do + var tpl = wiki.load_template(template_file) + if tpl.has_macro("ROOT_URL") then + var root_dir = dhref.relpath("") + # Avoid issues if the macro is just followed by a `/` (as with url prefix) + if root_dir == "" then root_dir = "." + tpl.replace("ROOT_URL", root_dir) + end + return tpl + end + # Replace macros in the template by wiki data. private fun tpl_page: TemplateString do - var tpl = wiki.load_template(template_file) + var tpl = load_template(template_file) if tpl.has_macro("TOP_MENU") then tpl.replace("TOP_MENU", tpl_menu) end @@ -210,33 +212,45 @@ redef class WikiArticle if tpl.has_macro("FOOTER") then tpl.replace("FOOTER", tpl_footer) end + if tpl.has_macro("TRAIL") then + tpl.replace("TRAIL", tpl_trail) + end return tpl 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) + return load_template(file) end # Generate the HTML page for this article. 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 "