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 # Decorators for `markdown` parsing.
20 # `Decorator` that outputs markdown.
24 redef var headlines
= new ArrayMap[String, HeadLine]
26 redef fun add_ruler
(v
, block
) do v
.add
"***\n"
28 redef fun add_headline
(v
, block
) do
30 var txt
= block
.block
.first_line
.value
31 var id
= strip_id
(txt
)
33 headlines
[id
] = new HeadLine(id
, txt
, lvl
)
39 redef fun add_paragraph
(v
, block
) do
44 redef fun add_code
(v
, block
) do
45 if block
isa BlockFence and block
.meta
!= null then
46 v
.add
"~~~{block.meta.to_s}"
56 redef fun add_blockquote
(v
, block
) do
62 redef fun add_unorderedlist
(v
, block
) do
63 in_unorderedlist
= true
65 in_unorderedlist
= false
67 private var in_unorderedlist
= false
69 redef fun add_orderedlist
(v
, block
) do
73 in_unorderedlist
= false
75 private var in_orderedlist
= false
76 private var current_li
= 0
78 redef fun add_listitem
(v
, block
) do
79 if in_unorderedlist
then
81 else if in_orderedlist
then
89 redef fun add_em
(v
, text
) do
95 redef fun add_strong
(v
, text
) do
101 redef fun add_strike
(v
, text
) do
107 redef fun add_image
(v
, link
, name
, comment
) do
111 append_value
(v
, link
)
112 if comment
!= null and not comment
.is_empty
then
114 append_value
(v
, comment
)
119 redef fun add_link
(v
, link
, name
, comment
) do
123 append_value
(v
, link
)
124 if comment
!= null and not comment
.is_empty
then
126 append_value
(v
, comment
)
131 redef fun add_abbr
(v
, name
, comment
) do
132 v
.add
"<abbr title=\""
133 append_value(v, comment)
139 redef fun add_span_code
(v
, text
, from
, to
) do
141 append_code
(v
, text
, from
, to
)
145 redef fun add_line_break
(v
) do
149 redef fun append_value
(v
, text
) do for c
in text
do escape_char
(v
, c
)
151 redef fun escape_char
(v
, c
) do v
.addc
(c
)
153 redef fun append_code
(v
, buffer
, from
, to
) do
154 for i
in [from
..to
[ do
159 redef fun strip_id
(txt
) do
161 var b
= new FlatBuffer
166 if not c
.is_letter
and
168 not allowed_id_chars
.has
(c
) then continue
174 # check for multiple id definitions
175 if headlines
.has_key
(key
) then
178 while headlines
.has_key
(key
) do
186 private var allowed_id_chars
: Array[Char] = ['-', '_', ':', '.']
189 # Decorator for span elements.
191 # InlineDecorator does not decorate things like paragraphs or headers.
192 class InlineDecorator
195 redef fun add_paragraph
(v
, block
) do v
.emit_in block
196 redef fun add_headline
(v
, block
) do v
.emit_in block
198 redef fun add_code
(v
, block
) do