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
)
71 # Is codeblock? Then just collect them
73 var part
= text
.substring_from
(4)
79 # Was a codblock just before the current line ?
80 close_codeblock
(n
or else root
)
83 if text
.substring
(0,3) == "~~~" then
85 while l
< text
.length
and text
.chars
[l
] == '~' do l
+= 1
86 in_fence
= text
.substring
(0, l
)
93 # The HTML node of the last line, so we know if we continue the same block
96 # No line or loss of indentation: reset
97 if text
.is_empty
or indent
< lastindent
then
100 if text
.is_empty
then continue
103 # Special first word: new paragraph
104 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
109 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
112 n
.add_class
("contract")
117 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
118 text
= text
.substring_from
(1).trim
120 ul
= new HTMLTag("ul")
123 n
= new HTMLTag("li")
127 # Nothing? then paragraph
135 # Because spaces and `\n` where trimmed
139 process_line
(n
, text
)
141 # Special case, the fist line is the synopsys and is in its own paragraph
142 if is_first_line
then
143 n
.add_class
("synopsys")
145 is_first_line
= false
149 # If the codeblock was the last code sequence
150 close_codeblock
(n
or else root
)
155 fun short_work
(mdoc
: MDoc): HTMLTag
157 var text
= mdoc
.content
.first
158 var n
= new HTMLTag("span")
159 n
.add_class
("synopsys")
160 n
.add_class
("nitdoc")
161 process_line
(n
, text
)
165 fun process_line
(n
: HTMLTag, text
: String)
167 # Loosly detect code parts
168 var parts
= text
.split
("`")
170 # Process each code parts, thus alternate between text and code
178 var n2
= new HTMLTag("code")
180 process_code
(n2
, part
)
182 is_text
= not is_text
186 fun close_codeblock
(root
: HTMLTag)
188 # Is there a codeblock to manage?
189 if not curblock
.is_empty
then
190 var n
= new HTMLTag("pre")
192 var btext
= curblock
.to_s
193 process_code
(n
, btext
)
198 fun process_code
(n
: HTMLTag, text
: String)
201 var ast
= toolcontext
.parse_something
(text
)
203 if ast
isa AError then
205 # n.attrs["title"] = ast.message
206 n
.add_class
("rawcode")
208 var v
= new HighlightVisitor
211 n
.add_class
("nitcode")
217 # Build a `<div>` element that contains the full documentation in HTML
218 fun full_markdown
: HTMLTag
220 var res
= full_markdown_cache
221 if res
!= null then return res
222 var tc
= new ToolContext
223 var d2m
= new Doc2Mdwn(tc
)
225 full_markdown_cache
= res
229 private var full_markdown_cache
: nullable HTMLTag
231 # Build a `<span>` element that contains the synopsys in HTML
232 fun short_markdown
: HTMLTag
234 var res
= short_markdown_cache
235 if res
!= null then return res
236 var tc
= new ToolContext
237 var d2m
= new Doc2Mdwn(tc
)
238 res
= d2m
.short_work
(self)
239 short_markdown_cache
= res
243 private var short_markdown_cache
: nullable HTMLTag