Merge branch 'doc_on_collection'
[nit.git] / src / test_markdown.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 # Program to test the `markdown` module on real source files.
16 module test_markdown
17
18 import modelize_property
19 import markdown
20
21 redef class ModelBuilder
22 fun test_markdown(page: HTMLTag, mmodule: MModule)
23 do
24 page.add_raw_html "<a id='{mmodule.full_name}'></a>"
25 page.add_raw_html "<h1>module {mmodule}</h1>"
26 if mmodule2nmodule.has_key(mmodule) then
27 do
28 var mdoc = mmodule.mdoc
29 if mdoc == null then break label x
30 page.add mdoc.full_markdown
31 end label x
32 for mclassdef in mmodule.mclassdefs do
33 do
34 var mdoc = mclassdef.mdoc
35 if mdoc != null then
36 if mclassdef.mclass.intro == mclassdef then page.add_raw_html "<a id='{mclassdef.mclass.full_name}'></a>"
37 page.add_raw_html "<h2>class {mclassdef}</h2>"
38 page.add mdoc.full_markdown
39 end
40 end
41 for mpropdef in mclassdef.mpropdefs do
42 var mdoc = mpropdef.mdoc
43 if mdoc != null then
44 if mpropdef.mproperty.intro == mpropdef then page.add_raw_html "<a id='{mpropdef.mproperty.full_name}'></a>"
45
46 page.add_raw_html "<h3>prop {mpropdef}</h3>"
47 page.add mdoc.full_markdown
48 end
49 end
50 end
51 end
52 end
53 end
54
55 var toolcontext = new ToolContext
56
57 var opt_full = new OptionBool("Process also imported modules", "--full")
58 toolcontext.option_context.add_option(opt_full)
59
60 toolcontext.process_options
61 var args = toolcontext.option_context.rest
62 if args.is_empty then
63 print "usage: test_markdown [options] file.nit..."
64 toolcontext.option_context.usage
65 return
66 end
67
68 var model = new Model
69 var modelbuilder = new ModelBuilder(model, toolcontext)
70
71 var mmodules = modelbuilder.parse(args)
72 modelbuilder.run_phases
73
74 var hv = new HighlightVisitor
75
76 var page = new HTMLTag("html")
77 page.add_raw_html """
78 <head>
79 <meta charset="utf-8">
80 <style type="text/css">
81 code {margin: 0 2px;
82 padding: 0px 5px;
83 border: 1px solid #ddd;
84 background-color: #f8f8f8;
85 border-radius: 3px;}
86 pre {
87 background-color: #f8f8f8;
88 border: 1px solid #ddd;
89 font-size: 13px;
90 line-height: 19px;
91 overflow: auto;
92 padding: 6px 6px;
93 border-radius: 3px;
94 }
95 .rawcode[title] {
96 border-color: red;
97 }
98 {{{hv.css_content}}}
99 </style>
100 </head><body>
101 """
102
103 if opt_full.value then
104 for m in model.mmodules do
105 modelbuilder.test_markdown(page, m)
106 end
107 else
108 for m in mmodules do
109 modelbuilder.test_markdown(page, m)
110 end
111 end
112
113 page.add_raw_html "</body>"
114 page.write_to(stdout)