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
(mdoc
: MDoc): 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 var is_first_line
= true
47 # Local variable to benefit adaptive typing
48 for text
in mdoc
.content
do
49 # Count the number of spaces
52 while text
.length
> indent
and text
.chars
[indent
] == ' ' do indent
+= 1
54 # Is codeblock? Then just collect them
56 var part
= text
.substring_from
(4)
62 # Was a codblock just before the current line ?
63 close_codeblock
(n
or else root
)
68 # The HTML node of the last line, so we know if we continue the same block
71 # No line or loss of indentation: reset
72 if text
.is_empty
or indent
< lastindent
then
75 if text
.is_empty
then continue
78 # Special first word: new paragraph
79 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
84 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
87 n
.add_class
("contract")
92 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
93 text
= text
.substring_from
(1).trim
95 ul
= new HTMLTag("ul")
102 # Nothing? then paragraph
110 # Because spaces and `\n` where trimmed
114 process_line
(n
, text
)
116 # Special case, the fist line is the synopsys and is in its own paragraph
117 if is_first_line
then
118 n
.add_class
("synopsys")
120 is_first_line
= false
124 # If the codeblock was the last code sequence
125 close_codeblock
(n
or else root
)
130 fun short_work
(mdoc
: MDoc): HTMLTag
132 var text
= mdoc
.content
.first
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 res
= full_markdown_cache
196 if res
!= null then return res
197 var tc
= new ToolContext
198 var d2m
= new Doc2Mdwn(tc
)
200 full_markdown_cache
= res
204 private var full_markdown_cache
: nullable HTMLTag
206 # Build a `<span>` element that contains the synopsys in HTML
207 fun short_markdown
: HTMLTag
209 var res
= short_markdown_cache
210 if res
!= null then return res
211 var tc
= new ToolContext
212 var d2m
= new Doc2Mdwn(tc
)
213 res
= d2m
.short_work
(self)
214 short_markdown_cache
= res
218 private var short_markdown_cache
: nullable HTMLTag