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 # Expected fencing closing tag (if any)
41 var in_fence
: nullable String = null
43 # The current element (p, li, etc.) if any
44 var n
: nullable HTMLTag = null
46 # The current ul element (if any)
47 var ul
: nullable HTMLTag = null
49 var is_first_line
= true
50 # Local variable to benefit adaptive typing
51 for text
in mdoc
.content
do
52 # Count the number of spaces
55 while text
.length
> indent
and text
.chars
[indent
] == ' ' do indent
+= 1
58 if in_fence
!= null then
60 if text
.substring
(0,in_fence
.length
) == in_fence
then
61 close_codeblock
(n
or else root
)
70 # Is codeblock? Then just collect them
72 # to allows 4 spaces including the one that follows the #
77 # Was a codblock just before the current line ?
78 close_codeblock
(n
or else root
)
81 if text
.substring
(0,3) == "~~~" then
83 while l
< text
.length
and text
.chars
[l
] == '~' do l
+= 1
84 in_fence
= text
.substring
(0, l
)
91 # The HTML node of the last line, so we know if we continue the same block
94 # No line or loss of indentation: reset
95 if text
.is_empty
or indent
< lastindent
then
98 if text
.is_empty
then continue
101 # Special first word: new paragraph
102 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
107 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
110 n
.add_class
("contract")
115 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
116 text
= text
.substring_from
(1).trim
118 ul
= new HTMLTag("ul")
121 n
= new HTMLTag("li")
125 # Nothing? then paragraph
133 # Because spaces and `\n` where trimmed
137 process_line
(n
, text
)
139 # Special case, the fist line is the synopsys and is in its own paragraph
140 if is_first_line
then
141 n
.add_class
("synopsys")
143 is_first_line
= false
147 # If the codeblock was the last code sequence
148 close_codeblock
(n
or else root
)
153 fun short_work
(mdoc
: MDoc): HTMLTag
155 var text
= mdoc
.content
.first
156 var n
= new HTMLTag("span")
157 n
.add_class
("synopsys")
158 n
.add_class
("nitdoc")
159 process_line
(n
, text
)
163 fun process_line
(n
: HTMLTag, text
: String)
165 # Loosly detect code parts
166 var parts
= text
.split
("`")
168 # Process each code parts, thus alternate between text and code
176 var n2
= new HTMLTag("code")
178 process_code
(n2
, part
)
180 is_text
= not is_text
184 fun close_codeblock
(root
: HTMLTag)
186 # Is there a codeblock to manage?
187 if not curblock
.is_empty
then
188 # determine the smalest indent
190 for text
in curblock
do
192 while indent
< text
.length
and text
.chars
[indent
] == ' ' do indent
+= 1
193 if minindent
== -1 or indent
< minindent
then
199 var btext
= new FlatBuffer
200 for text
in curblock
do
201 btext
.append text
.substring_from
(minindent
)
206 var n
= new HTMLTag("pre")
208 process_code
(n
, btext
.to_s
)
213 fun process_code
(n
: HTMLTag, text
: String)
216 var ast
= toolcontext
.parse_something
(text
)
218 if ast
isa AError then
220 # n.attrs["title"] = ast.message
221 n
.add_class
("rawcode")
223 var v
= new HighlightVisitor
226 n
.add_class
("nitcode")
232 # Build a `<div>` element that contains the full documentation in HTML
233 fun full_markdown
: HTMLTag
235 var res
= full_markdown_cache
236 if res
!= null then return res
237 var tc
= new ToolContext
238 var d2m
= new Doc2Mdwn(tc
)
240 full_markdown_cache
= res
244 private var full_markdown_cache
: nullable HTMLTag
246 # Build a `<span>` element that contains the synopsys in HTML
247 fun short_markdown
: HTMLTag
249 var res
= short_markdown_cache
250 if res
!= null then return res
251 var tc
= new ToolContext
252 var d2m
= new Doc2Mdwn(tc
)
253 res
= d2m
.short_work
(self)
254 short_markdown_cache
= res
258 private var short_markdown_cache
: nullable HTMLTag