markdown: add_wikilink accept a TokenWikiLink so clients can use the location
authorAlexandre Terrasa <alexandre@moz-code.org>
Sat, 6 Jun 2015 22:05:01 +0000 (18:05 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 24 Jun 2015 20:30:00 +0000 (16:30 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

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

index bdb27da..6af6d48 100644 (file)
@@ -227,38 +227,39 @@ private class NitiwikiDecorator
        # Article used to contextualize links.
        var context: WikiEntry
 
-       redef fun add_wikilink(v, link, name, comment) do
+       redef fun add_wikilink(v, token) do
+               var wiki = v.processor.as(NitiwikiMdProcessor).wiki
+               var target: nullable WikiEntry = null
                var anchor: nullable String = null
-               v.add "<a "
-               if not link.has_prefix("http://") and not link.has_prefix("https://") then
-                       var wiki = v.processor.as(NitiwikiMdProcessor).wiki
-                       var target: nullable WikiEntry = null
-                       if link.has("#") then
-                               var parts = link.split_with("#")
-                               link = parts.first
-                               anchor = parts.subarray(1, parts.length - 1).join("#")
-                       end
-                       if link.has("/") then
-                               target = wiki.lookup_entry_by_path(context, link.to_s)
-                       else
-                               target = wiki.lookup_entry_by_name(context, link.to_s)
-                               if target == null then
-                                       target = wiki.lookup_entry_by_title(context, link.to_s)
-                               end
-                       end
-                       if target != null then
-                               if name == null then name = target.title
-                               link = target.href_from(context)
-                       else
-                               var loc = context.src_path or else context.name
-                               wiki.message("Warning: unknown wikilink `{link}` (in {loc})", 0)
-                               v.add "class=\"broken\" "
+               var link = token.link
+               if link == null then return
+               if link.has("#") then
+                       var parts = link.split_with("#")
+                       link = parts.first
+                       anchor = parts.subarray(1, parts.length - 1).join("#")
+               end
+               if link.has("/") then
+                       target = wiki.lookup_entry_by_path(context, link.to_s)
+               else
+                       target = wiki.lookup_entry_by_name(context, link.to_s)
+                       if target == null then
+                               target = wiki.lookup_entry_by_title(context, link.to_s)
                        end
                end
+               v.add "<a "
+               var name = token.name
+               if target != null then
+                       if name == null then name = target.title
+                       link = target.url
+               else
+                       wiki.message("Warning: unknown wikilink `{link}` (in {context.src_path.as(not null)})", 0)
+                       v.add "class=\"broken\" "
+               end
                v.add "href=\""
                append_value(v, link)
                if anchor != null then append_value(v, "#{anchor}")
                v.add "\""
+               var comment = token.comment
                if comment != null and not comment.is_empty then
                        v.add " title=\""
                        append_value(v, comment)
diff --git a/lib/markdown/test_wikilinks.nit b/lib/markdown/test_wikilinks.nit
new file mode 100644 (file)
index 0000000..5c4fb7e
--- /dev/null
@@ -0,0 +1,73 @@
+# 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.
+
+# Test suites for module `markdown`
+module test_wikilinks is test_suite
+
+import test_markdown
+import wikilinks
+
+class TestTokenWikilink
+       super TestSuite
+
+       fun test_token_location1 do
+               var string = "[[wikilink]]"
+               var stack =  ["TokenWikiLink at 1,1--1,1"]
+               (new TestTokenProcessor(stack)).process(string)
+       end
+
+       fun test_token_location2 do
+               var string = "Hello [[World]]"
+               var stack =  ["TokenWikiLink at 1,7--1,7"]
+               (new TestTokenProcessor(stack)).process(string)
+       end
+
+       fun test_token_location3 do
+               var string = "\nHello\nworld [[wikilink]] !"
+               var stack =  ["TokenWikiLink at 3,7--3,7"]
+               (new TestTokenProcessor(stack)).process(string)
+       end
+
+       fun test_token_location4 do
+               var string = "[[link1]]\n\n[[link2]]"
+               var stack =  [
+                       "TokenWikiLink at 1,1--1,1",
+                       "TokenWikiLink at 3,1--3,1"]
+               (new TestTokenProcessor(stack)).process(string)
+       end
+
+       fun test_token_location5 do
+               var string = "[[link1]]\n[[link2]]"
+               var stack =  [
+                       "TokenWikiLink at 1,1--1,1",
+                       "TokenWikiLink at 2,1--2,1"]
+               (new TestTokenProcessor(stack)).process(string)
+       end
+
+       fun test_token_location6 do
+               var string = """
+[[doc: github]]
+
+[[loollll]]
+
+## Accessing the API
+
+[[doc: GithubAPI]]"""
+               var stack =  [
+                       "TokenWikiLink at 1,1--1,1",
+                       "TokenWikiLink at 3,1--3,1",
+                       "TokenWikiLink at 7,1--7,1"]
+               (new TestTokenProcessor(stack)).process(string)
+       end
+end
index 28fd1e0..9f3097c 100644 (file)
@@ -41,11 +41,11 @@ end
 redef class Decorator
 
        # Renders a `[[wikilink]]` item.
-       fun add_wikilink(v: EMITTER, link: Text, name, comment: nullable Text) do
-               if name != null then
-                       v.add "[[{name}|{link}]]"
+       fun add_wikilink(v: EMITTER, token: TokenWikiLink) do
+               if token.name != null then
+                       v.add "[[{token.name.to_s}|{token.link.to_s}]]"
                else
-                       v.add "[[{link}]]"
+                       v.add "[[{token.link.to_s}]]"
                end
        end
 end
@@ -67,7 +67,7 @@ class TokenWikiLink
        super TokenLink
 
        redef fun emit_hyper(v) do
-               v.decorator.add_wikilink(v, link.as(not null), name, comment)
+               v.decorator.add_wikilink(v, self)
        end
 
        redef fun check_link(v, out, start, token) do