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