1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Tool that produces highlighting for Nit programs
23 # The current highlight module
25 # It is used to know when to use anchored local links
26 var current_module
: MModule
28 # List of all highlighted modules
30 # It is used to have links that only targets highlighted entities
32 # Entities outside these modules will not be linked.
33 var mmodules
: Collection[MModule]
35 redef fun hrefto
(entity
) do return entity
.href
(self)
39 private fun href
(v
: NitlightVisitor): nullable String do return null
45 if self == v
.current_module
then return ""
46 if not v
.mmodules
.has
(self) then return null
47 return c_name
+ ".html"
54 # Because we only have code, just link to the introduction
62 var m
= mmodule
.href
(v
)
63 if m
== null then return null
71 # Because we only have code, just link to the introduction
79 var m
= mclassdef
.mmodule
.href
(v
)
80 if m
== null then return null
85 var toolcontext
= new ToolContext
87 # Try to colorize, even if programs are non valid
88 toolcontext
.keep_going
= true
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
)
102 var model
= new Model
103 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
105 var args
= toolcontext
.option_context
.rest
107 var mmodules
= modelbuilder
.parse_full
(args
)
108 modelbuilder
.run_phases
110 if opt_full
.value
then mmodules
= modelbuilder
.parsed_modules
112 var dir
= opt_dir
.value
115 else if mmodules
.length
> 1 then
116 print
"More than one module to render, use option -d"
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
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
)
135 page
.write_to_file
("{dir}/{mm.c_name}.txt")
137 page
.write_to
(stdout
)
143 for mm
in mmodules
do
144 if dir
!= null then toolcontext
.info
("write {dir}/{mm.c_name}.html", 1)
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
151 v
.include_loose_tokens
= true
152 v
.include_whole_lines
= true
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
158 var m
= modelbuilder
.mmodule2node
(mm
)
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>"""
166 page
.add_raw_html
"""
167 <style type="text/css">
172 page
.add_raw_html
"""<link rel="stylesheet" type="text/css" href="style.css" />"""
174 page
.add_raw_html v
.head_content
175 page
.add_raw_html
"</head><body><pre class='nit_code'>"
178 if not opt_fragment
.value
then
180 page
.add_raw_html
"</pre>"
181 page
.add_raw_html v
.foot_content
182 page
.add_raw_html
"</body>"
188 page
.write_to_file
("{dir}/{mm.c_name}.html")
190 page
.write_to
(stdout
)
195 toolcontext
.info
("write {dir}/index.html", 1)
197 var page
= new HTMLTag("html")
198 page
.add_raw_html
"""<head>
199 <meta charset="utf-8">
202 for mm
in mmodules
do
203 var n
= new HTMLTag("li")
204 var n2
= new HTMLTag("a")
207 n2
.attr
("href", "{mm.c_name}.html")
208 n2
.text
(mm
.full_name
)
210 page
.add_raw_html
"</li></body>"
211 page
.write_to_file
("{dir}/index.html")
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