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\block;
11 * Adds the 4 space indented code blocks
16 * identify a line as the beginning of a code block.
18 protected function identifyCode($line)
20 // indentation >= 4 or one tab is code
21 return ($l = $line[0]) === ' ' && $line[1] === ' ' && $line[2] === ' ' && $line[3] === ' ' || $l === "\t";
25 * Consume lines for a code block element
27 protected function consumeCode($lines, $current)
29 // consume until newline
32 for ($i = $current, $count = count($lines); $i < $count; $i++) {
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($lines[$i], ' ', 4) === 0)) {
37 $line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
39 // but also if it is empty and the next line is intended by 4 spaces or a tab
40 } elseif ((empty($line) || rtrim($line) === '') && isset($lines[$i + 1][0]) &&
41 ($lines[$i + 1][0] === "\t" || strncmp($lines[$i + 1], ' ', 4) === 0)) {
43 $line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
53 'content' => implode("\n", $content),
55 return [$block, --$i];
59 * Renders a code block
61 protected function renderCode($block)
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";