1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # Wikilinks are on the form `[[link]]`.
18 # They can also contain a custom title with the syntax `[[title|link]]`.
20 # By importing this module, you enable the `MarkdownProcessor` to recognize
21 # `TokenWikiLink` but nothing will happen until you define a
22 # `Decorator::add_wikilink` customized to your applciation domain.
25 intrude import markdown
27 # `MarkdownProcessor` is now able to parse wikilinks.
28 redef class MarkdownProcessor
30 redef fun token_at
(text
, pos
) do
32 if not token
isa TokenLink then return token
33 if pos
+ 1 < text
.length
then
35 if c
== '[' then return new TokenWikiLink(token
.location
, pos
, c
)
43 # Renders a `[[wikilink]]` item.
44 fun add_wikilink
(v
: EMITTER, token
: TokenWikiLink) do
45 if token
.name
!= null then
46 v
.add
"[[{token.name.to_s}|{token.link.to_s}]]"
48 v
.add
"[[{token.link.to_s}]]"
53 # A NitiWiki link token.
55 # Something of the form `[[foo]]`.
60 # * `[[Wikilink/Bar]]`
61 # * `[[Wikilink#foo]]`
62 # * `[[Wikilink/Bar#foo]]`
63 # * `[[title|Wikilink]]`
64 # * `[[title|Wikilink/Bar]]`
65 # * `[[title|Wikilink/Bar#foo]]`
69 redef fun emit_hyper
(v
) do
70 v
.decorator
.add_wikilink
(v
, self)
73 redef fun check_link
(v
, out
, start
, token
) do
74 var md
= v
.current_text
76 var tmp
= new FlatBuffer
77 pos
= md
.read_md_link_id
(tmp
, pos
)
78 if pos
< start
then return -1
79 var name
= tmp
.write_to_string
81 var parts
= name
.split_once_on
("|")
82 self.name
= parts
.first
89 pos
= md
.skip_spaces
(pos
)
90 if pos
< start
then return -1