This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TocEventSubscriber.php
129 lines (104 loc) · 3.68 KB
/
TocEventSubscriber.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
128
129
<?php
namespace Carew\Plugin\Toc;
use Carew\Event\Events;
use Carew\Document;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TocEventSubscriber implements EventSubscriberInterface
{
public function onDocument($event)
{
$document = $event->getSubject();
if (Document::TYPE_POST == $document->getType()) {
$this->buildToc($document);
} elseif (Document::TYPE_PAGE == $document->getType()) {
$this->buildToc($document);
}
}
public static function getSubscribedEvents()
{
return array(
Events::DOCUMENT => array(
array('onDocument', 256),
),
);
}
private function buildToc($document)
{
$body = $document->getBody();
if (preg_match('/^(\s|\n)+$/', $body)) {
return;
}
$dom = new \DOMDocument();
$dom->loadHtml('<?xml encoding="UTF-8">'.$body);
$xpath = new \DOMXPath($dom);
$toc = array();
$ids = array();
$isSpan = function ($node) {
return XML_ELEMENT_NODE === $node->nodeType && 'span' === $node->tagName;
};
$genId = function ($node) use (&$ids, $isSpan) {
$count = 0;
do {
if ($isSpan($node->lastChild)) {
$node = clone $node;
$node->removeChild($node->lastChild);
}
$id = preg_replace('{[^a-z0-9]}i', '-', strtolower(trim($node->nodeValue)));
$id = preg_replace('{-+}', '-', $id);
if ($count) {
$id .= '-'.($count+1);
}
$count++;
} while (isset($ids[$id]));
$ids[$id] = true;
return $id;
};
$getDesc = function ($node) use ($isSpan) {
if ($isSpan($node->lastChild)) {
return $node->lastChild->nodeValue;
}
return null;
};
$getTitle = function ($node) use ($isSpan) {
if ($isSpan($node->lastChild)) {
$node = clone $node;
$node->removeChild($node->lastChild);
}
return $node->nodeValue;
};
// build TOC & deep links
$h1 = $h2 = $h3 = $h4 = 0;
$nodes = $xpath->query('//*[self::h1 or self::h2 or self::h3 or self::h4]');
foreach ($nodes as $node) {
// set id and add anchor link
$id = $genId($node);
$title = $getTitle($node);
$desc = $getDesc($node);
$node->setAttribute('id', $id);
$link = $dom->createElement('a', '#');
$link->setAttribute('href', '#'.$id);
$link->setAttribute('class', 'anchor');
$node->appendChild($link);
// parse into a tree
switch ($node->nodeName) {
case 'h1':
$toc[++$h1] = array('title' => $title, 'id' => $id, 'desc' => $desc);
break;
case 'h2':
$toc[$h1][++$h2] = array('title' => $title, 'id' => $id, 'desc' => $desc);
break;
case 'h3':
$toc[$h1][$h2][++$h3] = array('title' => $title, 'id' => $id, 'desc' => $desc);
break;
case 'h4':
$toc[$h1][$h2][$h3][++$h4] = array('title' => $title, 'id' => $id, 'desc' => $desc);
break;
}
}
// save new body with IDs
$body = $dom->saveHtml();
$body = preg_replace('{.*<body>(.*)</body>.*}is', '$1', $body);
$document->setToc($toc);
$document->setBody($body);
}
}