nitiwiki: add `trail:` command to collect trails
[nit.git] / contrib / nitiwiki / src / wiki_links.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Wiki internal links handling.
16 module wiki_links
17
18 import wiki_base
19 import markdown::wikilinks
20 import ordered_tree
21
22 redef class Nitiwiki
23 # Looks up a WikiEntry by its `name`.
24 #
25 # Rules are:
26 # 1. Looks in the current section
27 # 2. Looks in the current section children
28 # 3. Looks in the current section parent
29 # 4. Looks up to wiki root
30 #
31 # Returns `null` if no article can be found.
32 fun lookup_entry_by_name(context: WikiEntry, name: String): nullable WikiEntry do
33 var section: nullable WikiEntry = context.parent or else context
34 var res = section.lookup_entry_by_name(name)
35 if res != null then return res
36 while section != null do
37 if section.name == name then return section
38 if section.children.has_key(name) then return section.children[name]
39 section = section.parent
40 end
41 return null
42 end
43
44 # Looks up a WikiEntry by its `title`.
45 #
46 # Rules are:
47 # 1. Looks in the current section
48 # 2. Looks in the current section children
49 # 3. Looks in the current section parent
50 # 4. Looks up to wiki root
51 #
52 # Returns `null` if no article can be found.
53 fun lookup_entry_by_title(context: WikiEntry, title: String): nullable WikiEntry do
54 var section: nullable WikiEntry = context.parent or else context
55 var res = section.lookup_entry_by_title(title)
56 if res != null then return res
57 while section != null do
58 if section.title.to_lower == title.to_lower then return section
59 for child in section.children.values do
60 if child.title.to_lower == title.to_lower then return child
61 end
62 section = section.parent
63 end
64 return null
65 end
66
67 # Looks up a WikiEntry by its `path`.
68 #
69 # Path can be relative from `context` like `context/entry`.
70 # Or absolute like `/entry1/entry2`.
71 #
72 # Returns `null` if no article can be found.
73 fun lookup_entry_by_path(context: WikiEntry, path: String): nullable WikiEntry do
74 var entry = context.parent or else context
75 var parts = path.split_with("/")
76 if path.has_prefix("/") then
77 entry = root_section
78 if parts.is_empty then return root_section.index
79 parts.shift
80 end
81 while not parts.is_empty do
82 var name = parts.shift
83 if name.is_empty then continue
84 if entry.name == name then continue
85 if not entry.children.has_key(name) then return null
86 entry = entry.children[name]
87 end
88 return entry
89 end
90
91 # Trails between pages
92 #
93 # Trails are represented as a forest of entries.
94 # This way it is possible to represent a flat-trail as a visit of a tree.
95 var trails = new OrderedTree[WikiEntry]
96 end
97
98 redef class WikiEntry
99
100 # Relative path to `self` from the target root_url
101 fun href: String do return breadcrumbs.join("/")
102
103 # A relative `href` to `self` from the page `context`.
104 #
105 # Should be used to navigate between documents.
106 fun href_from(context: WikiEntry): String
107 do
108 var res = context.href.dirname.relpath(href)
109 return res
110 end
111
112 # A relative hyperlink <a> to `self` from the page `context`.
113 #
114 # If `text` is not given, `title` will be used instead.
115 fun a_from(context: WikiEntry, text: nullable Text): Writable
116 do
117 var title = title.html_escape
118 if text == null then text = title else text = text.html_escape
119 var href = href_from(context)
120 return """<a href="{{{href}}}" title="{{{title}}}">{{{text}}}</a>"""
121 end
122
123 redef fun render do
124 super
125 if not is_dirty and not wiki.force_render then return
126 render_sidebar_wikilinks
127 end
128
129 # Search in `self` then `self.children` if an entry has the name `name`.
130 fun lookup_entry_by_name(name: String): nullable WikiEntry do
131 if children.has_key(name) then return children[name]
132 for child in children.values do
133 var res = child.lookup_entry_by_name(name)
134 if res != null then return res
135 end
136 return null
137 end
138
139 # Search in `self` then `self.children` if an entry has the title `title`.
140 fun lookup_entry_by_title(title: String): nullable WikiEntry do
141 for child in children.values do
142 if child.title.to_lower == title.to_lower then return child
143 end
144 for child in children.values do
145 var res = child.lookup_entry_by_title(title)
146 if res != null then return res
147 end
148 return null
149 end
150
151 private var md_proc: NitiwikiMdProcessor is lazy do
152 return new NitiwikiMdProcessor(wiki, self)
153 end
154
155 # Process wikilinks from sidebar.
156 private fun render_sidebar_wikilinks do
157 var blocks = sidebar.blocks
158 for i in [0..blocks.length[ do
159 blocks[i] = md_proc.process(blocks[i].to_s).write_to_string
160 md_proc.emitter.decorator.headlines.clear
161 end
162 end
163 end
164
165 redef class WikiSection
166
167 # The index page for this section.
168 #
169 # If no file `index.md` exists for this section,
170 # a summary is generated using contained articles.
171 var index: WikiArticle is lazy do
172 for child in children.values do
173 if child isa WikiArticle and child.is_index then return child
174 end
175 return new WikiSectionIndex(wiki, "index", self)
176 end
177 end
178
179 redef class WikiArticle
180
181 # Headlines ids and titles.
182 var headlines = new ArrayMap[String, HeadLine]
183
184 # Is `self` an index page?
185 #
186 # Checks if `self.name == "index"`.
187 fun is_index: Bool do return name == "index"
188
189 redef fun href do
190 if parent == null then
191 return "{name}.html"
192 else
193 return parent.href.join_path("{name}.html")
194 end
195 end
196
197 redef fun render do
198 super
199 if not is_dirty and not wiki.force_render or not has_source then return
200 content = md_proc.process(md.as(not null))
201 headlines.recover_with(md_proc.emitter.decorator.headlines)
202 end
203 end
204
205 # A `WikiArticle` that contains the section index tree.
206 class WikiSectionIndex
207 super WikiArticle
208
209 # The section described by `self`.
210 var section: WikiSection
211
212 redef fun title do return section.title
213
214 redef fun href do return section.href
215 end
216
217 # A MarkdownProcessor able to parse wiki links.
218 class NitiwikiMdProcessor
219 super MarkdownProcessor
220
221 # Wiki used to resolve links.
222 var wiki: Nitiwiki
223
224 # Article parsed by `self`.
225 #
226 # Used to contextualize links.
227 var context: WikiEntry
228
229 init do
230 emitter = new MarkdownEmitter(self)
231 emitter.decorator = new NitiwikiDecorator(wiki, context)
232 end
233 end
234
235 # The decorator associated to `MarkdownProcessor`.
236 class NitiwikiDecorator
237 super HTMLDecorator
238
239 # Wiki used to resolve links.
240 var wiki: Nitiwiki
241
242 # Article used to contextualize links.
243 var context: WikiEntry
244
245 redef fun add_wikilink(v, token) do
246 var wiki = v.processor.as(NitiwikiMdProcessor).wiki
247 var target: nullable WikiEntry = null
248 var anchor: nullable String = null
249 var link = token.link
250 if link == null then return
251 var name = token.name
252 v.add "<a "
253 if not link.has_prefix("http://") and not link.has_prefix("https://") then
254 # Extract commands from the link.
255 var command = null
256 var command_split = link.split_once_on(":")
257 if command_split.length > 1 then
258 command = command_split[0].trim
259 link = command_split[1].trim
260 end
261
262 if link.has("#") then
263 var parts = link.split_with("#")
264 link = parts.first
265 anchor = parts.subarray(1, parts.length - 1).join("#")
266 end
267 if link.has("/") then
268 target = wiki.lookup_entry_by_path(context, link.to_s)
269 else
270 target = wiki.lookup_entry_by_name(context, link.to_s)
271 if target == null then
272 target = wiki.lookup_entry_by_title(context, link.to_s)
273 end
274 end
275 if target != null then
276 if name == null then name = target.title
277 link = target.href_from(context)
278
279 if command == "trail" then
280 if target isa WikiSection then target = target.index
281 wiki.trails.add(context, target)
282 end
283 else
284 wiki.message("Warning: unknown wikilink `{link}` (in {context.src_path.as(not null)})", 0)
285 v.add "class=\"broken\" "
286 end
287 end
288 v.add "href=\""
289 append_value(v, link)
290 if anchor != null then append_value(v, "#{anchor}")
291 v.add "\""
292 var comment = token.comment
293 if comment != null and not comment.is_empty then
294 v.add " title=\""
295 append_value(v, comment)
296 v.add "\""
297 end
298 v.add ">"
299 if name == null then name = link
300 v.emit_text(name)
301 v.add "</a>"
302 end
303 end