-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsedownTablespan.php
127 lines (108 loc) · 3.97 KB
/
ParsedownTablespan.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
class ParsedownTablespan extends ParsedownExtra
{
const VERSION = '1.1.0';
protected function blockTableComplete(array $Block)
{
if ( ! isset($Block))
{
return null;
}
$HeaderElements =& $Block['element']['elements'][0]['elements'][0]['elements'];
for ($index = count($HeaderElements) - 1; $index >= 0; --$index)
{
$colspan = 1;
$HeaderElement =& $HeaderElements[$index];
while ($index && $HeaderElements[$index - 1]['handler']['argument'] === '>')
{
$colspan++;
$PreviousHeaderElement =& $HeaderElements[--$index];
$PreviousHeaderElement['merged'] = true;
if (isset($PreviousHeaderElement['attributes']))
{
$HeaderElement['attributes'] = $PreviousHeaderElement['attributes'];
}
}
if ($colspan > 1)
{
if ( ! isset($HeaderElement['attributes']))
{
$HeaderElement['attributes'] = array();
}
$HeaderElement['attributes']['colspan'] = $colspan;
}
}
for ($index = count($HeaderElements) - 1; $index >= 0; --$index)
{
if (isset($HeaderElements[$index]['merged']))
{
array_splice($HeaderElements, $index, 1);
}
}
$Rows =& $Block['element']['elements'][1]['elements'];
foreach ($Rows as $RowNo => &$Row)
{
$Elements =& $Row['elements'];
for ($index = count($Elements) - 1; $index >= 0; --$index)
{
$colspan = 1;
$Element =& $Elements[$index];
while ($index && $Elements[$index - 1]['handler']['argument'] === '>')
{
$colspan++;
$PreviousElement =& $Elements[--$index];
$PreviousElement['merged'] = true;
if (isset($PreviousElement['attributes']))
{
$Element['attributes'] = $PreviousElement['attributes'];
}
}
if ($colspan > 1)
{
if ( ! isset($Element['attributes']))
{
$Element['attributes'] = array();
}
$Element['attributes']['colspan'] = $colspan;
}
}
}
foreach ($Rows as $RowNo => &$Row)
{
$Elements =& $Row['elements'];
foreach ($Elements as $index => &$Element)
{
$rowspan = 1;
if (isset($Element['merged']))
{
continue;
}
while ($RowNo + $rowspan < count($Rows) && $index < count($Rows[$RowNo + $rowspan]['elements']) && $Rows[$RowNo + $rowspan]['elements'][$index]['handler']['argument'] === '^' && (@$Element['attributes']['colspan'] ?: null) === (@$Rows[$RowNo + $rowspan]['elements'][$index]['attributes']['colspan'] ?: null))
{
$Rows[$RowNo + $rowspan]['elements'][$index]['merged'] = true;
$rowspan++;
}
if ($rowspan > 1)
{
if ( ! isset($Element['attributes']))
{
$Element['attributes'] = array();
}
$Element['attributes']['rowspan'] = $rowspan;
}
}
}
foreach ($Rows as $RowNo => &$Row)
{
$Elements =& $Row['elements'];
for ($index = count($Elements) - 1; $index >= 0; --$index)
{
if (isset($Elements[$index]['merged']))
{
array_splice($Elements, $index, 1);
}
}
}
return $Block;
}
}