This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselflink.php
49 lines (45 loc) · 1.88 KB
/
selflink.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
<?php
// SelfLink extension, https://github.com/GiovanniSalmeri/yellow-selflink
class YellowSelfLink {
const VERSION = "0.8.16";
public $yellow; //access to API
// Handle initialisation
public function onLoad($yellow) {
$this->yellow = $yellow;
}
// Handle page content parsing of custom block
public function onParseContentShortcut($page, $name, $text, $type) {
$output = null;
if ($name=="a" && ($type=="block" || $type=="inline")) {
list($slug, $atext) = $this->yellow->toolbox->getTextList($text, " ", 2);
list($slug, $fragment) = $this->yellow->toolbox->getTextList($slug, "#", 2);
$slug = '/' . $slug;
if (substru($atext, 0, 2) == "- ") $atext = trim(substru($atext, 2));
$pages = $this->yellow->content->index(true);
$found = false; $loc = null;
foreach($pages as $page) {
$loc = $page->getLocation(true);
if (substr(rtrim($loc, '/'), -strlen($slug)) == $slug) {
$atext = $atext ? $atext : $page->get("title");
$found = true;
break;
}
}
if ($found) {
$output = "<a href=\"" . htmlspecialchars($loc) . ($fragment ? "#$fragment" : "") . "\">" . htmlspecialchars($atext) . "</a>";
} else {
$slug = htmlspecialchars(ltrim($slug, '/'));
$output = "<a class=\"missing\" href=\"" . htmlspecialchars($slug) . "\">" . htmlspecialchars($slug) . "</a>";
}
}
return $output;
}
// Handle page extra HTML data
public function onParsePageExtra($page, $name) {
$output = null;
if ($name == "header") {
$output = "<style>a.missing, a.missing:hover { color: red }</style>\n";
}
return $output;
}
}