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