1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 # Decorators for `markdown` parsing.
19 # `Decorator` that outputs markdown.
23 redef var headlines
= new ArrayMap[String, HeadLine]
25 redef fun add_ruler
(v
, block
) do v
.add
"***\n"
27 redef fun add_headline
(v
, block
) do
29 var txt
= block
.block
.first_line
.value
30 var id
= strip_id
(txt
)
32 headlines
[id
] = new HeadLine(id
, txt
, lvl
)
38 redef fun add_paragraph
(v
, block
) do
43 redef fun add_code
(v
, block
) do
44 if block
isa BlockFence and block
.meta
!= null then
45 v
.add
"~~~{block.meta.to_s}"
55 redef fun add_blockquote
(v
, block
) do
61 redef fun add_unorderedlist
(v
, block
) do
62 in_unorderedlist
= true
64 in_unorderedlist
= false
66 private var in_unorderedlist
= false
68 redef fun add_orderedlist
(v
, block
) do
72 in_unorderedlist
= false
74 private var in_orderedlist
= false
75 private var current_li
= 0
77 redef fun add_listitem
(v
, block
) do
78 if in_unorderedlist
then
80 else if in_orderedlist
then
88 redef fun add_em
(v
, text
) do
94 redef fun add_strong
(v
, text
) do
100 redef fun add_strike
(v
, text
) do
106 redef fun add_image
(v
, link
, name
, comment
) do
110 append_value
(v
, link
)
111 if comment
!= null and not comment
.is_empty
then
113 append_value
(v
, comment
)
118 redef fun add_link
(v
, link
, name
, comment
) do
122 append_value
(v
, link
)
123 if comment
!= null and not comment
.is_empty
then
125 append_value
(v
, comment
)
130 redef fun add_abbr
(v
, name
, comment
) do
131 v
.add
"<abbr title=\""
132 append_value(v, comment)
138 redef fun add_span_code
(v
, text
, from
, to
) do
140 append_code
(v
, text
, from
, to
)
144 redef fun add_line_break
(v
) do
148 redef fun append_value
(v
, text
) do for c
in text
do escape_char
(v
, c
)
150 redef fun escape_char
(v
, c
) do v
.addc
(c
)
152 redef fun append_code
(v
, buffer
, from
, to
) do
153 for i
in [from
..to
[ do
158 redef fun strip_id
(txt
) do
160 var b
= new FlatBuffer
165 if not c
.is_letter
and
167 not allowed_id_chars
.has
(c
) then continue
173 # check for multiple id definitions
174 if headlines
.has_key
(key
) then
177 while headlines
.has_key
(key
) do
185 private var allowed_id_chars
: Array[Char] = ['-', '_', ':', '.']