nitiwiki: make summary optional
[nit.git] / contrib / nitiwiki / src / wiki_base.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 # Base entities of a nitiwiki.
16 module wiki_base
17
18 import template::macro
19 import opts
20 import ini
21
22 # A Nitiwiki instance.
23 #
24 # Nitiwiki provide all base services used by `WikiSection` and `WikiArticle`.
25 # It manages content and renders pages.
26 #
27 # Each nitiwiki instance is linked to a config file.
28 # This file show to `nitiwiki` that a wiki is present in the current directory.
29 # Without it, nitiwiki will consider the directory as empty.
30 class Nitiwiki
31
32 # Wiki config object.
33 var config: WikiConfig
34
35 # Default config filename.
36 var config_filename = "config.ini"
37
38 # Force render on all file even if the source is unmodified.
39 var force_render = false is writable
40
41 # Verbosity level.
42 var verbose_level = 0 is writable
43
44 # Delete all the output files.
45 fun clean do
46 var out_dir = expand_path(config.root_dir, config.out_dir)
47 if out_dir.file_exists then out_dir.rmdir
48 end
49
50 # Synchronize local output with the distant `WikiConfig::rsync_dir`.
51 fun sync do
52 var root = expand_path(config.root_dir, config.out_dir)
53 sys.system "rsync -vr --delete {root}/ {config.rsync_dir}"
54 end
55
56 # Pull data from git repository.
57 fun fetch do
58 sys.system "git pull {config.git_origin} {config.git_branch}"
59 end
60
61 # Analyze wiki files from `dir` to build wiki entries.
62 #
63 # This method build a hierarchical structure of `WikiSection` and `WikiArticle`
64 # based on the markdown source structure.
65 fun parse do
66 var dir = expand_path(config.root_dir, config.source_dir)
67 root_section = new_section(dir)
68 var files = list_md_files(dir)
69 for file in files do
70 new_article(file)
71 end
72 end
73
74 # Render output.
75 fun render do end
76
77 # Show wiki status.
78 fun status do
79 print "nitiWiki"
80 print "name: {config.wiki_name}"
81 print "config: {config.ini_file}"
82 print "url: {config.root_url}"
83 print ""
84 if root_section.is_dirty then
85 print "There is modified files:"
86 var paths = entries.keys.to_a
87 var s = new DefaultComparator
88 s.sort(paths)
89 for path in paths do
90 var entry = entries[path]
91 if not entry.is_dirty then continue
92 var name = entry.name
93 if entry.has_source then name = entry.src_path.to_s
94 if entry.is_new then
95 print " + {name}"
96 else
97 print " * {name}"
98 end
99 end
100 print ""
101 print "Use nitiwiki --render to render modified files"
102 else
103 print "Wiki is up-to-date"
104 print ""
105 print "Use nitiwiki --fetch to pull modification from origin"
106 print "Use nitiwiki --rsync to synchronize distant output"
107 end
108 end
109
110 # Display msg if `level <= verbose_level`
111 fun message(msg: String, level: Int) do
112 if level <= verbose_level then print msg
113 end
114
115 # List markdown source files from a directory.
116 fun list_md_files(dir: String): Array[String] do
117 var files = new Array[String]
118 var pipe = new ProcessReader("find", dir, "-name", "*.{config.md_ext}")
119 while not pipe.eof do
120 var file = pipe.read_line
121 if file == "" then break # last line
122 var name = file.basename(".{config.md_ext}")
123 if name == "header" or name == "footer" or name == "menu" then continue
124 files.add file
125 end
126 pipe.close
127 pipe.wait
128 if pipe.status != 0 then exit 1
129 var s = new DefaultComparator
130 s.sort(files)
131 return files
132 end
133
134 # Does `src` have been modified since `target` creation?
135 #
136 # Always returns `true` if `--force` is on.
137 fun need_render(src, target: String): Bool do
138 if force_render then return true
139 if not target.file_exists then return true
140 return src.file_stat.mtime >= target.file_stat.mtime
141 end
142
143 # Create a new `WikiSection`.
144 #
145 # `path` is used to determine the place in the wiki hierarchy.
146 protected fun new_section(path: String): WikiSection do
147 path = path.simplify_path
148 if entries.has_key(path) then return entries[path].as(WikiSection)
149 var root = expand_path(config.root_dir, config.source_dir)
150 var name = path.basename("")
151 var section = new WikiSection(self, name)
152 entries[path] = section
153 if path == root then return section
154 var ppath = path.dirname
155 if ppath != path then
156 var parent = new_section(ppath)
157 parent.add_child(section)
158 end
159 section.try_load_config
160 return section
161 end
162
163 # Create a new `WikiArticle`.
164 #
165 # `path` is used to determine the ancestor sections.
166 protected fun new_article(path: String): WikiArticle do
167 if entries.has_key(path) then return entries[path].as(WikiArticle)
168 message("Found article `{path}`", 2)
169 var article = new WikiArticle.from_source(self, path)
170 var section = new_section(path.dirname)
171 section.add_child(article)
172 entries[path] = article
173 return article
174 end
175
176 # Wiki entries found in the last `lookup_hierarchy`.
177 var entries = new HashMap[String, WikiEntry]
178
179 # The root `WikiSection` of the site found in the last `lookup_hierarchy`.
180 var root_section: WikiSection is noinit
181
182 # Does a template named `name` exists for this wiki?
183 fun has_template(name: String): Bool do
184 return expand_path(config.root_dir, config.templates_dir, name).file_exists
185 end
186
187 # Load a template file as a `TemplateString`.
188 #
189 # REQUIRE: `has_template`
190 fun load_template(name: String): TemplateString do
191 if not has_template(name) then
192 message("Error: can't load template `{name}`", 0)
193 exit 1
194 end
195 var file = expand_path(config.root_dir, config.templates_dir, name)
196 var tpl = new TemplateString.from_file(file)
197 if tpl.has_macro("ROOT_URL") then
198 tpl.replace("ROOT_URL", config.root_url)
199 end
200 if tpl.has_macro("TITLE") then
201 tpl.replace("TITLE", config.wiki_name)
202 end
203 if tpl.has_macro("SUBTITLE") then
204 tpl.replace("SUBTITLE", config.wiki_desc)
205 end
206 if tpl.has_macro("LOGO") then
207 tpl.replace("LOGO", config.wiki_logo)
208 end
209 return tpl
210 end
211
212 # Does a sideblock named `name` exists for this wiki?
213 fun has_sideblock(name: String): Bool do
214 name = "{name}.{config.md_ext}"
215 return expand_path(config.root_dir, config.sidebar_dir, name).file_exists
216 end
217
218 # Load a markdown block with `name` from `WikiConfig::sidebar_dir`.
219 private fun load_sideblock(name: String): nullable String do
220 if not has_sideblock(name) then
221 message("Error: can't load sideblock `{name}`", 0)
222 return null
223 end
224 name = "{name}.{config.md_ext}"
225 var path = expand_path(config.root_dir, config.sidebar_dir, name)
226 var file = new FileReader.open(path)
227 var res = file.read_all
228 file.close
229 return res
230 end
231
232 # Join `parts` as a path and simplify it
233 fun expand_path(parts: String...): String do
234 var path = ""
235 for part in parts do
236 path = path.join_path(part)
237 end
238 return path.simplify_path
239 end
240
241 # Transform an id style name into a pretty printed name.
242 #
243 # Used to translate ids in beautiful page names.
244 fun pretty_name(name: String): String do
245 name = name.replace("_", " ")
246 name = name.capitalized
247 return name
248 end
249 end
250
251 # A wiki is composed of hierarchical entries.
252 abstract class WikiEntry
253
254 # `Nitiwiki` this entry belongs to.
255 var wiki: Nitiwiki
256
257 # Entry data
258
259 # Entry internal name.
260 #
261 # Mainly used in urls.
262 var name: String
263
264 # Displayed title for `self`.
265 #
266 # If `self` is the root entry then display the wiki `WikiConfig::wiki_name` instead.
267 fun title: String do
268 if is_root then return wiki.config.wiki_name
269 return wiki.pretty_name(name)
270 end
271
272 # Is this section rendered from a source document?
273 #
274 # Source is an abstract concept at this level.
275 # It can represent a directory, a source file,
276 # a part of a file, everything needed to
277 # extend this base framework.
278 fun has_source: Bool do return src_path != null
279
280 # Entry creation time.
281 #
282 # Returns `-1` if not `has_source`.
283 fun create_time: Int do
284 if not has_source then return -1
285 return src_full_path.file_stat.ctime
286 end
287
288 # Entry last modification time.
289 #
290 # Returns `-1` if not `has_source`.
291 fun last_edit_time: Int do
292 if not has_source then return -1
293 return src_full_path.file_stat.mtime
294 end
295
296 # Entry list rendering time.
297 #
298 # Returns `-1` if `is_new`.
299 fun last_render_time: Int do
300 if is_new then return -1
301 return out_full_path.file_stat.mtime
302 end
303
304 # Entries hierarchy
305
306 # Type of the parent entry.
307 type PARENT: WikiEntry
308
309 # Parent entry if any.
310 var parent: nullable PARENT = null
311
312 # Does `self` have a parent?
313 fun is_root: Bool do return parent == null
314
315 # Children labelled by `name`.
316 var children = new HashMap[String, WikiEntry]
317
318 # Does `self` have a child nammed `name`?
319 fun has_child(name: String): Bool do return children.keys.has(name)
320
321 # Retrieve the child called `name`.
322 fun child(name: String): WikiEntry do return children[name]
323
324 # Add a sub-entry to `self`.
325 fun add_child(entry: WikiEntry) do
326 entry.parent = self
327 children[entry.name] = entry
328 end
329
330 # Paths and urls
331
332 # Breadcrumbs from the `Nitiwiki::root_section` to `self`.
333 #
334 # Result is returned as an array containg ordered entries:
335 # `breadcrumbs.first` is the root entry and
336 # `breadcrumbs.last == self`
337 var breadcrumbs: Array[WikiEntry] is lazy do
338 var path = new Array[WikiEntry]
339 var entry: nullable WikiEntry = self
340 while entry != null and not entry.is_root do
341 path.add entry
342 entry = entry.parent
343 end
344 return path.reversed
345 end
346
347 # Sidebar relative to this wiki entry.
348 var sidebar = new WikiSidebar(self)
349
350 # Relative path from `wiki.config.root_dir` to source if any.
351 fun src_path: nullable String is abstract
352
353 # Absolute path to the source if any.
354 fun src_full_path: nullable String do
355 var src = src_path
356 if src == null then return null
357 return wiki.config.root_dir.join_path(src)
358 end
359
360 # Relative path from `wiki.config.root_dir` to rendered output.
361 #
362 # Like `src_path`, this method can represent a
363 # directory or a file.
364 fun out_path: String is abstract
365
366 # Absolute path to the output.
367 fun out_full_path: String do return wiki.config.root_dir.join_path(out_path)
368
369 # Rendering
370
371 # Does `self` have already been rendered?
372 fun is_new: Bool do return not out_full_path.file_exists
373
374 # Does `self` rendered output is outdated?
375 #
376 # Returns `true` if `is_new` then check in children.
377 fun is_dirty: Bool do
378 if is_new then return true
379 if has_source then
380 if last_edit_time >= last_render_time then return true
381 end
382 for child in children.values do
383 if child.is_dirty then return true
384 end
385 return false
386 end
387
388 # Render `self` and `children` is needed.
389 fun render do for child in children.values do child.render
390
391 # Templating
392
393 # Template file for `self`.
394 #
395 # Each entity can use a custom template.
396 # By default the template is inherited from the parent.
397 #
398 # If the root does not have a custom template,
399 # then returns the main wiki template file.
400 fun template_file: String do
401 if is_root then return wiki.config.template_file
402 return parent.template_file
403 end
404
405 # Header template file for `self`.
406 #
407 # Behave like `template_file`.
408 fun header_file: String do
409 if is_root then return wiki.config.header_file
410 return parent.header_file
411 end
412
413 # Footer template file for `self`.
414 #
415 # Behave like `template_file`.
416 fun footer_file: String do
417 if is_root then return wiki.config.footer_file
418 return parent.footer_file
419 end
420
421 # Menu template file for `self`.
422 #
423 # Behave like `template_file`.
424 fun menu_file: String do
425 if is_root then return wiki.config.menu_file
426 return parent.menu_file
427 end
428
429 # Display the entry `name`.
430 redef fun to_s do return name
431 end
432
433 # Each WikiSection is related to a source directory.
434 #
435 # A section can contain other sub-sections or pages.
436 class WikiSection
437 super WikiEntry
438
439 # A section can only have another section as parent.
440 redef type PARENT: WikiSection
441
442 redef fun title do
443 if has_config then
444 var title = config.title
445 if title != null then return title
446 end
447 return super
448 end
449
450 # Is this section hidden?
451 #
452 # Hidden section are rendered but not linked in menus.
453 fun is_hidden: Bool do
454 if has_config then return config.is_hidden
455 return false
456 end
457
458 # Source directory.
459 redef fun src_path: String do
460 if parent == null then
461 return wiki.config.source_dir
462 else
463 return wiki.expand_path(parent.src_path, name)
464 end
465 end
466
467 # Config
468
469 # Custom configuration file for this section.
470 var config: nullable SectionConfig = null
471
472 # Does this section have its own config file?
473 fun has_config: Bool do return config != null
474
475 # Try to load the config file for this section.
476 private fun try_load_config do
477 var cfile = wiki.expand_path(wiki.config.root_dir, src_path, wiki.config_filename)
478 if not cfile.file_exists then return
479 wiki.message("Custom config for section {name}", 1)
480 config = new SectionConfig(cfile)
481 end
482
483 # Templating
484
485 # Also check custom config.
486 redef fun template_file do
487 if has_config then
488 var tpl = config.template_file
489 if tpl != null then return tpl
490 end
491 if is_root then return wiki.config.template_file
492 return parent.template_file
493 end
494
495 # Also check custom config.
496 redef fun header_file do
497 if has_config then
498 var tpl = config.header_file
499 if tpl != null then return tpl
500 end
501 if is_root then return wiki.config.header_file
502 return parent.header_file
503 end
504
505 # Also check custom config.
506 redef fun footer_file do
507 if has_config then
508 var tpl = config.footer_file
509 if tpl != null then return tpl
510 end
511 if is_root then return wiki.config.footer_file
512 return parent.footer_file
513 end
514
515 # Also check custom config.
516 redef fun menu_file do
517 if has_config then
518 var tpl = config.menu_file
519 if tpl != null then return tpl
520 end
521 if is_root then return wiki.config.menu_file
522 return parent.menu_file
523 end
524 end
525
526 # Each WikiArticle is related to a HTML file.
527 #
528 # Article can be created from scratch using this API or
529 # automatically from a markdown source file (see: `from_source`).
530 class WikiArticle
531 super WikiEntry
532
533 # Articles can only have `WikiSection` as parents.
534 redef type PARENT: WikiSection
535
536 redef fun title: String do
537 if name == "index" and parent != null then return parent.title
538 return super
539 end
540
541 # Page content.
542 #
543 # What you want to be displayed in the page.
544 var content: nullable Writable = null is writable
545
546 # Create a new article using a markdown source file.
547 init from_source(wiki: Nitiwiki, md_file: String) do
548 src_full_path = md_file
549 init(wiki, md_file.basename(".{wiki.config.md_ext}"))
550 content = md
551 end
552
553 redef var src_full_path: nullable String = null
554
555 redef fun src_path do
556 if src_full_path == null then return null
557 return src_full_path.substring_from(wiki.config.root_dir.length)
558 end
559
560 # The page markdown source content.
561 #
562 # Extract the markdown text from `source_file`.
563 #
564 # REQUIRE: `has_source`.
565 var md: nullable String is lazy do
566 if not has_source then return null
567 var file = new FileReader.open(src_full_path.to_s)
568 var md = file.read_all
569 file.close
570 return md
571 end
572
573 # Returns true if has source and
574 # `last_edit_date` > 'last_render_date'.
575 redef fun is_dirty do
576 if super then return true
577 if has_source then
578 return wiki.need_render(src_full_path.to_s, out_full_path)
579 end
580 return false
581 end
582
583 redef fun to_s do return "{name} ({parent or else "null"})"
584 end
585
586 # The sidebar is displayed in front of the main panel of a `WikiEntry`.
587 class WikiSidebar
588
589 # Wiki used to parse sidebar blocks.
590 var wiki: Nitiwiki is lazy do return entry.wiki
591
592 # WikiEntry this panel is related to.
593 var entry: WikiEntry
594
595 # Blocks are ieces of markdown that will be rendered in the sidebar.
596 var blocks: Array[Text] is lazy do
597 var res = new Array[Text]
598 # TODO get blocks from the entry for more customization
599 for name in entry.wiki.config.sidebar_blocks do
600 var block = wiki.load_sideblock(name)
601 if block == null then continue
602 res.add block
603 end
604 return res
605 end
606 end
607
608 # Wiki configuration class.
609 #
610 # This class provides services that ensure static typing when accessing the `config.ini` file.
611 class WikiConfig
612 super ConfigTree
613
614 # Returns the config value at `key` or return `default` if no key was found.
615 private fun value_or_default(key: String, default: String): String do
616 if not has_key(key) then return default
617 return self[key]
618 end
619
620 # Site name displayed.
621 #
622 # The title is used as home title and in headers.
623 #
624 # * key: `wiki.name`
625 # * default: `MyWiki`
626 var wiki_name: String is lazy do return value_or_default("wiki.name", "MyWiki")
627
628 # Site description.
629 #
630 # Displayed in header.
631 #
632 # * key: `wiki.desc`
633 # * default: ``
634 var wiki_desc: String is lazy do return value_or_default("wiki.desc", "")
635
636 # Site logo url.
637 #
638 # Url of the image to be displayed in header.
639 #
640 # * key: `wiki.logo`
641 # * default: ``
642 var wiki_logo: String is lazy do return value_or_default("wiki.logo", "")
643
644 # Root url of the wiki.
645 #
646 # * key: `wiki.root_url`
647 # * default: `http://localhost/`
648 var root_url: String is lazy do return value_or_default("wiki.root_url", "http://localhost/")
649
650 # Markdown extension recognized by this wiki.
651 #
652 # We allow only one kind of extension per wiki.
653 # Files with other markdown extensions will be treated as resources.
654 #
655 # * key: `wiki.md_ext`
656 # * default: `md`
657 var md_ext: String is lazy do return value_or_default("wiki.md_ext", "md")
658
659 # Root directory of the wiki.
660 #
661 # Directory where the wiki files are stored locally.
662 #
663 # * key: `wiki.root_dir`
664 # * default: `./`
665 var root_dir: String is lazy do return value_or_default("wiki.root_dir", "./").simplify_path
666
667 # Pages directory.
668 #
669 # Directory where markdown source files are stored.
670 #
671 # * key: `wiki.source_dir
672 # * default: `pages/`
673 var source_dir: String is lazy do
674 return value_or_default("wiki.source_dir", "pages/").simplify_path
675 end
676
677 # Output directory.
678 #
679 # Directory where public wiki files are generated.
680 # **This path MUST be relative to `root_dir`.**
681 #
682 # * key: `wiki.out_dir`
683 # * default: `out/`
684 var out_dir: String is lazy do return value_or_default("wiki.out_dir", "out/").simplify_path
685
686 # Asset files directory.
687 #
688 # Directory where public assets like JS scripts or CSS files are stored.
689 # **This path MUST be relative to `root_dir`.**
690 #
691 # * key: `wiki.assets_dir`
692 # * default: `assets/`
693 var assets_dir: String is lazy do
694 return value_or_default("wiki.assets_dir", "assets/").simplify_path
695 end
696
697 # Template files directory.
698 #
699 # Directory where template used in HTML generation are stored.
700 # **This path MUST be relative to `root_dir`.**
701 #
702 # * key: `wiki.templates_dir`
703 # * default: `templates/`
704 var templates_dir: String is lazy do
705 return value_or_default("wiki.templates_dir", "templates/").simplify_path
706 end
707
708 # Main template file.
709 #
710 # The main template is used to specify the overall structure of a page.
711 #
712 # * key: `wiki.template`
713 # * default: `template.html`
714 var template_file: String is lazy do
715 return value_or_default("wiki.template", "template.html")
716 end
717
718 # Main header template file.
719 #
720 # Used to specify the structure of the page header.
721 # This is generally the place where you want to put your logo and wiki title.
722 #
723 # * key: `wiki.header`
724 # * default: `header.html`
725 var header_file: String is lazy do
726 return value_or_default("wiki.header", "header.html")
727 end
728
729 # Main menu template file.
730 #
731 # Used to specify the menu structure.
732 #
733 # * key: `wiki.menu`
734 # * default: `menu.html`
735 var menu_file: String is lazy do
736 return value_or_default("wiki.menu", "menu.html")
737 end
738
739 # Main footer file.
740 #
741 # The main footer is used to specify the structure of the page footer.
742 # This is generally the place where you want to put your copyright.
743 #
744 # * key: `wiki.footer`
745 # * default: `footer.html`
746 var footer_file: String is lazy do
747 return value_or_default("wiki.footer", "footer.html")
748 end
749
750 # Automatically add a summary.
751 #
752 # * key: `wiki.auto_summary`
753 # * default: `true`
754 var auto_summary: Bool is lazy do
755 return value_or_default("wiki.auto_summary", "true") == "true"
756 end
757
758 # Sidebar position.
759 #
760 # Position of the sidebar between `left`, `right` and `none`. Any other value
761 # will be considered as `none`.
762 #
763 # * key: `wiki.sidebar`
764 # * default: `left`
765 var sidebar: String is lazy do
766 return value_or_default("wiki.sidebar", "left")
767 end
768
769 # Sidebar markdown block to include.
770 #
771 # Blocks are specified by their filename without the extension.
772 #
773 # * key: `wiki.sidebar.blocks`
774 # * default: `[]`
775 var sidebar_blocks: Array[String] is lazy do
776 var res = new Array[String]
777 if not has_key("wiki.sidebar.blocks") then return res
778 for val in at("wiki.sidebar.blocks").values do
779 res.add val
780 end
781 return res
782 end
783
784 # Sidebar files directory.
785 #
786 # Directory where sidebar blocks are stored.
787 # **This path MUST be relative to `root_dir`.**
788 #
789 # * key: `wiki.sidebar_dir`
790 # * default: `sidebar/`
791 var sidebar_dir: String is lazy do
792 return value_or_default("wiki.sidebar_dir", "sidebar/").simplify_path
793 end
794
795 # Directory used by rsync to upload wiki files.
796 #
797 # This information is used to update your distant wiki files (like the webserver).
798 #
799 # * key: `wiki.rsync_dir`
800 # * default: ``
801 var rsync_dir: String is lazy do return value_or_default("wiki.rsync_dir", "")
802
803 # Remote repository used to pull modifications on sources.
804 #
805 # * key: `wiki.git_origin`
806 # * default: `origin`
807 var git_origin: String is lazy do return value_or_default("wiki.git_origin", "origin")
808
809 # Remote branch used to pull modifications on sources.
810 #
811 # * key: `wiki.git_branch`
812 # * default: `master`
813 var git_branch: String is lazy do return value_or_default("wiki.git_branch", "master")
814 end
815
816 # WikiSection custom configuration.
817 #
818 # Each section can provide its own config file to customize
819 # appearance or behavior.
820 class SectionConfig
821 super ConfigTree
822
823 # Returns the config value at `key` or `null` if no key was found.
824 private fun value_or_null(key: String): nullable String do
825 if not has_key(key) then return null
826 return self[key]
827 end
828
829 # Is this section hidden in sitemap and trees and menus?
830 fun is_hidden: Bool do return value_or_null("section.hidden") == "true"
831
832 # Custom section title if any.
833 fun title: nullable String do return value_or_null("section.title")
834
835 # Custom template file if any.
836 fun template_file: nullable String do return value_or_null("section.template")
837
838 # Custom header file if any.
839 fun header_file: nullable String do return value_or_null("section.header")
840
841 # Custom menu file if any.
842 fun menu_file: nullable String do return value_or_null("section.menu")
843
844 # Custom footer file if any.
845 fun footer_file: nullable String do return value_or_null("section.footer")
846 end