1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Base entities of a nitiwiki.
18 import template
::macro
22 # A Nitiwiki instance.
24 # Nitiwiki provide all base services used by `WikiSection` and `WikiArticle`.
25 # It manages content and renders pages.
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.
33 var config
: WikiConfig
35 # Default config filename.
36 var config_filename
= "config.ini"
38 # Force render on all file even if the source is unmodified.
39 var force_render
= false is writable
42 var verbose_level
= 0 is writable
44 # Delete all the output files.
46 var out_dir
= expand_path
(config
.root_dir
, config
.out_dir
)
47 if out_dir
.file_exists
then out_dir
.rmdir
50 # Synchronize local output with the distant `WikiConfig::rsync_dir`.
52 var root
= expand_path
(config
.root_dir
, config
.out_dir
)
53 sys
.system
"rsync -vr --delete {root}/ {config.rsync_dir}"
56 # Pull data from git repository.
58 sys
.system
"git pull {config.git_origin} {config.git_branch}"
61 # Analyze wiki files from `dir` to build wiki entries.
63 # This method build a hierarchical structure of `WikiSection` and `WikiArticle`
64 # based on the markdown source structure.
66 var dir
= expand_path
(config
.root_dir
, config
.source_dir
)
67 root_section
= new_section
(dir
)
68 var files
= list_md_files
(dir
)
80 print
"name: {config.wiki_name}"
81 print
"config: {config.ini_file}"
83 if root_section
.is_dirty
then
84 print
"There is modified files:"
85 var paths
= entries
.keys
.to_a
86 var s
= new DefaultComparator
89 var entry
= entries
[path
]
90 if not entry
.is_dirty
then continue
92 if entry
.has_source
then name
= entry
.src_path
.to_s
100 print
"Use nitiwiki --render to render modified files"
102 print
"Wiki is up-to-date"
104 print
"Use nitiwiki --fetch to pull modification from origin"
105 print
"Use nitiwiki --rsync to synchronize distant output"
109 # Display msg if `level <= verbose_level`
110 fun message
(msg
: String, level
: Int) do
111 if level
<= verbose_level
then print msg
114 # List markdown source files from a directory.
115 fun list_md_files
(dir
: String): Array[String] do
116 var files
= new Array[String]
117 var pipe
= new ProcessReader("find", dir
, "-name", "*.{config.md_ext}")
118 while not pipe
.eof
do
119 var file
= pipe
.read_line
120 if file
== "" then break # last line
121 var name
= file
.basename
(".{config.md_ext}")
122 if name
== "header" or name
== "footer" or name
== "menu" then continue
127 if pipe
.status
!= 0 then exit
1
128 var s
= new DefaultComparator
133 # Does `src` have been modified since `target` creation?
135 # Always returns `true` if `--force` is on.
136 fun need_render
(src
, target
: String): Bool do
137 if force_render
then return true
138 if not target
.file_exists
then return true
139 return src
.file_stat
.mtime
>= target
.file_stat
.mtime
142 # Create a new `WikiSection`.
144 # `path` is used to determine the place in the wiki hierarchy.
145 protected fun new_section
(path
: String): WikiSection do
146 path
= path
.simplify_path
147 if entries
.has_key
(path
) then return entries
[path
].as(WikiSection)
148 var root
= expand_path
(config
.root_dir
, config
.source_dir
)
149 var name
= path
.basename
("")
150 var section
= new WikiSection(self, name
)
151 entries
[path
] = section
152 if path
== root
then return section
153 var ppath
= path
.dirname
154 if ppath
!= path
then
155 var parent
= new_section
(ppath
)
156 parent
.add_child
(section
)
158 section
.try_load_config
162 # Create a new `WikiArticle`.
164 # `path` is used to determine the ancestor sections.
165 protected fun new_article
(path
: String): WikiArticle do
166 if entries
.has_key
(path
) then return entries
[path
].as(WikiArticle)
167 message
("Found article `{path}`", 2)
168 var article
= new WikiArticle.from_source
(self, path
)
169 var section
= new_section
(path
.dirname
)
170 section
.add_child
(article
)
171 entries
[path
] = article
175 # Wiki entries found in the last `lookup_hierarchy`.
176 var entries
= new HashMap[String, WikiEntry]
178 # The root `WikiSection` of the site found in the last `lookup_hierarchy`.
179 var root_section
: WikiSection is noinit
181 # Does a template named `name` exists for this wiki?
182 fun has_template
(name
: String): Bool do
183 return expand_path
(config
.root_dir
, config
.templates_dir
, name
).file_exists
186 # Load a template file as a `TemplateString`.
188 # REQUIRE: `has_template`
189 fun load_template
(name
: String): TemplateString do
190 if not has_template
(name
) then
191 message
("Error: can't load template `{name}`", 0)
194 var file
= expand_path
(config
.root_dir
, config
.templates_dir
, name
)
195 var tpl
= new TemplateString.from_file
(file
)
196 if tpl
.has_macro
("TITLE") then
197 tpl
.replace
("TITLE", config
.wiki_name
)
199 if tpl
.has_macro
("SUBTITLE") then
200 tpl
.replace
("SUBTITLE", config
.wiki_desc
)
202 if tpl
.has_macro
("LOGO") then
203 tpl
.replace
("LOGO", config
.wiki_logo
)
208 # Does a sideblock named `name` exists for this wiki?
209 fun has_sideblock
(name
: String): Bool do
210 name
= "{name}.{config.md_ext}"
211 return expand_path
(config
.root_dir
, config
.sidebar_dir
, name
).file_exists
214 # Load a markdown block with `name` from `WikiConfig::sidebar_dir`.
215 private fun load_sideblock
(name
: String): nullable String do
216 if not has_sideblock
(name
) then
217 message
("Error: can't load sideblock `{name}`", 0)
220 name
= "{name}.{config.md_ext}"
221 var path
= expand_path
(config
.root_dir
, config
.sidebar_dir
, name
)
222 var file
= new FileReader.open
(path
)
223 var res
= file
.read_all
228 # Join `parts` as a path and simplify it
229 fun expand_path
(parts
: String...): String do
232 path
= path
.join_path
(part
)
234 return path
.simplify_path
237 # Transform an id style name into a pretty printed name.
239 # Used to translate ids in beautiful page names.
240 fun pretty_name
(name
: String): String do
241 name
= name
.replace
("_", " ")
242 name
= name
.capitalized
247 # A wiki is composed of hierarchical entries.
248 abstract class WikiEntry
250 # `Nitiwiki` this entry belongs to.
255 # Entry internal name.
257 # Mainly used in urls.
260 # Displayed title for `self`.
262 # If `self` is the root entry then display the wiki `WikiConfig::wiki_name` instead.
264 if is_root
then return wiki
.config
.wiki_name
265 return wiki
.pretty_name
(name
)
268 # Is this section rendered from a source document?
270 # Source is an abstract concept at this level.
271 # It can represent a directory, a source file,
272 # a part of a file, everything needed to
273 # extend this base framework.
274 fun has_source
: Bool do return src_path
!= null
276 # Entry creation time.
278 # Returns `-1` if not `has_source`.
279 fun create_time
: Int do
280 if not has_source
then return -1
281 return src_full_path
.file_stat
.ctime
284 # Entry last modification time.
286 # Returns `-1` if not `has_source`.
287 fun last_edit_time
: Int do
288 if not has_source
then return -1
289 return src_full_path
.file_stat
.mtime
292 # Entry list rendering time.
294 # Returns `-1` if `is_new`.
295 fun last_render_time
: Int do
296 if is_new
then return -1
297 return out_full_path
.file_stat
.mtime
302 # Type of the parent entry.
303 type PARENT: WikiEntry
305 # Parent entry if any.
306 var parent
: nullable PARENT = null
308 # Does `self` have a parent?
309 fun is_root
: Bool do return parent
== null
311 # Children labelled by `name`.
312 var children
= new HashMap[String, WikiEntry]
314 # Does `self` have a child nammed `name`?
315 fun has_child
(name
: String): Bool do return children
.keys
.has
(name
)
317 # Retrieve the child called `name`.
318 fun child
(name
: String): WikiEntry do return children
[name
]
320 # Add a sub-entry to `self`.
321 fun add_child
(entry
: WikiEntry) do
323 children
[entry
.name
] = entry
328 # Breadcrumbs from the `Nitiwiki::root_section` to `self`.
330 # Result is returned as an array containg ordered entries:
331 # `breadcrumbs.first` is the root entry and
332 # `breadcrumbs.last == self`
333 var breadcrumbs
: Array[WikiEntry] is lazy
do
334 var path
= new Array[WikiEntry]
335 var entry
: nullable WikiEntry = self
336 while entry
!= null and not entry
.is_root
do
343 # Sidebar relative to this wiki entry.
344 var sidebar
= new WikiSidebar(self)
346 # Relative path from `wiki.config.root_dir` to source if any.
347 fun src_path
: nullable String is abstract
349 # Absolute path to the source if any.
350 fun src_full_path
: nullable String do
352 if src
== null then return null
353 return wiki
.config
.root_dir
.join_path
(src
)
356 # Relative path from `wiki.config.root_dir` to rendered output.
358 # Like `src_path`, this method can represent a
359 # directory or a file.
360 fun out_path
: String is abstract
362 # Absolute path to the output.
363 fun out_full_path
: String do return wiki
.config
.root_dir
.join_path
(out_path
)
367 # Does `self` have already been rendered?
368 fun is_new
: Bool do return not out_full_path
.file_exists
370 # Does `self` rendered output is outdated?
372 # Returns `true` if `is_new` then check in children.
373 fun is_dirty
: Bool do
374 if is_new
then return true
376 if last_edit_time
>= last_render_time
then return true
378 for child
in children
.values
do
379 if child
.is_dirty
then return true
384 # Render `self` and `children` is needed.
385 fun render
do for child
in children
.values
do child
.render
389 # Template file for `self`.
391 # Each entity can use a custom template.
392 # By default the template is inherited from the parent.
394 # If the root does not have a custom template,
395 # then returns the main wiki template file.
396 fun template_file
: String do
397 if is_root
then return wiki
.config
.template_file
398 return parent
.template_file
401 # Header template file for `self`.
403 # Behave like `template_file`.
404 fun header_file
: String do
405 if is_root
then return wiki
.config
.header_file
406 return parent
.header_file
409 # Footer template file for `self`.
411 # Behave like `template_file`.
412 fun footer_file
: String do
413 if is_root
then return wiki
.config
.footer_file
414 return parent
.footer_file
417 # Menu template file for `self`.
419 # Behave like `template_file`.
420 fun menu_file
: String do
421 if is_root
then return wiki
.config
.menu_file
422 return parent
.menu_file
425 # Display the entry `name`.
426 redef fun to_s
do return name
429 # Each WikiSection is related to a source directory.
431 # A section can contain other sub-sections or pages.
435 # A section can only have another section as parent.
436 redef type PARENT: WikiSection
440 var title
= config
.title
441 if title
!= null then return title
446 # Is this section hidden?
448 # Hidden section are rendered but not linked in menus.
449 fun is_hidden
: Bool do
450 if has_config
then return config
.is_hidden
455 redef fun src_path
: String do
456 if parent
== null then
457 return wiki
.config
.source_dir
459 return wiki
.expand_path
(parent
.src_path
, name
)
465 # Custom configuration file for this section.
466 var config
: nullable SectionConfig = null
468 # Does this section have its own config file?
469 fun has_config
: Bool do return config
!= null
471 # Try to load the config file for this section.
472 private fun try_load_config
do
473 var cfile
= wiki
.expand_path
(wiki
.config
.root_dir
, src_path
, wiki
.config_filename
)
474 if not cfile
.file_exists
then return
475 wiki
.message
("Custom config for section {name}", 1)
476 config
= new SectionConfig(cfile
)
481 # Also check custom config.
482 redef fun template_file
do
484 var tpl
= config
.template_file
485 if tpl
!= null then return tpl
487 if is_root
then return wiki
.config
.template_file
488 return parent
.template_file
491 # Also check custom config.
492 redef fun header_file
do
494 var tpl
= config
.header_file
495 if tpl
!= null then return tpl
497 if is_root
then return wiki
.config
.header_file
498 return parent
.header_file
501 # Also check custom config.
502 redef fun footer_file
do
504 var tpl
= config
.footer_file
505 if tpl
!= null then return tpl
507 if is_root
then return wiki
.config
.footer_file
508 return parent
.footer_file
511 # Also check custom config.
512 redef fun menu_file
do
514 var tpl
= config
.menu_file
515 if tpl
!= null then return tpl
517 if is_root
then return wiki
.config
.menu_file
518 return parent
.menu_file
522 # Each WikiArticle is related to a HTML file.
524 # Article can be created from scratch using this API or
525 # automatically from a markdown source file (see: `from_source`).
529 # Articles can only have `WikiSection` as parents.
530 redef type PARENT: WikiSection
532 redef fun title
: String do
533 if name
== "index" and parent
!= null then return parent
.title
539 # What you want to be displayed in the page.
540 var content
: nullable Writable = null is writable
542 # Create a new article using a markdown source file.
543 init from_source
(wiki
: Nitiwiki, md_file
: String) do
544 src_full_path
= md_file
545 init(wiki
, md_file
.basename
(".{wiki.config.md_ext}"))
549 redef var src_full_path
: nullable String = null
551 redef fun src_path
do
552 var src_full_path
= self.src_full_path
553 if src_full_path
== null then return null
554 var res
= wiki
.config
.root_dir
.relpath
(src_full_path
)
558 # The page markdown source content.
560 # Extract the markdown text from `source_file`.
562 # REQUIRE: `has_source`.
563 var md
: nullable String is lazy
do
564 if not has_source
then return null
565 var file
= new FileReader.open
(src_full_path
.to_s
)
566 var md
= file
.read_all
571 # Returns true if has source and
572 # `last_edit_date` > 'last_render_date'.
573 redef fun is_dirty
do
574 if super then return true
576 return wiki
.need_render
(src_full_path
.to_s
, out_full_path
)
581 redef fun to_s
do return "{name} ({parent or else "null"})"
584 # The sidebar is displayed in front of the main panel of a `WikiEntry`.
587 # Wiki used to parse sidebar blocks.
588 var wiki
: Nitiwiki is lazy
do return entry
.wiki
590 # WikiEntry this panel is related to.
593 # Blocks are ieces of markdown that will be rendered in the sidebar.
594 var blocks
: Array[Text] is lazy
do
595 var res
= new Array[Text]
596 # TODO get blocks from the entry for more customization
597 for name
in entry
.wiki
.config
.sidebar_blocks
do
598 var block
= wiki
.load_sideblock
(name
)
599 if block
== null then continue
606 # Wiki configuration class.
608 # This class provides services that ensure static typing when accessing the `config.ini` file.
612 # Returns the config value at `key` or return `default` if no key was found.
613 private fun value_or_default
(key
: String, default
: String): String do
614 if not has_key
(key
) then return default
618 # Site name displayed.
620 # The title is used as home title and in headers.
623 # * default: `MyWiki`
624 var wiki_name
: String is lazy
do return value_or_default
("wiki.name", "MyWiki")
628 # Displayed in header.
632 var wiki_desc
: String is lazy
do return value_or_default
("wiki.desc", "")
636 # Url of the image to be displayed in header.
640 var wiki_logo
: String is lazy
do return value_or_default
("wiki.logo", "")
642 # Markdown extension recognized by this wiki.
644 # We allow only one kind of extension per wiki.
645 # Files with other markdown extensions will be treated as resources.
647 # * key: `wiki.md_ext`
649 var md_ext
: String is lazy
do return value_or_default
("wiki.md_ext", "md")
651 # Root directory of the wiki.
653 # Directory where the wiki files are stored locally.
655 # * key: `wiki.root_dir`
657 var root_dir
: String is lazy
do return value_or_default
("wiki.root_dir", "./").simplify_path
661 # Directory where markdown source files are stored.
663 # * key: `wiki.source_dir
664 # * default: `pages/`
665 var source_dir
: String is lazy
do
666 return value_or_default
("wiki.source_dir", "pages/").simplify_path
671 # Directory where public wiki files are generated.
672 # **This path MUST be relative to `root_dir`.**
674 # * key: `wiki.out_dir`
676 var out_dir
: String is lazy
do return value_or_default
("wiki.out_dir", "out/").simplify_path
678 # Asset files directory.
680 # Directory where public assets like JS scripts or CSS files are stored.
681 # **This path MUST be relative to `root_dir`.**
683 # * key: `wiki.assets_dir`
684 # * default: `assets/`
685 var assets_dir
: String is lazy
do
686 return value_or_default
("wiki.assets_dir", "assets/").simplify_path
689 # Template files directory.
691 # Directory where template used in HTML generation are stored.
692 # **This path MUST be relative to `root_dir`.**
694 # * key: `wiki.templates_dir`
695 # * default: `templates/`
696 var templates_dir
: String is lazy
do
697 return value_or_default
("wiki.templates_dir", "templates/").simplify_path
700 # Main template file.
702 # The main template is used to specify the overall structure of a page.
704 # * key: `wiki.template`
705 # * default: `template.html`
706 var template_file
: String is lazy
do
707 return value_or_default
("wiki.template", "template.html")
710 # Main header template file.
712 # Used to specify the structure of the page header.
713 # This is generally the place where you want to put your logo and wiki title.
715 # * key: `wiki.header`
716 # * default: `header.html`
717 var header_file
: String is lazy
do
718 return value_or_default
("wiki.header", "header.html")
721 # Main menu template file.
723 # Used to specify the menu structure.
726 # * default: `menu.html`
727 var menu_file
: String is lazy
do
728 return value_or_default
("wiki.menu", "menu.html")
733 # The main footer is used to specify the structure of the page footer.
734 # This is generally the place where you want to put your copyright.
736 # * key: `wiki.footer`
737 # * default: `footer.html`
738 var footer_file
: String is lazy
do
739 return value_or_default
("wiki.footer", "footer.html")
742 # Automatically add a summary.
744 # * key: `wiki.auto_summary`
746 var auto_summary
: Bool is lazy
do
747 return value_or_default
("wiki.auto_summary", "true") == "true"
750 # Automatically add breadcrumbs.
752 # * key: `wiki.auto_breadcrumbs`
754 var auto_breadcrumbs
: Bool is lazy
do
755 return value_or_default
("wiki.auto_breadcrumbs", "true") == "true"
760 # Position of the sidebar between `left`, `right` and `none`. Any other value
761 # will be considered as `none`.
763 # * key: `wiki.sidebar`
765 var sidebar
: String is lazy
do
766 return value_or_default
("wiki.sidebar", "left")
769 # Sidebar markdown block to include.
771 # Blocks are specified by their filename without the extension.
773 # * key: `wiki.sidebar.blocks`
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
784 # Sidebar files directory.
786 # Directory where sidebar blocks are stored.
787 # **This path MUST be relative to `root_dir`.**
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
795 # Directory used by rsync to upload wiki files.
797 # This information is used to update your distant wiki files (like the webserver).
799 # * key: `wiki.rsync_dir`
801 var rsync_dir
: String is lazy
do return value_or_default
("wiki.rsync_dir", "")
803 # Remote repository used to pull modifications on sources.
805 # * key: `wiki.git_origin`
806 # * default: `origin`
807 var git_origin
: String is lazy
do return value_or_default
("wiki.git_origin", "origin")
809 # Remote branch used to pull modifications on sources.
811 # * key: `wiki.git_branch`
812 # * default: `master`
813 var git_branch
: String is lazy
do return value_or_default
("wiki.git_branch", "master")
815 # URL to source versionning used to display last changes
817 # * key: `wiki.last_changes`
819 var last_changes
: String is lazy
do return value_or_default
("wiki.last_changes", "")
821 # URL to source edition.
825 var edit
: String is lazy
do return value_or_default
("wiki.edit", "")
828 # WikiSection custom configuration.
830 # Each section can provide its own config file to customize
831 # appearance or behavior.
835 # Returns the config value at `key` or `null` if no key was found.
836 private fun value_or_null
(key
: String): nullable String do
837 if not has_key
(key
) then return null
841 # Is this section hidden in sitemap and trees and menus?
842 fun is_hidden
: Bool do return value_or_null
("section.hidden") == "true"
844 # Custom section title if any.
845 fun title
: nullable String do return value_or_null
("section.title")
847 # Custom template file if any.
848 fun template_file
: nullable String do return value_or_null
("section.template")
850 # Custom header file if any.
851 fun header_file
: nullable String do return value_or_null
("section.header")
853 # Custom menu file if any.
854 fun menu_file
: nullable String do return value_or_null
("section.menu")
856 # Custom footer file if any.
857 fun footer_file
: nullable String do return value_or_null
("section.footer")