-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoc.php
78 lines (68 loc) · 2.95 KB
/
atoc.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
<?php
// Advanced Table of Contents extension
class YellowAtoc {
const VERSION = "0.8.2";
public $yellow; // access to API
// Initialization
public function onLoad($yellow) {
$this->yellow = $yellow;
$this->yellow->system->setDefault("atocNumbering", "0");
$this->yellow->system->setDefault("atocLevel", "4");
}
// Handle page content in HTML format
public function onParseContentHtml($page, $text) {
$callback = function ($matches) use ($page) {
$location = $page->getPage("main")->getLocation(true);
$rawData = $page->getPage("main")->parserData;
$atocLevel = $this->yellow->system->get("atocLevel");
preg_match_all("/<h([1-$atocLevel]) id=\"(.*?)\">(.*?)<\/h\d>/i", $rawData, $matches, PREG_PATTERN_ORDER);
// Variables
if ($this->yellow->system->get("atocNumbering")) {$listType = "ol";} else {$listType = "ul";}
$level = $matches[1];
$anchor = $matches[2];
$title = $matches[3];
$min = min($level);
$count = count($level);
$prev = $min - 1;
$counter = 0;
// Start list
$output = "<!-- AToC -->" . "<nav class=\"atoc\">";
for ( $a = $counter; $a < $count; $a++ ) {
// Variables
$current = $level[$a];
$diff = $current - $prev;
$entry = "<a href=\"$location#$anchor[$a]\">$title[$a]</a>";
if ($current > $prev) {
for ($i = 1; $i <= $diff; $i++) {$output .= "\n<$listType><li>";}
} elseif ($current == $prev) {
$output .= "</li>\n<li>";
} elseif ( $current < $prev ) {
for ($i = -1; $i >= $diff; $i--) {$output .= "</li></$listType>\n";}
$output .= "</li>\n<li>";
}
$output .= "$entry";
if ($a == $count - 1) {
for ($i = 1; $i <= $diff; $i++) {$output .= "</li></$listType>\n";}
$output .= "</li>";
}
$prev = $current;
}
// Close list
$output .= "</$listType>\n";
$output .= "</nav>" . "<!-- /AToC -->";
return $output;
};
return preg_replace_callback("/<p>\[atoc\]<\/p>\n/i", $callback, $text);
}
// Handle page extra data
public function onParsePageExtra($page, $name) {
$output = null;
// Add stylesheet
if ($name=="header") {
$extensionLocation = $this->yellow->system->get("coreServerBase").$this->yellow->system->get("coreExtensionLocation");
$output = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{$extensionLocation}atoc.css\" />\n";
}
return $output;
return $name=="atoc" ? $this->onParseContentHtml($page, "<p>[atoc]</p>\n") : null;
}
}