|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- /**
- * @copyright Copyright (c) 2014 Carsten Brandt
- * @license https://github.com/cebe/markdown/blob/master/LICENSE
- * @link https://github.com/cebe/markdown#readme
- */
-
- namespace cebe\markdown\block;
-
- /**
- * Adds the headline blocks
- */
- trait HeadlineTrait
- {
- /**
- * identify a line as a headline
- */
- protected function identifyHeadline($line, $lines, $current)
- {
- return (
- // heading with #
- $line[0] === '#'
- ||
- // underlined headline
- !empty($lines[$current + 1]) &&
- (($l = $lines[$current + 1][0]) === '=' || $l === '-') &&
- preg_match('/^(\-+|=+)\s*$/', $lines[$current + 1])
- );
- }
-
- /**
- * Consume lines for a headline
- */
- protected function consumeHeadline($lines, $current)
- {
- if ($lines[$current][0] === '#') {
- // ATX headline
- $level = 1;
- while (isset($lines[$current][$level]) && $lines[$current][$level] === '#' && $level < 6) {
- $level++;
- }
- $block = [
- 'headline',
- 'content' => $this->parseInline(trim($lines[$current], "# \t")),
- 'level' => $level,
- ];
- return [$block, $current];
- } else {
- // underlined headline
- $block = [
- 'headline',
- 'content' => $this->parseInline($lines[$current]),
- 'level' => $lines[$current + 1][0] === '=' ? 1 : 2,
- ];
- return [$block, $current + 1];
- }
- }
-
- /**
- * Renders a headline
- */
- protected function renderHeadline($block)
- {
- $tag = 'h' . $block['level'];
- return "<$tag>" . $this->renderAbsy($block['content']) . "</$tag>\n";
- }
-
- abstract protected function parseInline($text);
- abstract protected function renderAbsy($absy);
- }
|