nitiwiki: introduce customizable sidebar
[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
21 redef class Nitiwiki
22 # Looks up a WikiEntry by its `name`.
23 #
24 # Rules are:
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
29 #
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
39 end
40 return null
41 end
42
43 # Looks up a WikiEntry by its `title`.
44 #
45 # Rules are:
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
50 #
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 == title then return section
58 for child in section.children.values do
59 if child.title == title then return child
60 end
61 section = section.parent
62 end
63 return null
64 end
65
66 # Looks up a WikiEntry by its `path`.
67 #
68 # Path can be relative from `context` like `context/entry`.
69 # Or absolute like `/entry1/entry2`.
70 #
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: nullable WikiEntry = context.parent or else context
74 var parts = path.split_with("/")
75 if path.has_prefix("/") then
76 entry = root_section
77 if parts.is_empty then return root_section.index
78 parts.shift
79 end
80 while not parts.is_empty do
81 var name = parts.shift
82 if name.is_empty then continue
83 if not entry.children.has_key(name) then return null
84 entry = entry.children[name]
85 end
86 return entry
87 end
88 end
89
90 redef class WikiEntry
91
92 # Url to `self` once generated.
93 fun url: String do return wiki.config.root_url.join_path(breadcrumbs.join("/"))
94
95 redef fun render do
96 super
97 if not is_dirty and not wiki.force_render then return
98 render_sidebar_wikilinks
99 end
100
101 # Search in `self` then `self.children` if an entry has the name `name`.
102 fun lookup_entry_by_name(name: String): nullable WikiEntry do
103 if children.has_key(name) then return children[name]
104 for child in children.values do
105 var res = child.lookup_entry_by_name(name)
106 if res != null then return res
107 end
108 return null
109 end
110
111 # Search in `self` then `self.children` if an entry has the title `title`.
112 fun lookup_entry_by_title(title: String): nullable WikiEntry do
113 for child in children.values do
114 if child.title == title then return child
115 end
116 for child in children.values do
117 var res = child.lookup_entry_by_title(title)
118 if res != null then return res
119 end
120 return null
121 end
122
123 private var md_proc: NitiwikiMdProcessor is lazy do
124 return new NitiwikiMdProcessor(wiki, self)
125 end
126
127 # Process wikilinks from sidebar.
128 private fun render_sidebar_wikilinks do
129 var blocks = sidebar.blocks
130 for i in [0..blocks.length[ do
131 blocks[i] = md_proc.process(blocks[i].to_s).write_to_string
132 md_proc.emitter.decorator.headlines.clear
133 end
134 end
135 end
136
137 redef class WikiSection
138
139 # The index page for this section.
140 #
141 # If no file `index.md` exists for this section,
142 # a summary is generated using contained articles.
143 var index: WikiArticle is lazy do
144 for child in children.values do
145 if child isa WikiArticle and child.is_index then return child
146 end
147 return new WikiSectionIndex(wiki, "index", self)
148 end
149 end
150
151 redef class WikiArticle
152
153 # Headlines ids and titles.
154 var headlines = new ArrayMap[String, HeadLine]
155
156 # Is `self` an index page?
157 #
158 # Checks if `self.name == "index"`.
159 fun is_index: Bool do return name == "index"
160
161 redef fun url do
162 if parent == null then
163 return wiki.config.root_url.join_path("{name}.html")
164 else
165 return parent.url.join_path("{name}.html")
166 end
167 end
168
169 redef fun render do
170 super
171 if not is_dirty and not wiki.force_render or not has_source then return
172 content = md_proc.process(md.as(not null))
173 headlines.recover_with(md_proc.emitter.decorator.headlines)
174 end
175 end
176
177 # A `WikiArticle` that contains the section index tree.
178 class WikiSectionIndex
179 super WikiArticle
180
181 # The section described by `self`.
182 var section: WikiSection
183
184 redef fun title do return section.title
185
186 redef fun url do return section.url
187 end
188
189 # A MarkdownProcessor able to parse wiki links.
190 class NitiwikiMdProcessor
191 super MarkdownProcessor
192
193 # Wiki used to resolve links.
194 var wiki: Nitiwiki
195
196 # Article parsed by `self`.
197 #
198 # Used to contextualize links.
199 var context: WikiEntry
200
201 init do
202 emitter = new MarkdownEmitter(self)
203 emitter.decorator = new NitiwikiDecorator(wiki, context)
204 end
205 end
206
207 private class NitiwikiDecorator
208 super HTMLDecorator
209
210 # Wiki used to resolve links.
211 var wiki: Nitiwiki
212
213 # Article used to contextualize links.
214 var context: WikiEntry
215
216 redef fun add_wikilink(v, link, name, comment) do
217 var wiki = v.processor.as(NitiwikiMdProcessor).wiki
218 var target: nullable WikiEntry = null
219 var anchor: nullable String = null
220 if link.has("#") then
221 var parts = link.split_with("#")
222 link = parts.first
223 anchor = parts.subarray(1, parts.length - 1).join("#")
224 end
225 if link.has("/") then
226 target = wiki.lookup_entry_by_path(context, link.to_s)
227 else
228 target = wiki.lookup_entry_by_name(context, link.to_s)
229 if target == null then
230 target = wiki.lookup_entry_by_title(context, link.to_s)
231 end
232 end
233 v.add "<a "
234 if target != null then
235 if name == null then name = target.title
236 link = target.url
237 else
238 var loc = context.src_path or else context.name
239 wiki.message("Warning: unknown wikilink `{link}` (in {loc})", 0)
240 v.add "class=\"broken\" "
241 end
242 v.add "href=\""
243 append_value(v, link)
244 if anchor != null then append_value(v, "#{anchor}")
245 v.add "\""
246 if comment != null and not comment.is_empty then
247 v.add " title=\""
248 append_value(v, comment)
249 v.add "\""
250 end
251 v.add ">"
252 if name == null then name = link
253 v.emit_text(name)
254 v.add "</a>"
255 end
256 end