nitlight: enable ToolContext::keep_going to still highlight invalid source-files
[nit.git] / src / nitlight.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 # Tool that produces highlighting for Nit programs
16 module nitlight
17
18 import highlight
19
20 var toolcontext = new ToolContext
21
22 # Disable `cached` because it causes issues when printing transformed AST. FIXME
23 toolcontext.cached_phase.disabled = true
24
25 # Try to colorize, even if programs are non valid
26 toolcontext.keep_going = true
27
28 var opt_fragment = new OptionBool("Omit document header and footer", "-f", "--fragment")
29 var opt_first_line = new OptionInt("Start the source file at this line (default: 1)", 0, "--first-line")
30 var opt_last_line = new OptionInt("End the source file at this line (default: to the end)", 0, "--last-line")
31 var opt_dir = new OptionString("Output html files in a specific directory (required if more than one module)", "-d", "--dir")
32 var opt_full = new OptionBool("Process also imported modules", "--full")
33 var opt_ast = new OptionBool("Generate specific HTML elements for each Node of the AST", "--ast")
34 toolcontext.option_context.add_option(opt_fragment, opt_first_line, opt_last_line, opt_dir, opt_full)
35 toolcontext.tooldescription = "Usage: nitlight [OPTION]... <file.nit>...\nGenerates HTML of highlited code from Nit source files."
36 toolcontext.process_options(args)
37
38 var model = new Model
39 var modelbuilder = new ModelBuilder(model, toolcontext)
40
41 var args = toolcontext.option_context.rest
42
43 var mmodules = modelbuilder.parse(args)
44 modelbuilder.run_phases
45
46 if opt_full.value then mmodules = model.mmodules
47
48 var dir = opt_dir.value
49 if dir != null then
50 dir.mkdir
51 else if mmodules.length > 1 then
52 print "More than one module to render, use option -d"
53 return
54 end
55
56 for mm in mmodules do
57 if dir != null then toolcontext.info("write {dir}/{mm.name}.html", 1)
58
59 var v = new HighlightVisitor
60
61 if opt_first_line.value != 0 then v.first_line = opt_first_line.value
62 if opt_last_line.value != 0 then v.last_line = opt_last_line.value
63 if opt_ast.value then v.with_ast = true
64 var page = null
65 var m = modelbuilder.mmodule2nmodule[mm]
66 if not opt_fragment.value then
67 page = new HTMLTag("html")
68 page.add_raw_html """<head>
69 <meta charset="utf-8">
70 <title>file {{{m.location.file.filename}}}</title>"""
71 if dir == null then
72 page.add_raw_html """
73 <style type="text/css">
74 {{{v.css_content}}}
75 </style>
76 """
77 else
78 page.add_raw_html """<link rel="stylesheet" type="text/css" href="style.css" />"""
79 end
80 page.add_raw_html v.head_content
81 page.add_raw_html "</head><body><pre class='nit_code'>"
82 end
83 v.enter_visit(m)
84 if not opt_fragment.value then
85 page.add(v.html)
86 page.add_raw_html "</pre>"
87 page.add_raw_html v.foot_content
88 page.add_raw_html "</body>"
89 else
90 page = v.html
91 end
92
93 if dir != null then
94 page.write_to_file("{dir}/{mm.name}.html")
95 else
96 page.write_to(stdout)
97 end
98 end
99
100 if dir != null then
101 toolcontext.info("write {dir}/index.html", 1)
102
103 var page = new HTMLTag("html")
104 page.add_raw_html """<head>
105 <meta charset="utf-8">
106 </head><body><ul>
107 """
108 for mm in mmodules do
109 var n = new HTMLTag("li")
110 var n2 = new HTMLTag("a")
111 page.add n
112 n.add n2
113 n2.attr("href", "{mm.name}.html")
114 n2.text(mm.name)
115 end
116 page.add_raw_html "</li></body>"
117 page.write_to_file("{dir}/index.html")
118
119 var v = new HighlightVisitor
120 toolcontext.info("write {dir}/style.css", 1)
121 var f = new OFStream.open("{dir}/style.css")
122 f.write v.css_content
123 f.close
124 end