3 * @copyright Copyright (c) 2014 Carsten Brandt
4 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5 * @link https://github.com/cebe/markdown#readme
8 namespace cebe\markdown;
11 * Markdown parser for the [initial markdown spec](http://daringfireball.net/projects/markdown/syntax).
13 * @author Carsten Brandt <mail@cebe.cc>
15 class Markdown extends Parser
17 // include block element parsing using traits
19 use block\HeadlineTrait;
21 parseInlineHtml as private;
24 // Check Ul List before headline
25 identifyUl as protected identifyBUl;
26 consumeUl as protected consumeBUl;
30 // Check Hr before checking lists
31 identifyHr as protected identifyAHr;
32 consumeHr as protected consumeAHr;
35 // include inline element parsing using traits
37 use inline\EmphStrongTrait;
41 * @var boolean whether to format markup according to HTML5 spec.
42 * Defaults to `false` which means that markup is formatted as HTML4.
44 public $html5 = false;
47 * @var array these are "escapeable" characters. When using one of these prefixed with a
48 * backslash, the character will be outputted without the backslash and is not interpreted
51 protected $escapeCharacters = [
56 '{', '}', // curly braces
57 '[', ']', // square brackets
58 '(', ')', // parentheses
61 '-', // minus sign (hyphen)
63 '!', // exclamation mark
71 protected function prepare()
74 $this->references = [];
78 * Consume lines for a paragraph
80 * Allow headlines and code to break paragraphs
82 protected function consumeParagraph($lines, $current)
84 // consume until newline
86 for ($i = $current, $count = count($lines); $i < $count; $i++) {
88 if (!empty($line) && ltrim($line) !== '' &&
89 !($line[0] === "\t" || $line[0] === " " && strncmp($line, ' ', 4) === 0) &&
90 !$this->identifyHeadline($line, $lines, $i))
99 'content' => $this->parseInline(implode("\n", $content)),
101 return [$block, --$i];
108 * Parses a newline indicated by two spaces on the end of a markdown line.
110 protected function renderText($text)
112 return str_replace(" \n", $this->html5 ? "<br>\n" : "<br />\n", $text[1]);