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.
15 # Wiki internal links handling.
19 import markdown
::wikilinks
22 # Looks up a WikiEntry by its `name`.
25 # 1. Looks in the current section
26 # 2. Looks in the current section children
27 # 3. Looks in the current section parent
28 # 4. Looks up to wiki root
30 # Returns `null` if no article can be found.
31 fun lookup_entry_by_name
(context
: WikiEntry, name
: String): nullable WikiEntry do
32 var section
: nullable WikiEntry = context
.parent
or else context
33 var res
= section
.lookup_entry_by_name
(name
)
34 if res
!= null then return res
35 while section
!= null do
36 if section
.name
== name
then return section
37 if section
.children
.has_key
(name
) then return section
.children
[name
]
38 section
= section
.parent
43 # Looks up a WikiEntry by its `title`.
46 # 1. Looks in the current section
47 # 2. Looks in the current section children
48 # 3. Looks in the current section parent
49 # 4. Looks up to wiki root
51 # Returns `null` if no article can be found.
52 fun lookup_entry_by_title
(context
: WikiEntry, title
: String): nullable WikiEntry do
53 var section
: nullable WikiEntry = context
.parent
or else context
54 var res
= section
.lookup_entry_by_title
(title
)
55 if res
!= null then return res
56 while section
!= null do
57 if section
.title
.to_lower
== title
.to_lower
then return section
58 for child
in section
.children
.values
do
59 if child
.title
.to_lower
== title
.to_lower
then return child
61 section
= section
.parent
66 # Looks up a WikiEntry by its `path`.
68 # Path can be relative from `context` like `context/entry`.
69 # Or absolute like `/entry1/entry2`.
71 # Returns `null` if no article can be found.
72 fun lookup_entry_by_path
(context
: WikiEntry, path
: String): nullable WikiEntry do
73 var entry
= context
.parent
or else context
74 var parts
= path
.split_with
("/")
75 if path
.has_prefix
("/") then
77 if parts
.is_empty
then return root_section
.index
80 while not parts
.is_empty
do
81 var name
= parts
.shift
82 if name
.is_empty
then continue
83 if entry
.name
== name
then continue
84 if not entry
.children
.has_key
(name
) then return null
85 entry
= entry
.children
[name
]
93 # Absolute url to `self` once generated.
94 # If you use this, the generated files will hard-code `root_url`
95 fun url
: String do return wiki
.config
.root_url
/ href
97 # Relative path to `self` from the target root_url
98 fun href
: String do return breadcrumbs
.join
("/")
100 # A relative `href` to `self` from the page `context`.
102 # Should be used to navigate between documents.
103 fun href_from
(context
: WikiEntry): String
105 var res
= context
.href
.dirname
.relpath
(href
)
111 if not is_dirty
and not wiki
.force_render
then return
112 render_sidebar_wikilinks
115 # Search in `self` then `self.children` if an entry has the name `name`.
116 fun lookup_entry_by_name
(name
: String): nullable WikiEntry do
117 if children
.has_key
(name
) then return children
[name
]
118 for child
in children
.values
do
119 var res
= child
.lookup_entry_by_name
(name
)
120 if res
!= null then return res
125 # Search in `self` then `self.children` if an entry has the title `title`.
126 fun lookup_entry_by_title
(title
: String): nullable WikiEntry do
127 for child
in children
.values
do
128 if child
.title
.to_lower
== title
.to_lower
then return child
130 for child
in children
.values
do
131 var res
= child
.lookup_entry_by_title
(title
)
132 if res
!= null then return res
137 private var md_proc
: NitiwikiMdProcessor is lazy
do
138 return new NitiwikiMdProcessor(wiki
, self)
141 # Process wikilinks from sidebar.
142 private fun render_sidebar_wikilinks
do
143 var blocks
= sidebar
.blocks
144 for i
in [0..blocks
.length
[ do
145 blocks
[i
] = md_proc
.process
(blocks
[i
].to_s
).write_to_string
146 md_proc
.emitter
.decorator
.headlines
.clear
151 redef class WikiSection
153 # The index page for this section.
155 # If no file `index.md` exists for this section,
156 # a summary is generated using contained articles.
157 var index
: WikiArticle is lazy
do
158 for child
in children
.values
do
159 if child
isa WikiArticle and child
.is_index
then return child
161 return new WikiSectionIndex(wiki
, "index", self)
165 redef class WikiArticle
167 # Headlines ids and titles.
168 var headlines
= new ArrayMap[String, HeadLine]
170 # Is `self` an index page?
172 # Checks if `self.name == "index"`.
173 fun is_index
: Bool do return name
== "index"
176 if parent
== null then
179 return parent
.href
.join_path
("{name}.html")
185 if not is_dirty
and not wiki
.force_render
or not has_source
then return
186 content
= md_proc
.process
(md
.as(not null))
187 headlines
.recover_with
(md_proc
.emitter
.decorator
.headlines
)
191 # A `WikiArticle` that contains the section index tree.
192 class WikiSectionIndex
195 # The section described by `self`.
196 var section
: WikiSection
198 redef fun title
do return section
.title
200 redef fun href
do return section
.href
203 # A MarkdownProcessor able to parse wiki links.
204 class NitiwikiMdProcessor
205 super MarkdownProcessor
207 # Wiki used to resolve links.
210 # Article parsed by `self`.
212 # Used to contextualize links.
213 var context
: WikiEntry
216 emitter
= new MarkdownEmitter(self)
217 emitter
.decorator
= new NitiwikiDecorator(wiki
, context
)
221 private class NitiwikiDecorator
224 # Wiki used to resolve links.
227 # Article used to contextualize links.
228 var context
: WikiEntry
230 redef fun add_wikilink
(v
, link
, name
, comment
) do
231 var anchor
: nullable String = null
233 if not link
.has_prefix
("http://") and not link
.has_prefix
("https://") then
234 var wiki
= v
.processor
.as(NitiwikiMdProcessor).wiki
235 var target
: nullable WikiEntry = null
236 if link
.has
("#") then
237 var parts
= link
.split_with
("#")
239 anchor
= parts
.subarray
(1, parts
.length
- 1).join
("#")
241 if link
.has
("/") then
242 target
= wiki
.lookup_entry_by_path
(context
, link
.to_s
)
244 target
= wiki
.lookup_entry_by_name
(context
, link
.to_s
)
245 if target
== null then
246 target
= wiki
.lookup_entry_by_title
(context
, link
.to_s
)
249 if target
!= null then
250 if name
== null then name
= target
.title
251 link
= target
.href_from
(context
)
253 var loc
= context
.src_path
or else context
.name
254 wiki
.message
("Warning: unknown wikilink `{link}` (in {loc})", 0)
255 v
.add
"class=\"broken\
" "
259 append_value(v, link)
260 if anchor != null then append_value(v, "#{anchor}")
262 if comment != null and not comment.is_empty then
264 append_value
(v
, comment
)
268 if name == null then name = link