+
+# A MarkdownProcessor able to parse wiki links.
+class NitiwikiMdProcessor
+ super MarkdownProcessor
+
+ # Wiki used to resolve links.
+ var wiki: Nitiwiki
+
+ # Article parsed by `self`.
+ #
+ # Used to contextualize links.
+ var context: WikiArticle
+
+ init do
+ 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
+ super HTMLDecorator
+
+ # Wiki used to resolve links.
+ var wiki: Nitiwiki
+
+ # Article used to contextualize links.
+ var context: WikiArticle
+
+ fun add_wikilink(v: MarkdownEmitter, link: Text, name, comment: nullable Text) do
+ var wiki = v.processor.as(NitiwikiMdProcessor).wiki
+ var target: nullable WikiEntry = null
+ var anchor: nullable String = 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
+ v.add "<a "
+ 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 "\""
+ if comment != null and not comment.is_empty then
+ v.add " title=\""
+ append_value(v, comment)
+ v.add "\""
+ end
+ v.add ">"
+ if name == null then name = link
+ v.emit_text(name)
+ 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