Completed
Pull Request — master (#151)
by Gena
02:22
created

TableTrait   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 0
dl 0
loc 109
ccs 52
cts 54
cp 0.963
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A identifyTable() 0 5 3
C consumeTable() 0 50 15
B renderTable() 0 20 5
B parseTd() 0 9 6
parseInline() 0 1 ?
renderAbsy() 0 1 ?
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\markdown\block;
9
10
/**
11
 * Adds the table blocks
12
 */
13
trait TableTrait
14
{
15
	private $_tableCellTag = 'td';
16
	private $_tableCellCount = 0;
17
	private $_tableCellAlign = [];
18
19
	/**
20
	 * identify a line as the beginning of a table block.
21
	 */
22 128
	protected function identifyTable($line, $lines, $current)
23
	{
24 128
		return strpos($line, '|') !== false && isset($lines[$current + 1])
25 128
			&& preg_match('~^\\s*\\|?(\\s*:?-[\\-\\s]*:?\\s*\\|\\s*:?-[\\-\\s]*:?\\s*)+\\|?\\s*$~', $lines[$current + 1]);
26
	}
27
28
	/**
29
	 * Consume lines for a table
30
	 */
31 2
	protected function consumeTable($lines, $current)
32
	{
33
		// consume until newline
34
35
		$block = [
36 2
			'table',
37
			'cols' => [],
38
			'rows' => [],
39
		];
40 2
		for ($i = $current, $count = count($lines); $i < $count; $i++) {
41 2
			$line = trim($lines[$i]);
42
43
			// extract alignment from second line
44 2
			if ($i == $current+1) {
45 2
				$cols = explode('|', trim($line, ' |'));
46 2
				foreach($cols as $col) {
47 2
					$col = trim($col);
48 2
					if (empty($col)) {
49
						$block['cols'][] = '';
50
						continue;
51
					}
52 2
					$l = ($col[0] === ':');
53 2
					$r = (substr($col, -1, 1) === ':');
54 2
					if ($l && $r) {
55 2
						$block['cols'][] = 'center';
56 2
					} elseif ($l) {
57 2
						$block['cols'][] = 'left';
58 2
					} elseif ($r) {
59 2
						$block['cols'][] = 'right';
60
					} else {
61 2
						$block['cols'][] = '';
62
					}
63
				}
64
65 2
				continue;
66
			}
67 2
			if ($line === '' || substr($lines[$i], 0, 4) === '    ') {
68 2
				break;
69
			}
70 2
			if ($line[0] === '|') {
71 2
				$line = substr($line, 1);
72
			}
73 2
			if (substr($line, -1, 1) === '|' && (substr($line, -2, 2) !== '\\|' || substr($line, -3, 3) === '\\\\|')) {
74 2
				$line = substr($line, 0, -1);
75
			}
76 2
			$block['rows'][] = $line;
77
		}
78
79 2
		return [$block, --$i];
80
	}
81
82
	/**
83
	 * render a table block
84
	 */
85 2
	protected function renderTable($block)
86
	{
87 2
		$content = '';
88 2
		$this->_tableCellAlign = $block['cols'];
89 2
		$content .= "<thead>\n";
90 2
		$first = true;
91 2
		foreach($block['rows'] as $row) {
92 2
			$this->_tableCellTag = $first ? 'th' : 'td';
93 2
			$align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount] . '"';
94 2
			$this->_tableCellCount++;
95 2
			$tds = "<$this->_tableCellTag$align>" . trim($this->renderAbsy($this->parseInline($row))) . "</$this->_tableCellTag>"; // TODO move this to the consume step
96 2
			$content .= "<tr>$tds</tr>\n";
97 2
			if ($first) {
98 2
				$content .= "</thead>\n<tbody>\n";
99
			}
100 2
			$first = false;
101 2
			$this->_tableCellCount = 0;
102
		}
103 2
		return "<table>\n$content</tbody>\n</table>\n";
104
	}
105
106
	/**
107
	 * @marker |
108
	 */
109 3
	protected function parseTd($markdown)
110
	{
111 3
		if (isset($this->context[1]) && $this->context[1] === 'table') {
112 2
			$align = empty($this->_tableCellAlign[$this->_tableCellCount]) ? '' : ' align="' . $this->_tableCellAlign[$this->_tableCellCount] . '"';
113 2
			$this->_tableCellCount++;
114 2
			return [['text', "</$this->_tableCellTag><$this->_tableCellTag$align>"], isset($markdown[1]) && $markdown[1] === ' ' ? 2 : 1]; // TODO make a absy node
115
		}
116 1
		return [['text', $markdown[0]], 1];
117
	}
118
119
	abstract protected function parseInline($text);
120
	abstract protected function renderAbsy($absy);
121
}
122