Blame view

vendor/cebe/markdown/block/HeadlineTrait.php 1.57 KB
70f4f18b   Administrator   first_commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  <?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] === '#' && !preg_match('/^#\d+/', $line)
  			||
  			// 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);
  }