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 var toolcontext
: ToolContext
25 # The lines of the current code block, empty is no current code block
26 var curblock
= new Array[String]
28 fun work
(ndoc
: ADoc): HTMLTag
30 var root
= new HTMLTag("div")
31 root
.add_class
("nitdoc")
33 # Indent level of the previous line
36 # Indent level of the current line
39 # The current element (p, li, etc.) if any
40 var n
: nullable HTMLTag = null
42 # The current ul element (if any)
43 var ul
: nullable HTMLTag = null
45 # Local variable to benefit adaptive typing
46 for c
in ndoc
.n_comment
do
47 # Remove the starting `#`
48 var text
= c
.text
.substring_from
(1)
50 # Count the number of spaces
53 while text
.length
> indent
and text
[indent
] == ' ' do indent
+= 1
55 # Is codeblock? Then just collect them
57 var part
= text
.substring_from
(4)
62 # Was a codblock just before the current line ?
63 close_codeblock
(n
or else root
)
68 # The HTML node of the last line, so we know if we continue the same block
71 # No line or loss of indentation: reset
72 if text
.is_empty
or indent
< lastindent
then
75 if text
.is_empty
then continue
78 # Special first word: new paragraph
79 if text
.has_prefix
("TODO") or text
.has_prefix
("FIXME") then
84 else if text
.has_prefix
("REQUIRE") or text
.has_prefix
("Require") or text
.has_prefix
("ENSURE") or text
.has_prefix
("Ensure") then
87 n
.add_class
("contract")
92 if text
.has_prefix
("* ") or text
.has_prefix
("- ") then
93 text
= text
.substring_from
(1).trim
95 ul
= new HTMLTag("ul")
102 # Nothing? then paragraph
110 # Because spaces and `\n` where trimmed
114 process_line
(n
, text
)
116 # Special case, the fist line is the synopsys and is in its own paragraph
117 if c
== ndoc
.n_comment
.first
then
118 n
.add_class
("synopsys")
123 # If the codeblock was the last code sequence
124 close_codeblock
(n
or else root
)
129 fun short_work
(ndoc
: ADoc): HTMLTag
131 var text
= ndoc
.n_comment
.first
.text
.substring_from
(1)
132 var n
= new HTMLTag("span")
133 n
.add_class
("synopsys")
134 n
.add_class
("nitdoc")
135 process_line
(n
, text
)
139 fun process_line
(n
: HTMLTag, text
: String)
141 # Loosly detect code parts
142 var parts
= text
.split
("`")
144 # Process each code parts, thus alternate between text and code
152 var n2
= new HTMLTag("code")
154 process_code
(n2
, part
)
156 is_text
= not is_text
160 fun close_codeblock
(root
: HTMLTag)
162 # Is there a codeblock to manage?
163 if not curblock
.is_empty
then
164 var n
= new HTMLTag("pre")
166 var btext
= curblock
.to_s
167 process_code
(n
, btext
)
172 fun process_code
(n
: HTMLTag, text
: String)
179 # Build a `<div>` element that contains the full documentation in HTML
180 fun full_markdown
: HTMLTag
182 var tc
= new ToolContext
183 var d2m
= new Doc2Mdwn(tc
)
184 return d2m
.work
(self)
187 # Build a `<span>` element that contains the synopsys in HTML
188 fun short_markdown
: HTMLTag
190 var tc
= new ToolContext
191 var d2m
= new Doc2Mdwn(tc
)
192 return d2m
.short_work
(self)