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 init do self.emitter
= new MarkdownEmitter(self)
38 # Process the mardown `input` string and return the processed output.
39 fun process
(input
: String): Streamable do
46 var parent
= read_lines
(input
)
47 parent
.remove_surrounding_empty_lines
48 recurse
(parent
, false)
49 # output processed text
50 return emitter
.emit
(parent
.kind
)
53 # Split `input` string into `MDLines` and create a parent `MDBlock` with it.
54 private fun read_lines
(input
: String): MDBlock do
55 var block
= new MDBlock
56 var value
= new FlatBuffer
58 while i
< input
.length
do
62 while not eol
and i
< input
.length
do
67 else if c
== '\t' then
68 var np
= pos
+ (4 - (pos
.bin_and
(3)))
81 var line
= new MDLine(value
.write_to_string
)
82 var is_link_ref
= check_link_ref
(line
)
84 if not is_link_ref
then block
.add_line line
89 # Check if line is a block link definition.
90 # Return `true` if line contains a valid link ref and save it into `link_refs`.
91 private fun check_link_ref
(line
: MDLine): Bool do
93 var is_link_ref
= false
94 var id
= new FlatBuffer
95 var link
= new FlatBuffer
96 var comment
= new FlatBuffer
98 if not line
.is_empty
and line
.leading
< 4 and line
.value
[line
.leading
] == '[' then
99 pos
= line
.leading
+ 1
100 pos
= md
.read_until
(id
, pos
, ']')
101 if not id
.is_empty
and pos
+ 2 < line
.value
.length
then
102 if line
.value
[pos
+ 1] == ':' then
104 pos
= md
.skip_spaces
(pos
)
105 if line
.value
[pos
] == '<' then
107 pos
= md
.read_until
(link
, pos
, '>')
110 pos
= md
.read_until
(link
, pos
, ' ', '\n')
112 if not link
.is_empty
then
113 pos
= md
.skip_spaces
(pos
)
114 if pos
> 0 and pos
< line
.value
.length
then
115 var c
= line
.value
[pos
]
116 if c
== '\"' or c
== '\'' or c == '(' then
119 pos = md.read_until(comment, pos, ')')
121 pos = md.read_until(comment, pos, c)
123 if pos > 0 then is_link_ref = true
132 if is_link_ref and not id.is_empty and not link.is_empty then
133 var lr = new LinkRef.with_title(link.write_to_string, comment.write_to_string)
134 add_link_ref(id.write_to_string, lr)
135 if comment.is_empty then last_link_ref = lr
138 comment = new FlatBuffer
139 if not line.is_empty and last_link_ref != null then
141 var c = line.value[pos]
142 if c == '\
"' or c == '\'' or c == '(' then
145 pos = md.read_until(comment, pos, ')')
147 pos = md.read_until(comment, pos, c)
150 if not comment.is_empty then last_link_ref.title = comment.write_to_string
152 if comment.is_empty then return false
158 # This list will be needed during output to expand links.
159 var link_refs: Map[String, LinkRef] = new HashMap[String, LinkRef]
161 # Last encountered link ref (for multiline definitions)
163 # Markdown allows link refs to be defined over two lines:
165 # [id]: http://example.com/longish/path/to/resource/here
166 # "Optional Title Here"
168 private var last_link_ref: nullable LinkRef = null
170 # Add a link ref to the list
171 fun add_link_ref(key: String, ref: LinkRef) do link_refs[key.to_lower] = ref
173 # Recursively split a `block`.
175 # The block is splitted according to the type of lines it contains.
176 # Some blocks can be splited again recursively like lists.
177 # The `in_list` mode is used to recurse on list and build
178 # nested paragraphs or code blocks.
179 fun recurse(root: MDBlock, in_list: Bool) do
180 var old_mode = self.in_list
181 var old_root = self.current_block
182 self.in_list = in_list
184 var line = root.first_line
185 while line != null and line.is_empty do
187 if line == null then return
192 while current_line != null do
193 line_kind(current_line.as(not null)).process(self)
195 self.in_list = old_mode
196 self.current_block = old_root
199 # Currently processed line.
200 # Used when visiting blocks with `recurse`.
201 var current_line: nullable MDLine = null is writable
203 # Currently processed block.
204 # Used when visiting blocks with `recurse`.
205 var current_block: nullable MDBlock = null is writable
207 # Is the current recursion in list mode?
208 # Used when visiting blocks with `recurse`
209 private var in_list = false
213 fun line_kind(md: MDLine): Line do
215 var leading = md.leading
216 var trailing = md.trailing
217 if md.is_empty then return new LineEmpty
218 if md.leading > 3 then return new LineCode
219 if value[leading] == '#' then return new LineHeadline
220 if value[leading] == '>' then return new LineBlockquote
222 if value.length - leading - trailing > 2 then
223 if value[leading] == '`' and md.count_chars_start('`') >= 3 then
226 if value[leading] == '~' and md.count_chars_start('~') >= 3 then
231 if value.length - leading - trailing > 2 and
232 (value[leading] == '*' or value[leading] == '-' or value[leading] == '_') then
233 if md.count_chars(value[leading]) >= 3 then
238 if value.length - leading >= 2 and value[leading + 1] == ' ' then
239 var c = value[leading]
240 if c == '*' or c == '-' or c == '+' then return new LineUList
243 if value.length - leading >= 3 and value[leading].is_digit then
245 while i < value.length and value[i].is_digit do i += 1
246 if i + 1 < value.length and value[i] == '.' and value[i + 1] == ' ' then
251 if value[leading] == '<' and md.check_html then return new LineXML
254 if next != null and not next.is_empty then
255 if next.count_chars('=') > 0 then
256 return new LineHeadline1
258 if next.count_chars('-') > 0 then
259 return new LineHeadline2
265 # Get the token kind at `pos`.
266 fun token_at(text: Text, pos: Int): Token do
278 if pos + 1 < text.length then
283 if pos + 2 < text.length then
291 if c0 != ' ' or c2 != ' ' then
292 return new TokenStrongStar(pos, c)
294 return new TokenEmStar(pos, c)
297 if c0 != ' ' or c1 != ' ' then
298 return new TokenEmStar(pos, c)
300 return new TokenNone(pos, c)
302 else if c == '_' then
304 if c0 != ' ' or c2 != ' 'then
305 return new TokenStrongUnderscore(pos, c)
307 return new TokenEmUnderscore(pos, c)
310 if c0 != ' ' or c1 != ' ' then
311 return new TokenEmUnderscore(pos, c)
313 return new TokenNone(pos, c)
315 else if c == '!' then
316 if c1 == '[' then return new TokenImage(pos, c)
317 return new TokenNone(pos, c)
318 else if c == '[' then
319 return new TokenLink(pos, c)
320 else if c == ']' then
321 return new TokenNone(pos, c)
322 else if c == '`' then
324 return new TokenCodeDouble(pos, c)
326 return new TokenCodeSingle(pos, c)
328 else if c == '\\' then
329 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
330 return new TokenEscape(pos
, c
)
332 return new TokenNone(pos
, c
)
334 else if c
== '<' then
335 return new TokenHTML(pos
, c
)
336 else if c
== '&' then
337 return new TokenEntity(pos
, c
)
339 return new TokenNone(pos
, c
)
343 # Find the position of a `token` in `self`.
344 fun find_token
(text
: Text, start
: Int, token
: Token): Int do
346 while pos
< text
.length
do
347 if token_at
(text
, pos
).is_same_type
(token
) then
356 # Emit output corresponding to blocks content.
358 # Blocks are created by a previous pass in `MarkdownProcessor`.
359 # The emitter use a `Decorator` to select the output format.
360 class MarkdownEmitter
362 # Processor containing link refs.
363 var processor
: MarkdownProcessor
365 # Decorator used for output.
366 # Default is `HTMLDecorator`
367 var decorator
: Decorator = new HTMLDecorator is writable
369 # Create a new `MarkdownEmitter` using a custom `decorator`.
370 init with_decorator
(processor
: MarkdownProcessor, decorator
: Decorator) do
372 self.decorator
= decorator
375 # Output `block` using `decorator` in the current buffer.
376 fun emit
(block
: Block): Text do
377 var buffer
= push_buffer
383 # Output the content of `block`.
384 fun emit_in
(block
: Block) do block
.emit_in
(self)
386 # Transform and emit mardown text
387 fun emit_text
(text
: Text) do
388 emit_text_until
(text
, 0, null)
391 # Transform and emit mardown text starting at `from` and
392 # until a token with the same type as `token` is found.
393 # Go until the end of text if `token` is null.
394 fun emit_text_until
(text
: Text, start
: Int, token
: nullable Token): Int do
395 var old_text
= current_text
396 var old_pos
= current_pos
399 while current_pos
< text
.length
do
400 var mt
= processor
.token_at
(text
, current_pos
)
401 if (token
!= null and not token
isa TokenNone) and
402 (mt
.is_same_type
(token
) or
403 (token
isa TokenEmStar and mt
isa TokenStrongStar) or
404 (token
isa TokenEmUnderscore and mt
isa TokenStrongUnderscore)) then
410 current_text
= old_text
411 current_pos
= old_pos
415 # Currently processed position in `current_text`.
416 # Used when visiting inline production with `emit_text_until`.
417 private var current_pos
: Int = -1
419 # Currently processed text.
420 # Used when visiting inline production with `emit_text_until`.
421 private var current_text
: nullable Text = null
424 private var buffer_stack
= new List[FlatBuffer]
426 # Push a new buffer on the stack.
427 private fun push_buffer
: FlatBuffer do
428 var buffer
= new FlatBuffer
429 buffer_stack
.add buffer
433 # Pop the last buffer.
434 private fun pop_buffer
do buffer_stack
.pop
436 # Current output buffer.
437 private fun current_buffer
: FlatBuffer do
438 assert not buffer_stack
.is_empty
439 return buffer_stack
.last
442 # Append `e` to current buffer.
443 fun add
(e
: Streamable) do
445 current_buffer
.append e
447 current_buffer
.append e
.write_to_string
451 # Append `c` to current buffer.
452 fun addc
(c
: Char) do current_buffer
.add c
454 # Append a "\n" line break.
455 fun addn
do current_buffer
.add
'\n'
459 # Links that are specified somewhere in the mardown document to be reused as shortcuts.
463 # [1]: http://example.com/ "Optional title"
469 # Optional link title
470 var title
: nullable String = null
472 # Is the link an abreviation?
473 var is_abbrev
= false
475 # Create a link with a title.
476 init with_title
(link
: String, title
: nullable String) do
482 # A `Decorator` is used to emit mardown into a specific format.
483 # Default decorator used is `HTMLDecorator`.
486 # Render a ruler block.
487 fun add_ruler
(v
: MarkdownEmitter, block
: BlockRuler) is abstract
489 # Render a headline block with corresponding level.
490 fun add_headline
(v
: MarkdownEmitter, block
: BlockHeadline) is abstract
492 # Render a paragraph block.
493 fun add_paragraph
(v
: MarkdownEmitter, block
: BlockParagraph) is abstract
495 # Render a code or fence block.
496 fun add_code
(v
: MarkdownEmitter, block
: BlockCode) is abstract
498 # Render a blockquote.
499 fun add_blockquote
(v
: MarkdownEmitter, block
: BlockQuote) is abstract
501 # Render an unordered list.
502 fun add_unorderedlist
(v
: MarkdownEmitter, block
: BlockUnorderedList) is abstract
504 # Render an ordered list.
505 fun add_orderedlist
(v
: MarkdownEmitter, block
: BlockOrderedList) is abstract
507 # Render a list item.
508 fun add_listitem
(v
: MarkdownEmitter, block
: BlockListItem) is abstract
510 # Render an emphasis text.
511 fun add_em
(v
: MarkdownEmitter, text
: Text) is abstract
513 # Render a strong text.
514 fun add_strong
(v
: MarkdownEmitter, text
: Text) is abstract
517 fun add_link
(v
: MarkdownEmitter, link
: Text, name
: Text, comment
: nullable Text) is abstract
520 fun add_image
(v
: MarkdownEmitter, link
: Text, name
: Text, comment
: nullable Text) is abstract
522 # Render an abbreviation.
523 fun add_abbr
(v
: MarkdownEmitter, name
: Text, comment
: Text) is abstract
525 # Render a code span reading from a buffer.
526 fun add_span_code
(v
: MarkdownEmitter, buffer
: Text, from
, to
: Int) is abstract
528 # Render a text and escape it.
529 fun append_value
(v
: MarkdownEmitter, value
: Text) is abstract
531 # Render code text from buffer and escape it.
532 fun append_code
(v
: MarkdownEmitter, buffer
: Text, from
, to
: Int) is abstract
534 # Render a character escape.
535 fun escape_char
(v
: MarkdownEmitter, char
: Char) is abstract
537 # Render a line break
538 fun add_line_break
(v
: MarkdownEmitter) is abstract
540 # Generate a new html valid id from a `String`.
541 fun strip_id
(txt
: String): String is abstract
543 # Found headlines during the processing labeled by their ids.
544 fun headlines
: ArrayMap[String, HeadLine] is abstract
547 # Class representing a markdown headline.
549 # Unique identifier of this headline.
552 # Text of the headline.
555 # Level of this headline.
557 # According toe the markdown specification, level must be in `[1..6]`.
561 # `Decorator` that outputs HTML.
565 redef var headlines
= new ArrayMap[String, HeadLine]
567 redef fun add_ruler
(v
, block
) do v
.add
"<hr/>\n"
569 redef fun add_headline
(v
, block
) do
571 var txt
= block
.block
.first_line
.value
572 var id
= strip_id
(txt
)
573 var lvl
= block
.depth
574 headlines
[id
] = new HeadLine(id
, txt
, lvl
)
576 v
.add
"<h{lvl} id=\"{id}\
">"
581 redef fun add_paragraph
(v
, block
) do
587 redef fun add_code
(v
, block
) do
590 v
.add
"</code></pre>\n"
593 redef fun add_blockquote
(v
, block
) do
594 v
.add
"<blockquote>\n"
596 v
.add
"</blockquote>\n"
599 redef fun add_unorderedlist
(v
, block
) do
605 redef fun add_orderedlist
(v
, block
) do
611 redef fun add_listitem
(v
, block
) do
617 redef fun add_em
(v
, text
) do
623 redef fun add_strong
(v
, text
) do
629 redef fun add_image
(v
, link
, name
, comment
) do
631 append_value(v, link)
633 append_value(v, name)
635 if comment
!= null and not comment
.is_empty
then
637 append_value(v, comment)
643 redef fun add_link
(v
, link
, name
, comment
) do
645 append_value(v, link)
647 if comment
!= null and not comment
.is_empty
then
649 append_value(v, comment)
657 redef fun add_abbr
(v
, name
, comment
) do
658 v
.add
"<abbr title=\""
659 append_value(v, comment)
665 redef fun add_span_code
(v
, text
, from
, to
) do
667 append_code
(v
, text
, from
, to
)
671 redef fun add_line_break
(v
) do
675 redef fun append_value
(v
, text
) do for c
in text
do escape_char
(v
, c
)
677 redef fun escape_char
(v
, c
) do
680 else if c
== '<' then
682 else if c
== '>' then
684 else if c
== '"' then
686 else if c
== '\'' then
693 redef fun append_code(v, buffer, from, to) do
694 for i in [from..to[ do
698 else if c == '<' then
700 else if c == '>' then
708 redef fun strip_id(txt) do
710 var b = new FlatBuffer
715 if not c.is_letter and
717 not allowed_id_chars.has(c) then continue
723 # check for multiple id definitions
724 if headlines.has_key(key) then
727 while headlines.has_key(key) do
735 private var allowed_id_chars: Array[Char] = ['-', '_
', ':', '.']
738 # A block of markdown lines.
739 # A `MDBlock` can contains lines and/or sub-blocks.
743 var kind: Block = new BlockNone(self) is writable
746 var first_line: nullable MDLine = null is writable
749 var last_line: nullable MDLine = null is writable
751 # First sub-block if any.
752 var first_block: nullable MDBlock = null is writable
754 # Last sub-block if any.
755 var last_block: nullable MDBlock = null is writable
757 # Previous block if any.
758 var prev: nullable MDBlock = null is writable
761 var next: nullable MDBlock = null is writable
763 # Does this block contain subblocks?
764 fun has_blocks: Bool do return first_block != null
767 fun count_blocks: Int do
769 var block = first_block
770 while block != null do
777 # Does this block contain lines?
778 fun has_lines: Bool do return first_line != null
781 fun count_lines: Int do
783 var line = first_line
784 while line != null do
791 # Split `self` creating a new sub-block having `line` has `last_line`.
792 fun split(line: MDLine): MDBlock do
793 var block = new MDBlock
794 block.first_line = first_line
795 block.last_line = line
796 first_line = line.next
798 if first_line == null then
801 first_line.prev = null
803 if first_block == null then
807 last_block.next = block
813 # Add a `line` to this block.
814 fun add_line(line: MDLine) do
815 if last_line == null then
819 last_line.next_empty = line.is_empty
820 line.prev_empty = last_line.is_empty
821 line.prev = last_line
822 last_line.next = line
827 # Remove `line` from this block.
828 fun remove_line(line: MDLine) do
829 if line.prev == null then
830 first_line = line.next
832 line.prev.next = line.next
834 if line.next == null then
835 last_line = line.prev
837 line.next.prev = line.prev
843 # Remove leading empty lines.
844 fun remove_leading_empty_lines: Bool do
845 var was_empty = false
846 var line = first_line
847 while line != null and line.is_empty do
855 # Remove trailing empty lines.
856 fun remove_trailing_empty_lines: Bool do
857 var was_empty = false
859 while line != null and line.is_empty do
867 # Remove leading and trailing empty lines.
868 fun remove_surrounding_empty_lines: Bool do
869 var was_empty = false
870 if remove_leading_empty_lines then was_empty = true
871 if remove_trailing_empty_lines then was_empty = true
875 # Remove list markers and up to 4 leading spaces.
876 # Used to clean nested lists.
877 fun remove_list_indent(v: MarkdownProcessor) do
878 var line = first_line
879 while line != null do
880 if not line.is_empty then
881 var kind = v.line_kind(line)
882 if kind isa LineList then
883 line.value = kind.extract_value(line)
885 line.value = line.value.substring_from(line.leading.min(4))
887 line.leading = line.process_leading
893 # Collect block line text.
895 var text = new FlatBuffer
896 var line = first_line
897 while line != null do
898 if not line.is_empty then
899 text.append line.text
904 return text.write_to_string
908 # Representation of a markdown block in the AST.
909 # Each `Block` is linked to a `MDBlock` that contains mardown code.
912 # The markdown block `self` is related to.
915 # Output `self` using `v.decorator`.
916 fun emit(v: MarkdownEmitter) do v.emit_in(self)
918 # Emit the containts of `self`, lines or blocks.
919 fun emit_in(v: MarkdownEmitter) do
920 block.remove_surrounding_empty_lines
921 if block.has_lines then
928 # Emit lines contained in `block`.
929 fun emit_lines(v: MarkdownEmitter) do
930 var tpl = v.push_buffer
931 var line = block.first_line
932 while line != null do
933 if not line.is_empty then
934 v.add line.value.substring(line.leading, line.value.length - line.trailing)
935 if line.trailing >= 2 then v.decorator.add_line_break(v)
937 if line.next != null then
946 # Emit sub-blocks contained in `block`.
947 fun emit_blocks(v: MarkdownEmitter) do
948 var block = self.block.first_block
949 while block != null do
956 # A block without any markdown specificities.
958 # Actually use the same implementation than `BlockCode`,
959 # this class is only used for typing purposes.
964 # A markdown blockquote.
968 redef fun emit(v) do v.decorator.add_blockquote(v, self)
970 # Remove blockquote markers.
971 private fun remove_block_quote_prefix(block: MDBlock) do
972 var line = block.first_line
973 while line != null do
974 if not line.is_empty then
975 if line.value[line.leading] == '>' then
976 var rem = line.leading + 1
977 if line.leading + 1 < line.value.length and
978 line.value[line.leading + 1] == ' ' then
981 line.value = line.value.substring_from(rem)
982 line.leading = line.process_leading
990 # A markdown code block.
994 # Number of char to skip at the beginning of the line.
996 # Block code lines start at 4 spaces.
997 protected var line_start = 4
999 redef fun emit(v) do v.decorator.add_code(v, self)
1001 redef fun emit_lines(v) do
1002 var line = block.first_line
1003 while line != null do
1004 if not line.is_empty then
1005 v.decorator.append_code(v, line.value, line_start, line.value.length)
1013 # A markdown code-fence block.
1015 # Actually use the same implementation than `BlockCode`,
1016 # this class is only used for typing purposes.
1020 # Fence code lines start at 0 spaces.
1021 redef var line_start = 0
1024 # A markdown headline.
1028 redef fun emit(v) do v.decorator.add_headline(v, self)
1030 # Depth of the headline used to determine the headline level.
1033 # Remove healine marks from lines contained in `self`.
1034 private fun transform_headline(block: MDBlock) do
1035 if depth > 0 then return
1037 var line = block.first_line
1038 if line.is_empty then return
1039 var start = line.leading
1040 while start < line.value.length and line.value[start] == '#' do
1044 while start
< line
.value
.length
and line
.value
[start
] == ' ' do
1047 if start
>= line
.value
.length
then
1048 line
.is_empty
= true
1050 var nend
= line
.value
.length
- line
.trailing
- 1
1051 while line
.value
[nend
] == '#' do nend
-= 1
1052 while line
.value
[nend
] == ' ' do nend
-= 1
1053 line
.value
= line
.value
.substring
(start
, nend
- start
+ 1)
1057 depth
= level
.min
(6)
1061 # A markdown list item block.
1065 redef fun emit
(v
) do v
.decorator
.add_listitem
(v
, self)
1068 # A markdown list block.
1069 # Can be either an ordered or unordered list, this class is mainly used to factorize code.
1070 abstract class BlockList
1073 # Split list block into list items sub-blocks.
1074 private fun init_block
(v
: MarkdownProcessor) do
1075 var line
= block
.first_line
1077 while line
!= null do
1078 var t
= v
.line_kind
(line
)
1079 if t
isa LineList or
1080 (not line
.is_empty
and (line
.prev_empty
and line
.leading
== 0 and
1081 not (t
isa LineList))) then
1082 var sblock
= block
.split
(line
.prev
.as(not null))
1083 sblock
.kind
= new BlockListItem(sblock
)
1087 var sblock
= block
.split
(block
.last_line
.as(not null))
1088 sblock
.kind
= new BlockListItem(sblock
)
1091 # Expand list items as paragraphs if needed.
1092 private fun expand_paragraphs
(block
: MDBlock) do
1093 var outer
= block
.first_block
1094 var inner
: nullable MDBlock
1095 var has_paragraph
= false
1096 while outer
!= null and not has_paragraph
do
1097 if outer
.kind
isa BlockListItem then
1098 inner
= outer
.first_block
1099 while inner
!= null and not has_paragraph
do
1100 if inner
.kind
isa BlockParagraph then
1101 has_paragraph
= true
1108 if has_paragraph
then
1109 outer
= block
.first_block
1110 while outer
!= null do
1111 if outer
.kind
isa BlockListItem then
1112 inner
= outer
.first_block
1113 while inner
!= null do
1114 if inner
.kind
isa BlockNone then
1115 inner
.kind
= new BlockParagraph(inner
)
1126 # A markdown ordered list.
1127 class BlockOrderedList
1130 redef fun emit
(v
) do v
.decorator
.add_orderedlist
(v
, self)
1133 # A markdown unordred list.
1134 class BlockUnorderedList
1137 redef fun emit
(v
) do v
.decorator
.add_unorderedlist
(v
, self)
1140 # A markdown paragraph block.
1141 class BlockParagraph
1144 redef fun emit
(v
) do v
.decorator
.add_paragraph
(v
, self)
1151 redef fun emit
(v
) do v
.decorator
.add_ruler
(v
, self)
1154 # Xml blocks that can be found in markdown markup.
1158 redef fun emit_lines
(v
) do
1159 var line
= block
.first_line
1160 while line
!= null do
1161 if not line
.is_empty
then v
.add line
.value
1171 # Text contained in this line.
1172 var value
: String is writable
1174 # Is this line empty?
1175 # Lines containing only spaces are considered empty.
1176 var is_empty
: Bool = true is writable
1178 # Previous line in `MDBlock` or null if first line.
1179 var prev
: nullable MDLine = null is writable
1181 # Next line in `MDBlock` or null if last line.
1182 var next
: nullable MDLine = null is writable
1184 # Is the previous line empty?
1185 var prev_empty
: Bool = false is writable
1187 # Is the next line empty?
1188 var next_empty
: Bool = false is writable
1190 # Initialize a new MDLine from its string value
1192 self.leading
= process_leading
1193 if leading
!= value
.length
then
1194 self.is_empty
= false
1195 self.trailing
= process_trailing
1199 # Set `value` as an empty String and update `leading`, `trailing` and is_`empty`.
1205 if prev
!= null then prev
.next_empty
= true
1206 if next
!= null then next
.prev_empty
= true
1209 # Number or leading spaces on this line.
1210 var leading
: Int = 0 is writable
1212 # Compute `leading` depending on `value`.
1213 fun process_leading
: Int do
1215 var value
= self.value
1216 while count
< value
.length
and value
[count
] == ' ' do count
+= 1
1217 if leading
== value
.length
then clear
1221 # Number of trailing spaces on this line.
1222 var trailing
: Int = 0 is writable
1224 # Compute `trailing` depending on `value`.
1225 fun process_trailing
: Int do
1227 var value
= self.value
1228 while value
[value
.length
- count
- 1] == ' ' do
1234 # Count the amount of `ch` in this line.
1235 # Return A value > 0 if this line only consists of `ch` end spaces.
1236 fun count_chars
(ch
: Char): Int do
1252 # Count the amount of `ch` at the start of this line ignoring spaces.
1253 fun count_chars_start
(ch
: Char): Int do
1268 # Last XML line if any.
1269 private var xml_end_line
: nullable MDLine = null
1271 # Does `value` contains valid XML markup?
1272 private fun check_html
: Bool do
1273 var tags
= new Array[String]
1274 var tmp
= new FlatBuffer
1276 if pos
+ 1 < value
.length
and value
[pos
+ 1] == '!' then
1277 if read_xml_comment
(self, pos
) > 0 then return true
1279 pos
= value
.read_xml
(tmp
, pos
, false)
1283 if not tag
.is_html_block
then
1291 var line
: nullable MDLine = self
1292 while line
!= null do
1293 while pos
< line
.value
.length
and line
.value
[pos
] != '<' do
1296 if pos
>= line
.value
.length
then
1297 if pos
- 2 >= 0 and line
.value
[pos
- 2] == '/' then
1299 if tags
.is_empty
then
1307 tmp
= new FlatBuffer
1308 var new_pos
= line
.value
.read_xml
(tmp
, pos
, false)
1311 if tag
.is_html_block
and not tag
== "hr" then
1312 if tmp
[1] == '/' then
1313 if tags
.last
!= tag
then
1321 if tags
.is_empty
then
1331 return tags
.is_empty
1336 # Read a XML comment.
1337 # Used by `check_html`.
1338 private fun read_xml_comment
(first_line
: MDLine, start
: Int): Int do
1339 var line
: nullable MDLine = first_line
1340 if start
+ 3 < line
.value
.length
then
1341 if line
.value
[2] == '-' and line
.value
[3] == '-' then
1343 while line
!= null do
1344 while pos
< line
.value
.length
and line
.value
[pos
] != '-' do
1347 if pos
== line
.value
.length
then
1351 if pos
+ 2 < line
.value
.length
then
1352 if line
.value
[pos
+ 1] == '-' and line
.value
[pos
+ 2] == '>' then
1353 first_line
.xml_end_line
= line
1365 # Extract the text of `self` without leading and trailing.
1366 fun text
: String do return value
.substring
(leading
, value
.length
- trailing
)
1373 # See `MarkdownProcessor::recurse`.
1374 fun process
(v
: MarkdownProcessor) is abstract
1377 # An empty markdown line.
1381 redef fun process
(v
) do
1382 v
.current_line
= v
.current_line
.next
1386 # A non-specific markdown construction.
1387 # Mainly used as part of another line construct such as paragraphs or lists.
1391 redef fun process
(v
) do
1392 var line
= v
.current_line
1394 var was_empty
= line
.prev_empty
1395 while line
!= null and not line
.is_empty
do
1396 var t
= v
.line_kind
(line
)
1397 if v
.in_list
and t
isa LineList then
1400 if t
isa LineCode or t
isa LineFence then
1403 if t
isa LineHeadline or t
isa LineHeadline1 or t
isa LineHeadline2 or
1404 t
isa LineHR or t
isa LineBlockquote or t
isa LineXML then
1410 if line
!= null and not line
.is_empty
then
1411 var block
= v
.current_block
.split
(line
.prev
.as(not null))
1412 if v
.in_list
and not was_empty
then
1413 block
.kind
= new BlockNone(block
)
1415 block
.kind
= new BlockParagraph(block
)
1417 v
.current_block
.remove_leading_empty_lines
1420 if line
!= null then
1421 block
= v
.current_block
.split
(line
)
1423 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1425 if v
.in_list
and (line
== null or not line
.is_empty
) and not was_empty
then
1426 block
.kind
= new BlockNone(block
)
1428 block
.kind
= new BlockParagraph(block
)
1430 v
.current_block
.remove_leading_empty_lines
1432 v
.current_line
= v
.current_block
.first_line
1436 # A line of markdown code.
1440 redef fun process
(v
) do
1441 var line
= v
.current_line
1443 while line
!= null and (line
.is_empty
or v
.line_kind
(line
) isa LineCode) do
1446 # split at block end line
1448 if line
!= null then
1449 block
= v
.current_block
.split
(line
.prev
.as(not null))
1451 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1453 block
.kind
= new BlockCode(block
)
1454 block
.remove_surrounding_empty_lines
1455 v
.current_line
= v
.current_block
.first_line
1459 # A line of raw XML.
1463 redef fun process
(v
) do
1464 var line
= v
.current_line
1465 var prev
= line
.prev
1466 if prev
!= null then v
.current_block
.split
(prev
)
1467 var block
= v
.current_block
.split
(line
.xml_end_line
.as(not null))
1468 block
.kind
= new BlockXML(block
)
1469 v
.current_block
.remove_leading_empty_lines
1470 v
.current_line
= v
.current_block
.first_line
1474 # A markdown blockquote line.
1475 class LineBlockquote
1478 redef fun process
(v
) do
1479 var line
= v
.current_line
1481 while line
!= null do
1482 if not line
.is_empty
and (line
.prev_empty
and
1483 line
.leading
== 0 and
1484 not v
.line_kind
(line
) isa LineBlockquote) then break
1489 if line
!= null then
1490 block
= v
.current_block
.split
(line
.prev
.as(not null))
1492 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1494 var kind
= new BlockQuote(block
)
1496 block
.remove_surrounding_empty_lines
1497 kind
.remove_block_quote_prefix
(block
)
1498 v
.current_line
= line
1499 v
.recurse
(block
, false)
1500 v
.current_line
= v
.current_block
.first_line
1504 # A markdown ruler line.
1508 redef fun process
(v
) do
1509 var line
= v
.current_line
1510 if line
.prev
!= null then v
.current_block
.split
(line
.prev
.as(not null))
1511 var block
= v
.current_block
.split
(line
.as(not null))
1512 block
.kind
= new BlockRuler(block
)
1513 v
.current_block
.remove_leading_empty_lines
1514 v
.current_line
= v
.current_block
.first_line
1518 # A markdown fence code line.
1522 redef fun process
(v
) do
1524 var line
= v
.current_line
.next
1525 while line
!= null do
1526 if v
.line_kind
(line
) isa LineFence then break
1529 if line
!= null then
1534 if line
!= null then
1535 block
= v
.current_block
.split
(line
.prev
.as(not null))
1537 block
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1539 block
.kind
= new BlockFence(block
)
1540 block
.first_line
.clear
1541 var last
= block
.last_line
1542 if last
!= null and v
.line_kind
(last
) isa LineFence then
1543 block
.last_line
.clear
1545 block
.remove_surrounding_empty_lines
1546 v
.current_line
= line
1550 # A markdown headline.
1554 redef fun process
(v
) do
1555 var line
= v
.current_line
1556 var lprev
= line
.prev
1557 if lprev
!= null then v
.current_block
.split
(lprev
)
1558 var block
= v
.current_block
.split
(line
.as(not null))
1559 var kind
= new BlockHeadline(block
)
1561 kind
.transform_headline
(block
)
1562 v
.current_block
.remove_leading_empty_lines
1563 v
.current_line
= v
.current_block
.first_line
1567 # A markdown headline of level 1.
1571 redef fun process
(v
) do
1572 var line
= v
.current_line
1573 var lprev
= line
.prev
1574 if lprev
!= null then v
.current_block
.split
(lprev
)
1576 var block
= v
.current_block
.split
(line
.as(not null))
1577 var kind
= new BlockHeadline(block
)
1579 kind
.transform_headline
(block
)
1581 v
.current_block
.remove_leading_empty_lines
1582 v
.current_line
= v
.current_block
.first_line
1586 # A markdown headline of level 2.
1590 redef fun process
(v
) do
1591 var line
= v
.current_line
1592 var lprev
= line
.prev
1593 if lprev
!= null then v
.current_block
.split
(lprev
)
1595 var block
= v
.current_block
.split
(line
.as(not null))
1596 var kind
= new BlockHeadline(block
)
1598 kind
.transform_headline
(block
)
1600 v
.current_block
.remove_leading_empty_lines
1601 v
.current_line
= v
.current_block
.first_line
1605 # A markdown list line.
1606 # Mainly used to factorize code between ordered and unordered lists.
1610 redef fun process
(v
) do
1611 var line
= v
.current_line
1613 while line
!= null do
1614 var t
= v
.line_kind
(line
)
1615 if not line
.is_empty
and (line
.prev_empty
and line
.leading
== 0 and
1616 not t
isa LineList) then break
1621 if line
!= null then
1622 list
= v
.current_block
.split
(line
.prev
.as(not null))
1624 list
= v
.current_block
.split
(v
.current_block
.last_line
.as(not null))
1626 var kind
= block_kind
(list
)
1628 list
.first_line
.prev_empty
= false
1629 list
.last_line
.next_empty
= false
1630 list
.remove_surrounding_empty_lines
1631 list
.first_line
.prev_empty
= false
1632 list
.last_line
.next_empty
= false
1634 var block
= list
.first_block
1635 while block
!= null do
1636 block
.remove_list_indent
(v
)
1637 v
.recurse
(block
, true)
1640 kind
.expand_paragraphs
(list
)
1641 v
.current_line
= line
1644 # Create a new block kind based on this line.
1645 protected fun block_kind
(block
: MDBlock): BlockList is abstract
1647 # Extract string value from `MDLine`.
1648 protected fun extract_value
(line
: MDLine): String is abstract
1651 # An ordered list line.
1655 redef fun block_kind
(block
) do return new BlockOrderedList(block
)
1657 redef fun extract_value
(line
) do
1658 return line
.value
.substring_from
(line
.value
.index_of
('.') + 2)
1662 # An unordered list line.
1666 redef fun block_kind
(block
) do return new BlockUnorderedList(block
)
1668 redef fun extract_value
(line
) do
1669 return line
.value
.substring_from
(line
.leading
+ 2)
1673 # A token represent a character in the markdown input.
1674 # Some tokens have a specific markup behaviour that is handled here.
1675 abstract class Token
1677 # Position of `self` in markdown input.
1680 # Character found at `pos` in the markdown input.
1683 # Output that token using `MarkdownEmitter::decorator`.
1684 fun emit
(v
: MarkdownEmitter) do v
.addc char
1687 # A token without a specific meaning.
1692 # An emphasis token.
1693 abstract class TokenEm
1696 redef fun emit
(v
) do
1697 var tmp
= v
.push_buffer
1698 var b
= v
.emit_text_until
(v
.current_text
.as(not null), pos
+ 1, self)
1701 v
.decorator
.add_em
(v
, tmp
)
1709 # An emphasis star token.
1714 # An emphasis underscore token.
1715 class TokenEmUnderscore
1720 abstract class TokenStrong
1723 redef fun emit
(v
) do
1724 var tmp
= v
.push_buffer
1725 var b
= v
.emit_text_until
(v
.current_text
.as(not null), pos
+ 2, self)
1728 v
.decorator
.add_strong
(v
, tmp
)
1729 v
.current_pos
= b
+ 1
1736 # A strong star token.
1737 class TokenStrongStar
1741 # A strong underscore token.
1742 class TokenStrongUnderscore
1747 # This class is mainly used to factorize work between single and double quoted span codes.
1748 abstract class TokenCode
1751 redef fun emit
(v
) do
1752 var a
= pos
+ next_pos
+ 1
1753 var b
= v
.processor
.find_token
(v
.current_text
.as(not null), a
, self)
1755 v
.current_pos
= b
+ next_pos
1756 while a
< b
and v
.current_text
[a
] == ' ' do a
+= 1
1758 while v
.current_text
[b
- 1] == ' ' do b
-= 1
1759 v
.decorator
.add_span_code
(v
, v
.current_text
.as(not null), a
, b
)
1766 private fun next_pos
: Int is abstract
1769 # A span code token.
1770 class TokenCodeSingle
1773 redef fun next_pos
do return 0
1776 # A doubled span code token.
1777 class TokenCodeDouble
1780 redef fun next_pos
do return 1
1783 # A link or image token.
1784 # This class is mainly used to factorize work between images and links.
1785 abstract class TokenLinkOrImage
1789 var link
: nullable Text = null
1792 var name
: nullable Text = null
1795 var comment
: nullable Text = null
1797 # Is the link construct an abbreviation?
1798 var is_abbrev
= false
1800 redef fun emit
(v
) do
1801 var tmp
= new FlatBuffer
1802 var b
= check_link
(v
, tmp
, pos
, self)
1811 # Emit the hyperlink as link or image.
1812 private fun emit_hyper
(v
: MarkdownEmitter) is abstract
1814 # Check if the link is a valid link.
1815 private fun check_link
(v
: MarkdownEmitter, out
: FlatBuffer, start
: Int, token
: Token): Int do
1816 var md
= v
.current_text
1818 if token
isa TokenLink then
1823 var tmp
= new FlatBuffer
1824 pos
= md
.read_md_link_id
(tmp
, pos
)
1825 if pos
< start
then return -1
1829 pos
= md
.skip_spaces
(pos
)
1831 var tid
= name
.write_to_string
.to_lower
1832 if v
.processor
.link_refs
.has_key
(tid
) then
1833 var lr
= v
.processor
.link_refs
[tid
]
1834 is_abbrev
= lr
.is_abbrev
1841 else if md
[pos
] == '(' then
1843 pos
= md
.skip_spaces
(pos
)
1844 if pos
< start
then return -1
1845 tmp
= new FlatBuffer
1846 var use_lt
= md
[pos
] == '<'
1848 pos
= md
.read_until
(tmp
, pos
+ 1, '>')
1850 pos
= md
.read_md_link
(tmp
, pos
)
1852 if pos
< start
then return -1
1853 if use_lt
then pos
+= 1
1854 link
= tmp
.write_to_string
1855 if md
[pos
] == ' ' then
1856 pos
= md
.skip_spaces
(pos
)
1857 if pos
> start
and md
[pos
] == '"' then
1859 tmp
= new FlatBuffer
1860 pos
= md
.read_until
(tmp
, pos
, '"')
1861 if pos
< start
then return -1
1862 comment
= tmp
.write_to_string
1864 pos
= md
.skip_spaces
(pos
)
1865 if pos
== -1 then return -1
1868 if md
[pos
] != ')' then return -1
1869 else if md
[pos
] == '[' then
1871 tmp
= new FlatBuffer
1872 pos
= md
.read_raw_until
(tmp
, pos
, ']')
1873 if pos
< start
then return -1
1875 if tmp
.length
> 0 then
1880 var tid
= id
.write_to_string
.to_lower
1881 if v
.processor
.link_refs
.has_key
(tid
) then
1882 var lr
= v
.processor
.link_refs
[tid
]
1887 var tid
= name
.write_to_string
.replace
("\n", " ").to_lower
1888 if v
.processor
.link_refs
.has_key
(tid
) then
1889 var lr
= v
.processor
.link_refs
[tid
]
1897 if link
== null then return -1
1902 # A markdown link token.
1904 super TokenLinkOrImage
1906 redef fun emit_hyper
(v
) do
1907 if is_abbrev
and comment
!= null then
1908 v
.decorator
.add_abbr
(v
, name
.as(not null), comment
.as(not null))
1910 v
.decorator
.add_link
(v
, link
.as(not null), name
.as(not null), comment
)
1915 # A markdown image token.
1917 super TokenLinkOrImage
1919 redef fun emit_hyper
(v
) do
1920 v
.decorator
.add_image
(v
, link
.as(not null), name
.as(not null), comment
)
1928 redef fun emit
(v
) do
1929 var tmp
= new FlatBuffer
1930 var b
= check_html
(v
, tmp
, v
.current_text
.as(not null), v
.current_pos
)
1935 v
.decorator
.escape_char
(v
, char
)
1939 # Is the HTML valid?
1940 # Also take care of link and mailto shortcuts.
1941 private fun check_html
(v
: MarkdownEmitter, out
: FlatBuffer, md
: Text, start
: Int): Int do
1942 # check for auto links
1943 var tmp
= new FlatBuffer
1944 var pos
= md
.read_until
(tmp
, start
+ 1, ':', ' ', '>', '\n')
1945 if pos
!= -1 and md
[pos
] == ':' and tmp
.is_link_prefix
then
1946 pos
= md
.read_until
(tmp
, pos
, '>')
1948 var link
= tmp
.write_to_string
1949 v
.decorator
.add_link
(v
, link
, link
, null)
1953 # TODO check for mailto
1954 # check for inline html
1955 if start
+ 2 < md
.length
then
1956 return md
.read_xml
(out
, start
, true)
1962 # An HTML entity token.
1966 redef fun emit
(v
) do
1967 var tmp
= new FlatBuffer
1968 var b
= check_entity
(tmp
, v
.current_text
.as(not null), pos
)
1973 v
.decorator
.escape_char
(v
, char
)
1977 # Is the entity valid?
1978 private fun check_entity
(out
: FlatBuffer, md
: Text, start
: Int): Int do
1979 var pos
= md
.read_until
(out
, start
, ';')
1980 if pos
< 0 or out
.length
< 3 then
1983 if out
[1] == '#' then
1984 if out
[2] == 'x' or out
[2] == 'X' then
1985 if out
.length
< 4 then return -1
1986 for i
in [3..out
.length
[ do
1988 if (c
< '0' or c
> '9') and (c
< 'a' and c
> 'f') and (c
< 'A' and c
> 'F') then
1993 for i
in [2..out
.length
[ do
1995 if c
< '0' or c
> '9' then return -1
2000 for i
in [1..out
.length
[ do
2002 if not c
.is_digit
and not c
.is_letter
then return -1
2005 # TODO check entity is valid
2006 # if out.is_entity then
2016 # A markdown escape token.
2020 redef fun emit
(v
) do
2022 v
.addc v
.current_text
[v
.current_pos
]
2028 # Get the position of the next non-space character.
2029 private fun skip_spaces
(start
: Int): Int do
2031 while pos
> -1 and pos
< length
and (self[pos
] == ' ' or self[pos
] == '\n') do
2034 if pos
< length
then return pos
2038 # Read `self` until `nend` and append it to the `out` buffer.
2039 # Escape markdown special chars.
2040 private fun read_until
(out
: FlatBuffer, start
: Int, nend
: Char...): Int do
2042 while pos
< length
do
2044 if c
== '\\' and pos
+ 1 < length
then
2045 pos
= escape
(out
, self[pos
+ 1], pos
)
2047 var end_reached
= false
2054 if end_reached
then break
2059 if pos
== length
then return -1
2063 # Read `self` as raw text until `nend` and append it to the `out` buffer.
2064 # No escape is made.
2065 private fun read_raw_until
(out
: FlatBuffer, start
: Int, nend
: Char...): Int do
2067 while pos
< length
do
2069 var end_reached
= false
2076 if end_reached
then break
2080 if pos
== length
then return -1
2084 # Read `self` as XML until `to` and append it to the `out` buffer.
2085 # Escape HTML special chars.
2086 private fun read_xml_until
(out
: FlatBuffer, from
: Int, to
: Char...): Int do
2089 var str_char
: nullable Char = null
2090 while pos
< length
do
2096 if pos
< length
then
2102 if c
== str_char
then
2109 if c
== '"' or c
== '\'' then
2114 var end_reached = false
2115 for n in [0..to.length[ do
2121 if end_reached then break
2126 if pos == length then return -1
2130 # Read `self` as XML and append it to the `out` buffer.
2131 # Safe mode can be activated to limit reading to valid xml.
2132 private fun read_xml(out: FlatBuffer, start: Int, safe_mode: Bool): Int do
2134 var is_close_tag = false
2135 if start + 1 >= length then return -1
2136 if self[start + 1] == '/' then
2139 else if self[start + 1] == '!' then
2143 is_close_tag = false
2147 var tmp = new FlatBuffer
2148 pos = read_xml_until(tmp, pos, ' ', '/', '>')
2149 if pos == -1 then return -1
2150 var tag = tmp.write_to_string.trim.to_lower
2151 if tag.is_html_unsafe then
2153 if is_close_tag then out.add '/'
2157 if is_close_tag then out.add '/'
2162 if is_close_tag then out.add '/'
2163 pos = read_xml_until(out, pos, ' ', '/', '>')
2165 if pos == -1 then return -1
2166 pos = read_xml_until(out, pos, '/', '>')
2167 if pos == -1 then return -1
2168 if self[pos] == '/' then
2170 pos = self.read_xml_until(out, pos + 1, '>')
2171 if pos == -1 then return -1
2173 if self[pos] == '>' then
2180 # Read a markdown link address and append it to the `out` buffer.
2181 private fun read_md_link(out: FlatBuffer, start: Int): Int do
2184 while pos < length do
2186 if c == '\\
' and pos + 1 < length then
2187 pos = escape(out, self[pos + 1], pos)
2189 var end_reached = false
2192 else if c == ' ' then
2193 if counter == 1 then end_reached = true
2194 else if c == ')' then
2196 if counter == 0 then end_reached = true
2198 if end_reached then break
2203 if pos == length then return -1
2207 # Read a markdown link text and append it to the `out` buffer.
2208 private fun read_md_link_id(out: FlatBuffer, start: Int): Int do
2211 while pos < length do
2213 var end_reached = false
2217 else if c == ']' then
2219 if counter == 0 then
2227 if end_reached then break
2230 if pos == length then return -1
2234 # Extract the XML tag name from a XML tag.
2235 private fun xml_tag: String do
2236 var tpl = new FlatBuffer
2238 if pos < length and self[1] == '/' then pos += 1
2239 while pos < length - 1 and (self[pos].is_digit or self[pos].is_letter) do
2243 return tpl.write_to_string.to_lower
2246 # Read and escape the markdown contained in `self`.
2247 private fun escape(out: FlatBuffer, c: Char, pos: Int): Int do
2248 if c == '\\
' or c == '[' or c == ']' or c == '(' or c == ')' or c == '{' or
2249 c == '}' or c == '#' or c == '"' or c == '\'' or c == '.' or c == '<' or
2250 c
== '>' or c
== '*' or c
== '+' or c
== '-' or c
== '_' or c
== '!' or
2251 c
== '`' or c
== '~' or c
== '^' then
2259 # Is `self` an unsafe HTML element?
2260 private fun is_html_unsafe
: Bool do return html_unsafe_tags
.has
(self.write_to_string
)
2262 # Is `self` a HRML block element?
2263 private fun is_html_block
: Bool do return html_block_tags
.has
(self.write_to_string
)
2265 # Is `self` a link prefix?
2266 private fun is_link_prefix
: Bool do return html_link_prefixes
.has
(self.write_to_string
)
2268 private fun html_unsafe_tags
: Array[String] do return once
["applet", "head", "body", "frame", "frameset", "iframe", "script", "object"]
2270 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"]
2272 private fun html_link_prefixes
: Array[String] do return once
["http", "https", "ftp", "ftps"]
2277 # Parse `self` as markdown and return the HTML representation
2279 # var md = "**Hello World!**"
2280 # var html = md.md_to_html
2281 # assert html == "<p><strong>Hello World!</strong></p>\n"
2282 fun md_to_html
: Streamable do
2283 var processor
= new MarkdownProcessor
2284 return processor
.process
(self)