c9db676cf5ca7e9d82d2d90b7d92b7733eb0a2fa
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 # Synopsys in a template
30 var tpl_short_comment
: Streamable is lazy
do
31 var res
= new Template
32 var syn
= nitdoc_inline_processor
.process
(content
.first
)
33 res
.add
"<span class=\"synopsys nitdoc\
">{syn}</span>"
38 # Full comment in a template
39 var tpl_comment
: Streamable is lazy
do
40 var res
= new Template
41 var lines
= content
.to_a
42 res
.add
"<div class=\"nitdoc\
">"
43 # do not use DocUnit as synopsys
44 if not content
.first
.has_prefix
(" ") and
45 not content
.first
.has_prefix
("\t") then
47 var syn
= nitdoc_inline_processor
.process
(lines
.shift
)
48 res
.add
"<p class=\"synopsys\
">{syn}</p>"
50 # check for annotations
51 for i
in [0 .. lines
.length
[ do
53 if line
.to_upper
.has_prefix
("ENSURE") or line
.to_upper
.has_prefix
("REQUIRE") then
54 var html
= nitdoc_inline_processor
.process
(line
)
55 lines
[i
] = "<p class=\"contract\
">{html}</p>"
56 else if line
.to_upper
.has_prefix
("TODO") or line
.to_upper
.has_prefix
("FIXME") then
57 var html
= nitdoc_inline_processor
.process
(line
)
58 lines
[i
] = "<p class=\"todo\
">{html}</p>"
62 res
.add nitdoc_md_processor
.process
(lines
.join
("\n"))
68 private class NitdocDecorator
71 var toolcontext
= new ToolContext
73 redef fun add_code
(v
, block
) do
75 if block
isa BlockFence and block
.meta
!= null then
76 meta
= block
.meta
.to_s
78 # Do not try to highlight non-nit code.
79 if meta
!= "nit" and meta
!= "nitish" then
80 v
.add
"<pre class=\"{meta}\
"><code>"
82 v
.add
"</code></pre>\n"
86 var code
= code_from_block
(block
)
87 var ast
= toolcontext
.parse_something
(code
)
88 if ast
isa AError then
89 v
.add
"<pre class=\"{meta}\
"><code>"
91 v
.add
"</code></pre>\n"
94 v
.add
"<pre class=\"nitcode\
"><code>"
95 var hl
= new HighlightVisitor
98 v
.add
"</code></pre>\n"
101 redef fun add_span_code
(v
, text
, from
, to
) do
103 var code
= code_from_text
(text
, from
, to
)
104 var ast
= toolcontext
.parse_something
(code
)
106 if ast
isa AError then
107 v
.add
"<code class=\"rawcode\
">"
108 append_code
(v
, text
, from
, to
)
110 v
.add
"<code class=\"nitcode\
">"
111 var hl
= new HighlightVisitor
118 fun code_from_text
(buffer
: Text, from
, to
: Int): String do
119 var out
= new FlatBuffer
120 for i
in [from
..to
[ do out
.add buffer
[i
]
121 return out
.write_to_string
124 fun code_from_block
(block
: BlockCode): String do
125 var infence
= block
isa BlockFence
126 var text
= new FlatBuffer
127 var line
= block
.block
.first_line
128 while line
!= null do
129 if not line
.is_empty
then
131 if not infence
and str
.has_prefix
(" ") then
132 text
.append str
.substring
(4, str
.length
- line
.trailing
)
140 return text
.write_to_string
144 private class InlineDecorator
145 super NitdocDecorator
147 redef fun add_paragraph
(v
, block
) do
151 redef fun add_code
(v
, block
) do
153 var ast
= toolcontext
.parse_something
(block
.block
.text
.to_s
)
154 if ast
isa AError then
160 v
.add
"<code class=\"nitcode\
">"
161 var hl
= new HighlightVisitor
168 # Get a markdown processor for Nitdoc comments.
169 private fun nitdoc_md_processor
: MarkdownProcessor do
170 var proc
= new MarkdownProcessor
171 proc
.emitter
.decorator
= new NitdocDecorator
175 # Get a markdown inline processor for Nitdoc comments.
177 # This processor is specificaly designed to inlinable doc elements like synopsys.
178 private fun nitdoc_inline_processor
: MarkdownProcessor do
179 var proc
= new MarkdownProcessor
180 proc
.emitter
.decorator
= new InlineDecorator