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