contrib/nitiwiki: extract link handling in its own module
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 7 May 2015 05:39:55 +0000 (01:39 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 7 May 2015 14:03:53 +0000 (10:03 -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 [new file with mode: 0644]

index dc019ed..56cbbcb 100644 (file)
@@ -16,7 +16,6 @@
 module wiki_base
 
 import template::macro
-import markdown
 import opts
 import ini
 
@@ -516,18 +515,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
+               content = md
        end
 
        redef var src_full_path: nullable String = null
index 2bb8f8c..461e9e5 100644 (file)
@@ -15,7 +15,7 @@
 # HTML wiki rendering
 module wiki_html
 
-import wiki_base
+import wiki_links
 
 redef class Nitiwiki
 
@@ -48,10 +48,6 @@ redef class Nitiwiki
 end
 
 redef class WikiEntry
-
-       # Url to `self` once generated.
-       fun url: String do return wiki.config.root_url.join_path(breadcrumbs.join("/"))
-
        # Get a `<a>` template link to `self`
        fun tpl_link: Writable do
                return "<a href=\"{url}\">{title}</a>"
@@ -104,17 +100,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.
-       var index: WikiArticle is lazy 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.
@@ -174,26 +159,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
 
 
@@ -318,15 +290,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
 
diff --git a/contrib/nitiwiki/src/wiki_links.nit b/contrib/nitiwiki/src/wiki_links.nit
new file mode 100644 (file)
index 0000000..9379c4d
--- /dev/null
@@ -0,0 +1,83 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Wiki internal links handling.
+module wiki_links
+
+import wiki_base
+import markdown
+
+redef class WikiEntry
+
+       # Url to `self` once generated.
+       fun url: String do return wiki.config.root_url.join_path(breadcrumbs.join("/"))
+
+       redef fun render do
+               super
+               if not is_dirty and not wiki.force_render then return
+       end
+end
+
+redef class WikiSection
+
+       # The index page for this section.
+       #
+       # If no file `index.md` exists for this section,
+       # a summary is generated using contained articles.
+       var index: WikiArticle is lazy 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
+end
+
+redef class WikiArticle
+
+       # Headlines ids and titles.
+       var headlines = new ArrayMap[String, HeadLine]
+
+       # Is `self` an index page?
+       #
+       # Checks if `self.name == "index"`.
+       fun is_index: Bool do return name == "index"
+
+       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
+
+       redef fun render do
+               super
+               if not is_dirty and not wiki.force_render or not has_source then return
+               var md_proc = new MarkdownProcessor
+               content = md_proc.process(md.as(not null))
+               headlines.recover_with(md_proc.emitter.decorator.headlines)
+       end
+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
+end