nitiwiki: introduce customizable sidebar
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 2 Jun 2015 04:47:02 +0000 (00:47 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 10 Jun 2015 00:39:03 +0000 (20:39 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/nitiwiki/src/wiki_base.nit
contrib/nitiwiki/src/wiki_html.nit
contrib/nitiwiki/src/wiki_links.nit

index c8df2d5..b96805b 100644 (file)
@@ -209,6 +209,26 @@ class Nitiwiki
                return tpl
        end
 
+       # Does a sideblock named `name` exists for this wiki?
+       fun has_sideblock(name: String): Bool do
+               name = "{name}.{config.md_ext}"
+               return expand_path(config.root_dir, config.sidebar_dir, name).file_exists
+       end
+
+       # Load a markdown block with `name` from `WikiConfig::sidebar_dir`.
+       private fun load_sideblock(name: String): nullable String do
+               if not has_sideblock(name) then
+                       message("Error: can't load sideblock `{name}`", 0)
+                       return null
+               end
+               name = "{name}.{config.md_ext}"
+               var path = expand_path(config.root_dir, config.sidebar_dir, name)
+               var file = new FileReader.open(path)
+               var res = file.read_all
+               file.close
+               return res
+       end
+
        # Join `parts` as a path and simplify it
        fun expand_path(parts: String...): String do
                var path = ""
@@ -324,6 +344,9 @@ abstract class WikiEntry
                return path.reversed
        end
 
+       # Sidebar relative to this wiki entry.
+       var sidebar = new WikiSidebar(self)
+
        # Relative path from `wiki.config.root_dir` to source if any.
        fun src_path: nullable String is abstract
 
@@ -560,6 +583,28 @@ class WikiArticle
        redef fun to_s do return "{name} ({parent or else "null"})"
 end
 
+# The sidebar is displayed in front of the main panel of a `WikiEntry`.
+class WikiSidebar
+
+       # Wiki used to parse sidebar blocks.
+       var wiki: Nitiwiki is lazy do return entry.wiki
+
+       # WikiEntry this panel is related to.
+       var entry: WikiEntry
+
+       # Blocks are ieces of markdown that will be rendered in the sidebar.
+       var blocks: Array[Text] is lazy do
+               var res = new Array[Text]
+               # TODO get blocks from the entry for more customization
+               for name in entry.wiki.config.sidebar_blocks do
+                       var block = wiki.load_sideblock(name)
+                       if block == null then continue
+                       res.add block
+               end
+               return res
+       end
+end
+
 # Wiki configuration class.
 #
 # This class provides services that ensure static typing when accessing the `config.ini` file.
@@ -713,6 +758,32 @@ class WikiConfig
                return value_or_default("wiki.sidebar", "left")
        end
 
+       # Sidebar markdown block to include.
+       #
+       # Blocks are specified by their filename without the extension.
+       #
+       # * key: `wiki.sidebar.blocks`
+       # * default: `[]`
+       var sidebar_blocks: Array[String] is lazy do
+               var res = new Array[String]
+               if not has_key("wiki.sidebar.blocks") then return res
+               for val in at("wiki.sidebar.blocks").values do
+                       res.add val
+               end
+               return res
+       end
+
+       # Sidebar files directory.
+       #
+       # Directory where sidebar blocks are stored.
+       # **This path MUST be relative to `root_dir`.**
+       #
+       # * key: `wiki.sidebar_dir`
+       # * default: `sidebar/`
+       var sidebar_dir: String is lazy do
+               return value_or_default("wiki.sidebar_dir", "sidebar/").simplify_path
+       end
+
        # Directory used by rsync to upload wiki files.
        #
        # This information is used to update your distant wiki files (like the webserver).
index 1b582ab..7f1ac4d 100644 (file)
@@ -212,14 +212,18 @@ redef class WikiArticle
                var article = new TplArticle
                article.body = content
                article.breadcrumbs = new TplBreadcrumbs(self)
-               tpl_sidebar.blocks.add tpl_summary
                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
+               res.blocks.add tpl_summary
+               res.blocks.add_all sidebar.blocks
+               return res
+       end
 
        # Generate the HTML summary for this article.
        #
@@ -382,9 +386,9 @@ class TplSidebar
 
        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
index 5235699..2e9cf59 100644 (file)
@@ -95,6 +95,7 @@ redef class WikiEntry
        redef fun render do
                super
                if not is_dirty and not wiki.force_render then return
+               render_sidebar_wikilinks
        end
 
        # Search in `self` then `self.children` if an entry has the name `name`.
@@ -122,6 +123,15 @@ redef class WikiEntry
        private var md_proc: NitiwikiMdProcessor is lazy do
                return new NitiwikiMdProcessor(wiki, self)
        end
+
+       # Process wikilinks from sidebar.
+       private fun render_sidebar_wikilinks do
+               var blocks = sidebar.blocks
+               for i in [0..blocks.length[ do
+                       blocks[i] = md_proc.process(blocks[i].to_s).write_to_string
+                       md_proc.emitter.decorator.headlines.clear
+               end
+       end
 end
 
 redef class WikiSection