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
20 private import highlight
21 private import parser_util
23 # The class that does the convertion from a `ADoc` to HTML
24 private class Doc2Mdwn
25 var toolcontext
: ToolContext
27 # The lines of the current code block, empty is no current code block
28 var curblock
= new Array[String]
30 fun work
(mdoc
: MDoc): HTMLTag
32 var root
= new HTMLTag("div")
33 root
.add_class
("nitdoc")
35 # Indent level of the previous line
38 # Indent level of the current line
41 # Expected fencing closing tag (if any)
42 var in_fence
: nullable String = null
44 # The current element (p, li, etc.) if any
45 var n
: nullable HTMLTag = null
47 # The current ul element (if any)
48 var ul
: nullable HTMLTag = null
50 var is_first_line
= true
51 # Local variable to benefit adaptive typing
52 for text
in mdoc
.content
do
53 # Count the number of spaces
56 while text
.length
> indent
and text
.chars
[indent
] == ' ' do indent
+= 1
59 if in_fence
!= null then
61 if text
.substring
(0,in_fence
.length
) == in_fence
then
62 close_codeblock
(n
or else root
)
71 # Is codeblock? Then just collect them
73 # to allows 4 spaces including the one that follows the #
78 # Was a codblock just before the current line ?
79 close_codeblock
(n
or else root
)
82 if text
.substring
(0,3) == "~~~" then
84 while l
< text
.length
and text
.chars
[l
] == '~' do l
+= 1
85 in_fence
= text
.substring
(0, l
)
92 # The HTML node of the last line, so we know if we continue the same block
95 # No line or loss of indentation: reset
96 if text
.is_empty
or indent
< lastindent
then
99 if text
.is_empty
then continue
102 # Special first word: new paragraph
103 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
108 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
111 n
.add_class
("contract")
116 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
117 text
= text
.substring_from
(1).trim
119 ul
= new HTMLTag("ul")
122 n
= new HTMLTag("li")
126 # Nothing? then paragraph
134 # Because spaces and `\n` where trimmed
138 process_line
(n
, text
)
140 # Special case, the fist line is the synopsys and is in its own paragraph
141 if is_first_line
then
142 n
.add_class
("synopsys")
144 is_first_line
= false
148 # If the codeblock was the last code sequence
149 close_codeblock
(n
or else root
)
154 fun short_work
(mdoc
: MDoc): HTMLTag
156 var text
= mdoc
.content
.first
157 var n
= new HTMLTag("span")
158 n
.add_class
("synopsys")
159 n
.add_class
("nitdoc")
160 process_line
(n
, text
)
164 fun process_line
(n
: HTMLTag, text
: String)
166 # Loosly detect code parts
167 var parts
= text
.split
("`")
169 # Process each code parts, thus alternate between text and code
177 var n2
= new HTMLTag("code")
179 process_code
(n2
, part
)
181 is_text
= not is_text
185 fun close_codeblock
(root
: HTMLTag)
187 # Is there a codeblock to manage?
188 if not curblock
.is_empty
then
189 # determine the smalest indent
191 for text
in curblock
do
193 while indent
< text
.length
and text
.chars
[indent
] == ' ' do indent
+= 1
194 if minindent
== -1 or indent
< minindent
then
200 var btext
= new FlatBuffer
201 for text
in curblock
do
202 btext
.append text
.substring_from
(minindent
)
207 var n
= new HTMLTag("pre")
209 process_code
(n
, btext
.to_s
)
214 fun process_code
(n
: HTMLTag, text
: String)
217 var ast
= toolcontext
.parse_something
(text
)
219 if ast
isa AError then
221 # n.attrs["title"] = ast.message
222 n
.add_class
("rawcode")
224 var v
= new HighlightVisitor
227 n
.add_class
("nitcode")
233 # Build a `<div>` element that contains the full documentation in HTML
234 fun full_markdown
: HTMLTag
236 var res
= full_markdown_cache
237 if res
!= null then return res
238 var tc
= new ToolContext
239 var d2m
= new Doc2Mdwn(tc
)
241 full_markdown_cache
= res
245 private var full_markdown_cache
: nullable HTMLTag
247 # Build a `<span>` element that contains the synopsys in HTML
248 fun short_markdown
: HTMLTag
250 var res
= short_markdown_cache
251 if res
!= null then return res
252 var tc
= new ToolContext
253 var d2m
= new Doc2Mdwn(tc
)
254 res
= d2m
.short_work
(self)
255 short_markdown_cache
= res
259 private var short_markdown_cache
: nullable HTMLTag