|
- <?php
-
-
- namespace cebe\markdown\block;
-
-
- trait HeadlineTrait
- {
-
-
- protected function identifyHeadline($line, $lines, $current)
- {
- return (
-
- $line[0] === '#' && !preg_match('/^#\d+/', $line)
- ||
-
- !empty($lines[$current + 1]) &&
- (($l = $lines[$current + 1][0]) === '=' || $l === '-') &&
- preg_match('/^(\-+|=+)\s*$/', $lines[$current + 1])
- );
- }
-
-
-
- protected function consumeHeadline($lines, $current)
- {
- if ($lines[$current][0] === '#') {
-
- $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 {
-
- $block = [
- 'headline',
- 'content' => $this->parseInline($lines[$current]),
- 'level' => $lines[$current + 1][0] === '=' ? 1 : 2,
- ];
- return [$block, $current + 1];
- }
- }
-
-
-
- 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);
- }
|