nitiwiki: change position of sidebar from config
[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 # Join `parts` as a path and simplify it
213 fun expand_path(parts: String...): String do
214 var path = ""
215 for part in parts do
216 path = path.join_path(part)
217 end
218 return path.simplify_path
219 end
220
221 # Transform an id style name into a pretty printed name.
222 #
223 # Used to translate ids in beautiful page names.
224 fun pretty_name(name: String): String do
225 name = name.replace("_", " ")
226 name = name.capitalized
227 return name
228 end
229 end
230
231 # A wiki is composed of hierarchical entries.
232 abstract class WikiEntry
233
234 # `Nitiwiki` this entry belongs to.
235 var wiki: Nitiwiki
236
237 # Entry data
238
239 # Entry internal name.
240 #
241 # Mainly used in urls.
242 var name: String
243
244 # Displayed title for `self`.
245 #
246 # If `self` is the root entry then display the wiki `WikiConfig::wiki_name` instead.
247 fun title: String do
248 if is_root then return wiki.config.wiki_name
249 return wiki.pretty_name(name)
250 end
251
252 # Is this section rendered from a source document?
253 #
254 # Source is an abstract concept at this level.
255 # It can represent a directory, a source file,
256 # a part of a file, everything needed to
257 # extend this base framework.
258 fun has_source: Bool do return src_path != null
259
260 # Entry creation time.
261 #
262 # Returns `-1` if not `has_source`.
263 fun create_time: Int do
264 if not has_source then return -1
265 return src_full_path.file_stat.ctime
266 end
267
268 # Entry last modification time.
269 #
270 # Returns `-1` if not `has_source`.
271 fun last_edit_time: Int do
272 if not has_source then return -1
273 return src_full_path.file_stat.mtime
274 end
275
276 # Entry list rendering time.
277 #
278 # Returns `-1` if `is_new`.
279 fun last_render_time: Int do
280 if is_new then return -1
281 return out_full_path.file_stat.mtime
282 end
283
284 # Entries hierarchy
285
286 # Type of the parent entry.
287 type PARENT: WikiEntry
288
289 # Parent entry if any.
290 var parent: nullable PARENT = null
291
292 # Does `self` have a parent?
293 fun is_root: Bool do return parent == null
294
295 # Children labelled by `name`.
296 var children = new HashMap[String, WikiEntry]
297
298 # Does `self` have a child nammed `name`?
299 fun has_child(name: String): Bool do return children.keys.has(name)
300
301 # Retrieve the child called `name`.
302 fun child(name: String): WikiEntry do return children[name]
303
304 # Add a sub-entry to `self`.
305 fun add_child(entry: WikiEntry) do
306 entry.parent = self
307 children[entry.name] = entry
308 end
309
310 # Paths and urls
311
312 # Breadcrumbs from the `Nitiwiki::root_section` to `self`.
313 #
314 # Result is returned as an array containg ordered entries:
315 # `breadcrumbs.first` is the root entry and
316 # `breadcrumbs.last == self`
317 var breadcrumbs: Array[WikiEntry] is lazy do
318 var path = new Array[WikiEntry]
319 var entry: nullable WikiEntry = self
320 while entry != null and not entry.is_root do
321 path.add entry
322 entry = entry.parent
323 end
324 return path.reversed
325 end
326
327 # Relative path from `wiki.config.root_dir` to source if any.
328 fun src_path: nullable String is abstract
329
330 # Absolute path to the source if any.
331 fun src_full_path: nullable String do
332 var src = src_path
333 if src == null then return null
334 return wiki.config.root_dir.join_path(src)
335 end
336
337 # Relative path from `wiki.config.root_dir` to rendered output.
338 #
339 # Like `src_path`, this method can represent a
340 # directory or a file.
341 fun out_path: String is abstract
342
343 # Absolute path to the output.
344 fun out_full_path: String do return wiki.config.root_dir.join_path(out_path)
345
346 # Rendering
347
348 # Does `self` have already been rendered?
349 fun is_new: Bool do return not out_full_path.file_exists
350
351 # Does `self` rendered output is outdated?
352 #
353 # Returns `true` if `is_new` then check in children.
354 fun is_dirty: Bool do
355 if is_new then return true
356 if has_source then
357 if last_edit_time >= last_render_time then return true
358 end
359 for child in children.values do
360 if child.is_dirty then return true
361 end
362 return false
363 end
364
365 # Render `self` and `children` is needed.
366 fun render do for child in children.values do child.render
367
368 # Templating
369
370 # Template file for `self`.
371 #
372 # Each entity can use a custom template.
373 # By default the template is inherited from the parent.
374 #
375 # If the root does not have a custom template,
376 # then returns the main wiki template file.
377 fun template_file: String do
378 if is_root then return wiki.config.template_file
379 return parent.template_file
380 end
381
382 # Header template file for `self`.
383 #
384 # Behave like `template_file`.
385 fun header_file: String do
386 if is_root then return wiki.config.header_file
387 return parent.header_file
388 end
389
390 # Footer template file for `self`.
391 #
392 # Behave like `template_file`.
393 fun footer_file: String do
394 if is_root then return wiki.config.footer_file
395 return parent.footer_file
396 end
397
398 # Menu template file for `self`.
399 #
400 # Behave like `template_file`.
401 fun menu_file: String do
402 if is_root then return wiki.config.menu_file
403 return parent.menu_file
404 end
405
406 # Display the entry `name`.
407 redef fun to_s do return name
408 end
409
410 # Each WikiSection is related to a source directory.
411 #
412 # A section can contain other sub-sections or pages.
413 class WikiSection
414 super WikiEntry
415
416 # A section can only have another section as parent.
417 redef type PARENT: WikiSection
418
419 redef fun title do
420 if has_config then
421 var title = config.title
422 if title != null then return title
423 end
424 return super
425 end
426
427 # Is this section hidden?
428 #
429 # Hidden section are rendered but not linked in menus.
430 fun is_hidden: Bool do
431 if has_config then return config.is_hidden
432 return false
433 end
434
435 # Source directory.
436 redef fun src_path: String do
437 if parent == null then
438 return wiki.config.source_dir
439 else
440 return wiki.expand_path(parent.src_path, name)
441 end
442 end
443
444 # Config
445
446 # Custom configuration file for this section.
447 var config: nullable SectionConfig = null
448
449 # Does this section have its own config file?
450 fun has_config: Bool do return config != null
451
452 # Try to load the config file for this section.
453 private fun try_load_config do
454 var cfile = wiki.expand_path(wiki.config.root_dir, src_path, wiki.config_filename)
455 if not cfile.file_exists then return
456 wiki.message("Custom config for section {name}", 1)
457 config = new SectionConfig(cfile)
458 end
459
460 # Templating
461
462 # Also check custom config.
463 redef fun template_file do
464 if has_config then
465 var tpl = config.template_file
466 if tpl != null then return tpl
467 end
468 if is_root then return wiki.config.template_file
469 return parent.template_file
470 end
471
472 # Also check custom config.
473 redef fun header_file do
474 if has_config then
475 var tpl = config.header_file
476 if tpl != null then return tpl
477 end
478 if is_root then return wiki.config.header_file
479 return parent.header_file
480 end
481
482 # Also check custom config.
483 redef fun footer_file do
484 if has_config then
485 var tpl = config.footer_file
486 if tpl != null then return tpl
487 end
488 if is_root then return wiki.config.footer_file
489 return parent.footer_file
490 end
491
492 # Also check custom config.
493 redef fun menu_file do
494 if has_config then
495 var tpl = config.menu_file
496 if tpl != null then return tpl
497 end
498 if is_root then return wiki.config.menu_file
499 return parent.menu_file
500 end
501 end
502
503 # Each WikiArticle is related to a HTML file.
504 #
505 # Article can be created from scratch using this API or
506 # automatically from a markdown source file (see: `from_source`).
507 class WikiArticle
508 super WikiEntry
509
510 # Articles can only have `WikiSection` as parents.
511 redef type PARENT: WikiSection
512
513 redef fun title: String do
514 if name == "index" and parent != null then return parent.title
515 return super
516 end
517
518 # Page content.
519 #
520 # What you want to be displayed in the page.
521 var content: nullable Writable = null is writable
522
523 # Create a new article using a markdown source file.
524 init from_source(wiki: Nitiwiki, md_file: String) do
525 src_full_path = md_file
526 init(wiki, md_file.basename(".{wiki.config.md_ext}"))
527 content = md
528 end
529
530 redef var src_full_path: nullable String = null
531
532 redef fun src_path do
533 if src_full_path == null then return null
534 return src_full_path.substring_from(wiki.config.root_dir.length)
535 end
536
537 # The page markdown source content.
538 #
539 # Extract the markdown text from `source_file`.
540 #
541 # REQUIRE: `has_source`.
542 var md: nullable String is lazy do
543 if not has_source then return null
544 var file = new FileReader.open(src_full_path.to_s)
545 var md = file.read_all
546 file.close
547 return md
548 end
549
550 # Returns true if has source and
551 # `last_edit_date` > 'last_render_date'.
552 redef fun is_dirty do
553 if super then return true
554 if has_source then
555 return wiki.need_render(src_full_path.to_s, out_full_path)
556 end
557 return false
558 end
559
560 redef fun to_s do return "{name} ({parent or else "null"})"
561 end
562
563 # Wiki configuration class.
564 #
565 # This class provides services that ensure static typing when accessing the `config.ini` file.
566 class WikiConfig
567 super ConfigTree
568
569 # Returns the config value at `key` or return `default` if no key was found.
570 private fun value_or_default(key: String, default: String): String do
571 if not has_key(key) then return default
572 return self[key]
573 end
574
575 # Site name displayed.
576 #
577 # The title is used as home title and in headers.
578 #
579 # * key: `wiki.name`
580 # * default: `MyWiki`
581 var wiki_name: String is lazy do return value_or_default("wiki.name", "MyWiki")
582
583 # Site description.
584 #
585 # Displayed in header.
586 #
587 # * key: `wiki.desc`
588 # * default: ``
589 var wiki_desc: String is lazy do return value_or_default("wiki.desc", "")
590
591 # Site logo url.
592 #
593 # Url of the image to be displayed in header.
594 #
595 # * key: `wiki.logo`
596 # * default: ``
597 var wiki_logo: String is lazy do return value_or_default("wiki.logo", "")
598
599 # Root url of the wiki.
600 #
601 # * key: `wiki.root_url`
602 # * default: `http://localhost/`
603 var root_url: String is lazy do return value_or_default("wiki.root_url", "http://localhost/")
604
605 # Markdown extension recognized by this wiki.
606 #
607 # We allow only one kind of extension per wiki.
608 # Files with other markdown extensions will be treated as resources.
609 #
610 # * key: `wiki.md_ext`
611 # * default: `md`
612 var md_ext: String is lazy do return value_or_default("wiki.md_ext", "md")
613
614 # Root directory of the wiki.
615 #
616 # Directory where the wiki files are stored locally.
617 #
618 # * key: `wiki.root_dir`
619 # * default: `./`
620 var root_dir: String is lazy do return value_or_default("wiki.root_dir", "./").simplify_path
621
622 # Pages directory.
623 #
624 # Directory where markdown source files are stored.
625 #
626 # * key: `wiki.source_dir
627 # * default: `pages/`
628 var source_dir: String is lazy do
629 return value_or_default("wiki.source_dir", "pages/").simplify_path
630 end
631
632 # Output directory.
633 #
634 # Directory where public wiki files are generated.
635 # **This path MUST be relative to `root_dir`.**
636 #
637 # * key: `wiki.out_dir`
638 # * default: `out/`
639 var out_dir: String is lazy do return value_or_default("wiki.out_dir", "out/").simplify_path
640
641 # Asset files directory.
642 #
643 # Directory where public assets like JS scripts or CSS files are stored.
644 # **This path MUST be relative to `root_dir`.**
645 #
646 # * key: `wiki.assets_dir`
647 # * default: `assets/`
648 var assets_dir: String is lazy do
649 return value_or_default("wiki.assets_dir", "assets/").simplify_path
650 end
651
652 # Template files directory.
653 #
654 # Directory where template used in HTML generation are stored.
655 # **This path MUST be relative to `root_dir`.**
656 #
657 # * key: `wiki.templates_dir`
658 # * default: `templates/`
659 var templates_dir: String is lazy do
660 return value_or_default("wiki.templates_dir", "templates/").simplify_path
661 end
662
663 # Main template file.
664 #
665 # The main template is used to specify the overall structure of a page.
666 #
667 # * key: `wiki.template`
668 # * default: `template.html`
669 var template_file: String is lazy do
670 return value_or_default("wiki.template", "template.html")
671 end
672
673 # Main header template file.
674 #
675 # Used to specify the structure of the page header.
676 # This is generally the place where you want to put your logo and wiki title.
677 #
678 # * key: `wiki.header`
679 # * default: `header.html`
680 var header_file: String is lazy do
681 return value_or_default("wiki.header", "header.html")
682 end
683
684 # Main menu template file.
685 #
686 # Used to specify the menu structure.
687 #
688 # * key: `wiki.menu`
689 # * default: `menu.html`
690 var menu_file: String is lazy do
691 return value_or_default("wiki.menu", "menu.html")
692 end
693
694 # Main footer file.
695 #
696 # The main footer is used to specify the structure of the page footer.
697 # This is generally the place where you want to put your copyright.
698 #
699 # * key: `wiki.footer`
700 # * default: `footer.html`
701 var footer_file: String is lazy do
702 return value_or_default("wiki.footer", "footer.html")
703 end
704
705 # Sidebar position.
706 #
707 # Position of the sidebar between `left`, `right` and `none`. Any other value
708 # will be considered as `none`.
709 #
710 # * key: `wiki.sidebar`
711 # * default: `left`
712 var sidebar: String is lazy do
713 return value_or_default("wiki.sidebar", "left")
714 end
715
716 # Directory used by rsync to upload wiki files.
717 #
718 # This information is used to update your distant wiki files (like the webserver).
719 #
720 # * key: `wiki.rsync_dir`
721 # * default: ``
722 var rsync_dir: String is lazy do return value_or_default("wiki.rsync_dir", "")
723
724 # Remote repository used to pull modifications on sources.
725 #
726 # * key: `wiki.git_origin`
727 # * default: `origin`
728 var git_origin: String is lazy do return value_or_default("wiki.git_origin", "origin")
729
730 # Remote branch used to pull modifications on sources.
731 #
732 # * key: `wiki.git_branch`
733 # * default: `master`
734 var git_branch: String is lazy do return value_or_default("wiki.git_branch", "master")
735 end
736
737 # WikiSection custom configuration.
738 #
739 # Each section can provide its own config file to customize
740 # appearance or behavior.
741 class SectionConfig
742 super ConfigTree
743
744 # Returns the config value at `key` or `null` if no key was found.
745 private fun value_or_null(key: String): nullable String do
746 if not has_key(key) then return null
747 return self[key]
748 end
749
750 # Is this section hidden in sitemap and trees and menus?
751 fun is_hidden: Bool do return value_or_null("section.hidden") == "true"
752
753 # Custom section title if any.
754 fun title: nullable String do return value_or_null("section.title")
755
756 # Custom template file if any.
757 fun template_file: nullable String do return value_or_null("section.template")
758
759 # Custom header file if any.
760 fun header_file: nullable String do return value_or_null("section.header")
761
762 # Custom menu file if any.
763 fun menu_file: nullable String do return value_or_null("section.menu")
764
765 # Custom footer file if any.
766 fun footer_file: nullable String do return value_or_null("section.footer")
767 end