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.
20 # Parse a markdown string and split it in blocks.
22 # Blocks are then outputed by an `MarkdownEmitter`.
26 # var proc = new MarkdownProcessor
27 # var html = proc.process("**Hello World!**")
28 # assert html == "<p><strong>Hello World!</strong></p>\n"
30 # SEE: `String::md_to_html` for a shortcut.
31 class MarkdownProcessor
33 # `MarkdownEmitter` used for ouput.
34 var emitter
: MarkdownEmitter is noinit
36 # Work in extended mode (default).
38 # Behavior changes when using extended mode:
40 # * Lists and code blocks end a paragraph
42 # In normal markdown the following:
45 # * and this is not a list
49 # <p>This is a paragraph
50 # * and this is not a list</p>
52 # When using extended mode this changes to:
54 # <p>This is a paragraph</p>
56 # <li>and this is not a list</li>
59 # * Fences code blocks
61 # If you don't want to indent your all your code with 4 spaces,
62 # you can wrap your code in ``` ``` ``` or `~~~`.
68 # print "Hello World!"
74 # If you want to use syntax highlighting tools, most of them need to know what kind
75 # of language they are highlighting.
76 # You can add an optional language identifier after the fence declaration to output
77 # it in the HTML render.
82 # print "# Hello World!".md_to_html
87 # <pre class="nit"><code>import markdown
89 # print "Hello World!".md_to_html
92 # * Underscores (Emphasis)
94 # Underscores in the middle of a word like:
98 # normally produces this:
100 # <p>Con<em>cat</em>this</p>
102 # With extended mode they don't result in emphasis.
104 # <p>Con_cat_this</p>
108 # Like in [GFM](https://help.github.com/articles/github-flavored-markdown),
109 # strikethrought span is marked with `~~`.
115 # <del>Mistaken text.</del>
118 init do self.emitter
= new MarkdownEmitter(self)
120 # Process the mardown `input` string and return the processed output.
121 fun process
(input
: String): Streamable do
128 var parent
= read_lines
(input
)
129 parent
.remove_surrounding_empty_lines
130 recurse
(parent
, false)
131 # output processed text
132 return emitter
.emit
(parent
.kind
)
135 # Split `input` string into `MDLines` and create a parent `MDBlock` with it.
136 private fun read_lines
(input
: String): MDBlock do
137 var block
= new MDBlock
138 var value
= new FlatBuffer
140 while i
< input
.length
do
144 while not eol
and i
< input
.length
do
149 else if c
== '\t' then
150 var np
= pos
+ (4 - (pos
.bin_and
(3)))
163 var line
= new MDLine(value
.write_to_string
)
164 var is_link_ref
= check_link_ref
(line
)
166 if not is_link_ref
then block
.add_line line
171 # Check if line is a block link definition.
172 # Return `true` if line contains a valid link ref and save it into `link_refs`.
173 private fun check_link_ref
(line
: MDLine): Bool do
175 var is_link_ref
= false
176 var id
= new FlatBuffer
177 var link
= new FlatBuffer
178 var comment
= new FlatBuffer
180 if not line
.is_empty
and line
.leading
< 4 and line
.value
[line
.leading
] == '[' then
181 pos
= line
.leading
+ 1
182 pos
= md
.read_until
(id
, pos
, ']')
183 if not id
.is_empty
and pos
+ 2 < line
.value
.length
then
184 if line
.value
[pos
+ 1] == ':' then
186 pos
= md
.skip_spaces
(pos
)
187 if line
.value
[pos
] == '<' then
189 pos
= md
.read_until
(link
, pos
, '>')
192 pos
= md
.read_until
(link
, pos
, ' ', '\n')
194 if not link
.is_empty
then
195 pos
= md
.skip_spaces
(pos
)
196 if pos
> 0 and pos
< line
.value
.length
then
197 var c
= line
.value
[pos
]
198 if c
== '\"' or c
== '\'' or c == '(' then
201 pos = md.read_until(comment, pos, ')')
203 pos = md.read_until(comment, pos, c)
205 if pos > 0 then is_link_ref = true
214 if is_link_ref and not id.is_empty and not link.is_empty then
215 var lr = new LinkRef.with_title(link.write_to_string, comment.write_to_string)
216 add_link_ref(id.write_to_string, lr)
217 if comment.is_empty then last_link_ref = lr
220 comment = new FlatBuffer
221 if not line.is_empty and last_link_ref != null then
223 var c = line.value[pos]
224 if c == '\
"' or c == '\'' or c == '(' then
227 pos = md.read_until(comment, pos, ')')
229 pos = md.read_until(comment, pos, c)
232 if not comment.is_empty then last_link_ref.title = comment.write_to_string
234 if comment.is_empty then return false
240 # This list will be needed during output to expand links.
241 var link_refs: Map[String, LinkRef] = new HashMap[String, LinkRef]
243 # Last encountered link ref (for multiline definitions)
245 # Markdown allows link refs to be defined over two lines:
247 # [id]: http://example.com/longish/path/to/resource/here
248 # "Optional Title Here"
250 private var last_link_ref: nullable LinkRef = null
252 # Add a link ref to the list
253 fun add_link_ref(key: String, ref: LinkRef) do link_refs[key.to_lower] = ref
255 # Recursively split a `block`.
257 # The block is splitted according to the type of lines it contains.
258 # Some blocks can be splited again recursively like lists.
259 # The `in_list` mode is used to recurse on list and build
260 # nested paragraphs or code blocks.
261 fun recurse(root: MDBlock, in_list: Bool) do
262 var old_mode = self.in_list
263 var old_root = self.current_block
264 self.in_list = in_list
266 var line = root.first_line
267 while line != null and line.is_empty do
269 if line == null then return
274 while current_line != null do
275 line_kind(current_line.as(not null)).process(self)
277 self.in_list = old_mode
278 self.current_block = old_root
281 # Currently processed line.
282 # Used when visiting blocks with `recurse`.
283 var current_line: nullable MDLine = null is writable
285 # Currently processed block.
286 # Used when visiting blocks with `recurse`.
287 var current_block: nullable MDBlock = null is writable
289 # Is the current recursion in list mode?
290 # Used when visiting blocks with `recurse`
291 private var in_list = false
295 fun line_kind(md: MDLine): Line do
297 var leading = md.leading
298 var trailing = md.trailing
299 if md.is_empty then return new LineEmpty
300 if md.leading > 3 then return new LineCode
301 if value[leading] == '#' then return new LineHeadline
302 if value[leading] == '>' then return new LineBlockquote
305 if value.length - leading - trailing > 2 then
306 if value[leading] == '`' and md.count_chars_start('`') >= 3 then
309 if value[leading] == '~' and md.count_chars_start('~') >= 3 then
315 if value.length - leading - trailing > 2 and
316 (value[leading] == '*' or value[leading] == '-' or value[leading] == '_') then
317 if md.count_chars(value[leading]) >= 3 then
322 if value.length - leading >= 2 and value[leading + 1] == ' ' then
323 var c = value[leading]
324 if c == '*' or c == '-' or c == '+' then return new LineUList
327 if value.length - leading >= 3 and value[leading].is_digit then
329 while i < value.length and value[i].is_digit do i += 1
330 if i + 1 < value.length and value[i] == '.' and value[i + 1] == ' ' then
335 if value[leading] == '<' and md.check_html then return new LineXML
338 if next != null and not next.is_empty then
339 if next.count_chars('=') > 0 then
340 return new LineHeadline1
342 if next.count_chars('-') > 0 then
343 return new LineHeadline2
349 # Get the token kind at `pos`.
350 fun token_at(text: Text, pos: Int): Token do
362 if pos + 1 < text.length then
367 if pos + 2 < text.length then
375 if c0 != ' ' or c2 != ' ' then
376 return new TokenStrongStar(pos, c)
378 return new TokenEmStar(pos, c)
381 if c0 != ' ' or c1 != ' ' then
382 return new TokenEmStar(pos, c)
384 return new TokenNone(pos, c)
386 else if c == '_' then
388 if c0 != ' ' or c2 != ' 'then
389 return new TokenStrongUnderscore(pos, c)
391 return new TokenEmUnderscore(pos, c)
395 if (c0.is_letter or c0.is_digit) and c0 != '_' and
396 (c1.is_letter or c1.is_digit) then
397 return new TokenNone(pos, c)
399 return new TokenEmUnderscore(pos, c)
402 if c0 != ' ' or c1 != ' ' then
403 return new TokenEmUnderscore(pos, c)
405 return new TokenNone(pos, c)
407 else if c == '!' then
408 if c1 == '[' then return new TokenImage(pos, c)
409 return new TokenNone(pos, c)
410 else if c == '[' then
411 return new TokenLink(pos, c)
412 else if c == ']' then
413 return new TokenNone(pos, c)
414 else if c == '`' then
416 return new TokenCodeDouble(pos, c)
418 return new TokenCodeSingle(pos, c)
420 else if c == '\\' then
421 if c1 == '\\' or c1 == '[' or c1 == ']' or c1 == '(' or c1 == ')' or c1 == '{' or c1 == '}' or c1 == '#' or c1 == '"' or c1 == '\
'' or c1
== '.' or c1
== '<' or c1
== '>' or c1
== '*' or c1
== '+' or c1
== '-' or c1
== '_' or c1
== '!' or c1
== '`' or c1
== '~' or c1
== '^' then
422 return new TokenEscape(pos
, c
)
424 return new TokenNone(pos
, c
)
426 else if c
== '<' then
427 return new TokenHTML(pos
, c
)
428 else if c
== '&' then
429 return new TokenEntity(pos
, c
)
432 if c
== '~' and c1
== '~' then
433 return new TokenStrike(pos
, c
)
436 return new TokenNone(pos
, c
)
440 # Find the position of a `token` in `self`.
441 fun find_token
(text
: Text, start
: Int, token
: Token): Int do
443 while pos
< text
.length
do
444 if token_at
(text
, pos
).is_same_type
(token
) then
453 # Emit output corresponding to blocks content.
455 # Blocks are created by a previous pass in `MarkdownProcessor`.
456 # The emitter use a `Decorator` to select the output format.
457 class MarkdownEmitter
459 # Processor containing link refs.
460 var processor
: MarkdownProcessor
462 # Decorator used for output.
463 # Default is `HTMLDecorator`
464 var decorator
: Decorator = new HTMLDecorator is writable
466 # Create a new `MarkdownEmitter` using a custom `decorator`.
467 init with_decorator
(processor
: MarkdownProcessor, decorator
: Decorator) do
469 self.decorator
= decorator
472 # Output `block` using `decorator` in the current buffer.
473 fun emit
(block
: Block): Text do
474 var buffer
= push_buffer
480 # Output the content of `block`.
481 fun emit_in
(block
: Block) do block
.emit_in
(self)
483 # Transform and emit mardown text
484 fun emit_text
(text
: Text) do
485 emit_text_until
(text
, 0, null)
488 # Transform and emit mardown text starting at `from` and
489 # until a token with the same type as `token` is found.
490 # Go until the end of text if `token` is null.
491 fun emit_text_until
(text
: Text, start
: Int, token
: nullable Token): Int do
492 var old_text
= current_text
493 var old_pos
= current_pos
496 while current_pos
< text
.length
do
497 var mt
= processor
.token_at
(text
, current_pos
)
498 if (token
!= null and not token
isa TokenNone) and
499 (mt
.is_same_type
(token
) or
500 (token
isa TokenEmStar and mt
isa TokenStrongStar) or
501 (token
isa TokenEmUnderscore and mt
isa TokenStrongUnderscore)) then
507 current_text
= old_text
508 current_pos
= old_pos
512 # Currently processed position in `current_text`.
513 # Used when visiting inline production with `emit_text_until`.
514 private var current_pos
: Int = -1
516 # Currently processed text.
517 # Used when visiting inline production with `emit_text_until`.
518 private var current_text
: nullable Text = null
521 private var buffer_stack
= new List[FlatBuffer]
523 # Push a new buffer on the stack.
524 private fun push_buffer
: FlatBuffer do
525 var buffer
= new FlatBuffer
526 buffer_stack
.add buffer
530 # Pop the last buffer.
531 private fun pop_buffer
do buffer_stack
.pop
533 # Current output buffer.
534 private fun current_buffer
: FlatBuffer do
535 assert not buffer_stack
.is_empty
536 return buffer_stack
.last
539 # Append `e` to current buffer.
540 fun add
(e
: Streamable) do
542 current_buffer
.append e
544 current_buffer
.append e
.write_to_string
548 # Append `c` to current buffer.
549 fun addc
(c
: Char) do current_buffer
.add c
551 # Append a "\n" line break.
552 fun addn
do current_buffer
.add
'\n'
556 # Links that are specified somewhere in the mardown document to be reused as shortcuts.
560 # [1]: http://example.com/ "Optional title"
566 # Optional link title
567 var title
: nullable String = null
569 # Is the link an abreviation?
570 var is_abbrev
= false
572 # Create a link with a title.
573 init with_title
(link
: String, title
: nullable String) do
579 # A `Decorator` is used to emit mardown into a specific format.
580 # Default decorator used is `HTMLDecorator`.
583 # Render a ruler block.
584 fun add_ruler
(v
: MarkdownEmitter, block
: BlockRuler) is abstract
586 # Render a headline block with corresponding level.
587 fun add_headline
(v
: MarkdownEmitter, block
: BlockHeadline) is abstract
589 # Render a paragraph block.
590 fun add_paragraph
(v
: MarkdownEmitter, block
: BlockParagraph) is abstract
592 # Render a code or fence block.
593 fun add_code
(v
: MarkdownEmitter, block
: BlockCode) is abstract
595 # Render a blockquote.
596 fun add_blockquote
(v
: MarkdownEmitter, block
: BlockQuote) is abstract
598 # Render an unordered list.
599 fun add_unorderedlist
(v
: MarkdownEmitter, block
: BlockUnorderedList) is abstract
601 # Render an ordered list.
602 fun add_orderedlist
(v
: MarkdownEmitter, block
: BlockOrderedList) is abstract
604 # Render a list item.
605 fun add_listitem
(v
: MarkdownEmitter, block
: BlockListItem) is abstract
607 # Render an emphasis text.
608 fun add_em
(v
: MarkdownEmitter, text
: Text) is abstract
610 # Render a strong text.
611 fun add_strong
(v
: MarkdownEmitter, text
: Text) is abstract
613 # Render a strike text.
615 # Extended mode only (see `MarkdownProcessor::ext_mode`)
616 fun add_strike
(v
: MarkdownEmitter, text
: Text) is abstract
619 fun add_link
(v
: MarkdownEmitter, link
: Text, name
: Text, comment
: nullable Text) is abstract
622 fun add_image
(v
: MarkdownEmitter, link
: Text, name
: Text, comment
: nullable Text) is abstract
624 # Render an abbreviation.
625 fun add_abbr
(v
: MarkdownEmitter, name
: Text, comment
: Text) is abstract
627 # Render a code span reading from a buffer.
628 fun add_span_code
(v
: MarkdownEmitter, buffer
: Text, from
, to
: Int) is abstract
630 # Render a text and escape it.
631 fun append_value
(v
: MarkdownEmitter, value
: Text) is abstract
633 # Render code text from buffer and escape it.
634 fun append_code
(v
: MarkdownEmitter, buffer
: Text, from
, to
: Int) is abstract
636 # Render a character escape.
637 fun escape_char
(v
: MarkdownEmitter, char
: Char) is abstract
639 # Render a line break
640 fun add_line_break
(v
: MarkdownEmitter) is abstract
642 # Generate a new html valid id from a `String`.
643 fun strip_id
(txt
: String): String is abstract
645 # Found headlines during the processing labeled by their ids.
646 fun headlines
: ArrayMap[String, HeadLine] is abstract
649 # Class representing a markdown headline.
651 # Unique identifier of this headline.
654 # Text of the headline.
657 # Level of this headline.
659 # According toe the markdown specification, level must be in `[1..6]`.
663 # `Decorator` that outputs HTML.
667 redef var headlines
= new ArrayMap[String, HeadLine]
669 redef fun add_ruler
(v
, block
) do v
.add
"<hr/>\n"
671 redef fun add_headline
(v
, block
) do
673 var txt
= block
.block
.first_line
.value
674 var id
= strip_id
(txt
)
675 var lvl
= block
.depth
676 headlines
[id
] = new HeadLine(id
, txt
, lvl
)
678 v
.add
"<h{lvl} id=\"{id}\
">"
683 redef fun add_paragraph
(v
, block
) do
689 redef fun add_code
(v
, block
) do
690 if block
isa BlockFence and block
.meta
!= null then
691 v
.add
"<pre class=\"{block.meta.to_s}\
"><code>"
696 v
.add
"</code></pre>\n"
699 redef fun add_blockquote
(v
, block
) do
700 v
.add
"<blockquote>\n"
702 v
.add
"</blockquote>\n"
705 redef fun add_unorderedlist
(v
, block
) do
711 redef fun add_orderedlist
(v
, block
) do
717 redef fun add_listitem
(v
, block
) do
723 redef fun add_em
(v
, text
) do
729 redef fun add_strong
(v
, text
) do
735 redef fun add_strike
(v
, text
) do
741 redef fun add_image
(v
, link
, name
, comment
) do
743 append_value(v, link)
745 append_value(v, name)
747 if comment
!= null and not comment
.is_empty
then
749 append_value(v, comment)
755 redef fun add_link
(v
, link
, name
, comment
) do
757 append_value(v, link)
759 if comment
!= null and not comment
.is_empty
then
761 append_value(v, comment)
769 redef fun add_abbr
(v
, name
, comment
) do
770 v
.add
"<abbr title=\""
771 append_value(v, comment)
777 redef fun add_span_code
(v
, text
, from
, to
) do
779 append_code
(v
, text
, from
, to
)
783 redef fun add_line_break
(v
) do
787 redef fun append_value
(v
, text
) do for c
in text
do escape_char
(v
, c
)
789 redef fun escape_char
(v
, c
) do
792 else if c
== '<' then
794 else if c
== '>' then
796 else if c
== '"' then
798 else if c
== '\'' then
805 redef fun append_code(v, buffer, from, to) do
806 for i in [from..to[ do
810 else if c == '<' then
812 else if c == '>' then
820 redef fun strip_id(txt) do
822 var b = new FlatBuffer
827 if not c.is_letter and
829 not allowed_id_chars.has(c) then continue
835 # check for multiple id definitions
836 if headlines.has_key(key) then
839 while headlines.has_key(key) do
847 private var allowed_id_chars: Array[Char] = ['-', '_
', ':', '.']
850 # A block of markdown lines.
851 # A `MDBlock` can contains lines and/or sub-blocks.
855 var kind: Block = new BlockNone(self) is writable
858 var first_line: nullable MDLine = null is writable
861 var last_line: nullable MDLine = null is writable
863 # First sub-block if any.
864 var first_block: nullable MDBlock = null is writable
866 # Last sub-block if any.
867 var last_block: nullable MDBlock = null is writable
869 # Previous block if any.
870 var prev: nullable MDBlock = null is writable
873 var next: nullable MDBlock = null is writable
875 # Does this block contain subblocks?
876 fun has_blocks: Bool do return first_block != null
879 fun count_blocks: Int do
881 var block = first_block
882 while block != null do
889 # Does this block contain lines?
890 fun has_lines: Bool do return first_line != null
893 fun count_lines: Int do
895 var line = first_line
896 while line != null do
903 # Split `self` creating a new sub-block having `line` has `last_line`.
904 fun split(line: MDLine): MDBlock do
905 var block = new MDBlock
906 block.first_line = first_line
907 block.last_line = line
908 first_line = line.next
910 if first_line == null then
913 first_line.prev = null
915 if first_block == null then
919 last_block.next = block
925 # Add a `line` to this block.
926 fun add_line(line: MDLine) do
927 if last_line == null then
931 last_line.next_empty = line.is_empty
932 line.prev_empty = last_line.is_empty
933 line.prev = last_line
934 last_line.next = line
939 # Remove `line` from this block.
940 fun remove_line(line: MDLine) do
941 if line.prev == null then
942 first_line = line.next
944 line.prev.next = line.next
946 if line.next == null then
947 last_line = line.prev
949 line.next.prev = line.prev
955 # Remove leading empty lines.
956 fun remove_leading_empty_lines: Bool do
957 var was_empty = false
958 var line = first_line
959 while line != null and line.is_empty do
967 # Remove trailing empty lines.
968 fun remove_trailing_empty_lines: Bool do
969 var was_empty = false
971 while line != null and line.is_empty do
979 # Remove leading and trailing empty lines.
980 fun remove_surrounding_empty_lines: Bool do
981 var was_empty = false
982 if remove_leading_empty_lines then was_empty = true
983 if remove_trailing_empty_lines then was_empty = true
987 # Remove list markers and up to 4 leading spaces.
988 # Used to clean nested lists.
989 fun remove_list_indent(v: MarkdownProcessor) do
990 var line = first_line
991 while line != null do
992 if not line.is_empty then
993 var kind = v.line_kind(line)
994 if kind isa LineList then
995 line.value = kind.extract_value(line)
997 line.value = line.value.substring_from(line.leading.min(4))
999 line.leading = line.process_leading
1005 # Collect block line text.
1007 var text = new FlatBuffer
1008 var line = first_line
1009 while line != null do
1010 if not line.is_empty then
1011 text.append line.text
1016 return text.write_to_string
1020 # Representation of a markdown block in the AST.
1021 # Each `Block` is linked to a `MDBlock` that contains mardown code.
1022 abstract class Block
1024 # The markdown block `self` is related to.
1027 # Output `self` using `v.decorator`.
1028 fun emit(v: MarkdownEmitter) do v.emit_in(self)
1030 # Emit the containts of `self`, lines or blocks.
1031 fun emit_in(v: MarkdownEmitter) do
1032 block.remove_surrounding_empty_lines
1033 if block.has_lines then
1040 # Emit lines contained in `block`.
1041 fun emit_lines(v: MarkdownEmitter) do
1042 var tpl = v.push_buffer
1043 var line = block.first_line
1044 while line != null do
1045 if not line.is_empty then
1046 v.add line.value.substring(line.leading, line.value.length - line.trailing)
1047 if line.trailing >= 2 then v.decorator.add_line_break(v)
1049 if line.next != null then
1058 # Emit sub-blocks contained in `block`.
1059 fun emit_blocks(v: MarkdownEmitter) do
1060 var block = self.block.first_block
1061 while block != null do
1068 # A block without any markdown specificities.
1070 # Actually use the same implementation than `BlockCode`,
1071 # this class is only used for typing purposes.
1076 # A markdown blockquote.
1080 redef fun emit(v) do v.decorator.add_blockquote(v, self)
1082 # Remove blockquote markers.
1083 private fun remove_block_quote_prefix(block: MDBlock) do
1084 var line = block.first_line
1085 while line != null do
1086 if not line.is_empty then
1087 if line.value[line.leading] == '>' then
1088 var rem = line.leading + 1
1089 if line.leading + 1 < line.value.length and
1090 line.value[line.leading + 1] == ' ' then
1093 line.value = line.value.substring_from(rem)
1094 line.leading = line.process_leading
1102 # A markdown code block.
1106 # Number of char to skip at the beginning of the line.
1108 # Block code lines start at 4 spaces.
1109 protected var line_start = 4
1111 redef fun emit(v) do v.decorator.add_code(v, self)
1113 redef fun emit_lines(v) do
1114 var line = block.first_line
1115 while line != null do
1116 if not line.is_empty then
1117 v.decorator.append_code(v, line.value, line_start, line.value.length)
1125 # A markdown code-fence block.
1127 # Actually use the same implementation than `BlockCode`,
1128 # this class is only used for typing purposes.
1132 # Any string found after fence token.
1133 var meta: nullable Text
1135 # Fence code lines start at 0 spaces.
1136 redef var line_start = 0
1139 # A markdown headline.
1143 redef fun emit(v) do v.decorator.add_headline(v, self)
1145 # Depth of the headline used to determine the headline level.
1148 # Remove healine marks from lines contained in `self`.
1149 private fun transform_headline(block: MDBlock) do
1150 if depth > 0 then return
1152 var line = block.first_line
1153 if line.is_empty then return
1154 var start = line.leading
1155 while start < line.value.length and line.value[start] == '#' do
1159 while start
< line
.value
.length
and line
.value
[start
] == ' ' do
1162 if start
>= line
.value
.length
then
1163 line
.is_empty
= true
1165 var nend
= line
.value
.length
- line
.trailing
- 1
1166 while line
.value
[nend
] == '#' do nend
-= 1
1167 while line
.value
[nend
] == ' ' do nend
-= 1
1168 line
.value
= line
.value
.substring
(start
, nend
- start
+ 1)
1172 depth
= level
.min
(6)
1176 # A markdown list item block.
1180 redef fun emit
(v
) do v
.decorator
.add_listitem
(v
, self)
1183 # A markdown list block.
1184 # Can be either an ordered or unordered list, this class is mainly used to factorize code.
1185 abstract class BlockList
1188 # Split list block into list items sub-blocks.
1189 private fun init_block
(v
: MarkdownProcessor) do
1190 var line
= block
.first_line
1192 while line
!= null do
1193 var t
= v
.line_kind
(line
)
1194 if t
isa LineList or
1195 (not line
.is_empty
and (line
.prev_empty
and line
.leading
== 0 and
1196 not (t
isa LineList))) then
1197 var sblock
= block
.split
(line
.prev
.as(not null))
1198 sblock
.kind
= new BlockListItem(sblock
)
1202 var sblock
= block
.split
(block
.last_line
.as(not null))
1203 sblock
.kind
= new BlockListItem(sblock
)
1206 # Expand list items as paragraphs if needed.
1207 private fun expand_paragraphs
(block
: MDBlock) do
1208 var outer
= block
.first_block
1209 var inner
: nullable MDBlock
1210 var has_paragraph
= false
1211 while outer
!= null and not has_paragraph
do
1212 if outer
.kind
isa BlockListItem then
1213 inner
= outer
.first_block
1214 while inner
!= null and not has_paragraph
do
1215 if inner
.kind
isa BlockParagraph then
1216 has_paragraph
= true
1223 if has_paragraph
then
1224 outer
= block
.first_block
1225 while outer
!= null do
1226 if outer
.kind
isa BlockListItem then
1227 inner
= outer
.first_block
1228 while inner
!= null do
1229 if inner
.kind
isa BlockNone then
1230 inner
.kind
= new BlockParagraph(inner
)
1241 # A markdown ordered list.
1242 class BlockOrderedList
1245 redef fun emit
(v
) do v
.decorator
.add_orderedlist
(v
, self)
1248 # A markdown unordred list.
1249 class BlockUnorderedList
1252 redef fun emit
(v
) do v
.decorator
.add_unorderedlist
(v
, self)
1255 # A markdown paragraph block.
1256 class BlockParagraph
1259 redef fun emit
(v
) do v
.decorator
.add_paragraph
(v
, self)
1266 redef fun emit
(v
) do v
.decorator
.add_ruler
(v
, self)
1269 # Xml blocks that can be found in markdown markup.
1273 redef fun emit_lines
(v
) do
1274 var line
= block
.first_line
1275 while line
!= null do
1276 if not line
.is_empty
then v
.add line
.value
1286 # Text contained in this line.
1287 var value
: String is writable
1289 # Is this line empty?
1290 # Lines containing only spaces are considered empty.
1291 var is_empty
: Bool = true is writable
1293 # Previous line in `MDBlock` or null if first line.
1294 var prev
: nullable MDLine = null is writable
1296 # Next line in `MDBlock` or null if last line.
1297 var next
: nullable MDLine = null is writable
1299 # Is the previous line empty?
1300 var prev_empty
: Bool = false is writable
1302 # Is the next line empty?
1303 var next_empty
: Bool = false is writable
1305 # Initialize a new MDLine from its string value
1307 self.leading
= process_leading
1308 if leading
!= value
.length
then
1309 self.is_empty
= false
1310 self.trailing
= process_trailing
1314 # Set `value` as an empty String and update `leading`, `trailing` and is_`empty`.
1320 if prev
!= null then prev
.next_empty
= true
1321 if next
!= null then next
.prev_empty
= true
1324 # Number or leading spaces on this line.
1325 var leading
: Int = 0 is writable
1327 # Compute `leading` depending on `value`.
1328 fun process_leading
: Int do
1330 var value
= self.value
1331 while count
< value
.length
and value
[count
] == ' ' do count
+= 1
1332 if leading
== value
.length
then clear
1336 # Number of trailing spaces on this line.
1337 var trailing
: Int = 0 is writable
1339 # Compute `trailing` depending on `value`.
1340 fun process_trailing
: Int do
1342 var value
= self.value
1343 while value
[value
.length
- count
- 1] == ' ' do
1349 # Count the amount of `ch` in this line.
1350 # Return A value > 0 if this line only consists of `ch` end spaces.
1351 fun count_chars
(ch
: Char): Int do
1367 # Count the amount of `ch` at the start of this line ignoring spaces.
1368 fun count_chars_start
(ch
: Char): Int do
1383 # Last XML line if any.
1384 private var xml_end_line
: nullable MDLine = null
1386 # Does `value` contains valid XML markup?
1387 private fun check_html
: Bool do
1388 var tags
= new Array[String]
1389 var tmp
= new FlatBuffer
1391 if pos
+ 1 < value
.length
and value
[pos
+ 1] == '!' then
1392 if read_xml_comment
(self, pos
) > 0 then return true
1394 pos
= value
.read_xml
(tmp
, pos
, false)
1398 if not tag
.is_html_block
then
1406 var line
: nullable MDLine = self
1407 while line
!= null do
1408 while pos
< line
.value
.length
and line
.value
[pos
] != '<' do
1411 if pos
>= line
.value
.length
then
1412 if pos
- 2 >= 0 and line
.value
[pos
- 2] == '/' then
1414 if tags
.is_empty
then
1422 tmp
= new FlatBuffer
1423 var new_pos
= line
.value
.read_xml
(tmp
, pos
, false)
1426 if tag
.is_html_block
and not tag
== "hr" then
1427 if tmp
[1] == '/' then
1428 if tags
.last
!= tag
then
1436 if tags
.is_empty
then
1446 return tags
.is_empty
1451 # Read a XML comment.
1452 # Used by `check_html`.
1453 private fun read_xml_comment
(first_line
: MDLine, start
: Int): Int do
1454 var line
: nullable MDLine = first_line
1455 if start
+ 3 < line
.value
.length
then
1456 if line
.value
[2] == '-' and line
.value
[3] == '-' then
1458 while line
!= null do
1459 while pos
< line
.value
.length
and line
.value
[pos
] != '-' do
1462 if pos
== line
.value
.length
then
1466 if pos
+ 2 < line
.value
.length
then
1467 if line
.value
[pos
+ 1] == '-' and line
.value
[pos
+ 2] == '>' then
1468 first_line
.xml_end_line
= line
1480 # Extract the text of `self` without leading and trailing.
1481 fun text
: String do return value
.substring
(leading
, value
.length
- trailing
)
1488 # See `MarkdownProcessor::recurse`.
1489 fun process
(v
: MarkdownProcessor) is abstract
1492 # An empty markdown line.
1496 redef fun process
(v
) do
1497 v
.current_line
= v
.current_line
.next
1501 # A non-specific markdown construction.
1502 # Mainly used as part of another line construct such as paragraphs or lists.
1506 redef fun process
(v
) do
1507 var line
= v
.current_line
1509 var was_empty
= line
.prev_empty
1510 while line
!= null and not line
.is_empty
do
1511 var t
= v
.line_kind
(line
)
1512 if (v
.in_list
or v
.ext_mode
) and t
isa LineList then
1515 if v
.ext_mode
and (t
isa LineCode or t
isa LineFence) then
1518 if t
isa LineHeadline or t
isa LineHeadline1 or t
isa LineHeadline2 or
1519 t
isa LineHR or t
isa LineBlockquote or t
isa LineXML then
1525 if line
!= null and not line
.is_empty
then
1526 var block
= v
.current_block
.split
(line
.prev
.as(not null))
1527 if v
.in_list
and not was_empty
then
1528 block
.kind
= new BlockNone(block
)
1530 block
.kind
= new BlockParagraph(block
)
1532 v
.current_block
.remove_leading_empty_lines
1535 if line
!= null then
1536 block
= v
.current_block
.split
(line
)
1538 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1540 if v
.in_list
and (line
== null or not line
.is_empty
) and not was_empty
then
1541 block
.kind
= new BlockNone(block
)
1543 block
.kind
= new BlockParagraph(block
)
1545 v
.current_block
.remove_leading_empty_lines
1547 v
.current_line
= v
.current_block
.first_line
1551 # A line of markdown code.
1555 redef fun process
(v
) do
1556 var line
= v
.current_line
1558 while line
!= null and (line
.is_empty
or v
.line_kind
(line
) isa LineCode) do
1561 # split at block end line
1563 if line
!= null then
1564 block
= v
.current_block
.split
(line
.prev
.as(not null))
1566 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1568 block
.kind
= new BlockCode(block
)
1569 block
.remove_surrounding_empty_lines
1570 v
.current_line
= v
.current_block
.first_line
1574 # A line of raw XML.
1578 redef fun process
(v
) do
1579 var line
= v
.current_line
1580 var prev
= line
.prev
1581 if prev
!= null then v
.current_block
.split
(prev
)
1582 var block
= v
.current_block
.split
(line
.xml_end_line
.as(not null))
1583 block
.kind
= new BlockXML(block
)
1584 v
.current_block
.remove_leading_empty_lines
1585 v
.current_line
= v
.current_block
.first_line
1589 # A markdown blockquote line.
1590 class LineBlockquote
1593 redef fun process
(v
) do
1594 var line
= v
.current_line
1596 while line
!= null do
1597 if not line
.is_empty
and (line
.prev_empty
and
1598 line
.leading
== 0 and
1599 not v
.line_kind
(line
) isa LineBlockquote) then break
1604 if line
!= null then
1605 block
= v
.current_block
.split
(line
.prev
.as(not null))
1607 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1609 var kind
= new BlockQuote(block
)
1611 block
.remove_surrounding_empty_lines
1612 kind
.remove_block_quote_prefix
(block
)
1613 v
.current_line
= line
1614 v
.recurse
(block
, false)
1615 v
.current_line
= v
.current_block
.first_line
1619 # A markdown ruler line.
1623 redef fun process
(v
) do
1624 var line
= v
.current_line
1625 if line
.prev
!= null then v
.current_block
.split
(line
.prev
.as(not null))
1626 var block
= v
.current_block
.split
(line
.as(not null))
1627 block
.kind
= new BlockRuler(block
)
1628 v
.current_block
.remove_leading_empty_lines
1629 v
.current_line
= v
.current_block
.first_line
1633 # A markdown fence code line.
1637 redef fun process
(v
) do
1639 var line
= v
.current_line
.next
1640 while line
!= null do
1641 if v
.line_kind
(line
) isa LineFence then break
1644 if line
!= null then
1649 if line
!= null then
1650 block
= v
.current_block
.split
(line
.prev
.as(not null))
1652 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1654 var meta
= block
.first_line
.value
.meta_from_fence
1655 block
.kind
= new BlockFence(block
, meta
)
1656 block
.first_line
.clear
1657 var last
= block
.last_line
1658 if last
!= null and v
.line_kind
(last
) isa LineFence then
1659 block
.last_line
.clear
1661 block
.remove_surrounding_empty_lines
1662 v
.current_line
= line
1666 # A markdown headline.
1670 redef fun process
(v
) do
1671 var line
= v
.current_line
1672 var lprev
= line
.prev
1673 if lprev
!= null then v
.current_block
.split
(lprev
)
1674 var block
= v
.current_block
.split
(line
.as(not null))
1675 var kind
= new BlockHeadline(block
)
1677 kind
.transform_headline
(block
)
1678 v
.current_block
.remove_leading_empty_lines
1679 v
.current_line
= v
.current_block
.first_line
1683 # A markdown headline of level 1.
1687 redef fun process
(v
) do
1688 var line
= v
.current_line
1689 var lprev
= line
.prev
1690 if lprev
!= null then v
.current_block
.split
(lprev
)
1692 var block
= v
.current_block
.split
(line
.as(not null))
1693 var kind
= new BlockHeadline(block
)
1695 kind
.transform_headline
(block
)
1697 v
.current_block
.remove_leading_empty_lines
1698 v
.current_line
= v
.current_block
.first_line
1702 # A markdown headline of level 2.
1706 redef fun process
(v
) do
1707 var line
= v
.current_line
1708 var lprev
= line
.prev
1709 if lprev
!= null then v
.current_block
.split
(lprev
)
1711 var block
= v
.current_block
.split
(line
.as(not null))
1712 var kind
= new BlockHeadline(block
)
1714 kind
.transform_headline
(block
)
1716 v
.current_block
.remove_leading_empty_lines
1717 v
.current_line
= v
.current_block
.first_line
1721 # A markdown list line.
1722 # Mainly used to factorize code between ordered and unordered lists.
1726 redef fun process
(v
) do
1727 var line
= v
.current_line
1729 while line
!= null do
1730 var t
= v
.line_kind
(line
)
1731 if not line
.is_empty
and (line
.prev_empty
and line
.leading
== 0 and
1732 not t
isa LineList) then break
1737 if line
!= null then
1738 list
= v
.current_block
.split
(line
.prev
.as(not null))
1740 list
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1742 var kind
= block_kind
(list
)
1744 list
.first_line
.prev_empty
= false
1745 list
.last_line
.next_empty
= false
1746 list
.remove_surrounding_empty_lines
1747 list
.first_line
.prev_empty
= false
1748 list
.last_line
.next_empty
= false
1750 var block
= list
.first_block
1751 while block
!= null do
1752 block
.remove_list_indent
(v
)
1753 v
.recurse
(block
, true)
1756 kind
.expand_paragraphs
(list
)
1757 v
.current_line
= line
1760 # Create a new block kind based on this line.
1761 protected fun block_kind
(block
: MDBlock): BlockList is abstract
1763 # Extract string value from `MDLine`.
1764 protected fun extract_value
(line
: MDLine): String is abstract
1767 # An ordered list line.
1771 redef fun block_kind
(block
) do return new BlockOrderedList(block
)
1773 redef fun extract_value
(line
) do
1774 return line
.value
.substring_from
(line
.value
.index_of
('.') + 2)
1778 # An unordered list line.
1782 redef fun block_kind
(block
) do return new BlockUnorderedList(block
)
1784 redef fun extract_value
(line
) do
1785 return line
.value
.substring_from
(line
.leading
+ 2)
1789 # A token represent a character in the markdown input.
1790 # Some tokens have a specific markup behaviour that is handled here.
1791 abstract class Token
1793 # Position of `self` in markdown input.
1796 # Character found at `pos` in the markdown input.
1799 # Output that token using `MarkdownEmitter::decorator`.
1800 fun emit
(v
: MarkdownEmitter) do v
.addc char
1803 # A token without a specific meaning.
1808 # An emphasis token.
1809 abstract class TokenEm
1812 redef fun emit
(v
) do
1813 var tmp
= v
.push_buffer
1814 var b
= v
.emit_text_until
(v
.current_text
.as(not null), pos
+ 1, self)
1817 v
.decorator
.add_em
(v
, tmp
)
1825 # An emphasis star token.
1830 # An emphasis underscore token.
1831 class TokenEmUnderscore
1836 abstract class TokenStrong
1839 redef fun emit
(v
) do
1840 var tmp
= v
.push_buffer
1841 var b
= v
.emit_text_until
(v
.current_text
.as(not null), pos
+ 2, self)
1844 v
.decorator
.add_strong
(v
, tmp
)
1845 v
.current_pos
= b
+ 1
1852 # A strong star token.
1853 class TokenStrongStar
1857 # A strong underscore token.
1858 class TokenStrongUnderscore
1863 # This class is mainly used to factorize work between single and double quoted span codes.
1864 abstract class TokenCode
1867 redef fun emit
(v
) do
1868 var a
= pos
+ next_pos
+ 1
1869 var b
= v
.processor
.find_token
(v
.current_text
.as(not null), a
, self)
1871 v
.current_pos
= b
+ next_pos
1872 while a
< b
and v
.current_text
[a
] == ' ' do a
+= 1
1874 while v
.current_text
[b
- 1] == ' ' do b
-= 1
1875 v
.decorator
.add_span_code
(v
, v
.current_text
.as(not null), a
, b
)
1882 private fun next_pos
: Int is abstract
1885 # A span code token.
1886 class TokenCodeSingle
1889 redef fun next_pos
do return 0
1892 # A doubled span code token.
1893 class TokenCodeDouble
1896 redef fun next_pos
do return 1
1899 # A link or image token.
1900 # This class is mainly used to factorize work between images and links.
1901 abstract class TokenLinkOrImage
1905 var link
: nullable Text = null
1908 var name
: nullable Text = null
1911 var comment
: nullable Text = null
1913 # Is the link construct an abbreviation?
1914 var is_abbrev
= false
1916 redef fun emit
(v
) do
1917 var tmp
= new FlatBuffer
1918 var b
= check_link
(v
, tmp
, pos
, self)
1927 # Emit the hyperlink as link or image.
1928 private fun emit_hyper
(v
: MarkdownEmitter) is abstract
1930 # Check if the link is a valid link.
1931 private fun check_link
(v
: MarkdownEmitter, out
: FlatBuffer, start
: Int, token
: Token): Int do
1932 var md
= v
.current_text
1934 if token
isa TokenLink then
1939 var tmp
= new FlatBuffer
1940 pos
= md
.read_md_link_id
(tmp
, pos
)
1941 if pos
< start
then return -1
1945 pos
= md
.skip_spaces
(pos
)
1947 var tid
= name
.write_to_string
.to_lower
1948 if v
.processor
.link_refs
.has_key
(tid
) then
1949 var lr
= v
.processor
.link_refs
[tid
]
1950 is_abbrev
= lr
.is_abbrev
1957 else if md
[pos
] == '(' then
1959 pos
= md
.skip_spaces
(pos
)
1960 if pos
< start
then return -1
1961 tmp
= new FlatBuffer
1962 var use_lt
= md
[pos
] == '<'
1964 pos
= md
.read_until
(tmp
, pos
+ 1, '>')
1966 pos
= md
.read_md_link
(tmp
, pos
)
1968 if pos
< start
then return -1
1969 if use_lt
then pos
+= 1
1970 link
= tmp
.write_to_string
1971 if md
[pos
] == ' ' then
1972 pos
= md
.skip_spaces
(pos
)
1973 if pos
> start
and md
[pos
] == '"' then
1975 tmp
= new FlatBuffer
1976 pos
= md
.read_until
(tmp
, pos
, '"')
1977 if pos
< start
then return -1
1978 comment
= tmp
.write_to_string
1980 pos
= md
.skip_spaces
(pos
)
1981 if pos
== -1 then return -1
1984 if md
[pos
] != ')' then return -1
1985 else if md
[pos
] == '[' then
1987 tmp
= new FlatBuffer
1988 pos
= md
.read_raw_until
(tmp
, pos
, ']')
1989 if pos
< start
then return -1
1991 if tmp
.length
> 0 then
1996 var tid
= id
.write_to_string
.to_lower
1997 if v
.processor
.link_refs
.has_key
(tid
) then
1998 var lr
= v
.processor
.link_refs
[tid
]
2003 var tid
= name
.write_to_string
.replace
("\n", " ").to_lower
2004 if v
.processor
.link_refs
.has_key
(tid
) then
2005 var lr
= v
.processor
.link_refs
[tid
]
2013 if link
== null then return -1
2018 # A markdown link token.
2020 super TokenLinkOrImage
2022 redef fun emit_hyper
(v
) do
2023 if is_abbrev
and comment
!= null then
2024 v
.decorator
.add_abbr
(v
, name
.as(not null), comment
.as(not null))
2026 v
.decorator
.add_link
(v
, link
.as(not null), name
.as(not null), comment
)
2031 # A markdown image token.
2033 super TokenLinkOrImage
2035 redef fun emit_hyper
(v
) do
2036 v
.decorator
.add_image
(v
, link
.as(not null), name
.as(not null), comment
)
2044 redef fun emit
(v
) do
2045 var tmp
= new FlatBuffer
2046 var b
= check_html
(v
, tmp
, v
.current_text
.as(not null), v
.current_pos
)
2051 v
.decorator
.escape_char
(v
, char
)
2055 # Is the HTML valid?
2056 # Also take care of link and mailto shortcuts.
2057 private fun check_html
(v
: MarkdownEmitter, out
: FlatBuffer, md
: Text, start
: Int): Int do
2058 # check for auto links
2059 var tmp
= new FlatBuffer
2060 var pos
= md
.read_until
(tmp
, start
+ 1, ':', ' ', '>', '\n')
2061 if pos
!= -1 and md
[pos
] == ':' and tmp
.is_link_prefix
then
2062 pos
= md
.read_until
(tmp
, pos
, '>')
2064 var link
= tmp
.write_to_string
2065 v
.decorator
.add_link
(v
, link
, link
, null)
2069 # TODO check for mailto
2070 # check for inline html
2071 if start
+ 2 < md
.length
then
2072 return md
.read_xml
(out
, start
, true)
2078 # An HTML entity token.
2082 redef fun emit
(v
) do
2083 var tmp
= new FlatBuffer
2084 var b
= check_entity
(tmp
, v
.current_text
.as(not null), pos
)
2089 v
.decorator
.escape_char
(v
, char
)
2093 # Is the entity valid?
2094 private fun check_entity
(out
: FlatBuffer, md
: Text, start
: Int): Int do
2095 var pos
= md
.read_until
(out
, start
, ';')
2096 if pos
< 0 or out
.length
< 3 then
2099 if out
[1] == '#' then
2100 if out
[2] == 'x' or out
[2] == 'X' then
2101 if out
.length
< 4 then return -1
2102 for i
in [3..out
.length
[ do
2104 if (c
< '0' or c
> '9') and (c
< 'a' and c
> 'f') and (c
< 'A' and c
> 'F') then
2109 for i
in [2..out
.length
[ do
2111 if c
< '0' or c
> '9' then return -1
2116 for i
in [1..out
.length
[ do
2118 if not c
.is_digit
and not c
.is_letter
then return -1
2121 # TODO check entity is valid
2122 # if out.is_entity then
2132 # A markdown escape token.
2136 redef fun emit
(v
) do
2138 v
.addc v
.current_text
[v
.current_pos
]
2142 # A markdown strike token.
2144 # Extended mode only (see `MarkdownProcessor::ext_mode`)
2148 redef fun emit
(v
) do
2149 var tmp
= v
.push_buffer
2150 var b
= v
.emit_text_until
(v
.current_text
.as(not null), pos
+ 2, self)
2153 v
.decorator
.add_strike
(v
, tmp
)
2154 v
.current_pos
= b
+ 1
2163 # Get the position of the next non-space character.
2164 private fun skip_spaces
(start
: Int): Int do
2166 while pos
> -1 and pos
< length
and (self[pos
] == ' ' or self[pos
] == '\n') do
2169 if pos
< length
then return pos
2173 # Read `self` until `nend` and append it to the `out` buffer.
2174 # Escape markdown special chars.
2175 private fun read_until
(out
: FlatBuffer, start
: Int, nend
: Char...): Int do
2177 while pos
< length
do
2179 if c
== '\\' and pos
+ 1 < length
then
2180 pos
= escape
(out
, self[pos
+ 1], pos
)
2182 var end_reached
= false
2189 if end_reached
then break
2194 if pos
== length
then return -1
2198 # Read `self` as raw text until `nend` and append it to the `out` buffer.
2199 # No escape is made.
2200 private fun read_raw_until
(out
: FlatBuffer, start
: Int, nend
: Char...): Int do
2202 while pos
< length
do
2204 var end_reached
= false
2211 if end_reached
then break
2215 if pos
== length
then return -1
2219 # Read `self` as XML until `to` and append it to the `out` buffer.
2220 # Escape HTML special chars.
2221 private fun read_xml_until
(out
: FlatBuffer, from
: Int, to
: Char...): Int do
2224 var str_char
: nullable Char = null
2225 while pos
< length
do
2231 if pos
< length
then
2237 if c
== str_char
then
2244 if c
== '"' or c
== '\'' then
2249 var end_reached = false
2250 for n in [0..to.length[ do
2256 if end_reached then break
2261 if pos == length then return -1
2265 # Read `self` as XML and append it to the `out` buffer.
2266 # Safe mode can be activated to limit reading to valid xml.
2267 private fun read_xml(out: FlatBuffer, start: Int, safe_mode: Bool): Int do
2269 var is_close_tag = false
2270 if start + 1 >= length then return -1
2271 if self[start + 1] == '/' then
2274 else if self[start + 1] == '!' then
2278 is_close_tag = false
2282 var tmp = new FlatBuffer
2283 pos = read_xml_until(tmp, pos, ' ', '/', '>')
2284 if pos == -1 then return -1
2285 var tag = tmp.write_to_string.trim.to_lower
2286 if tag.is_html_unsafe then
2288 if is_close_tag then out.add '/'
2292 if is_close_tag then out.add '/'
2297 if is_close_tag then out.add '/'
2298 pos = read_xml_until(out, pos, ' ', '/', '>')
2300 if pos == -1 then return -1
2301 pos = read_xml_until(out, pos, '/', '>')
2302 if pos == -1 then return -1
2303 if self[pos] == '/' then
2305 pos = self.read_xml_until(out, pos + 1, '>')
2306 if pos == -1 then return -1
2308 if self[pos] == '>' then
2315 # Read a markdown link address and append it to the `out` buffer.
2316 private fun read_md_link(out: FlatBuffer, start: Int): Int do
2319 while pos < length do
2321 if c == '\\
' and pos + 1 < length then
2322 pos = escape(out, self[pos + 1], pos)
2324 var end_reached = false
2327 else if c == ' ' then
2328 if counter == 1 then end_reached = true
2329 else if c == ')' then
2331 if counter == 0 then end_reached = true
2333 if end_reached then break
2338 if pos == length then return -1
2342 # Read a markdown link text and append it to the `out` buffer.
2343 private fun read_md_link_id(out: FlatBuffer, start: Int): Int do
2346 while pos < length do
2348 var end_reached = false
2352 else if c == ']' then
2354 if counter == 0 then
2362 if end_reached then break
2365 if pos == length then return -1
2369 # Extract the XML tag name from a XML tag.
2370 private fun xml_tag: String do
2371 var tpl = new FlatBuffer
2373 if pos < length and self[1] == '/' then pos += 1
2374 while pos < length - 1 and (self[pos].is_digit or self[pos].is_letter) do
2378 return tpl.write_to_string.to_lower
2381 # Read and escape the markdown contained in `self`.
2382 private fun escape(out: FlatBuffer, c: Char, pos: Int): Int do
2383 if c == '\\
' or c == '[' or c == ']' or c == '(' or c == ')' or c == '{' or
2384 c == '}' or c == '#' or c == '"' or c == '\'' or c == '.' or c == '<' or
2385 c
== '>' or c
== '*' or c
== '+' or c
== '-' or c
== '_' or c
== '!' or
2386 c
== '`' or c
== '~' or c
== '^' then
2394 # Extract string found at end of fence opening.
2395 private fun meta_from_fence
: nullable Text do
2396 for i
in [0..chars
.length
[ do
2399 if c
!= ' ' and c
!= '`' and c
!= '~' then
2400 return substring_from
(i
).trim
2406 # Is `self` an unsafe HTML element?
2407 private fun is_html_unsafe
: Bool do return html_unsafe_tags
.has
(self.write_to_string
)
2409 # Is `self` a HRML block element?
2410 private fun is_html_block
: Bool do return html_block_tags
.has
(self.write_to_string
)
2412 # Is `self` a link prefix?
2413 private fun is_link_prefix
: Bool do return html_link_prefixes
.has
(self.write_to_string
)
2415 private fun html_unsafe_tags
: Array[String] do return once
["applet", "head", "body", "frame", "frameset", "iframe", "script", "object"]
2417 private fun html_block_tags
: Array[String] do return once
["address", "article", "aside", "audio", "blockquote", "canvas", "dd", "div", "dl", "fieldset", "figcaption", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "noscript", "ol", "output", "p", "pre", "section", "table", "tfoot", "ul", "video"]
2419 private fun html_link_prefixes
: Array[String] do return once
["http", "https", "ftp", "ftps"]
2424 # Parse `self` as markdown and return the HTML representation
2426 # var md = "**Hello World!**"
2427 # var html = md.md_to_html
2428 # assert html == "<p><strong>Hello World!</strong></p>\n"
2429 fun md_to_html
: Streamable do
2430 var processor
= new MarkdownProcessor
2431 return processor
.process
(self)