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 # Transform Nit verbatim documentation into HTML
22 # The class that does the convertion from a `ADoc` to HTML
23 private class Doc2Mdwn
24 var toolcontext
: ToolContext
26 # The lines of the current code block, empty is no current code block
27 var curblock
= new Array[String]
29 fun work
(ndoc
: ADoc): HTMLTag
31 var root
= new HTMLTag("div")
32 root
.add_class
("nitdoc")
34 # Indent level of the previous line
37 # Indent level of the current line
40 # The current element (p, li, etc.) if any
41 var n
: nullable HTMLTag = null
43 # The current ul element (if any)
44 var ul
: nullable HTMLTag = null
46 # Local variable to benefit adaptive typing
47 for c
in ndoc
.n_comment
do
48 # Remove the starting `#`
49 var text
= c
.text
.substring_from
(1)
51 # Count the number of spaces
54 while text
.length
> indent
and text
.chars
[indent
] == ' ' do indent
+= 1
56 # Is codeblock? Then just collect them
58 var part
= text
.substring_from
(4)
63 # Was a codblock just before the current line ?
64 close_codeblock
(n
or else root
)
69 # The HTML node of the last line, so we know if we continue the same block
72 # No line or loss of indentation: reset
73 if text
.is_empty
or indent
< lastindent
then
76 if text
.is_empty
then continue
79 # Special first word: new paragraph
80 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
85 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
88 n
.add_class
("contract")
93 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
94 text
= text
.substring_from
(1).trim
96 ul
= new HTMLTag("ul")
103 # Nothing? then paragraph
111 # Because spaces and `\n` where trimmed
115 process_line
(n
, text
)
117 # Special case, the fist line is the synopsys and is in its own paragraph
118 if c
== ndoc
.n_comment
.first
then
119 n
.add_class
("synopsys")
124 # If the codeblock was the last code sequence
125 close_codeblock
(n
or else root
)
130 fun short_work
(ndoc
: ADoc): HTMLTag
132 var text
= ndoc
.n_comment
.first
.text
.substring_from
(1)
133 var n
= new HTMLTag("span")
134 n
.add_class
("synopsys")
135 n
.add_class
("nitdoc")
136 process_line
(n
, text
)
140 fun process_line
(n
: HTMLTag, text
: String)
142 # Loosly detect code parts
143 var parts
= text
.split
("`")
145 # Process each code parts, thus alternate between text and code
153 var n2
= new HTMLTag("code")
155 process_code
(n2
, part
)
157 is_text
= not is_text
161 fun close_codeblock
(root
: HTMLTag)
163 # Is there a codeblock to manage?
164 if not curblock
.is_empty
then
165 var n
= new HTMLTag("pre")
167 var btext
= curblock
.to_s
168 process_code
(n
, btext
)
173 fun process_code
(n
: HTMLTag, text
: String)
176 var ast
= toolcontext
.parse_something
(text
)
178 if ast
isa AError then
180 # n.attrs["title"] = ast.message
181 n
.add_class
("rawcode")
183 var v
= new HighlightVisitor
186 n
.add_class
("nitcode")
192 # Build a `<div>` element that contains the full documentation in HTML
193 fun full_markdown
: HTMLTag
195 var tc
= new ToolContext
196 var d2m
= new Doc2Mdwn(tc
)
197 return d2m
.work
(self)
200 # Build a `<span>` element that contains the synopsys in HTML
201 fun short_markdown
: HTMLTag
203 var tc
= new ToolContext
204 var d2m
= new Doc2Mdwn(tc
)
205 return d2m
.short_work
(self)