nitlight: add --txt for text-based highlighting
[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 htmlight
19
20 class NitlightVisitor
21 super HtmlightVisitor
22
23 # The current highlight module
24 #
25 # It is used to know when to use anchored local links
26 var current_module: MModule
27
28 # List of all highlighted modules
29 #
30 # It is used to have links that only targets highlighted entities
31 #
32 # Entities outside these modules will not be linked.
33 var mmodules: Collection[MModule]
34
35 redef fun hrefto(entity) do return entity.href(self)
36 end
37
38 redef class MEntity
39 private fun href(v: NitlightVisitor): nullable String do return null
40 end
41
42 redef class MModule
43 redef fun href(v)
44 do
45 if self == v.current_module then return ""
46 if not v.mmodules.has(self) then return null
47 return c_name + ".html"
48 end
49 end
50
51 redef class MClass
52 redef fun href(v)
53 do
54 # Because we only have code, just link to the introduction
55 return intro.href(v)
56 end
57 end
58
59 redef class MClassDef
60 redef fun href(v)
61 do
62 var m = mmodule.href(v)
63 if m == null then return null
64 return m + "#" + to_s
65 end
66 end
67
68 redef class MProperty
69 redef fun href(v)
70 do
71 # Because we only have code, just link to the introduction
72 return intro.href(v)
73 end
74 end
75
76 redef class MPropDef
77 redef fun href(v)
78 do
79 var m = mclassdef.mmodule.href(v)
80 if m == null then return null
81 return m + "#" + to_s
82 end
83 end
84
85 var toolcontext = new ToolContext
86
87 # Try to colorize, even if programs are non valid
88 toolcontext.keep_going = true
89
90 var opt_fragment = new OptionBool("Omit document header and footer", "-f", "--fragment")
91 var opt_line_id_prefix = new OptionString("Prefix of the id of each line `<span>` element", "--line-id-prefix")
92 var opt_first_line = new OptionInt("Start the source file at this line (default: 1)", 0, "--first-line")
93 var opt_last_line = new OptionInt("End the source file at this line (default: to the end)", 0, "--last-line")
94 var opt_dir = new OptionString("Output html files in a specific directory (required if more than one module)", "-d", "--dir")
95 var opt_full = new OptionBool("Process also imported modules", "--full")
96 var opt_ast = new OptionBool("Generate specific HTML elements for each Node of the AST", "--ast")
97 var opt_txt = new OptionBool("Generate text with ANSI coloring escape sequences", "--txt")
98 toolcontext.option_context.add_option(opt_fragment, opt_line_id_prefix, opt_first_line, opt_last_line, opt_dir, opt_full, opt_ast, opt_txt)
99 toolcontext.tooldescription = "Usage: nitlight [OPTION]... <file.nit>...\nGenerates HTML of highlited code from Nit source files."
100 toolcontext.process_options(args)
101
102 var model = new Model
103 var modelbuilder = new ModelBuilder(model, toolcontext)
104
105 var args = toolcontext.option_context.rest
106
107 var mmodules = modelbuilder.parse_full(args)
108 modelbuilder.run_phases
109
110 if opt_full.value then mmodules = modelbuilder.parsed_modules
111
112 var dir = opt_dir.value
113 if dir != null then
114 dir.mkdir
115 else if mmodules.length > 1 then
116 print "More than one module to render, use option -d"
117 return
118 end
119
120 if opt_txt.value then
121 for mm in mmodules do
122 var v = new AnsiHighlightVisitor
123 v.include_loose_tokens = true
124 v.include_whole_lines = true
125
126 if opt_first_line.value != 0 then v.first_line = opt_first_line.value
127 if opt_last_line.value != 0 then v.last_line = opt_last_line.value
128 var m = modelbuilder.mmodule2node(mm)
129 assert m != null
130
131 v.highlight_node(m)
132 var page = v.result
133
134 if dir != null then
135 page.write_to_file("{dir}/{mm.c_name}.txt")
136 else
137 page.write_to(stdout)
138 end
139 end
140 return
141 end
142
143 for mm in mmodules do
144 if dir != null then toolcontext.info("write {dir}/{mm.c_name}.html", 1)
145
146 var v = new NitlightVisitor(mm, mmodules)
147 var prefix = opt_line_id_prefix.value
148 if prefix != null then
149 v.line_id_prefix = prefix.trim
150 end
151 v.include_loose_tokens = true
152 v.include_whole_lines = true
153
154 if opt_first_line.value != 0 then v.first_line = opt_first_line.value
155 if opt_last_line.value != 0 then v.last_line = opt_last_line.value
156 if opt_ast.value then v.with_ast = true
157 var page = null
158 var m = modelbuilder.mmodule2node(mm)
159 assert m != null
160 if not opt_fragment.value then
161 page = new HTMLTag("html")
162 page.add_raw_html """<head>
163 <meta charset="utf-8">
164 <title>file {{{m.location.file.filename}}}</title>"""
165 if dir == null then
166 page.add_raw_html """
167 <style type="text/css">
168 {{{v.css_content}}}
169 </style>
170 """
171 else
172 page.add_raw_html """<link rel="stylesheet" type="text/css" href="style.css" />"""
173 end
174 page.add_raw_html v.head_content
175 page.add_raw_html "</head><body><pre class='nit_code'>"
176 end
177 v.highlight_node(m)
178 if not opt_fragment.value then
179 page.add(v.html)
180 page.add_raw_html "</pre>"
181 page.add_raw_html v.foot_content
182 page.add_raw_html "</body>"
183 else
184 page = v.html
185 end
186
187 if dir != null then
188 page.write_to_file("{dir}/{mm.c_name}.html")
189 else
190 page.write_to(stdout)
191 end
192 end
193
194 if dir != null then
195 toolcontext.info("write {dir}/index.html", 1)
196
197 var page = new HTMLTag("html")
198 page.add_raw_html """<head>
199 <meta charset="utf-8">
200 </head><body><ul>
201 """
202 for mm in mmodules do
203 var n = new HTMLTag("li")
204 var n2 = new HTMLTag("a")
205 page.add n
206 n.add n2
207 n2.attr("href", "{mm.c_name}.html")
208 n2.text(mm.full_name)
209 end
210 page.add_raw_html "</li></body>"
211 page.write_to_file("{dir}/index.html")
212
213 var v = new HtmlightVisitor
214 toolcontext.info("write {dir}/style.css", 1)
215 var f = new FileWriter.open("{dir}/style.css")
216 f.write v.css_content
217 f.close
218 end