update NOTICE
[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_line_id_prefix = new OptionString("Prefix of the id of each line <span> element", "--line-id-prefix")
30 var opt_first_line = new OptionInt("Start the source file at this line (default: 1)", 0, "--first-line")
31 var opt_last_line = new OptionInt("End the source file at this line (default: to the end)", 0, "--last-line")
32 var opt_dir = new OptionString("Output html files in a specific directory (required if more than one module)", "-d", "--dir")
33 var opt_full = new OptionBool("Process also imported modules", "--full")
34 var opt_ast = new OptionBool("Generate specific HTML elements for each Node of the AST", "--ast")
35 toolcontext.option_context.add_option(opt_fragment, opt_line_id_prefix, opt_first_line, opt_last_line, opt_dir, opt_full)
36 toolcontext.tooldescription = "Usage: nitlight [OPTION]... <file.nit>...\nGenerates HTML of highlited code from Nit source files."
37 toolcontext.process_options(args)
38
39 var model = new Model
40 var modelbuilder = new ModelBuilder(model, toolcontext)
41
42 var args = toolcontext.option_context.rest
43
44 var mmodules = modelbuilder.parse_full(args)
45 modelbuilder.run_phases
46
47 if opt_full.value then mmodules = model.mmodules
48
49 var dir = opt_dir.value
50 if dir != null then
51 dir.mkdir
52 else if mmodules.length > 1 then
53 print "More than one module to render, use option -d"
54 return
55 end
56
57 for mm in mmodules do
58 if dir != null then toolcontext.info("write {dir}/{mm.name}.html", 1)
59
60 var v = new HighlightVisitor
61 var prefix = opt_line_id_prefix.value
62 if prefix != null then
63 v.line_id_prefix = prefix.trim
64 end
65
66 if opt_first_line.value != 0 then v.first_line = opt_first_line.value
67 if opt_last_line.value != 0 then v.last_line = opt_last_line.value
68 if opt_ast.value then v.with_ast = true
69 var page = null
70 var m = modelbuilder.mmodule2node(mm)
71 assert m != null
72 if not opt_fragment.value then
73 page = new HTMLTag("html")
74 page.add_raw_html """<head>
75 <meta charset="utf-8">
76 <title>file {{{m.location.file.filename}}}</title>"""
77 if dir == null then
78 page.add_raw_html """
79 <style type="text/css">
80 {{{v.css_content}}}
81 </style>
82 """
83 else
84 page.add_raw_html """<link rel="stylesheet" type="text/css" href="style.css" />"""
85 end
86 page.add_raw_html v.head_content
87 page.add_raw_html "</head><body><pre class='nit_code'>"
88 end
89 v.enter_visit(m)
90 if not opt_fragment.value then
91 page.add(v.html)
92 page.add_raw_html "</pre>"
93 page.add_raw_html v.foot_content
94 page.add_raw_html "</body>"
95 else
96 page = v.html
97 end
98
99 if dir != null then
100 page.write_to_file("{dir}/{mm.name}.html")
101 else
102 page.write_to(stdout)
103 end
104 end
105
106 if dir != null then
107 toolcontext.info("write {dir}/index.html", 1)
108
109 var page = new HTMLTag("html")
110 page.add_raw_html """<head>
111 <meta charset="utf-8">
112 </head><body><ul>
113 """
114 for mm in mmodules do
115 var n = new HTMLTag("li")
116 var n2 = new HTMLTag("a")
117 page.add n
118 n.add n2
119 n2.attr("href", "{mm.name}.html")
120 n2.text(mm.name)
121 end
122 page.add_raw_html "</li></body>"
123 page.write_to_file("{dir}/index.html")
124
125 var v = new HighlightVisitor
126 toolcontext.info("write {dir}/style.css", 1)
127 var f = new OFStream.open("{dir}/style.css")
128 f.write v.css_content
129 f.close
130 end