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
21 # The class that does the convertion from a `ADoc` to HTML
22 private class Doc2Mdwn
23 # The lines of the current code block, empty is no current code block
24 var curblock
= new Array[String]
26 fun work
(ndoc
: ADoc): HTMLTag
28 var root
= new HTMLTag("div")
29 root
.add_class
("nitdoc")
31 # Indent level of the previous line
34 # Indent level of the current line
37 # The current element (p, li, etc.) if any
38 var n
: nullable HTMLTag = null
40 # The current ul element (if any)
41 var ul
: nullable HTMLTag = null
43 # Local variable to benefit adaptive typing
44 for c
in ndoc
.n_comment
do
45 # Remove the starting `#`
46 var text
= c
.text
.substring_from
(1)
48 # Count the number of spaces
51 while text
.length
> indent
and text
[indent
] == ' ' do indent
+= 1
53 # Is codeblock? Then just collect them
55 var part
= text
.substring_from
(4)
60 # Was a codblock just before the current line ?
61 close_codeblock
(n
or else root
)
66 # The HTML node of the last line, so we know if we continue the same block
69 # No line or loss of indentation: reset
70 if text
.is_empty
or indent
< lastindent
then
73 if text
.is_empty
then continue
76 # Special first word: new paragraph
77 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
82 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
85 n
.add_class
("contract")
90 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
91 text
= text
.substring_from
(1).trim
93 ul
= new HTMLTag("ul")
100 # Nothing? then paragraph
108 # Because spaces and `\n` where trimmed
112 process_line
(n
, text
)
114 # Special case, the fist line is the synopsys and is in its own paragraph
115 if c
== ndoc
.n_comment
.first
then
116 n
.add_class
("synopsys")
121 # If the codeblock was the last code sequence
122 close_codeblock
(n
or else root
)
127 fun short_work
(ndoc
: ADoc): HTMLTag
129 var text
= ndoc
.n_comment
.first
.text
.substring_from
(1)
130 var n
= new HTMLTag("span")
131 n
.add_class
("synopsys")
132 n
.add_class
("nitdoc")
133 process_line
(n
, text
)
137 fun process_line
(n
: HTMLTag, text
: String)
139 # Loosly detect code parts
140 var parts
= text
.split
("`")
142 # Process each code parts, thus alternate between text and code
150 var n2
= new HTMLTag("code")
152 process_code
(n2
, part
)
154 is_text
= not is_text
158 fun close_codeblock
(root
: HTMLTag)
160 # Is there a codeblock to manage?
161 if not curblock
.is_empty
then
162 var n
= new HTMLTag("pre")
164 var btext
= curblock
.to_s
165 process_code
(n
, btext
)
170 fun process_code
(n
: HTMLTag, text
: String)
177 # Build a `<div>` element that contains the full documentation in HTML
178 fun full_markdown
: HTMLTag
180 var d2m
= new Doc2Mdwn
181 return d2m
.work
(self)
184 # Build a `<span>` element that contains the synopsys in HTML
185 fun short_markdown
: HTMLTag
187 var d2m
= new Doc2Mdwn
188 return d2m
.short_work
(self)