1 | <?php |
||
13 | trait CodeTrait |
||
14 | { |
||
15 | /** |
||
16 | * identify a line as the beginning of a code block. |
||
17 | */ |
||
18 | 199 | protected function identifyCode($line) |
|
23 | |||
24 | /** |
||
25 | * Consume lines for a code block element |
||
26 | */ |
||
27 | 46 | protected function consumeCode($lines, $current) |
|
28 | { |
||
29 | // consume until newline |
||
30 | |||
31 | 46 | $content = []; |
|
32 | 46 | for ($i = $current, $count = count($lines); $i < $count; $i++) { |
|
33 | 46 | $line = $lines[$i]; |
|
34 | |||
35 | // a line is considered to belong to this code block as long as it is intended by 4 spaces or a tab |
||
36 | 46 | if (isset($line[0]) && ($line[0] === "\t" || strncmp($line, ' ', 4) === 0)) { |
|
37 | 46 | $line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4); |
|
38 | 46 | $content[] = $line; |
|
39 | // but also if it is empty and the next line is intended by 4 spaces or a tab |
||
40 | 40 | } elseif (($line === '' || rtrim($line) === '') && isset($lines[$i + 1][0]) && |
|
41 | 40 | ($lines[$i + 1][0] === "\t" || strncmp($lines[$i + 1], ' ', 4) === 0)) { |
|
42 | 12 | if ($line !== '') { |
|
43 | $line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4); |
||
44 | } |
||
45 | 12 | $content[] = $line; |
|
46 | } else { |
||
47 | 40 | break; |
|
48 | } |
||
49 | } |
||
50 | |||
51 | $block = [ |
||
52 | 46 | 'code', |
|
53 | 46 | 'content' => implode("\n", $content), |
|
54 | ]; |
||
55 | 46 | return [$block, --$i]; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Renders a code block |
||
60 | */ |
||
61 | 37 | protected function renderCode($block) |
|
66 | } |
||
67 |