|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\HTMLToMarkdown\Converter; |
|
4
|
|
|
|
|
5
|
|
|
use League\HTMLToMarkdown\Configuration; |
|
6
|
|
|
use League\HTMLToMarkdown\ConfigurationAwareInterface; |
|
7
|
|
|
use League\HTMLToMarkdown\ElementInterface; |
|
8
|
|
|
|
|
9
|
|
|
class TableConverter implements ConverterInterface, PreConverterInterface, ConfigurationAwareInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Configuration |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Configuration $config |
|
18
|
|
|
*/ |
|
19
|
3 |
|
public function setConfig(Configuration $config) |
|
20
|
|
|
{ |
|
21
|
3 |
|
$this->config = $config; |
|
22
|
3 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $alignments = array( |
|
28
|
|
|
'left' => ':--', |
|
29
|
|
|
'right' => '--:', |
|
30
|
|
|
'center' => ':-:', |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $columnAlignments = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var str |
|
40
|
|
|
*/ |
|
41
|
|
|
private $caption = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ElementInterface $element |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function preConvert(ElementInterface $element) |
|
49
|
|
|
{ |
|
50
|
3 |
|
$tag = $element->getTagName(); |
|
51
|
|
|
// Only table cells and caption are allowed to contain content. |
|
52
|
|
|
// Remove all text between other table elements. |
|
53
|
3 |
|
if ($tag !== 'th' and $tag !== 'td' and $tag !== 'caption') { |
|
54
|
3 |
|
foreach ($element->getChildren() as $child) { |
|
55
|
3 |
|
if ($child->isText()) { |
|
56
|
3 |
|
$child->setFinalMarkdown(''); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
3 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param ElementInterface $element |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function convert(ElementInterface $element) |
|
68
|
|
|
{ |
|
69
|
3 |
|
$value = $element->getValue(); |
|
70
|
|
|
|
|
71
|
3 |
|
switch ($element->getTagName()) { |
|
72
|
3 |
|
case 'table': |
|
73
|
3 |
|
$this->columnAlignments = array(); |
|
74
|
3 |
|
if ($this->caption) { |
|
75
|
3 |
|
$side = $this->config->getOption('table_caption_side'); |
|
76
|
3 |
|
if ($side === 'top') { |
|
77
|
3 |
|
$value = $this->caption . "\n" . $value; |
|
78
|
3 |
|
} else if ($side === 'bottom') { |
|
79
|
3 |
|
$value .= $this->caption; |
|
80
|
|
|
} |
|
81
|
3 |
|
$this->caption = null; |
|
82
|
|
|
} |
|
83
|
3 |
|
return $value . "\n"; |
|
84
|
3 |
|
case 'caption': |
|
85
|
3 |
|
$this->caption = trim($value); |
|
86
|
3 |
|
return ''; |
|
87
|
3 |
|
case 'tr': |
|
88
|
3 |
|
$value .= "|\n"; |
|
89
|
3 |
|
if ($this->columnAlignments !== null) { |
|
90
|
3 |
|
$value .= "|" . implode("|", $this->columnAlignments) . "|\n"; |
|
91
|
3 |
|
$this->columnAlignments = null; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
3 |
|
return $value; |
|
94
|
3 |
|
case 'th': |
|
95
|
3 |
|
case 'td': |
|
96
|
3 |
|
if ($this->columnAlignments !== null) { |
|
97
|
3 |
|
$align = $element->getAttribute('align'); |
|
98
|
3 |
|
$this->columnAlignments[] = isset(self::$alignments[$align]) ? self::$alignments[$align] : '---'; |
|
99
|
|
|
} |
|
100
|
3 |
|
$value = str_replace("\n", ' ', $value); |
|
101
|
3 |
|
$value = str_replace('|', $this->config->getOption('table_pipe_escape'), $value); |
|
102
|
3 |
|
return '| ' . trim($value) . ' '; |
|
103
|
3 |
|
case 'thead': |
|
104
|
3 |
|
case 'tbody': |
|
105
|
3 |
|
case 'tfoot': |
|
106
|
3 |
|
case 'colgroup': |
|
107
|
3 |
|
case 'col': |
|
108
|
3 |
|
return $value; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string[] |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function getSupportedTags() |
|
116
|
|
|
{ |
|
117
|
3 |
|
return array('table', 'tr', 'th', 'td', 'thead', 'tbody', 'tfoot', 'colgroup', 'col', 'caption'); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..