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