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