nitiwiki: change position of sidebar from config
[nit.git] / contrib / nitiwiki / src / wiki_base.nit
index d5db709..c8df2d5 100644 (file)
@@ -16,7 +16,6 @@
 module wiki_base
 
 import template::macro
-import markdown
 import opts
 import ini
 
@@ -54,6 +53,7 @@ class Nitiwiki
                sys.system "rsync -vr --delete {root}/ {config.rsync_dir}"
        end
 
+       # Pull data from git repository.
        fun fetch do
                sys.system "git pull {config.git_origin} {config.git_branch}"
        end
@@ -71,6 +71,9 @@ class Nitiwiki
                end
        end
 
+       # Render output.
+       fun render do end
+
        # Show wiki status.
        fun status do
                print "nitiWiki"
@@ -104,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
@@ -112,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 IProcess("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
@@ -162,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)
@@ -184,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
@@ -211,6 +218,9 @@ class Nitiwiki
                return path.simplify_path
        end
 
+       # Transform an id style name into a pretty printed name.
+       #
+       # Used to translate ids in beautiful page names.
        fun pretty_name(name: String): String do
                name = name.replace("_", " ")
                name = name.capitalized
@@ -304,7 +314,7 @@ abstract class WikiEntry
        # Result is returned as an array containg ordered entries:
        # `breadcrumbs.first` is the root entry and
        # `breadcrumbs.last == self`
-       fun breadcrumbs: Array[WikiEntry] is cached do
+       var breadcrumbs: Array[WikiEntry] is lazy do
                var path = new Array[WikiEntry]
                var entry: nullable WikiEntry = self
                while entry != null and not entry.is_root do
@@ -443,7 +453,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
 
@@ -508,18 +518,13 @@ class WikiArticle
        # Page content.
        #
        # What you want to be displayed in the page.
-       var content: nullable Streamable = 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
@@ -534,9 +539,9 @@ class WikiArticle
        # Extract the markdown text from `source_file`.
        #
        # REQUIRE: `has_source`.
-       fun md: String is cached do
-               assert has_source
-               var file = new IFStream.open(src_full_path.to_s)
+       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
                return md
@@ -573,7 +578,7 @@ class WikiConfig
        #
        # * key: `wiki.name`
        # * default: `MyWiki`
-       fun wiki_name: String is cached do return value_or_default("wiki.name", "MyWiki")
+       var wiki_name: String is lazy do return value_or_default("wiki.name", "MyWiki")
 
        # Site description.
        #
@@ -581,7 +586,7 @@ class WikiConfig
        #
        # * key: `wiki.desc`
        # * default: ``
-       fun wiki_desc: String is cached do return value_or_default("wiki.desc", "")
+       var wiki_desc: String is lazy do return value_or_default("wiki.desc", "")
 
        # Site logo url.
        #
@@ -589,14 +594,22 @@ class WikiConfig
        #
        # * key: `wiki.logo`
        # * default: ``
-       fun wiki_logo: String is cached do return value_or_default("wiki.logo", "")
+       var wiki_logo: String is lazy do return value_or_default("wiki.logo", "")
 
        # Root url of the wiki.
        #
        # * key: `wiki.root_url`
        # * default: `http://localhost/`
-       fun root_url: String is cached do return value_or_default("wiki.root_url", "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.
        #
@@ -604,7 +617,7 @@ class WikiConfig
        #
        # * key: `wiki.root_dir`
        # * default: `./`
-       fun root_dir: String is cached do return value_or_default("wiki.root_dir", "./").simplify_path
+       var root_dir: String is lazy do return value_or_default("wiki.root_dir", "./").simplify_path
 
        # Pages directory.
        #
@@ -612,7 +625,7 @@ class WikiConfig
        #
        # * key: `wiki.source_dir
        # * default: `pages/`
-       fun source_dir: String is cached do
+       var source_dir: String is lazy do
                return value_or_default("wiki.source_dir", "pages/").simplify_path
        end
 
@@ -623,7 +636,7 @@ class WikiConfig
        #
        # * key: `wiki.out_dir`
        # * default: `out/`
-       fun out_dir: String is cached do return value_or_default("wiki.out_dir", "out/").simplify_path
+       var out_dir: String is lazy do return value_or_default("wiki.out_dir", "out/").simplify_path
 
        # Asset files directory.
        #
@@ -632,7 +645,7 @@ class WikiConfig
        #
        # * key: `wiki.assets_dir`
        # * default: `assets/`
-       fun assets_dir: String is cached do
+       var assets_dir: String is lazy do
                return value_or_default("wiki.assets_dir", "assets/").simplify_path
        end
 
@@ -643,7 +656,7 @@ class WikiConfig
        #
        # * key: `wiki.templates_dir`
        # * default: `templates/`
-       fun templates_dir: String is cached do
+       var templates_dir: String is lazy do
                return value_or_default("wiki.templates_dir", "templates/").simplify_path
        end
 
@@ -653,7 +666,7 @@ class WikiConfig
        #
        # * key: `wiki.template`
        # * default: `template.html`
-       fun template_file: String is cached do
+       var template_file: String is lazy do
                return value_or_default("wiki.template", "template.html")
        end
 
@@ -664,7 +677,7 @@ class WikiConfig
        #
        # * key: `wiki.header`
        # * default: `header.html`
-       fun header_file: String is cached do
+       var header_file: String is lazy do
                return value_or_default("wiki.header", "header.html")
        end
 
@@ -674,7 +687,7 @@ class WikiConfig
        #
        # * key: `wiki.menu`
        # * default: `menu.html`
-       fun menu_file: String is cached do
+       var menu_file: String is lazy do
                return value_or_default("wiki.menu", "menu.html")
        end
 
@@ -685,29 +698,40 @@ class WikiConfig
        #
        # * key: `wiki.footer`
        # * default: `footer.html`
-       fun footer_file: String is cached do
+       var footer_file: String is lazy do
                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
+
        # Directory used by rsync to upload wiki files.
        #
        # This information is used to update your distant wiki files (like the webserver).
        #
        # * key: `wiki.rsync_dir`
        # * default: ``
-       fun rsync_dir: String is cached do return value_or_default("wiki.rsync_dir", "")
+       var rsync_dir: String is lazy do return value_or_default("wiki.rsync_dir", "")
 
        # Remote repository used to pull modifications on sources.
        #
        # * key: `wiki.git_origin`
        # * default: `origin`
-       fun git_origin: String is cached do return value_or_default("wiki.git_origin", "origin")
+       var git_origin: String is lazy do return value_or_default("wiki.git_origin", "origin")
 
        # Remote branch used to pull modifications on sources.
        #
        # * key: `wiki.git_branch`
        # * default: `master`
-       fun git_branch: String is cached do return value_or_default("wiki.git_branch", "master")
+       var git_branch: String is lazy do return value_or_default("wiki.git_branch", "master")
 end
 
 # WikiSection custom configuration.