nitiwiki: introduce customizable sidebar
[nit.git] / contrib / nitiwiki / src / wiki_base.nit
index 1527d4c..b96805b 100644 (file)
@@ -16,7 +16,6 @@
 module wiki_base
 
 import template::macro
-import markdown
 import opts
 import ini
 
@@ -72,6 +71,9 @@ class Nitiwiki
                end
        end
 
+       # Render output.
+       fun render do end
+
        # Show wiki status.
        fun status do
                print "nitiWiki"
@@ -105,7 +107,7 @@ class Nitiwiki
                end
        end
 
-       # Display msg if `level >= verbose_level`
+       # Display msg if `level <= verbose_level`
        fun message(msg: String, level: Int) do
                if level <= verbose_level then print msg
        end
@@ -113,11 +115,11 @@ class Nitiwiki
        # List markdown source files from a directory.
        fun list_md_files(dir: String): Array[String] do
                var files = new Array[String]
-               var pipe = new ProcessReader("find", dir, "-name", "*.md")
+               var pipe = new ProcessReader("find", dir, "-name", "*.{config.md_ext}")
                while not pipe.eof do
                        var file = pipe.read_line
                        if file == "" then break # last line
-                       var name = file.basename(".md")
+                       var name = file.basename(".{config.md_ext}")
                        if name == "header" or name == "footer" or name == "menu" then continue
                        files.add file
                end
@@ -163,6 +165,7 @@ class Nitiwiki
        # `path` is used to determine the ancestor sections.
        protected fun new_article(path: String): WikiArticle do
                if entries.has_key(path) then return entries[path].as(WikiArticle)
+               message("Found article `{path}`", 2)
                var article = new WikiArticle.from_source(self, path)
                var section = new_section(path.dirname)
                section.add_child(article)
@@ -185,7 +188,10 @@ class Nitiwiki
        #
        # REQUIRE: `has_template`
        fun load_template(name: String): TemplateString do
-               assert has_template(name)
+               if not has_template(name) then
+                       message("Error: can't load template `{name}`", 0)
+                       exit 1
+               end
                var file = expand_path(config.root_dir, config.templates_dir, name)
                var tpl = new TemplateString.from_file(file)
                if tpl.has_macro("ROOT_URL") then
@@ -203,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 = ""
@@ -318,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
 
@@ -447,7 +476,7 @@ class WikiSection
        private fun try_load_config do
                var cfile = wiki.expand_path(wiki.config.root_dir, src_path, wiki.config_filename)
                if not cfile.file_exists then return
-               wiki.message("Custom config for section {name}", 2)
+               wiki.message("Custom config for section {name}", 1)
                config = new SectionConfig(cfile)
        end
 
@@ -512,18 +541,13 @@ class WikiArticle
        # Page content.
        #
        # What you want to be displayed in the page.
-       var content: nullable Writable = null
+       var content: nullable Writable = null is writable
 
-       # Headlines ids and titles.
-       var headlines = new ArrayMap[String, HeadLine]
-
-       # Create a new articleu sing a markdown source file.
+       # Create a new article using a markdown source file.
        init from_source(wiki: Nitiwiki, md_file: String) do
                src_full_path = md_file
-               init(wiki, md_file.basename(".md"))
-               var md_proc = new MarkdownProcessor
-               content = md_proc.process(md)
-               headlines = md_proc.emitter.decorator.headlines
+               init(wiki, md_file.basename(".{wiki.config.md_ext}"))
+               content = md
        end
 
        redef var src_full_path: nullable String = null
@@ -538,8 +562,8 @@ class WikiArticle
        # Extract the markdown text from `source_file`.
        #
        # REQUIRE: `has_source`.
-       var md: String is lazy do
-               assert has_source
+       var md: nullable String is lazy do
+               if not has_source then return null
                var file = new FileReader.open(src_full_path.to_s)
                var md = file.read_all
                file.close
@@ -559,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.
@@ -601,6 +647,14 @@ class WikiConfig
        # * default: `http://localhost/`
        var root_url: String is lazy do return value_or_default("wiki.root_url", "http://localhost/")
 
+       # Markdown extension recognized by this wiki.
+       #
+       # We allow only one kind of extension per wiki.
+       # Files with other markdown extensions will be treated as resources.
+       #
+       # * key: `wiki.md_ext`
+       # * default: `md`
+       var md_ext: String is lazy do return value_or_default("wiki.md_ext", "md")
 
        # Root directory of the wiki.
        #
@@ -693,6 +747,43 @@ class WikiConfig
                return value_or_default("wiki.footer", "footer.html")
        end
 
+       # Sidebar position.
+       #
+       # Position of the sidebar between `left`, `right` and `none`. Any other value
+       # will be considered as `none`.
+       #
+       # * key: `wiki.sidebar`
+       # * default: `left`
+       var sidebar: String is lazy do
+               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).