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
21 super HighlightVisitor
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 toolcontext
.option_context
.add_option
(opt_fragment
, opt_line_id_prefix
, opt_first_line
, opt_last_line
, opt_dir
, opt_full
)
98 toolcontext
.tooldescription
= "Usage: nitlight [OPTION]... <file.nit>...\nGenerates HTML of highlited code from Nit source files."
99 toolcontext
.process_options
(args
)
101 var model
= new Model
102 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
104 var args
= toolcontext
.option_context
.rest
106 var mmodules
= modelbuilder
.parse_full
(args
)
107 modelbuilder
.run_phases
109 if opt_full
.value
then mmodules
= modelbuilder
.parsed_modules
111 var dir
= opt_dir
.value
114 else if mmodules
.length
> 1 then
115 print
"More than one module to render, use option -d"
119 for mm
in mmodules
do
120 if dir
!= null then toolcontext
.info
("write {dir}/{mm.c_name}.html", 1)
122 var v
= new NitlightVisitor(mm
, mmodules
)
123 var prefix
= opt_line_id_prefix
.value
124 if prefix
!= null then
125 v
.line_id_prefix
= prefix
.trim
127 v
.include_loose_tokens
= true
128 v
.include_whole_lines
= true
130 if opt_first_line
.value
!= 0 then v
.first_line
= opt_first_line
.value
131 if opt_last_line
.value
!= 0 then v
.last_line
= opt_last_line
.value
132 if opt_ast
.value
then v
.with_ast
= true
134 var m
= modelbuilder
.mmodule2node
(mm
)
136 if not opt_fragment
.value
then
137 page
= new HTMLTag("html")
138 page
.add_raw_html
"""<head>
139 <meta charset="utf-8">
140 <title>file {{{m.location.file.filename}}}</title>"""
142 page
.add_raw_html
"""
143 <style type="text/css">
148 page
.add_raw_html
"""<link rel="stylesheet" type="text/css" href="style.css" />"""
150 page
.add_raw_html v
.head_content
151 page
.add_raw_html
"</head><body><pre class='nit_code'>"
154 if not opt_fragment
.value
then
156 page
.add_raw_html
"</pre>"
157 page
.add_raw_html v
.foot_content
158 page
.add_raw_html
"</body>"
164 page
.write_to_file
("{dir}/{mm.c_name}.html")
166 page
.write_to
(stdout
)
171 toolcontext
.info
("write {dir}/index.html", 1)
173 var page
= new HTMLTag("html")
174 page
.add_raw_html
"""<head>
175 <meta charset="utf-8">
178 for mm
in mmodules
do
179 var n
= new HTMLTag("li")
180 var n2
= new HTMLTag("a")
183 n2
.attr
("href", "{mm.c_name}.html")
184 n2
.text
(mm
.full_name
)
186 page
.add_raw_html
"</li></body>"
187 page
.write_to_file
("{dir}/index.html")
189 var v
= new HighlightVisitor
190 toolcontext
.info
("write {dir}/style.css", 1)
191 var f
= new FileWriter.open
("{dir}/style.css")
192 f
.write v
.css_content