nitiwiki: render trail navigation on the macro-placeholder %`TRAIL%`
authorJean Privat <jean@pryen.org>
Wed, 11 Nov 2015 16:17:34 +0000 (11:17 -0500)
committerJean Privat <jean@pryen.org>
Wed, 11 Nov 2015 20:29:50 +0000 (15:29 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

contrib/nitiwiki/src/wiki_html.nit

index 2e96567..05c06f9 100644 (file)
@@ -95,6 +95,9 @@ redef class WikiSection
                        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
 
@@ -209,6 +212,9 @@ 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
 
@@ -293,6 +299,37 @@ redef class WikiArticle
                return tpl
        end
 
+       # Generate navigation links for the trail of this article, if any.
+       #
+       # A trail is generated if the article include or is included in a trail.
+       # See `wiki.trails` for details.
+       fun tpl_trail: Writable do
+               if not wiki.trails.has(self) then return ""
+
+               # Get the position of `self` in the trail
+               var flat = wiki.trails.to_a
+               var pos = flat.index_of(self)
+               assert pos >= 0
+
+               var res = new Template
+               res.add "<ul class=\"trail\">"
+               if pos > 0 then
+                       var target = flat[pos-1]
+                       res.add "<li>{target.a_from(self, "prev")}</li>"
+               end
+               var parent = wiki.trails.parent(self)
+               if parent != null then
+                       res.add "<li>{parent.a_from(self, "up")}</li>"
+               end
+               if pos < flat.length - 1 then
+                       var target = flat[pos+1]
+                       res.add "<li>{target.a_from(self, "next")}</li>"
+               end
+               res.add "</ul>"
+
+               return res
+       end
+
        # Generate the HTML footer for this article.
        fun tpl_footer: Writable do
                var file = footer_file