contrib/nitiwiki: extract link handling in its own module
[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
20
21 redef class WikiEntry
22
23 # Url to `self` once generated.
24 fun url: String do return wiki.config.root_url.join_path(breadcrumbs.join("/"))
25
26 redef fun render do
27 super
28 if not is_dirty and not wiki.force_render then return
29 end
30 end
31
32 redef class WikiSection
33
34 # The index page for this section.
35 #
36 # If no file `index.md` exists for this section,
37 # a summary is generated using contained articles.
38 var index: WikiArticle is lazy do
39 for child in children.values do
40 if child isa WikiArticle and child.is_index then return child
41 end
42 return new WikiSectionIndex(wiki, "index", self)
43 end
44 end
45
46 redef class WikiArticle
47
48 # Headlines ids and titles.
49 var headlines = new ArrayMap[String, HeadLine]
50
51 # Is `self` an index page?
52 #
53 # Checks if `self.name == "index"`.
54 fun is_index: Bool do return name == "index"
55
56 redef fun url do
57 if parent == null then
58 return wiki.config.root_url.join_path("{name}.html")
59 else
60 return parent.url.join_path("{name}.html")
61 end
62 end
63
64 redef fun render do
65 super
66 if not is_dirty and not wiki.force_render or not has_source then return
67 var md_proc = new MarkdownProcessor
68 content = md_proc.process(md.as(not null))
69 headlines.recover_with(md_proc.emitter.decorator.headlines)
70 end
71 end
72
73 # A `WikiArticle` that contains the section index tree.
74 class WikiSectionIndex
75 super WikiArticle
76
77 # The section described by `self`.
78 var section: WikiSection
79
80 redef fun title do return section.title
81
82 redef fun url do return section.url
83 end