PluginProbe
OPcache Manager / 3.3.0
OPcache Manager v3.3.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.0 2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.14.0 2.2.0 2.3.0 2.3.1 2.3.2 2.4.0 2.5.0 2.6.0 All 36 releases
opcache-manager / includes / libraries / markdown / block / FencedCodeTrait.php

FencedCodeTrait.php in OPcache Manager 3.3.0, at includes/libraries/markdown/block/FencedCodeTrait.php

59 lines 1.4 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 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 ($line[0] === '`' && strncmp($line, '```', 3) === 0) ||
25 ($line[0] === '~' && strncmp($line, '~~~', 3) === 0) ||
26 (isset($line[3]) && (
27 ($line[3] === '`' && strncmp(ltrim($line), '```', 3) === 0) ||
28 ($line[3] === '~' && strncmp(ltrim($line), '~~~', 3) === 0)
29 ));
30 }
31
32 /**
33 * Consume lines for a fenced code block
34 */
35 protected function consumeFencedCode($lines, $current)
36 {
37 $line = ltrim($lines[$current]);
38 $fence = substr($line, 0, $pos = strrpos($line, $line[0]) + 1);
39 $language = rtrim(substr($line, $pos));
40 // consume until end fence
41 $content = [];
42 for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
43 if (($pos = strpos($line = $lines[$i], $fence)) === false || $pos > 3) {
44 $content[] = $line;
45 } else {
46 break;
47 }
48 }
49 $block = [
50 'code',
51 'content' => implode("\n", $content),
52 ];
53 if (!empty($language)) {
54 $block['language'] = $language;
55 }
56 return [$block, $i];
57 }
58 }
59