PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / markdown / block / CodeTrait.php

CodeTrait.php in DecaLog 4.4.0, at includes/libraries/markdown/block/CodeTrait.php

67 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\markdownparser\block;
9
10 /**
11 * Adds the 4 space indented code blocks
12 */
13 trait CodeTrait
14 {
15 /**
16 * identify a line as the beginning of a code block.
17 */
18 protected function identifyCode($line)
19 {
20 // indentation >= 4 or one tab is code
21 return ($l = $line[0]) === ' ' && $line[1] === ' ' && $line[2] === ' ' && $line[3] === ' ' || $l === "\t";
22 }
23
24 /**
25 * Consume lines for a code block element
26 */
27 protected function consumeCode($lines, $current)
28 {
29 // consume until newline
30
31 $content = [];
32 for ($i = $current, $count = count($lines); $i < $count; $i++) {
33 $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 if (isset($line[0]) && ($line[0] === "\t" || strncmp($line, ' ', 4) === 0)) {
37 $line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
38 $content[] = $line;
39 // but also if it is empty and the next line is intended by 4 spaces or a tab
40 } elseif (($line === '' || rtrim($line) === '') && isset($lines[$i + 1][0]) &&
41 ($lines[$i + 1][0] === "\t" || strncmp($lines[$i + 1], ' ', 4) === 0)) {
42 if ($line !== '') {
43 $line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
44 }
45 $content[] = $line;
46 } else {
47 break;
48 }
49 }
50
51 $block = [
52 'code',
53 'content' => implode("\n", $content),
54 ];
55 return [$block, --$i];
56 }
57
58 /**
59 * Renders a code block
60 */
61 protected function renderCode($block)
62 {
63 $class = isset($block['language']) ? ' class="language-' . $block['language'] . '"' : '';
64 return "<pre><code$class>" . htmlspecialchars($block['content'] . "\n", ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . "</code></pre>\n";
65 }
66 }
67