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 # Handle markdown formatting in Nit comments.
20 private import parser_util
23 # Comment synopsys HTML escaped
24 var short_comment
: String is lazy
do return content
.first
.html_escape
26 # Full comment HTML escaped
27 var full_comment
: String is lazy
do return content
.join
("\n").html_escape
29 private var markdown_proc
: MarkdownProcessor is lazy
do
30 return original_mentity
.model
.nitdoc_md_processor
33 private var inline_proc
: MarkdownProcessor is lazy
do
34 return original_mentity
.model
.nitdoc_inline_processor
37 # Synopsys in a template
38 var tpl_short_comment
: Writable is lazy
do
39 var res
= new Template
40 var syn
= inline_proc
.process
(content
.first
)
41 res
.add
"<span class=\"synopsys nitdoc\
">{syn}</span>"
46 # Full comment in a template
47 var tpl_comment
: Writable is lazy
do
48 var res
= new Template
49 var lines
= content
.to_a
50 res
.add
"<div class=\"nitdoc\
">"
51 # do not use DocUnit as synopsys
52 if not content
.first
.has_prefix
(" ") and
53 not content
.first
.has_prefix
("\t") then
55 var syn
= inline_proc
.process
(lines
.shift
)
56 res
.add
"<p class=\"synopsys\
">{syn}</p>"
58 # check for annotations
59 for i
in [0 .. lines
.length
[ do
61 if line
.to_upper
.has_prefix
("ENSURE") or line
.to_upper
.has_prefix
("REQUIRE") then
62 var html
= inline_proc
.process
(line
)
63 lines
[i
] = "<p class=\"contract\
">{html}</p>"
64 else if line
.to_upper
.has_prefix
("TODO") or line
.to_upper
.has_prefix
("FIXME") then
65 var html
= inline_proc
.process
(line
)
66 lines
[i
] = "<p class=\"todo\
">{html}</p>"
70 res
.add markdown_proc
.process
(lines
.join
("\n"))
76 private class NitdocDecorator
79 var toolcontext
= new ToolContext
81 redef fun add_code
(v
, block
) do
83 if block
isa BlockFence and block
.meta
!= null then
84 meta
= block
.meta
.to_s
86 # Do not try to highlight non-nit code.
87 if meta
!= "nit" and meta
!= "nitish" then
88 v
.add
"<pre class=\"{meta}\
"><code>"
90 v
.add
"</code></pre>\n"
94 var code
= code_from_block
(block
)
95 var ast
= toolcontext
.parse_something
(code
)
96 if ast
isa AError then
97 v
.add
"<pre class=\"{meta}\
"><code>"
99 v
.add
"</code></pre>\n"
102 v
.add
"<pre class=\"nitcode\
"><code>"
103 var hl
= new HighlightVisitor
104 hl
.line_id_prefix
= ""
107 v
.add
"</code></pre>\n"
110 redef fun add_span_code
(v
, text
, from
, to
) do
112 var code
= code_from_text
(text
, from
, to
)
113 var ast
= toolcontext
.parse_something
(code
)
115 if ast
isa AError then
116 v
.add
"<code class=\"rawcode\
">"
117 append_code
(v
, text
, from
, to
)
119 v
.add
"<code class=\"nitcode\
">"
120 var hl
= new HighlightVisitor
121 hl
.line_id_prefix
= ""
128 fun code_from_text
(buffer
: Text, from
, to
: Int): String do
129 var out
= new FlatBuffer
130 for i
in [from
..to
[ do out
.add buffer
[i
]
131 return out
.write_to_string
134 fun code_from_block
(block
: BlockCode): String do
135 var infence
= block
isa BlockFence
136 var text
= new FlatBuffer
137 var line
= block
.block
.first_line
138 while line
!= null do
139 if not line
.is_empty
then
141 if not infence
and str
.has_prefix
(" ") then
142 text
.append str
.substring
(4, str
.length
- line
.trailing
)
150 return text
.write_to_string
154 # Decorator for span elements.
156 # Because inline comments can appear as span elements,
157 # InlineDecorator do not decorate things like paragraphs or headers.
158 private class InlineDecorator
159 super NitdocDecorator
161 redef fun add_paragraph
(v
, block
) do
165 redef fun add_headline
(v
, block
) do
169 redef fun add_code
(v
, block
) do
171 var ast
= toolcontext
.parse_something
(block
.block
.text
.to_s
)
172 if ast
isa AError then
178 v
.add
"<code class=\"nitcode\
">"
179 var hl
= new HighlightVisitor
187 # Get a markdown processor for Nitdoc comments.
188 private var nitdoc_md_processor
: MarkdownProcessor is lazy
do
189 var proc
= new MarkdownProcessor
190 proc
.emitter
.decorator
= new NitdocDecorator
194 # Get a markdown inline processor for Nitdoc comments.
196 # This processor is specificaly designed to inlinable doc elements like synopsys.
197 private var nitdoc_inline_processor
: MarkdownProcessor is lazy
do
198 var proc
= new MarkdownProcessor
199 proc
.emitter
.decorator
= new InlineDecorator