Removed modules/contrib/media module to allow update to the core media module
[yaffs-website] / vendor / cebe / markdown / block / FencedCodeTrait.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\block;
9
10 /**
11  * Adds the fenced code blocks
12  *
13  * automatically included 4 space indented code blocks
14  */
15 trait FencedCodeTrait
16 {
17         use CodeTrait;
18
19         /**
20          * identify a line as the beginning of a fenced code block.
21          */
22         protected function identifyFencedCode($line)
23         {
24                 return ($l = $line[0]) === '`' && strncmp($line, '```', 3) === 0 ||
25                                 $l === '~' && strncmp($line, '~~~', 3) === 0;
26         }
27
28         /**
29          * Consume lines for a fenced code block
30          */
31         protected function consumeFencedCode($lines, $current)
32         {
33                 // consume until ```
34                 $line = rtrim($lines[$current]);
35                 $fence = substr($line, 0, $pos = strrpos($line, $line[0]) + 1);
36                 $language = substr($line, $pos);
37                 $content = [];
38                 for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
39                         if (rtrim($line = $lines[$i]) !== $fence) {
40                                 $content[] = $line;
41                         } else {
42                                 break;
43                         }
44                 }
45                 $block = [
46                         'code',
47                         'content' => implode("\n", $content),
48                 ];
49                 if (!empty($language)) {
50                         $block['language'] = $language;
51                 }
52                 return [$block, $i];
53         }
54 }