lib/markdown: move wikilinks parsing to lib/markdown
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 21 May 2015 19:38:30 +0000 (15:38 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 21 May 2015 19:38:30 +0000 (15:38 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/nitiwiki/src/wiki_links.nit
lib/markdown/wikilinks.nit [new file with mode: 0644]

index bbbfea0..bfdeb86 100644 (file)
@@ -16,7 +16,7 @@
 module wiki_links
 
 import wiki_base
-intrude import markdown
+import markdown::wikilinks
 
 redef class Nitiwiki
        # Looks up a WikiEntry by its `name`.
@@ -189,16 +189,6 @@ class NitiwikiMdProcessor
                emitter = new MarkdownEmitter(self)
                emitter.decorator = new NitiwikiDecorator(wiki, context)
        end
-
-       redef fun token_at(text, pos) do
-               var token = super
-               if not token isa TokenLink then return token
-               if pos + 1 < text.length then
-                       var c = text[pos + 1]
-                       if c == '[' then return new TokenWikiLink(pos, c)
-               end
-               return token
-       end
 end
 
 private class NitiwikiDecorator
@@ -210,7 +200,7 @@ private class NitiwikiDecorator
        # Article used to contextualize links.
        var context: WikiArticle
 
-       fun add_wikilink(v: MarkdownEmitter, link: Text, name, comment: nullable Text) do
+       redef fun add_wikilink(v, link, name, comment) do
                var wiki = v.processor.as(NitiwikiMdProcessor).wiki
                var target: nullable WikiEntry = null
                var anchor: nullable String = null
@@ -250,46 +240,3 @@ private class NitiwikiDecorator
                v.add "</a>"
        end
 end
-
-# A NitiWiki link token.
-#
-# Something of the form `[[foo]]`.
-#
-# Allowed formats:
-#
-# * `[[Wikilink]]`
-# * `[[Wikilink/Bar]]`
-# * `[[Wikilink#foo]]`
-# * `[[Wikilink/Bar#foo]]`
-# * `[[title|Wikilink]]`
-# * `[[title|Wikilink/Bar]]`
-# * `[[title|Wikilink/Bar#foo]]`
-class TokenWikiLink
-       super TokenLink
-
-       redef fun emit_hyper(v) do
-               v.decorator.as(NitiwikiDecorator).add_wikilink(v, link.as(not null), name, comment)
-       end
-
-       redef fun check_link(v, out, start, token) do
-               var md = v.current_text
-               var pos = start + 2
-               var tmp = new FlatBuffer
-               pos = md.read_md_link_id(tmp, pos)
-               if pos < start then return -1
-               var name = tmp.write_to_string
-               if name.has("|") then
-                       var parts = name.split_once_on("|")
-                       self.name = parts.first
-                       self.link = parts[1]
-               else
-                       self.name = null
-                       self.link = name
-               end
-               pos += 1
-               pos = md.skip_spaces(pos)
-               if pos < start then return -1
-               pos += 1
-               return pos
-       end
-end
diff --git a/lib/markdown/wikilinks.nit b/lib/markdown/wikilinks.nit
new file mode 100644 (file)
index 0000000..e341194
--- /dev/null
@@ -0,0 +1,94 @@
+# 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.
+
+# Wikilinks handling.
+#
+# Wikilinks are on the form `[[link]]`.
+# They can also contain a custom title with the syntax `[[title|link]]`.
+#
+# By importing this module, you enable the `MarkdownProcessor` to recognize
+# `TokenWikiLink` but nothing will happen until you define a
+# `Decorator::add_wikilink` customized to your applciation domain.
+module wikilinks
+
+intrude import markdown
+
+# `MarkdownProcessor` is now able to parse wikilinks.
+redef class MarkdownProcessor
+
+       redef fun token_at(text, pos) do
+               var token = super
+               if not token isa TokenLink then return token
+               if pos + 1 < text.length then
+                       var c = text[pos + 1]
+                       if c == '[' then return new TokenWikiLink(pos, c)
+               end
+               return token
+       end
+end
+
+redef class Decorator
+
+       # Renders a `[[wikilink]]` item.
+       fun add_wikilink(v: MarkdownEmitter, link: Text, name, comment: nullable Text) do
+               if name != null then
+                       v.add "[[{name}|{link}]]"
+               else
+                       v.add "[[{link}]]"
+               end
+       end
+end
+
+# A NitiWiki link token.
+#
+# Something of the form `[[foo]]`.
+#
+# Allowed formats:
+#
+# * `[[Wikilink]]`
+# * `[[Wikilink/Bar]]`
+# * `[[Wikilink#foo]]`
+# * `[[Wikilink/Bar#foo]]`
+# * `[[title|Wikilink]]`
+# * `[[title|Wikilink/Bar]]`
+# * `[[title|Wikilink/Bar#foo]]`
+class TokenWikiLink
+       super TokenLink
+
+       redef fun emit_hyper(v) do
+               v.decorator.add_wikilink(v, link.as(not null), name, comment)
+       end
+
+       redef fun check_link(v, out, start, token) do
+               var md = v.current_text
+               var pos = start + 2
+               var tmp = new FlatBuffer
+               pos = md.read_md_link_id(tmp, pos)
+               if pos < start then return -1
+               var name = tmp.write_to_string
+               if name.has("|") then
+                       var parts = name.split_once_on("|")
+                       self.name = parts.first
+                       self.link = parts[1]
+               else
+                       self.name = null
+                       self.link = name
+               end
+               pos += 1
+               pos = md.skip_spaces(pos)
+               if pos < start then return -1
+               pos += 1
+               return pos
+       end
+end