Merge: More keep going
[nit.git] / contrib / nitiwiki / src / wiki_html.nit
index 89546ce..46518aa 100644 (file)
 # 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
@@ -44,15 +46,23 @@ 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 `<a>` template link to `self`
-       fun tpl_link: Streamable do
+       fun tpl_link: Writable do
                return "<a href=\"{url}\">{title}</a>"
        end
 end
@@ -103,17 +113,6 @@ redef class WikiSection
                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
 
        # Render the section hierarchy as a html tree.
@@ -173,26 +172,13 @@ 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
                file.dirname.mkdir
                tpl_page.write_to_file file
-               super
        end
 
 
@@ -215,7 +201,7 @@ redef class WikiArticle
        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)
@@ -237,7 +223,7 @@ redef class WikiArticle
        # 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\">"
@@ -245,8 +231,7 @@ redef class WikiArticle
                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
@@ -265,7 +250,7 @@ redef class WikiArticle
        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)
@@ -288,7 +273,7 @@ redef class WikiArticle
        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)
@@ -317,15 +302,7 @@ class WikiSitemap
 end
 
 # A `WikiArticle` that contains the section index tree.
-class WikiSectionIndex
-       super WikiArticle
-
-       # The section described by `self`.
-       var section: WikiSection
-
-       redef fun title do return section.title
-
-       redef fun url do return section.url
+redef class WikiSectionIndex
 
        redef var is_dirty = false
 
@@ -342,10 +319,10 @@ class TplArticle
        super Template
 
        # Article title.
-       var title: nullable Streamable = null
+       var title: nullable Writable = null
 
        # Article HTML body.
-       var body: nullable Streamable = null
+       var body: nullable Writable = null
 
        # Sidebar of this article (if any).
        var sidebar: nullable TplSidebar = null
@@ -354,7 +331,7 @@ class TplArticle
        var breadcrumbs: nullable TplBreadcrumbs = null
 
        # Init `self` with a `title`.
-       init with_title(title: Streamable) do
+       init with_title(title: Writable) do
                self.title = title
        end
 
@@ -389,7 +366,7 @@ class TplSidebar
        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