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
76 # Was a codblock just before the current line ?
77 close_codeblock
(n
or else root
)
80 if text
.substring
(0,3) == "~~~" then
82 while l
< text
.length
and text
.chars
[l
] == '~' do l
+= 1
83 in_fence
= text
.substring
(0, l
)
90 # The HTML node of the last line, so we know if we continue the same block
93 # No line or loss of indentation: reset
94 if text
.is_empty
or indent
< lastindent
then
97 if text
.is_empty
then continue
100 # Special first word: new paragraph
101 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
106 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
109 n
.add_class
("contract")
114 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
115 text
= text
.substring_from
(1).trim
117 ul
= new HTMLTag("ul")
120 n
= new HTMLTag("li")
124 # Nothing? then paragraph
132 # Because spaces and `\n` where trimmed
136 process_line
(n
, text
)
138 # Special case, the fist line is the synopsys and is in its own paragraph
139 if is_first_line
then
140 n
.add_class
("synopsys")
142 is_first_line
= false
146 # If the codeblock was the last code sequence
147 close_codeblock
(n
or else root
)
152 fun short_work
(mdoc
: MDoc): HTMLTag
154 var text
= mdoc
.content
.first
155 var n
= new HTMLTag("span")
156 n
.add_class
("synopsys")
157 n
.add_class
("nitdoc")
158 process_line
(n
, text
)
162 fun process_line
(n
: HTMLTag, text
: String)
164 # Loosly detect code parts
165 var parts
= text
.split
("`")
167 # Process each code parts, thus alternate between text and code
175 var n2
= new HTMLTag("code")
177 process_code
(n2
, part
)
179 is_text
= not is_text
183 fun close_codeblock
(root
: HTMLTag)
185 # Is there a codeblock to manage?
186 if not curblock
.is_empty
then
187 # determine the smalest indent
189 for text
in curblock
do
191 while indent
< text
.length
and text
.chars
[indent
] == ' ' do indent
+= 1
192 if minindent
== -1 or indent
< minindent
then
198 var btext
= new FlatBuffer
199 for text
in curblock
do
200 btext
.append text
.substring_from
(minindent
)
205 var n
= new HTMLTag("pre")
207 process_code
(n
, btext
.to_s
)
212 fun process_code
(n
: HTMLTag, text
: String)
215 var ast
= toolcontext
.parse_something
(text
)
217 if ast
isa AError then
219 # n.attrs["title"] = ast.message
220 n
.add_class
("rawcode")
222 var v
= new HighlightVisitor
225 n
.add_class
("nitcode")
231 # Build a `<div>` element that contains the full documentation in HTML
232 fun full_markdown
: HTMLTag
234 var res
= full_markdown_cache
235 if res
!= null then return res
236 var tc
= new ToolContext
237 var d2m
= new Doc2Mdwn(tc
)
239 full_markdown_cache
= res
243 private var full_markdown_cache
: nullable HTMLTag
245 # Build a `<span>` element that contains the synopsys in HTML
246 fun short_markdown
: HTMLTag
248 var res
= short_markdown_cache
249 if res
!= null then return res
250 var tc
= new ToolContext
251 var d2m
= new Doc2Mdwn(tc
)
252 res
= d2m
.short_work
(self)
253 short_markdown_cache
= res
257 private var short_markdown_cache
: nullable HTMLTag