ff206c6453f15dfa14b9200c77cba6bc5d10cd24
[yaffs-website] / vendor / cebe / markdown / GithubMarkdown.php
1 <?php
2 /**
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
6  */
7
8 namespace cebe\markdown;
9
10 /**
11  * Markdown parser for github flavored markdown.
12  *
13  * @author Carsten Brandt <mail@cebe.cc>
14  */
15 class GithubMarkdown extends Markdown
16 {
17         // include block element parsing using traits
18         use block\TableTrait;
19         use block\FencedCodeTrait;
20
21         // include inline element parsing using traits
22         use inline\StrikeoutTrait;
23         use inline\UrlLinkTrait;
24
25         /**
26          * @var boolean whether to interpret newlines as `<br />`-tags.
27          * This feature is useful for comments where newlines are often meant to be real new lines.
28          */
29         public $enableNewlines = false;
30
31         /**
32          * @inheritDoc
33          */
34         protected $escapeCharacters = [
35                 // from Markdown
36                 '\\', // backslash
37                 '`', // backtick
38                 '*', // asterisk
39                 '_', // underscore
40                 '{', '}', // curly braces
41                 '[', ']', // square brackets
42                 '(', ')', // parentheses
43                 '#', // hash mark
44                 '+', // plus sign
45                 '-', // minus sign (hyphen)
46                 '.', // dot
47                 '!', // exclamation mark
48                 '<', '>',
49                 // added by GithubMarkdown
50                 ':', // colon
51                 '|', // pipe
52         ];
53
54
55
56         /**
57          * Consume lines for a paragraph
58          *
59          * Allow headlines, lists and code to break paragraphs
60          */
61         protected function consumeParagraph($lines, $current)
62         {
63                 // consume until newline
64                 $content = [];
65                 for ($i = $current, $count = count($lines); $i < $count; $i++) {
66                         $line = $lines[$i];
67                         if (!empty($line) && ltrim($line) !== '' &&
68                                 !($line[0] === "\t" || $line[0] === " " && strncmp($line, '    ', 4) === 0) &&
69                                 !$this->identifyHeadline($line, $lines, $i) &&
70                                 !$this->identifyUl($line, $lines, $i) &&
71                                 !$this->identifyOl($line, $lines, $i))
72                         {
73                                 $content[] = $line;
74                         } else {
75                                 break;
76                         }
77                 }
78                 $block = [
79                         'paragraph',
80                         'content' => $this->parseInline(implode("\n", $content)),
81                 ];
82                 return [$block, --$i];
83         }
84
85         /**
86          * @inheritdocs
87          *
88          * Parses a newline indicated by two spaces on the end of a markdown line.
89          */
90         protected function renderText($text)
91         {
92                 if ($this->enableNewlines) {
93                         $br = $this->html5 ? "<br>\n" : "<br />\n";
94                         return strtr($text[1], ["  \n" => $br, "\n" => $br]);
95                 } else {
96                         return parent::renderText($text);
97                 }
98         }
99 }