lib/markdown: promote `BlockFence::meta` to `BlockCode` to simplify clients
[nit.git] / lib / markdown / markdown.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Markdown parsing.
16 module markdown
17
18 import template
19
20 # Parse a markdown string and split it in blocks.
21 #
22 # Blocks are then outputed by an `MarkdownEmitter`.
23 #
24 # Usage:
25 #
26 # var proc = new MarkdownProcessor
27 # var html = proc.process("**Hello World!**")
28 # assert html == "<p><strong>Hello World!</strong></p>\n"
29 #
30 # SEE: `String::md_to_html` for a shortcut.
31 class MarkdownProcessor
32
33 # `MarkdownEmitter` used for ouput.
34 var emitter: MarkdownEmitter is noinit, protected writable
35
36 # Work in extended mode (default).
37 #
38 # Behavior changes when using extended mode:
39 #
40 # * Lists and code blocks end a paragraph
41 #
42 # In normal markdown the following:
43 #
44 # ~~~md
45 # This is a paragraph
46 # * and this is not a list
47 # ~~~
48 #
49 # Will produce:
50 #
51 # ~~~html
52 # <p>This is a paragraph
53 # * and this is not a list</p>
54 # ~~~
55 #
56 # When using extended mode this changes to:
57 #
58 # ~~~html
59 # <p>This is a paragraph</p>
60 # <ul>
61 # <li>and this is not a list</li>
62 # </ul>
63 # ~~~
64 #
65 # * Fences code blocks
66 #
67 # If you don't want to indent your all your code with 4 spaces,
68 # you can wrap your code in ``` ``` ``` or `~~~`.
69 #
70 # Here's an example:
71 #
72 # ~~~md
73 # fun test do
74 # print "Hello World!"
75 # end
76 # ~~~
77 #
78 # * Code blocks meta
79 #
80 # If you want to use syntax highlighting tools, most of them need to know what kind
81 # of language they are highlighting.
82 # You can add an optional language identifier after the fence declaration to output
83 # it in the HTML render.
84 #
85 # ```nit
86 # import markdown
87 #
88 # print "# Hello World!".md_to_html
89 # ```
90 #
91 # Becomes
92 #
93 # ~~~html
94 # <pre class="nit"><code>import markdown
95 #
96 # print "Hello World!".md_to_html
97 # </code></pre>
98 # ~~~
99 #
100 # * Underscores (Emphasis)
101 #
102 # Underscores in the middle of a word like:
103 #
104 # ~~~md
105 # Con_cat_this
106 # ~~~
107 #
108 # normally produces this:
109 #
110 # ~~~html
111 # <p>Con<em>cat</em>this</p>
112 # ~~~
113 #
114 # With extended mode they don't result in emphasis.
115 #
116 # ~~~html
117 # <p>Con_cat_this</p>
118 # ~~~
119 #
120 # * Strikethrough
121 #
122 # Like in [GFM](https://help.github.com/articles/github-flavored-markdown),
123 # strikethrought span is marked with `~~`.
124 #
125 # ~~~md
126 # ~~Mistaken text.~~
127 # ~~~
128 #
129 # becomes
130 #
131 # ~~~html
132 # <del>Mistaken text.</del>
133 # ~~~
134 var ext_mode = true
135
136 init do self.emitter = new MarkdownEmitter(self)
137
138 # Process the mardown `input` string and return the processed output.
139 fun process(input: String): Writable do
140 # init processor
141 link_refs.clear
142 last_link_ref = null
143 current_line = null
144 current_block = null
145 # parse markdown
146 var parent = read_lines(input)
147 parent.remove_surrounding_empty_lines
148 recurse(parent, false)
149 # output processed text
150 return emitter.emit(parent.kind)
151 end
152
153 # Split `input` string into `MDLines` and create a parent `MDBlock` with it.
154 private fun read_lines(input: String): MDBlock do
155 var block = new MDBlock(new MDLocation(1, 1, 1, 1))
156 var value = new FlatBuffer
157 var i = 0
158
159 var line_pos = 0
160 var col_pos = 0
161
162 while i < input.length do
163 value.clear
164 var pos = 0
165 var eol = false
166 while not eol and i < input.length do
167 col_pos += 1
168 var c = input[i]
169 if c == '\n' then
170 eol = true
171 else if c == '\t' then
172 var np = pos + (4 - (pos & 3))
173 while pos < np do
174 value.add ' '
175 pos += 1
176 end
177 else
178 pos += 1
179 value.add c
180 end
181 i += 1
182 end
183 line_pos += 1
184
185 var loc = new MDLocation(line_pos, 1, line_pos, col_pos)
186 var line = new MDLine(loc, value.write_to_string)
187 var is_link_ref = check_link_ref(line)
188 # Skip link refs
189 if not is_link_ref then block.add_line line
190 col_pos = 0
191 end
192 return block
193 end
194
195 # Check if line is a block link definition.
196 # Return `true` if line contains a valid link ref and save it into `link_refs`.
197 private fun check_link_ref(line: MDLine): Bool do
198 var md = line.value
199 var is_link_ref = false
200 var id = new FlatBuffer
201 var link = new FlatBuffer
202 var comment = new FlatBuffer
203 var pos = -1
204 if not line.is_empty and line.leading < 4 and line.value[line.leading] == '[' then
205 pos = line.leading + 1
206 pos = md.read_until(id, pos, ']')
207 if not id.is_empty and pos + 2 < line.value.length then
208 if line.value[pos + 1] == ':' then
209 pos += 2
210 pos = md.skip_spaces(pos)
211 if line.value[pos] == '<' then
212 pos += 1
213 pos = md.read_until(link, pos, '>')
214 pos += 1
215 else
216 pos = md.read_until(link, pos, ' ', '\n')
217 end
218 if not link.is_empty then
219 pos = md.skip_spaces(pos)
220 if pos > 0 and pos < line.value.length then
221 var c = line.value[pos]
222 if c == '\"' or c == '\'' or c == '(' then
223 pos += 1
224 if c == '(' then
225 pos = md.read_until(comment, pos, ')')
226 else
227 pos = md.read_until(comment, pos, c)
228 end
229 if pos > 0 then is_link_ref = true
230 end
231 else
232 is_link_ref = true
233 end
234 end
235 end
236 end
237 end
238 if is_link_ref and not id.is_empty and not link.is_empty then
239 var lr = new LinkRef.with_title(link.write_to_string, comment.write_to_string)
240 add_link_ref(id.write_to_string, lr)
241 if comment.is_empty then last_link_ref = lr
242 return true
243 else
244 comment = new FlatBuffer
245 if not line.is_empty and last_link_ref != null then
246 pos = line.leading
247 var c = line.value[pos]
248 if c == '\"' or c == '\'' or c == '(' then
249 pos += 1
250 if c == '(' then
251 pos = md.read_until(comment, pos, ')')
252 else
253 pos = md.read_until(comment, pos, c)
254 end
255 end
256 if not comment.is_empty then last_link_ref.title = comment.write_to_string
257 end
258 if comment.is_empty then return false
259 return true
260 end
261 end
262
263 # Known link refs
264 # This list will be needed during output to expand links.
265 var link_refs: Map[String, LinkRef] = new HashMap[String, LinkRef]
266
267 # Last encountered link ref (for multiline definitions)
268 #
269 # Markdown allows link refs to be defined over two lines:
270 #
271 # ~~~md
272 # [id]: http://example.com/longish/path/to/resource/here
273 # "Optional Title Here"
274 # ~~~
275 #
276 private var last_link_ref: nullable LinkRef = null
277
278 # Add a link ref to the list
279 fun add_link_ref(key: String, ref: LinkRef) do link_refs[key.to_lower] = ref
280
281 # Recursively split a `block`.
282 #
283 # The block is splitted according to the type of lines it contains.
284 # Some blocks can be splited again recursively like lists.
285 # The `in_list` mode is used to recurse on list and build
286 # nested paragraphs or code blocks.
287 fun recurse(root: MDBlock, in_list: Bool) do
288 var old_mode = self.in_list
289 var old_root = self.current_block
290 self.in_list = in_list
291
292 var line = root.first_line
293 while line != null and line.is_empty do
294 line = line.next
295 if line == null then return
296 end
297
298 current_line = line
299 current_block = root
300 while current_line != null do
301 line_kind(current_line.as(not null)).process(self)
302 end
303 self.in_list = old_mode
304 self.current_block = old_root
305 end
306
307 # Currently processed line.
308 # Used when visiting blocks with `recurse`.
309 var current_line: nullable MDLine = null is writable
310
311 # Currently processed block.
312 # Used when visiting blocks with `recurse`.
313 var current_block: nullable MDBlock = null is writable
314
315 # Is the current recursion in list mode?
316 # Used when visiting blocks with `recurse`
317 private var in_list = false
318
319 # The type of line.
320 # see: `md_line_*`
321 fun line_kind(md: MDLine): Line do
322 var value = md.value
323 var leading = md.leading
324 var trailing = md.trailing
325 if md.is_empty then return new LineEmpty
326 if md.leading > 3 then return new LineCode
327 if value[leading] == '#' then return new LineHeadline
328 if value[leading] == '>' then return new LineBlockquote
329
330 if ext_mode then
331 if value.length - leading - trailing > 2 then
332 if value[leading] == '`' and md.count_chars_start('`') >= 3 then
333 return new LineFence
334 end
335 if value[leading] == '~' and md.count_chars_start('~') >= 3 then
336 return new LineFence
337 end
338 end
339 end
340
341 if value.length - leading - trailing > 2 and
342 (value[leading] == '*' or value[leading] == '-' or value[leading] == '_') then
343 if md.count_chars(value[leading]) >= 3 then
344 return new LineHR
345 end
346 end
347
348 if value.length - leading >= 2 and value[leading + 1] == ' ' then
349 var c = value[leading]
350 if c == '*' or c == '-' or c == '+' then return new LineUList
351 end
352
353 if value.length - leading >= 3 and value[leading].is_digit then
354 var i = leading + 1
355 while i < value.length and value[i].is_digit do i += 1
356 if i + 1 < value.length and value[i] == '.' and value[i + 1] == ' ' then
357 return new LineOList
358 end
359 end
360
361 if value[leading] == '<' and md.check_html then return new LineXML
362
363 var next = md.next
364 if next != null and not next.is_empty then
365 if next.count_chars('=') > 0 then
366 return new LineHeadline1
367 end
368 if next.count_chars('-') > 0 then
369 return new LineHeadline2
370 end
371 end
372 return new LineOther
373 end
374
375 # Get the token kind at `pos`.
376 fun token_at(text: Text, pos: Int): Token do
377 var c0: Char
378 var c1: Char
379 var c2: Char
380
381 if pos > 0 then
382 c0 = text[pos - 1]
383 else
384 c0 = ' '
385 end
386 var c = text[pos]
387
388 if pos + 1 < text.length then
389 c1 = text[pos + 1]
390 else
391 c1 = ' '
392 end
393 if pos + 2 < text.length then
394 c2 = text[pos + 2]
395 else
396 c2 = ' '
397 end
398
399 var loc = new MDLocation(
400 current_loc.line_start,
401 current_loc.column_start + pos,
402 current_loc.line_start,
403 current_loc.column_start + pos)
404
405 if c == '*' then
406 if c1 == '*' then
407 if c0 != ' ' or c2 != ' ' then
408 return new TokenStrongStar(loc, pos, c)
409 else
410 return new TokenEmStar(loc, pos, c)
411 end
412 end
413 if c0 != ' ' or c1 != ' ' then
414 return new TokenEmStar(loc, pos, c)
415 else
416 return new TokenNone(loc, pos, c)
417 end
418 else if c == '_' then
419 if c1 == '_' then
420 if c0 != ' ' or c2 != ' 'then
421 return new TokenStrongUnderscore(loc, pos, c)
422 else
423 return new TokenEmUnderscore(loc, pos, c)
424 end
425 end
426 if ext_mode then
427 if (c0.is_letter or c0.is_digit) and c0 != '_' and
428 (c1.is_letter or c1.is_digit) then
429 return new TokenNone(loc, pos, c)
430 else
431 return new TokenEmUnderscore(loc, pos, c)
432 end
433 end
434 if c0 != ' ' or c1 != ' ' then
435 return new TokenEmUnderscore(loc, pos, c)
436 else
437 return new TokenNone(loc, pos, c)
438 end
439 else if c == '!' then
440 if c1 == '[' then return new TokenImage(loc, pos, c)
441 return new TokenNone(loc, pos, c)
442 else if c == '[' then
443 return new TokenLink(loc, pos, c)
444 else if c == ']' then
445 return new TokenNone(loc, pos, c)
446 else if c == '`' then
447 if c1 == '`' then
448 return new TokenCodeDouble(loc, pos, c)
449 else
450 return new TokenCodeSingle(loc, pos, c)
451 end
452 else if c == '\\' then
453 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
454 return new TokenEscape(loc, pos, c)
455 else
456 return new TokenNone(loc, pos, c)
457 end
458 else if c == '<' then
459 return new TokenHTML(loc, pos, c)
460 else if c == '&' then
461 return new TokenEntity(loc, pos, c)
462 else
463 if ext_mode then
464 if c == '~' and c1 == '~' then
465 return new TokenStrike(loc, pos, c)
466 end
467 end
468 return new TokenNone(loc, pos, c)
469 end
470 end
471
472 # Find the position of a `token` in `self`.
473 fun find_token(text: Text, start: Int, token: Token): Int do
474 var pos = start
475 while pos < text.length do
476 if token_at(text, pos).is_same_type(token) then
477 return pos
478 end
479 pos += 1
480 end
481 return -1
482 end
483
484 # Location used for next parsed token.
485 #
486 # This location can be changed by the emitter to adjust with `\n` found
487 # in the input.
488 private fun current_loc: MDLocation do return emitter.current_loc
489 end
490
491 # Emit output corresponding to blocks content.
492 #
493 # Blocks are created by a previous pass in `MarkdownProcessor`.
494 # The emitter use a `Decorator` to select the output format.
495 class MarkdownEmitter
496
497 # Kind of processor used for parsing.
498 type PROCESSOR: MarkdownProcessor
499
500 # Processor containing link refs.
501 var processor: PROCESSOR
502
503 # Kind of decorator used for decoration.
504 type DECORATOR: Decorator
505
506 # Decorator used for output.
507 # Default is `HTMLDecorator`
508 var decorator: DECORATOR is writable, lazy do
509 return new HTMLDecorator
510 end
511
512 # Create a new `MarkdownEmitter` using a custom `decorator`.
513 init with_decorator(processor: PROCESSOR, decorator: DECORATOR) do
514 init processor
515 self.decorator = decorator
516 end
517
518 # Output `block` using `decorator` in the current buffer.
519 fun emit(block: Block): Text do
520 var buffer = push_buffer
521 block.emit(self)
522 pop_buffer
523 return buffer
524 end
525
526 # Output the content of `block`.
527 fun emit_in(block: Block) do block.emit_in(self)
528
529 # Transform and emit mardown text
530 fun emit_text(text: Text) do emit_text_until(text, 0, null)
531
532 # Transform and emit mardown text starting at `start` and
533 # until a token with the same type as `token` is found.
534 # Go until the end of `text` if `token` is null.
535 fun emit_text_until(text: Text, start: Int, token: nullable Token): Int do
536 var old_text = current_text
537 var old_pos = current_pos
538 current_text = text
539 current_pos = start
540 while current_pos < text.length do
541 if text[current_pos] == '\n' then
542 current_loc.line_start += 1
543 current_loc.column_start = -current_pos
544 end
545 var mt = processor.token_at(text, current_pos)
546 if (token != null and not token isa TokenNone) and
547 (mt.is_same_type(token) or
548 (token isa TokenEmStar and mt isa TokenStrongStar) or
549 (token isa TokenEmUnderscore and mt isa TokenStrongUnderscore)) then
550 return current_pos
551 end
552 mt.emit(self)
553 current_pos += 1
554 end
555 current_text = old_text
556 current_pos = old_pos
557 return -1
558 end
559
560 # Currently processed position in `current_text`.
561 # Used when visiting inline production with `emit_text_until`.
562 private var current_pos: Int = -1
563
564 # Currently processed text.
565 # Used when visiting inline production with `emit_text_until`.
566 private var current_text: nullable Text = null
567
568 # Stacked buffers.
569 private var buffer_stack = new List[FlatBuffer]
570
571 # Push a new buffer on the stack.
572 private fun push_buffer: FlatBuffer do
573 var buffer = new FlatBuffer
574 buffer_stack.add buffer
575 return buffer
576 end
577
578 # Pop the last buffer.
579 private fun pop_buffer do buffer_stack.pop
580
581 # Current output buffer.
582 private fun current_buffer: FlatBuffer do
583 assert not buffer_stack.is_empty
584 return buffer_stack.last
585 end
586
587 # Stacked locations.
588 private var loc_stack = new List[MDLocation]
589
590 # Push a new MDLocation on the stack.
591 private fun push_loc(location: MDLocation) do loc_stack.add location
592
593 # Pop the last buffer.
594 private fun pop_loc: MDLocation do return loc_stack.pop
595
596 # Current output buffer.
597 private fun current_loc: MDLocation do
598 assert not loc_stack.is_empty
599 return loc_stack.last
600 end
601
602 # Append `e` to current buffer.
603 fun add(e: Writable) do
604 if e isa Text then
605 current_buffer.append e
606 else
607 current_buffer.append e.write_to_string
608 end
609 end
610
611 # Append `c` to current buffer.
612 fun addc(c: Char) do add c.to_s
613
614 # Append a "\n" line break.
615 fun addn do add "\n"
616 end
617
618 # A Link Reference.
619 # Links that are specified somewhere in the mardown document to be reused as shortcuts.
620 #
621 # ~~~raw
622 # [1]: http://example.com/ "Optional title"
623 # ~~~
624 class LinkRef
625
626 # Link href
627 var link: String
628
629 # Optional link title
630 var title: nullable String = null
631
632 # Is the link an abreviation?
633 var is_abbrev = false
634
635 # Create a link with a title.
636 init with_title(link: String, title: nullable String) do
637 self.link = link
638 self.title = title
639 end
640 end
641
642 # A `Decorator` is used to emit mardown into a specific format.
643 # Default decorator used is `HTMLDecorator`.
644 interface Decorator
645
646 # Kind of emitter used for decoration.
647 type EMITTER: MarkdownEmitter
648
649 # Render a single plain char.
650 #
651 # Redefine this method to add special escaping for plain text.
652 fun add_char(v: EMITTER, c: Char) do v.addc c
653
654 # Render a ruler block.
655 fun add_ruler(v: EMITTER, block: BlockRuler) is abstract
656
657 # Render a headline block with corresponding level.
658 fun add_headline(v: EMITTER, block: BlockHeadline) is abstract
659
660 # Render a paragraph block.
661 fun add_paragraph(v: EMITTER, block: BlockParagraph) is abstract
662
663 # Render a code or fence block.
664 fun add_code(v: EMITTER, block: BlockCode) is abstract
665
666 # Render a blockquote.
667 fun add_blockquote(v: EMITTER, block: BlockQuote) is abstract
668
669 # Render an unordered list.
670 fun add_unorderedlist(v: EMITTER, block: BlockUnorderedList) is abstract
671
672 # Render an ordered list.
673 fun add_orderedlist(v: EMITTER, block: BlockOrderedList) is abstract
674
675 # Render a list item.
676 fun add_listitem(v: EMITTER, block: BlockListItem) is abstract
677
678 # Render an emphasis text.
679 fun add_em(v: EMITTER, text: Text) is abstract
680
681 # Render a strong text.
682 fun add_strong(v: EMITTER, text: Text) is abstract
683
684 # Render a strike text.
685 #
686 # Extended mode only (see `MarkdownProcessor::ext_mode`)
687 fun add_strike(v: EMITTER, text: Text) is abstract
688
689 # Render a link.
690 fun add_link(v: EMITTER, link: Text, name: Text, comment: nullable Text) is abstract
691
692 # Render an image.
693 fun add_image(v: EMITTER, link: Text, name: Text, comment: nullable Text) is abstract
694
695 # Render an abbreviation.
696 fun add_abbr(v: EMITTER, name: Text, comment: Text) is abstract
697
698 # Render a code span reading from a buffer.
699 fun add_span_code(v: EMITTER, buffer: Text, from, to: Int) is abstract
700
701 # Render a text and escape it.
702 fun append_value(v: EMITTER, value: Text) is abstract
703
704 # Render code text from buffer and escape it.
705 fun append_code(v: EMITTER, buffer: Text, from, to: Int) is abstract
706
707 # Render a character escape.
708 fun escape_char(v: EMITTER, char: Char) is abstract
709
710 # Render a line break
711 fun add_line_break(v: EMITTER) is abstract
712
713 # Generate a new html valid id from a `String`.
714 fun strip_id(txt: String): String is abstract
715
716 # Found headlines during the processing labeled by their ids.
717 fun headlines: ArrayMap[String, HeadLine] is abstract
718 end
719
720 # Class representing a markdown headline.
721 class HeadLine
722 # Unique identifier of this headline.
723 var id: String
724
725 # Text of the headline.
726 var title: String
727
728 # Level of this headline.
729 #
730 # According toe the markdown specification, level must be in `[1..6]`.
731 var level: Int
732 end
733
734 # `Decorator` that outputs HTML.
735 class HTMLDecorator
736 super Decorator
737
738 redef var headlines = new ArrayMap[String, HeadLine]
739
740 redef fun add_ruler(v, block) do v.add "<hr/>\n"
741
742 redef fun add_headline(v, block) do
743 # save headline
744 var txt = block.block.first_line.value
745 var id = strip_id(txt)
746 var lvl = block.depth
747 headlines[id] = new HeadLine(id, txt, lvl)
748 # output it
749 v.add "<h{lvl} id=\"{id}\">"
750 v.emit_in block
751 v.add "</h{lvl}>\n"
752 end
753
754 redef fun add_paragraph(v, block) do
755 v.add "<p>"
756 v.emit_in block
757 v.add "</p>\n"
758 end
759
760 redef fun add_code(v, block) do
761 if block isa BlockFence and block.meta != null then
762 v.add "<pre class=\"{block.meta.to_s}\"><code>"
763 else
764 v.add "<pre><code>"
765 end
766 v.emit_in block
767 v.add "</code></pre>\n"
768 end
769
770 redef fun add_blockquote(v, block) do
771 v.add "<blockquote>\n"
772 v.emit_in block
773 v.add "</blockquote>\n"
774 end
775
776 redef fun add_unorderedlist(v, block) do
777 v.add "<ul>\n"
778 v.emit_in block
779 v.add "</ul>\n"
780 end
781
782 redef fun add_orderedlist(v, block) do
783 v.add "<ol>\n"
784 v.emit_in block
785 v.add "</ol>\n"
786 end
787
788 redef fun add_listitem(v, block) do
789 v.add "<li>"
790 v.emit_in block
791 v.add "</li>\n"
792 end
793
794 redef fun add_em(v, text) do
795 v.add "<em>"
796 v.add text
797 v.add "</em>"
798 end
799
800 redef fun add_strong(v, text) do
801 v.add "<strong>"
802 v.add text
803 v.add "</strong>"
804 end
805
806 redef fun add_strike(v, text) do
807 v.add "<del>"
808 v.add text
809 v.add "</del>"
810 end
811
812 redef fun add_image(v, link, name, comment) do
813 v.add "<img src=\""
814 append_value(v, link)
815 v.add "\" alt=\""
816 append_value(v, name)
817 v.add "\""
818 if comment != null and not comment.is_empty then
819 v.add " title=\""
820 append_value(v, comment)
821 v.add "\""
822 end
823 v.add "/>"
824 end
825
826 redef fun add_link(v, link, name, comment) do
827 v.add "<a href=\""
828 append_value(v, link)
829 v.add "\""
830 if comment != null and not comment.is_empty then
831 v.add " title=\""
832 append_value(v, comment)
833 v.add "\""
834 end
835 v.add ">"
836 v.emit_text(name)
837 v.add "</a>"
838 end
839
840 redef fun add_abbr(v, name, comment) do
841 v.add "<abbr title=\""
842 append_value(v, comment)
843 v.add "\">"
844 v.emit_text(name)
845 v.add "</abbr>"
846 end
847
848 redef fun add_span_code(v, text, from, to) do
849 v.add "<code>"
850 append_code(v, text, from, to)
851 v.add "</code>"
852 end
853
854 redef fun add_line_break(v) do
855 v.add "<br/>"
856 end
857
858 redef fun append_value(v, text) do for c in text do escape_char(v, c)
859
860 redef fun escape_char(v, c) do
861 if c == '&' then
862 v.add "&amp;"
863 else if c == '<' then
864 v.add "&lt;"
865 else if c == '>' then
866 v.add "&gt;"
867 else if c == '"' then
868 v.add "&quot;"
869 else if c == '\'' then
870 v.add "&apos;"
871 else
872 v.addc c
873 end
874 end
875
876 redef fun append_code(v, buffer, from, to) do
877 for i in [from..to[ do
878 var c = buffer[i]
879 if c == '&' then
880 v.add "&amp;"
881 else if c == '<' then
882 v.add "&lt;"
883 else if c == '>' then
884 v.add "&gt;"
885 else
886 v.addc c
887 end
888 end
889 end
890
891 redef fun strip_id(txt) do
892 # strip id
893 var b = new FlatBuffer
894 for c in txt do
895 if c == ' ' then
896 b.add '_'
897 else
898 if not c.is_letter and
899 not c.is_digit and
900 not allowed_id_chars.has(c) then continue
901 b.add c
902 end
903 end
904 var res = b.to_s
905 var key = res
906 # check for multiple id definitions
907 if headlines.has_key(key) then
908 var i = 1
909 key = "{res}_{i}"
910 while headlines.has_key(key) do
911 i += 1
912 key = "{res}_{i}"
913 end
914 end
915 return key
916 end
917
918 private var allowed_id_chars: Array[Char] = ['-', '_', ':', '.']
919 end
920
921 # Location in a Markdown input.
922 class MDLocation
923
924 # Starting line number (starting from 1).
925 var line_start: Int
926
927 # Starting column number (starting from 1).
928 var column_start: Int
929
930 # Stopping line number (starting from 1).
931 var line_end: Int
932
933 # Stopping column number (starting from 1).
934 var column_end: Int
935
936 redef fun to_s do return "{line_start},{column_start}--{line_end},{column_end}"
937
938 # Return a copy of `self`.
939 fun copy: MDLocation do
940 return new MDLocation(line_start, column_start, line_end, column_end)
941 end
942 end
943
944 # A block of markdown lines.
945 # A `MDBlock` can contains lines and/or sub-blocks.
946 class MDBlock
947
948 # Position of `self` in the input.
949 var location: MDLocation
950
951 # Kind of block.
952 # See `Block`.
953 var kind: Block = new BlockNone(self) is writable
954
955 # First line if any.
956 var first_line: nullable MDLine = null is writable
957
958 # Last line if any.
959 var last_line: nullable MDLine = null is writable
960
961 # First sub-block if any.
962 var first_block: nullable MDBlock = null is writable
963
964 # Last sub-block if any.
965 var last_block: nullable MDBlock = null is writable
966
967 # Previous block if any.
968 var prev: nullable MDBlock = null is writable
969
970 # Next block if any.
971 var next: nullable MDBlock = null is writable
972
973 # Does this block contain subblocks?
974 fun has_blocks: Bool do return first_block != null
975
976 # Count sub-blocks.
977 fun count_blocks: Int do
978 var count = 0
979 var block = first_block
980 while block != null do
981 count += 1
982 block = block.next
983 end
984 return count
985 end
986
987 # Does this block contain lines?
988 fun has_lines: Bool do return first_line != null
989
990 # Count block lines.
991 fun count_lines: Int do
992 var count = 0
993 var line = first_line
994 while line != null do
995 count += 1
996 line = line.next
997 end
998 return count
999 end
1000
1001 # Split `self` creating a new sub-block having `line` has `last_line`.
1002 fun split(line: MDLine): MDBlock do
1003 # location for new block
1004 var new_loc = new MDLocation(
1005 first_line.location.line_start,
1006 first_line.location.column_start,
1007 line.location.line_end,
1008 line.location.column_end)
1009 # create block
1010 var block = new MDBlock(new_loc)
1011 block.first_line = first_line
1012 block.last_line = line
1013 first_line = line.next
1014 line.next = null
1015 if first_line == null then
1016 last_line = null
1017 else
1018 first_line.prev = null
1019 # update current block loc
1020 location.line_start = first_line.location.line_start
1021 location.column_start = first_line.location.column_start
1022 end
1023 if first_block == null then
1024 first_block = block
1025 last_block = block
1026 else
1027 last_block.next = block
1028 last_block = block
1029 end
1030 return block
1031 end
1032
1033 # Add a `line` to this block.
1034 fun add_line(line: MDLine) do
1035 if last_line == null then
1036 first_line = line
1037 last_line = line
1038 else
1039 last_line.next_empty = line.is_empty
1040 line.prev_empty = last_line.is_empty
1041 line.prev = last_line
1042 last_line.next = line
1043 last_line = line
1044 end
1045 end
1046
1047 # Remove `line` from this block.
1048 fun remove_line(line: MDLine) do
1049 if line.prev == null then
1050 first_line = line.next
1051 else
1052 line.prev.next = line.next
1053 end
1054 if line.next == null then
1055 last_line = line.prev
1056 else
1057 line.next.prev = line.prev
1058 end
1059 line.prev = null
1060 line.next = null
1061 end
1062
1063 # Remove leading empty lines.
1064 fun remove_leading_empty_lines: Bool do
1065 var was_empty = false
1066 var line = first_line
1067 while line != null and line.is_empty do
1068 remove_line line
1069 line = first_line
1070 was_empty = true
1071 end
1072 return was_empty
1073 end
1074
1075 # Remove trailing empty lines.
1076 fun remove_trailing_empty_lines: Bool do
1077 var was_empty = false
1078 var line = last_line
1079 while line != null and line.is_empty do
1080 remove_line line
1081 line = last_line
1082 was_empty = true
1083 end
1084 return was_empty
1085 end
1086
1087 # Remove leading and trailing empty lines.
1088 fun remove_surrounding_empty_lines: Bool do
1089 var was_empty = false
1090 if remove_leading_empty_lines then was_empty = true
1091 if remove_trailing_empty_lines then was_empty = true
1092 return was_empty
1093 end
1094
1095 # Remove list markers and up to 4 leading spaces.
1096 # Used to clean nested lists.
1097 fun remove_list_indent(v: MarkdownProcessor) do
1098 var line = first_line
1099 while line != null do
1100 if not line.is_empty then
1101 var kind = v.line_kind(line)
1102 if kind isa LineList then
1103 line.value = kind.extract_value(line)
1104 else
1105 line.value = line.value.substring_from(line.leading.min(4))
1106 end
1107 line.leading = line.process_leading
1108 end
1109 line = line.next
1110 end
1111 end
1112
1113 # Collect block line text.
1114 fun text: String do
1115 var text = new FlatBuffer
1116 var line = first_line
1117 while line != null do
1118 if not line.is_empty then
1119 text.append line.text
1120 end
1121 text.append "\n"
1122 line = line.next
1123 end
1124 return text.write_to_string
1125 end
1126 end
1127
1128 # Representation of a markdown block in the AST.
1129 # Each `Block` is linked to a `MDBlock` that contains mardown code.
1130 abstract class Block
1131
1132 # The markdown block `self` is related to.
1133 var block: MDBlock
1134
1135 # Output `self` using `v.decorator`.
1136 fun emit(v: MarkdownEmitter) do v.emit_in(self)
1137
1138 # Emit the containts of `self`, lines or blocks.
1139 fun emit_in(v: MarkdownEmitter) do
1140 block.remove_surrounding_empty_lines
1141 if block.has_lines then
1142 emit_lines(v)
1143 else
1144 emit_blocks(v)
1145 end
1146 end
1147
1148 # Emit lines contained in `block`.
1149 fun emit_lines(v: MarkdownEmitter) do
1150 var tpl = v.push_buffer
1151 var line = block.first_line
1152 while line != null do
1153 if not line.is_empty then
1154 v.add line.value.substring(line.leading, line.value.length - line.trailing)
1155 if line.trailing >= 2 then v.decorator.add_line_break(v)
1156 end
1157 if line.next != null then
1158 v.addn
1159 end
1160 line = line.next
1161 end
1162 v.pop_buffer
1163 v.emit_text(tpl)
1164 end
1165
1166 # Emit sub-blocks contained in `block`.
1167 fun emit_blocks(v: MarkdownEmitter) do
1168 var block = self.block.first_block
1169 while block != null do
1170 v.push_loc(block.location)
1171 block.kind.emit(v)
1172 v.pop_loc
1173 block = block.next
1174 end
1175 end
1176
1177 # The raw content of the block as a multi-line string.
1178 fun raw_content: String do
1179 var infence = self isa BlockFence
1180 var text = new FlatBuffer
1181 var line = self.block.first_line
1182 while line != null do
1183 if not line.is_empty then
1184 var str = line.value
1185 if not infence and str.has_prefix(" ") then
1186 text.append str.substring(4, str.length - line.trailing)
1187 else
1188 text.append str
1189 end
1190 end
1191 text.append "\n"
1192 line = line.next
1193 end
1194 return text.write_to_string
1195 end
1196 end
1197
1198 # A block without any markdown specificities.
1199 #
1200 # Actually use the same implementation than `BlockCode`,
1201 # this class is only used for typing purposes.
1202 class BlockNone
1203 super Block
1204 end
1205
1206 # A markdown blockquote.
1207 class BlockQuote
1208 super Block
1209
1210 redef fun emit(v) do v.decorator.add_blockquote(v, self)
1211
1212 # Remove blockquote markers.
1213 private fun remove_block_quote_prefix(block: MDBlock) do
1214 var line = block.first_line
1215 while line != null do
1216 if not line.is_empty then
1217 if line.value[line.leading] == '>' then
1218 var rem = line.leading + 1
1219 if line.leading + 1 < line.value.length and
1220 line.value[line.leading + 1] == ' ' then
1221 rem += 1
1222 end
1223 line.value = line.value.substring_from(rem)
1224 line.leading = line.process_leading
1225 end
1226 end
1227 line = line.next
1228 end
1229 end
1230 end
1231
1232 # A markdown code block.
1233 class BlockCode
1234 super Block
1235
1236 # Any string found after fence token.
1237 var meta: nullable Text
1238
1239 # Number of char to skip at the beginning of the line.
1240 #
1241 # Block code lines start at 4 spaces.
1242 protected var line_start = 4
1243
1244 redef fun emit(v) do v.decorator.add_code(v, self)
1245
1246 redef fun emit_lines(v) do
1247 var line = block.first_line
1248 while line != null do
1249 if not line.is_empty then
1250 v.decorator.append_code(v, line.value, line_start, line.value.length)
1251 end
1252 v.addn
1253 line = line.next
1254 end
1255 end
1256 end
1257
1258 # A markdown code-fence block.
1259 #
1260 # Actually use the same implementation than `BlockCode`,
1261 # this class is only used for typing purposes.
1262 class BlockFence
1263 super BlockCode
1264
1265 # Fence code lines start at 0 spaces.
1266 redef var line_start = 0
1267 end
1268
1269 # A markdown headline.
1270 class BlockHeadline
1271 super Block
1272
1273 redef fun emit(v) do
1274 var loc = block.location.copy
1275 loc.column_start += start
1276 v.push_loc(loc)
1277 v.decorator.add_headline(v, self)
1278 v.pop_loc
1279 end
1280
1281 private var start = 0
1282
1283 # Depth of the headline used to determine the headline level.
1284 var depth = 0
1285
1286 # Remove healine marks from lines contained in `self`.
1287 private fun transform_headline(block: MDBlock) do
1288 if depth > 0 then return
1289 var level = 0
1290 var line = block.first_line
1291 if line.is_empty then return
1292 var start = line.leading
1293 while start < line.value.length and line.value[start] == '#' do
1294 level += 1
1295 start += 1
1296 end
1297 while start < line.value.length and line.value[start] == ' ' do
1298 start += 1
1299 end
1300 if start >= line.value.length then
1301 line.is_empty = true
1302 else
1303 var nend = line.value.length - line.trailing - 1
1304 while line.value[nend] == '#' do nend -= 1
1305 while line.value[nend] == ' ' do nend -= 1
1306 line.value = line.value.substring(start, nend - start + 1)
1307 line.leading = 0
1308 line.trailing = 0
1309 end
1310 self.start = start
1311 depth = level.min(6)
1312 end
1313 end
1314
1315 # A markdown list item block.
1316 class BlockListItem
1317 super Block
1318
1319 redef fun emit(v) do v.decorator.add_listitem(v, self)
1320 end
1321
1322 # A markdown list block.
1323 # Can be either an ordered or unordered list, this class is mainly used to factorize code.
1324 abstract class BlockList
1325 super Block
1326
1327 # Split list block into list items sub-blocks.
1328 private fun init_block(v: MarkdownProcessor) do
1329 var line = block.first_line
1330 line = line.next
1331 while line != null do
1332 var t = v.line_kind(line)
1333 if t isa LineList or
1334 (not line.is_empty and (line.prev_empty and line.leading == 0 and
1335 not (t isa LineList))) then
1336 var sblock = block.split(line.prev.as(not null))
1337 sblock.kind = new BlockListItem(sblock)
1338 end
1339 line = line.next
1340 end
1341 var sblock = block.split(block.last_line.as(not null))
1342 sblock.kind = new BlockListItem(sblock)
1343 end
1344
1345 # Expand list items as paragraphs if needed.
1346 private fun expand_paragraphs(block: MDBlock) do
1347 var outer = block.first_block
1348 var inner: nullable MDBlock
1349 var has_paragraph = false
1350 while outer != null and not has_paragraph do
1351 if outer.kind isa BlockListItem then
1352 inner = outer.first_block
1353 while inner != null and not has_paragraph do
1354 if inner.kind isa BlockParagraph then
1355 has_paragraph = true
1356 end
1357 inner = inner.next
1358 end
1359 end
1360 outer = outer.next
1361 end
1362 if has_paragraph then
1363 outer = block.first_block
1364 while outer != null do
1365 if outer.kind isa BlockListItem then
1366 inner = outer.first_block
1367 while inner != null do
1368 if inner.kind isa BlockNone then
1369 inner.kind = new BlockParagraph(inner)
1370 end
1371 inner = inner.next
1372 end
1373 end
1374 outer = outer.next
1375 end
1376 end
1377 end
1378 end
1379
1380 # A markdown ordered list.
1381 class BlockOrderedList
1382 super BlockList
1383
1384 redef fun emit(v) do v.decorator.add_orderedlist(v, self)
1385 end
1386
1387 # A markdown unordred list.
1388 class BlockUnorderedList
1389 super BlockList
1390
1391 redef fun emit(v) do v.decorator.add_unorderedlist(v, self)
1392 end
1393
1394 # A markdown paragraph block.
1395 class BlockParagraph
1396 super Block
1397
1398 redef fun emit(v) do v.decorator.add_paragraph(v, self)
1399 end
1400
1401 # A markdown ruler.
1402 class BlockRuler
1403 super Block
1404
1405 redef fun emit(v) do v.decorator.add_ruler(v, self)
1406 end
1407
1408 # Xml blocks that can be found in markdown markup.
1409 class BlockXML
1410 super Block
1411
1412 redef fun emit_lines(v) do
1413 var line = block.first_line
1414 while line != null do
1415 if not line.is_empty then v.add line.value
1416 v.addn
1417 line = line.next
1418 end
1419 end
1420 end
1421
1422 # A markdown line.
1423 class MDLine
1424
1425 # Location of `self` in the original input.
1426 var location: MDLocation
1427
1428 # Text contained in this line.
1429 var value: String is writable
1430
1431 # Is this line empty?
1432 # Lines containing only spaces are considered empty.
1433 var is_empty: Bool = true is writable
1434
1435 # Previous line in `MDBlock` or null if first line.
1436 var prev: nullable MDLine = null is writable
1437
1438 # Next line in `MDBlock` or null if last line.
1439 var next: nullable MDLine = null is writable
1440
1441 # Is the previous line empty?
1442 var prev_empty: Bool = false is writable
1443
1444 # Is the next line empty?
1445 var next_empty: Bool = false is writable
1446
1447 # Initialize a new MDLine from its string value
1448 init do
1449 self.leading = process_leading
1450 if leading != value.length then
1451 self.is_empty = false
1452 self.trailing = process_trailing
1453 end
1454 end
1455
1456 # Set `value` as an empty String and update `leading`, `trailing` and is_`empty`.
1457 fun clear do
1458 value = ""
1459 leading = 0
1460 trailing = 0
1461 is_empty = true
1462 if prev != null then prev.next_empty = true
1463 if next != null then next.prev_empty = true
1464 end
1465
1466 # Number or leading spaces on this line.
1467 var leading: Int = 0 is writable
1468
1469 # Compute `leading` depending on `value`.
1470 fun process_leading: Int do
1471 var count = 0
1472 var value = self.value
1473 while count < value.length and value[count] == ' ' do count += 1
1474 if leading == value.length then clear
1475 return count
1476 end
1477
1478 # Number of trailing spaces on this line.
1479 var trailing: Int = 0 is writable
1480
1481 # Compute `trailing` depending on `value`.
1482 fun process_trailing: Int do
1483 var count = 0
1484 var value = self.value
1485 while value[value.length - count - 1] == ' ' do
1486 count += 1
1487 end
1488 return count
1489 end
1490
1491 # Count the amount of `ch` in this line.
1492 # Return A value > 0 if this line only consists of `ch` end spaces.
1493 fun count_chars(ch: Char): Int do
1494 var count = 0
1495 for c in value do
1496 if c == ' ' then
1497 continue
1498 end
1499 if c == ch then
1500 count += 1
1501 continue
1502 end
1503 count = 0
1504 break
1505 end
1506 return count
1507 end
1508
1509 # Count the amount of `ch` at the start of this line ignoring spaces.
1510 fun count_chars_start(ch: Char): Int do
1511 var count = 0
1512 for c in value do
1513 if c == ' ' then
1514 continue
1515 end
1516 if c == ch then
1517 count += 1
1518 else
1519 break
1520 end
1521 end
1522 return count
1523 end
1524
1525 # Last XML line if any.
1526 private var xml_end_line: nullable MDLine = null
1527
1528 # Does `value` contains valid XML markup?
1529 private fun check_html: Bool do
1530 var tags = new Array[String]
1531 var tmp = new FlatBuffer
1532 var pos = leading
1533 if pos + 1 < value.length and value[pos + 1] == '!' then
1534 if read_xml_comment(self, pos) > 0 then return true
1535 end
1536 pos = value.read_xml(tmp, pos, false)
1537 var tag: String
1538 if pos > -1 then
1539 tag = tmp.xml_tag
1540 if not tag.is_html_block then
1541 return false
1542 end
1543 if tag == "hr" then
1544 xml_end_line = self
1545 return true
1546 end
1547 tags.add tag
1548 var line: nullable MDLine = self
1549 while line != null do
1550 while pos < line.value.length and line.value[pos] != '<' do
1551 pos += 1
1552 end
1553 if pos >= line.value.length then
1554 if pos - 2 >= 0 and line.value[pos - 2] == '/' then
1555 tags.pop
1556 if tags.is_empty then
1557 xml_end_line = line
1558 break
1559 end
1560 end
1561 line = line.next
1562 pos = 0
1563 else
1564 tmp = new FlatBuffer
1565 var new_pos = line.value.read_xml(tmp, pos, false)
1566 if new_pos > 0 then
1567 tag = tmp.xml_tag
1568 if tag.is_html_block and not tag == "hr" then
1569 if tmp[1] == '/' then
1570 if tags.last != tag then
1571 return false
1572 end
1573 tags.pop
1574 else
1575 tags.add tag
1576 end
1577 end
1578 if tags.is_empty then
1579 xml_end_line = line
1580 break
1581 end
1582 pos = new_pos
1583 else
1584 pos += 1
1585 end
1586 end
1587 end
1588 return tags.is_empty
1589 end
1590 return false
1591 end
1592
1593 # Read a XML comment.
1594 # Used by `check_html`.
1595 private fun read_xml_comment(first_line: MDLine, start: Int): Int do
1596 var line: nullable MDLine = first_line
1597 if start + 3 < line.value.length then
1598 if line.value[2] == '-' and line.value[3] == '-' then
1599 var pos = start + 4
1600 while line != null do
1601 while pos < line.value.length and line.value[pos] != '-' do
1602 pos += 1
1603 end
1604 if pos == line.value.length then
1605 line = line.next
1606 pos = 0
1607 else
1608 if pos + 2 < line.value.length then
1609 if line.value[pos + 1] == '-' and line.value[pos + 2] == '>' then
1610 first_line.xml_end_line = line
1611 return pos + 3
1612 end
1613 end
1614 pos += 1
1615 end
1616 end
1617 end
1618 end
1619 return -1
1620 end
1621
1622 # Extract the text of `self` without leading and trailing.
1623 fun text: String do return value.substring(leading, value.length - trailing)
1624 end
1625
1626 # A markdown line.
1627 interface Line
1628
1629 # Parse the line.
1630 # See `MarkdownProcessor::recurse`.
1631 fun process(v: MarkdownProcessor) is abstract
1632 end
1633
1634 # An empty markdown line.
1635 class LineEmpty
1636 super Line
1637
1638 redef fun process(v) do
1639 v.current_line = v.current_line.next
1640 end
1641 end
1642
1643 # A non-specific markdown construction.
1644 # Mainly used as part of another line construct such as paragraphs or lists.
1645 class LineOther
1646 super Line
1647
1648 redef fun process(v) do
1649 var line = v.current_line
1650 # go to block end
1651 var was_empty = line.prev_empty
1652 while line != null and not line.is_empty do
1653 var t = v.line_kind(line)
1654 if (v.in_list or v.ext_mode) and t isa LineList then
1655 break
1656 end
1657 if v.ext_mode and (t isa LineCode or t isa LineFence) then
1658 break
1659 end
1660 if t isa LineHeadline or t isa LineHeadline1 or t isa LineHeadline2 or
1661 t isa LineHR or t isa LineBlockquote or t isa LineXML then
1662 break
1663 end
1664 line = line.next
1665 end
1666 # build block
1667 if line != null and not line.is_empty then
1668 var block = v.current_block.split(line.prev.as(not null))
1669 if v.in_list and not was_empty then
1670 block.kind = new BlockNone(block)
1671 else
1672 block.kind = new BlockParagraph(block)
1673 end
1674 v.current_block.remove_leading_empty_lines
1675 else
1676 var block: MDBlock
1677 if line != null then
1678 block = v.current_block.split(line)
1679 else
1680 block = v.current_block.split(v.current_block.last_line.as(not null))
1681 end
1682 if v.in_list and (line == null or not line.is_empty) and not was_empty then
1683 block.kind = new BlockNone(block)
1684 else
1685 block.kind = new BlockParagraph(block)
1686 end
1687 v.current_block.remove_leading_empty_lines
1688 end
1689 v.current_line = v.current_block.first_line
1690 end
1691 end
1692
1693 # A line of markdown code.
1694 class LineCode
1695 super Line
1696
1697 redef fun process(v) do
1698 var line = v.current_line
1699 # lookup block end
1700 while line != null and (line.is_empty or v.line_kind(line) isa LineCode) do
1701 line = line.next
1702 end
1703 # split at block end line
1704 var block: MDBlock
1705 if line != null then
1706 block = v.current_block.split(line.prev.as(not null))
1707 else
1708 block = v.current_block.split(v.current_block.last_line.as(not null))
1709 end
1710 block.kind = new BlockCode(block)
1711 block.remove_surrounding_empty_lines
1712 v.current_line = v.current_block.first_line
1713 end
1714 end
1715
1716 # A line of raw XML.
1717 class LineXML
1718 super Line
1719
1720 redef fun process(v) do
1721 var line = v.current_line
1722 var prev = line.prev
1723 if prev != null then v.current_block.split(prev)
1724 var block = v.current_block.split(line.xml_end_line.as(not null))
1725 block.kind = new BlockXML(block)
1726 v.current_block.remove_leading_empty_lines
1727 v.current_line = v.current_block.first_line
1728 end
1729 end
1730
1731 # A markdown blockquote line.
1732 class LineBlockquote
1733 super Line
1734
1735 redef fun process(v) do
1736 var line = v.current_line
1737 # go to bquote end
1738 while line != null do
1739 if not line.is_empty and (line.prev_empty and
1740 line.leading == 0 and
1741 not v.line_kind(line) isa LineBlockquote) then break
1742 line = line.next
1743 end
1744 # build sub block
1745 var block: MDBlock
1746 if line != null then
1747 block = v.current_block.split(line.prev.as(not null))
1748 else
1749 block = v.current_block.split(v.current_block.last_line.as(not null))
1750 end
1751 var kind = new BlockQuote(block)
1752 block.kind = kind
1753 block.remove_surrounding_empty_lines
1754 kind.remove_block_quote_prefix(block)
1755 v.current_line = line
1756 v.recurse(block, false)
1757 v.current_line = v.current_block.first_line
1758 end
1759 end
1760
1761 # A markdown ruler line.
1762 class LineHR
1763 super Line
1764
1765 redef fun process(v) do
1766 var line = v.current_line
1767 if line.prev != null then v.current_block.split(line.prev.as(not null))
1768 var block = v.current_block.split(line.as(not null))
1769 block.kind = new BlockRuler(block)
1770 v.current_block.remove_leading_empty_lines
1771 v.current_line = v.current_block.first_line
1772 end
1773 end
1774
1775 # A markdown fence code line.
1776 class LineFence
1777 super Line
1778
1779 redef fun process(v) do
1780 # go to fence end
1781 var line = v.current_line.next
1782 while line != null do
1783 if v.line_kind(line) isa LineFence then break
1784 line = line.next
1785 end
1786 if line != null then
1787 line = line.next
1788 end
1789 # build fence block
1790 var block: MDBlock
1791 if line != null then
1792 block = v.current_block.split(line.prev.as(not null))
1793 else
1794 block = v.current_block.split(v.current_block.last_line.as(not null))
1795 end
1796 block.remove_surrounding_empty_lines
1797 var meta = block.first_line.value.meta_from_fence
1798 block.kind = new BlockFence(block, meta)
1799 block.first_line.clear
1800 var last = block.last_line
1801 if last != null and v.line_kind(last) isa LineFence then
1802 block.last_line.clear
1803 end
1804 block.remove_surrounding_empty_lines
1805 v.current_line = line
1806 end
1807 end
1808
1809 # A markdown headline.
1810 class LineHeadline
1811 super Line
1812
1813 redef fun process(v) do
1814 var line = v.current_line
1815 var lprev = line.prev
1816 if lprev != null then v.current_block.split(lprev)
1817 var block = v.current_block.split(line.as(not null))
1818 var kind = new BlockHeadline(block)
1819 block.kind = kind
1820 kind.transform_headline(block)
1821 v.current_block.remove_leading_empty_lines
1822 v.current_line = v.current_block.first_line
1823 end
1824 end
1825
1826 # A markdown headline of level 1.
1827 class LineHeadline1
1828 super LineHeadline
1829
1830 redef fun process(v) do
1831 var line = v.current_line
1832 var lprev = line.prev
1833 if lprev != null then v.current_block.split(lprev)
1834 line.next.clear
1835 var block = v.current_block.split(line.as(not null))
1836 var kind = new BlockHeadline(block)
1837 kind.depth = 1
1838 kind.transform_headline(block)
1839 block.kind = kind
1840 v.current_block.remove_leading_empty_lines
1841 v.current_line = v.current_block.first_line
1842 end
1843 end
1844
1845 # A markdown headline of level 2.
1846 class LineHeadline2
1847 super LineHeadline
1848
1849 redef fun process(v) do
1850 var line = v.current_line
1851 var lprev = line.prev
1852 if lprev != null then v.current_block.split(lprev)
1853 line.next.clear
1854 var block = v.current_block.split(line.as(not null))
1855 var kind = new BlockHeadline(block)
1856 kind.depth = 2
1857 kind.transform_headline(block)
1858 block.kind = kind
1859 v.current_block.remove_leading_empty_lines
1860 v.current_line = v.current_block.first_line
1861 end
1862 end
1863
1864 # A markdown list line.
1865 # Mainly used to factorize code between ordered and unordered lists.
1866 abstract class LineList
1867 super Line
1868
1869 redef fun process(v) do
1870 var line = v.current_line
1871 # go to list end
1872 while line != null do
1873 var t = v.line_kind(line)
1874 if not line.is_empty and (line.prev_empty and line.leading == 0 and
1875 not t isa LineList) then break
1876 line = line.next
1877 end
1878 # build list block
1879 var list: MDBlock
1880 if line != null then
1881 list = v.current_block.split(line.prev.as(not null))
1882 else
1883 list = v.current_block.split(v.current_block.last_line.as(not null))
1884 end
1885 var kind = block_kind(list)
1886 list.kind = kind
1887 list.first_line.prev_empty = false
1888 list.last_line.next_empty = false
1889 list.remove_surrounding_empty_lines
1890 list.first_line.prev_empty = false
1891 list.last_line.next_empty = false
1892 kind.init_block(v)
1893 var block = list.first_block
1894 while block != null do
1895 block.remove_list_indent(v)
1896 v.recurse(block, true)
1897 block = block.next
1898 end
1899 kind.expand_paragraphs(list)
1900 v.current_line = line
1901 end
1902
1903 # Create a new block kind based on this line.
1904 protected fun block_kind(block: MDBlock): BlockList is abstract
1905
1906 # Extract string value from `MDLine`.
1907 protected fun extract_value(line: MDLine): String is abstract
1908 end
1909
1910 # An ordered list line.
1911 class LineOList
1912 super LineList
1913
1914 redef fun block_kind(block) do return new BlockOrderedList(block)
1915
1916 redef fun extract_value(line) do
1917 return line.value.substring_from(line.value.index_of('.') + 2)
1918 end
1919 end
1920
1921 # An unordered list line.
1922 class LineUList
1923 super LineList
1924
1925 redef fun block_kind(block) do return new BlockUnorderedList(block)
1926
1927 redef fun extract_value(line) do
1928 return line.value.substring_from(line.leading + 2)
1929 end
1930 end
1931
1932 # A token represent a character in the markdown input.
1933 # Some tokens have a specific markup behaviour that is handled here.
1934 abstract class Token
1935
1936 # Location of `self` in the original input.
1937 var location: MDLocation
1938
1939 # Position of `self` in input independant from lines.
1940 var pos: Int
1941
1942 # Character found at `pos` in the markdown input.
1943 var char: Char
1944
1945 # Output that token using `MarkdownEmitter::decorator`.
1946 fun emit(v: MarkdownEmitter) do v.decorator.add_char(v, char)
1947 end
1948
1949 # A token without a specific meaning.
1950 class TokenNone
1951 super Token
1952 end
1953
1954 # An emphasis token.
1955 abstract class TokenEm
1956 super Token
1957
1958 redef fun emit(v) do
1959 var tmp = v.push_buffer
1960 var b = v.emit_text_until(v.current_text.as(not null), pos + 1, self)
1961 v.pop_buffer
1962 if b > 0 then
1963 v.decorator.add_em(v, tmp)
1964 v.current_pos = b
1965 else
1966 v.addc char
1967 end
1968 end
1969 end
1970
1971 # An emphasis star token.
1972 class TokenEmStar
1973 super TokenEm
1974 end
1975
1976 # An emphasis underscore token.
1977 class TokenEmUnderscore
1978 super TokenEm
1979 end
1980
1981 # A strong token.
1982 abstract class TokenStrong
1983 super Token
1984
1985 redef fun emit(v) do
1986 var tmp = v.push_buffer
1987 var b = v.emit_text_until(v.current_text.as(not null), pos + 2, self)
1988 v.pop_buffer
1989 if b > 0 then
1990 v.decorator.add_strong(v, tmp)
1991 v.current_pos = b + 1
1992 else
1993 v.addc char
1994 end
1995 end
1996 end
1997
1998 # A strong star token.
1999 class TokenStrongStar
2000 super TokenStrong
2001 end
2002
2003 # A strong underscore token.
2004 class TokenStrongUnderscore
2005 super TokenStrong
2006 end
2007
2008 # A code token.
2009 # This class is mainly used to factorize work between single and double quoted span codes.
2010 abstract class TokenCode
2011 super Token
2012
2013 redef fun emit(v) do
2014 var a = pos + next_pos + 1
2015 var b = v.processor.find_token(v.current_text.as(not null), a, self)
2016 if b > 0 then
2017 v.current_pos = b + next_pos
2018 while a < b and v.current_text[a] == ' ' do a += 1
2019 if a < b then
2020 while v.current_text[b - 1] == ' ' do b -= 1
2021 v.decorator.add_span_code(v, v.current_text.as(not null), a, b)
2022 end
2023 else
2024 v.addc char
2025 end
2026 end
2027
2028 private fun next_pos: Int is abstract
2029 end
2030
2031 # A span code token.
2032 class TokenCodeSingle
2033 super TokenCode
2034
2035 redef fun next_pos do return 0
2036 end
2037
2038 # A doubled span code token.
2039 class TokenCodeDouble
2040 super TokenCode
2041
2042 redef fun next_pos do return 1
2043 end
2044
2045 # A link or image token.
2046 # This class is mainly used to factorize work between images and links.
2047 abstract class TokenLinkOrImage
2048 super Token
2049
2050 # Link adress
2051 var link: nullable Text = null
2052
2053 # Link text
2054 var name: nullable Text = null
2055
2056 # Link title
2057 var comment: nullable Text = null
2058
2059 # Is the link construct an abbreviation?
2060 var is_abbrev = false
2061
2062 redef fun emit(v) do
2063 var tmp = new FlatBuffer
2064 var b = check_link(v, tmp, pos, self)
2065 if b > 0 then
2066 emit_hyper(v)
2067 v.current_pos = b
2068 else
2069 v.addc char
2070 end
2071 end
2072
2073 # Emit the hyperlink as link or image.
2074 private fun emit_hyper(v: MarkdownEmitter) is abstract
2075
2076 # Check if the link is a valid link.
2077 private fun check_link(v: MarkdownEmitter, out: FlatBuffer, start: Int, token: Token): Int do
2078 var md = v.current_text
2079 var pos
2080 if token isa TokenLink then
2081 pos = start + 1
2082 else
2083 pos = start + 2
2084 end
2085 var tmp = new FlatBuffer
2086 pos = md.read_md_link_id(tmp, pos)
2087 if pos < start then return -1
2088 name = tmp
2089 var old_pos = pos
2090 pos += 1
2091 pos = md.skip_spaces(pos)
2092 if pos < start then
2093 var tid = name.write_to_string.to_lower
2094 if v.processor.link_refs.has_key(tid) then
2095 var lr = v.processor.link_refs[tid]
2096 is_abbrev = lr.is_abbrev
2097 link = lr.link
2098 comment = lr.title
2099 pos = old_pos
2100 else
2101 return -1
2102 end
2103 else if md[pos] == '(' then
2104 pos += 1
2105 pos = md.skip_spaces(pos)
2106 if pos < start then return -1
2107 tmp = new FlatBuffer
2108 var use_lt = md[pos] == '<'
2109 if use_lt then
2110 pos = md.read_until(tmp, pos + 1, '>')
2111 else
2112 pos = md.read_md_link(tmp, pos)
2113 end
2114 if pos < start then return -1
2115 if use_lt then pos += 1
2116 link = tmp.write_to_string
2117 if md[pos] == ' ' then
2118 pos = md.skip_spaces(pos)
2119 if pos > start and md[pos] == '"' then
2120 pos += 1
2121 tmp = new FlatBuffer
2122 pos = md.read_until(tmp, pos, '"')
2123 if pos < start then return -1
2124 comment = tmp.write_to_string
2125 pos += 1
2126 pos = md.skip_spaces(pos)
2127 if pos == -1 then return -1
2128 end
2129 end
2130 if pos < start then return -1
2131 if md[pos] != ')' then return -1
2132 else if md[pos] == '[' then
2133 pos += 1
2134 tmp = new FlatBuffer
2135 pos = md.read_raw_until(tmp, pos, ']')
2136 if pos < start then return -1
2137 var id
2138 if tmp.length > 0 then
2139 id = tmp
2140 else
2141 id = name
2142 end
2143 var tid = id.write_to_string.to_lower
2144 if v.processor.link_refs.has_key(tid) then
2145 var lr = v.processor.link_refs[tid]
2146 link = lr.link
2147 comment = lr.title
2148 end
2149 else
2150 var tid = name.write_to_string.replace("\n", " ").to_lower
2151 if v.processor.link_refs.has_key(tid) then
2152 var lr = v.processor.link_refs[tid]
2153 link = lr.link
2154 comment = lr.title
2155 pos = old_pos
2156 else
2157 return -1
2158 end
2159 end
2160 if link == null then return -1
2161 return pos
2162 end
2163 end
2164
2165 # A markdown link token.
2166 class TokenLink
2167 super TokenLinkOrImage
2168
2169 redef fun emit_hyper(v) do
2170 if is_abbrev and comment != null then
2171 v.decorator.add_abbr(v, name.as(not null), comment.as(not null))
2172 else
2173 v.decorator.add_link(v, link.as(not null), name.as(not null), comment)
2174 end
2175 end
2176 end
2177
2178 # A markdown image token.
2179 class TokenImage
2180 super TokenLinkOrImage
2181
2182 redef fun emit_hyper(v) do
2183 v.decorator.add_image(v, link.as(not null), name.as(not null), comment)
2184 end
2185 end
2186
2187 # A HTML/XML token.
2188 class TokenHTML
2189 super Token
2190
2191 redef fun emit(v) do
2192 var tmp = new FlatBuffer
2193 var b = check_html(v, tmp, v.current_text.as(not null), v.current_pos)
2194 if b > 0 then
2195 v.add tmp
2196 v.current_pos = b
2197 else
2198 v.decorator.escape_char(v, char)
2199 end
2200 end
2201
2202 # Is the HTML valid?
2203 # Also take care of link and mailto shortcuts.
2204 private fun check_html(v: MarkdownEmitter, out: FlatBuffer, md: Text, start: Int): Int do
2205 # check for auto links
2206 var tmp = new FlatBuffer
2207 var pos = md.read_until(tmp, start + 1, ':', ' ', '>', '\n')
2208 if pos != -1 and md[pos] == ':' and tmp.is_link_prefix then
2209 pos = md.read_until(tmp, pos, '>')
2210 if pos != -1 then
2211 var link = tmp.write_to_string
2212 v.decorator.add_link(v, link, link, null)
2213 return pos
2214 end
2215 end
2216 # TODO check for mailto
2217 # check for inline html
2218 if start + 2 < md.length then
2219 return md.read_xml(out, start, true)
2220 end
2221 return -1
2222 end
2223 end
2224
2225 # An HTML entity token.
2226 class TokenEntity
2227 super Token
2228
2229 redef fun emit(v) do
2230 var tmp = new FlatBuffer
2231 var b = check_entity(tmp, v.current_text.as(not null), pos)
2232 if b > 0 then
2233 v.add tmp
2234 v.current_pos = b
2235 else
2236 v.decorator.escape_char(v, char)
2237 end
2238 end
2239
2240 # Is the entity valid?
2241 private fun check_entity(out: FlatBuffer, md: Text, start: Int): Int do
2242 var pos = md.read_until(out, start, ';')
2243 if pos < 0 or out.length < 3 then
2244 return -1
2245 end
2246 if out[1] == '#' then
2247 if out[2] == 'x' or out[2] == 'X' then
2248 if out.length < 4 then return -1
2249 for i in [3..out.length[ do
2250 var c = out[i]
2251 if (c < '0' or c > '9') and (c < 'a' and c > 'f') and (c < 'A' and c > 'F') then
2252 return -1
2253 end
2254 end
2255 else
2256 for i in [2..out.length[ do
2257 var c = out[i]
2258 if c < '0' or c > '9' then return -1
2259 end
2260 end
2261 out.add ';'
2262 else
2263 for i in [1..out.length[ do
2264 var c = out[i]
2265 if not c.is_digit and not c.is_letter then return -1
2266 end
2267 out.add ';'
2268 # TODO check entity is valid
2269 # if out.is_entity then
2270 return pos
2271 # else
2272 # return -1
2273 # end
2274 end
2275 return pos
2276 end
2277 end
2278
2279 # A markdown escape token.
2280 class TokenEscape
2281 super Token
2282
2283 redef fun emit(v) do
2284 v.current_pos += 1
2285 v.addc v.current_text[v.current_pos]
2286 end
2287 end
2288
2289 # A markdown strike token.
2290 #
2291 # Extended mode only (see `MarkdownProcessor::ext_mode`)
2292 class TokenStrike
2293 super Token
2294
2295 redef fun emit(v) do
2296 var tmp = v.push_buffer
2297 var b = v.emit_text_until(v.current_text.as(not null), pos + 2, self)
2298 v.pop_buffer
2299 if b > 0 then
2300 v.decorator.add_strike(v, tmp)
2301 v.current_pos = b + 1
2302 else
2303 v.addc char
2304 end
2305 end
2306 end
2307
2308 redef class Text
2309
2310 # Get the position of the next non-space character.
2311 private fun skip_spaces(start: Int): Int do
2312 var pos = start
2313 while pos > -1 and pos < length and (self[pos] == ' ' or self[pos] == '\n') do
2314 pos += 1
2315 end
2316 if pos < length then return pos
2317 return -1
2318 end
2319
2320 # Read `self` until `nend` and append it to the `out` buffer.
2321 # Escape markdown special chars.
2322 private fun read_until(out: FlatBuffer, start: Int, nend: Char...): Int do
2323 var pos = start
2324 while pos < length do
2325 var c = self[pos]
2326 if c == '\\' and pos + 1 < length then
2327 pos = escape(out, self[pos + 1], pos)
2328 else
2329 var end_reached = false
2330 for n in nend do
2331 if c == n then
2332 end_reached = true
2333 break
2334 end
2335 end
2336 if end_reached then break
2337 out.add c
2338 end
2339 pos += 1
2340 end
2341 if pos == length then return -1
2342 return pos
2343 end
2344
2345 # Read `self` as raw text until `nend` and append it to the `out` buffer.
2346 # No escape is made.
2347 private fun read_raw_until(out: FlatBuffer, start: Int, nend: Char...): Int do
2348 var pos = start
2349 while pos < length do
2350 var c = self[pos]
2351 var end_reached = false
2352 for n in nend do
2353 if c == n then
2354 end_reached = true
2355 break
2356 end
2357 end
2358 if end_reached then break
2359 out.add c
2360 pos += 1
2361 end
2362 if pos == length then return -1
2363 return pos
2364 end
2365
2366 # Read `self` as XML until `to` and append it to the `out` buffer.
2367 # Escape HTML special chars.
2368 private fun read_xml_until(out: FlatBuffer, from: Int, to: Char...): Int do
2369 var pos = from
2370 var in_str = false
2371 var str_char: nullable Char = null
2372 while pos < length do
2373 var c = self[pos]
2374 if in_str then
2375 if c == '\\' then
2376 out.add c
2377 pos += 1
2378 if pos < length then
2379 out.add c
2380 pos += 1
2381 end
2382 continue
2383 end
2384 if c == str_char then
2385 in_str = false
2386 out.add c
2387 pos += 1
2388 continue
2389 end
2390 end
2391 if c == '"' or c == '\'' then
2392 in_str = true
2393 str_char = c
2394 end
2395 if not in_str then
2396 var end_reached = false
2397 for n in [0..to.length[ do
2398 if c == to[n] then
2399 end_reached = true
2400 break
2401 end
2402 end
2403 if end_reached then break
2404 end
2405 out.add c
2406 pos += 1
2407 end
2408 if pos == length then return -1
2409 return pos
2410 end
2411
2412 # Read `self` as XML and append it to the `out` buffer.
2413 # Safe mode can be activated to limit reading to valid xml.
2414 private fun read_xml(out: FlatBuffer, start: Int, safe_mode: Bool): Int do
2415 var pos = 0
2416 var is_valid = true
2417 var is_close_tag = false
2418 if start + 1 >= length then return -1
2419 if self[start + 1] == '/' then
2420 is_close_tag = true
2421 pos = start + 2
2422 else if self[start + 1] == '!' then
2423 out.append "<!"
2424 return start + 1
2425 else
2426 is_close_tag = false
2427 pos = start + 1
2428 end
2429 if safe_mode then
2430 var tmp = new FlatBuffer
2431 pos = read_xml_until(tmp, pos, ' ', '/', '>')
2432 if pos == -1 then return -1
2433 var tag = tmp.write_to_string.trim.to_lower
2434 if not tag.is_valid_html_tag then
2435 out.append "&lt;"
2436 pos = -1
2437 else if tag.is_html_unsafe then
2438 is_valid = false
2439 out.append "&lt;"
2440 if is_close_tag then out.add '/'
2441 out.append tmp
2442 else
2443 out.append "<"
2444 if is_close_tag then out.add '/'
2445 out.append tmp
2446 end
2447 else
2448 out.add '<'
2449 if is_close_tag then out.add '/'
2450 pos = read_xml_until(out, pos, ' ', '/', '>')
2451 end
2452 if pos == -1 then return -1
2453 pos = read_xml_until(out, pos, '/', '>')
2454 if pos == -1 then return -1
2455 if self[pos] == '/' then
2456 out.append " /"
2457 pos = self.read_xml_until(out, pos + 1, '>')
2458 if pos == -1 then return -1
2459 end
2460 if self[pos] == '>' then
2461 if is_valid then
2462 out.add '>'
2463 else
2464 out.append "&gt;"
2465 end
2466 return pos
2467 end
2468 return -1
2469 end
2470
2471 # Read a markdown link address and append it to the `out` buffer.
2472 private fun read_md_link(out: FlatBuffer, start: Int): Int do
2473 var pos = start
2474 var counter = 1
2475 while pos < length do
2476 var c = self[pos]
2477 if c == '\\' and pos + 1 < length then
2478 pos = escape(out, self[pos + 1], pos)
2479 else
2480 var end_reached = false
2481 if c == '(' then
2482 counter += 1
2483 else if c == ' ' then
2484 if counter == 1 then end_reached = true
2485 else if c == ')' then
2486 counter -= 1
2487 if counter == 0 then end_reached = true
2488 end
2489 if end_reached then break
2490 out.add c
2491 end
2492 pos += 1
2493 end
2494 if pos == length then return -1
2495 return pos
2496 end
2497
2498 # Read a markdown link text and append it to the `out` buffer.
2499 private fun read_md_link_id(out: FlatBuffer, start: Int): Int do
2500 var pos = start
2501 var counter = 1
2502 while pos < length do
2503 var c = self[pos]
2504 var end_reached = false
2505 if c == '[' then
2506 counter += 1
2507 out.add c
2508 else if c == ']' then
2509 counter -= 1
2510 if counter == 0 then
2511 end_reached = true
2512 else
2513 out.add c
2514 end
2515 else
2516 out.add c
2517 end
2518 if end_reached then break
2519 pos += 1
2520 end
2521 if pos == length then return -1
2522 return pos
2523 end
2524
2525 # Extract the XML tag name from a XML tag.
2526 private fun xml_tag: String do
2527 var tpl = new FlatBuffer
2528 var pos = 1
2529 if pos < length and self[1] == '/' then pos += 1
2530 while pos < length - 1 and (self[pos].is_digit or self[pos].is_letter) do
2531 tpl.add self[pos]
2532 pos += 1
2533 end
2534 return tpl.write_to_string.to_lower
2535 end
2536
2537 private fun is_valid_html_tag: Bool do
2538 if is_empty then return false
2539 for c in self do
2540 if not c.is_alpha then return false
2541 end
2542 return true
2543 end
2544
2545 # Read and escape the markdown contained in `self`.
2546 private fun escape(out: FlatBuffer, c: Char, pos: Int): Int do
2547 if c == '\\' or c == '[' or c == ']' or c == '(' or c == ')' or c == '{' or
2548 c == '}' or c == '#' or c == '"' or c == '\'' or c == '.' or c == '<' or
2549 c == '>' or c == '*' or c == '+' or c == '-' or c == '_' or c == '!' or
2550 c == '`' or c == '~' or c == '^' then
2551 out.add c
2552 return pos + 1
2553 end
2554 out.add '\\'
2555 return pos
2556 end
2557
2558 # Extract string found at end of fence opening.
2559 private fun meta_from_fence: nullable Text do
2560 for i in [0..chars.length[ do
2561 var c = chars[i]
2562 if c != ' ' and c != '`' and c != '~' then
2563 return substring_from(i).trim
2564 end
2565 end
2566 return null
2567 end
2568
2569 # Is `self` an unsafe HTML element?
2570 private fun is_html_unsafe: Bool do return html_unsafe_tags.has(self.write_to_string)
2571
2572 # Is `self` a HRML block element?
2573 private fun is_html_block: Bool do return html_block_tags.has(self.write_to_string)
2574
2575 # Is `self` a link prefix?
2576 private fun is_link_prefix: Bool do return html_link_prefixes.has(self.write_to_string)
2577
2578 private fun html_unsafe_tags: Array[String] do return once ["applet", "head", "body", "frame", "frameset", "iframe", "script", "object"]
2579
2580 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"]
2581
2582 private fun html_link_prefixes: Array[String] do return once ["http", "https", "ftp", "ftps"]
2583 end
2584
2585 redef class String
2586
2587 # Parse `self` as markdown and return the HTML representation
2588 #.
2589 # var md = "**Hello World!**"
2590 # var html = md.md_to_html
2591 # assert html == "<p><strong>Hello World!</strong></p>\n"
2592 fun md_to_html: Writable do
2593 var processor = new MarkdownProcessor
2594 return processor.process(self)
2595 end
2596 end