Merge: doc: fixed some typos and other misc. corrections
[nit.git] / contrib / nitiwiki / src / nitiwiki.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 # A wiki engine based on markdown files and git.
16 module nitiwiki
17
18 import wiki_html
19 import markdown_highlight
20 import config
21
22 # Locate nit directory
23 private fun compute_nit_dir(opt_nit_dir: OptionString): String do
24 # the option has precedence
25 var res = opt_nit_dir.value
26 if res != null then
27 if not check_nit_dir(res) then
28 print "Fatal Error: --nit-dir does not seem to be a valid base Nit directory: {res}"
29 exit 1
30 end
31 return res
32 end
33
34 # then the environ variable has precedence
35 res = "NIT_DIR".environ
36 if not res.is_empty then
37 if not check_nit_dir(res) then
38 print "Fatal Error: NIT_DIR does not seem to be a valid base Nit directory: {res}"
39 exit 1
40 end
41 return res
42 end
43
44 # find the runpath of the program from argv[0]
45 res = "{sys.program_name.dirname}/.."
46 if check_nit_dir(res) then return res.simplify_path
47
48 # find the runpath of the process from /proc
49 var exe = "/proc/self/exe"
50 if exe.file_exists then
51 res = exe.realpath
52 res = res.dirname.join_path("..")
53 if check_nit_dir(res) then return res.simplify_path
54 end
55
56 # search in the PATH
57 var ps = "PATH".environ.split(":")
58 for p in ps do
59 res = p/".."
60 if check_nit_dir(res) then return res.simplify_path
61 end
62
63 print "Fatal Error: Cannot locate a valid base nit directory. It is quite unexpected. Try to set the environment variable `NIT_DIR` or to use the `--nit-dir` option."
64 exit 1
65 abort
66 end
67
68 private fun check_nit_dir(res: String): Bool do
69 return res.file_exists and "{res}/src/nit.nit".file_exists
70 end
71
72 var opt_verbose = new OptionCount("Verbose level", "-v")
73 var opt_config = new OptionString("Path to config.ini file", "-c", "--config")
74 var opt_init = new OptionBool("Initialize a new wiki in the current directory", "--init")
75 var opt_status = new OptionBool("Display wiki status", "--status")
76 var opt_render = new OptionBool("Render the out directory from markdown sources", "-r", "--render")
77 var opt_force = new OptionBool("Force render even if source files are unchanged", "-f", "--force")
78 var opt_clean = new OptionBool("Clean the output directory", "--clean")
79 var opt_rsync = new OptionBool("Synchronize outdir with distant wiki using rsync", "-s", "--rsync")
80 var opt_fetch = new OptionBool("Render local source from git repo", "--fetch")
81 var opt_nit_dir = new OptionString("Nit base directory", "--nit-dir")
82
83 var config = new Config
84 config.add_option(opt_verbose, opt_config)
85 config.add_option(opt_init, opt_status, opt_render, opt_force)
86 config.add_option(opt_clean, opt_rsync, opt_fetch, opt_nit_dir)
87
88 var usage = new Buffer
89 usage.append "Usage: nitiwiki [OPTION]...\n"
90 usage.append "A wiki engine based on markdown files and git."
91 config.tool_description = usage.write_to_string
92
93 config.parse_options(args)
94
95 if config.opt_help.value then
96 config.usage
97 exit 0
98 end
99
100 var config_filename = "config.ini"
101
102 # --init
103 if opt_init.value then
104 if config_filename.file_exists then
105 print "Already in a nitiwiki directory."
106 exit 0
107 end
108 var nitiwiki_home = "{compute_nit_dir(opt_nit_dir)}/contrib/nitiwiki"
109 var tpl = "{nitiwiki_home}/examples/default/"
110 if not tpl.file_exists then
111 print "Cannot find {tpl} files."
112 print "Maybe your NIT_DIR is not set properly?"
113 print "You can initialize nitiwiki manually by copying the default skeletton here."
114 exit 1
115 end
116 sys.system "cp -R -- {tpl.escape_to_sh}/* ."
117 print "Initialized new nitiwiki."
118 print "Set wiki settings by editing {config_filename}."
119 exit 0
120 end
121
122 # load config files
123
124 # --config
125 var config_file = opt_config.value
126 if config_file == null then
127 config_file = config_filename
128 end
129
130 if not config_file.file_exists then
131 print "Not in a nitiwiki directory."
132 print "Use --init to initialize one here."
133 exit 0
134 end
135
136 var wiki_config = new WikiConfig(config_file)
137 var wiki = new Nitiwiki(wiki_config)
138
139 # --verbose
140 wiki.verbose_level = opt_verbose.value
141
142 # --clean
143 if opt_clean.value then
144 wiki.clean
145 end
146
147 # --fetch
148 if opt_fetch.value then
149 wiki.fetch
150 end
151
152 # --render
153 if opt_render.value then
154 wiki.parse
155 # --force
156 wiki.force_render = opt_force.value
157 wiki.render
158 end
159
160 # --rsync
161 if opt_rsync.value then
162 wiki.sync
163 end
164
165 # --status
166 if opt_status.value or
167 (not opt_clean.value and
168 not opt_fetch.value and
169 not opt_render.value and
170 not opt_rsync.value) then
171 wiki.parse
172 wiki.status
173 end