1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sioen; |
4
|
|
|
|
5
|
|
|
use Sioen\HtmlToJson\BlockquoteConverter; |
6
|
|
|
use Sioen\HtmlToJson\HeadingConverter; |
7
|
|
|
use Sioen\HtmlToJson\IframeConverter; |
8
|
|
|
use Sioen\HtmlToJson\ImageConverter; |
9
|
|
|
use Sioen\HtmlToJson\ListConverter; |
10
|
|
|
use Sioen\HtmlToJson\ParagraphConverter; |
11
|
|
|
use Sioen\HtmlToJson\BaseConverter; |
12
|
|
|
use Sioen\HtmlToJson\Converter; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class HtmlToJson |
16
|
|
|
* |
17
|
|
|
* Converts html to a json object that can be understood by Sir Trevor |
18
|
|
|
* |
19
|
|
|
* @version 1.1.0 |
20
|
|
|
* @author Wouter Sioen <[email protected]> |
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
22
|
|
|
*/ |
23
|
|
|
class HtmlToJson |
24
|
|
|
{ |
25
|
|
|
/** @var array */ |
26
|
|
|
private $converters; |
27
|
|
|
|
28
|
1 |
|
public function __construct() |
29
|
|
|
{ |
30
|
1 |
|
$this->addConverter(new HeadingConverter()); |
31
|
1 |
|
$this->addConverter(new ListConverter()); |
32
|
1 |
|
$this->addConverter(new BlockquoteConverter()); |
33
|
1 |
|
$this->addConverter(new IframeConverter()); |
34
|
1 |
|
$this->addConverter(new ImageConverter()); |
35
|
1 |
|
$this->addConverter(new BaseConverter()); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function addConverter(Converter $converter) |
39
|
|
|
{ |
40
|
1 |
|
$this->converters[] = $converter; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Converts html to the json Sir Trevor requires |
45
|
|
|
* |
46
|
|
|
* @param string $html |
47
|
|
|
* @return string The json string |
48
|
|
|
*/ |
49
|
1 |
|
public function toJson($html) |
50
|
|
|
{ |
51
|
|
|
// Strip white space between tags to prevent creation of empty #text nodes |
52
|
1 |
|
$html = preg_replace('~>\s+<~', '><', $html); |
53
|
1 |
|
$document = new \DOMDocument(); |
54
|
|
|
|
55
|
|
|
// Load UTF-8 HTML hack (from http://bit.ly/pVDyCt) |
56
|
1 |
|
$document->loadHTML('<?xml encoding="UTF-8">' . $html); |
57
|
1 |
|
$document->encoding = 'UTF-8'; |
58
|
|
|
|
59
|
|
|
// fetch the body of the document. All html is stored in there |
60
|
1 |
|
$body = $document->getElementsByTagName("body")->item(0); |
61
|
|
|
|
62
|
1 |
|
$data = array(); |
63
|
|
|
|
64
|
|
|
// loop trough the child nodes and convert them |
65
|
1 |
|
if ($body) { |
66
|
1 |
|
foreach ($body->childNodes as $node) { |
67
|
1 |
|
$data[] = $this->convert($node); |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
return json_encode(array('data' => $data)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Converts one node to json |
76
|
|
|
* |
77
|
|
|
* @param \DOMElement $node |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
1 |
|
private function convert(\DOMElement $node) |
81
|
|
|
{ |
82
|
1 |
|
foreach ($this->converters as $converter) { |
83
|
1 |
|
if ($converter->matches($node)) { |
84
|
1 |
|
return $converter->toJson($node); |
85
|
|
|
} |
86
|
1 |
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|