-
Notifications
You must be signed in to change notification settings - Fork 0
/
gensite
executable file
·90 lines (83 loc) · 3.21 KB
/
gensite
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
#!/usr/local/bin/php
<?php
$nav = getSitemap("nav/nav.json");
$html = buildSitemap($nav->sidemenu);
$html = tidy($html);
$out = new stdClass();
$out->nav = $html;
$out->topnav = buildTopNav($nav->topmenu);
$index = file_get_contents("index.template.html");
$index = preg_replace_callback("/\%\%(.+?)\%\%/", function($match) {
global $out;
if (property_exists($out, $match[1])) {
return $out->{$match[1]};
} else {
return "";
}
}, $index);
print $index."\n";
function buildTopnav($json) {
$out = "";
foreach ($json as $idx=>$item) {
$out .= '<li class="nav-item d-none d-sm-inline-block"><a href="'.$item->link.'" class="nav-link">'.$item->title.'</a></li>';
}
return $out;
}
function buildSitemap($json, $lvl=0) {
$out = str_repeat("\t", $lvl);
$arrow = '<i class="right fas fa-angle-left" onclick="console.log(this.closest(\'li\'));this.closest(\'li\').classList.add(\'menu-open\');"></i>';
$out .= "<ul class=\"nav nav-treeview nav-pills nav-sidebar flex-column nav-child-indent nav-collapse-hide-child\" data-widget=\"treeview\" role=\"menu\" data-accordion=\"false\">";
foreach ($json as $idx=>$item) {
if (property_exists($item, "_include")) {
$item->_children = getSitemap($item->_include);
}
$hide = "";
$out .= str_repeat("\t\t", $lvl);
if (property_exists($item, "hidden") && $item->hidden) {
$hide = " style='display:none;' ";
}
$out .= "<li class=\"nav-item\"$hide>";
$ico = "";
if ($item->icon) {
if (preg_match("/(webm|gif|jpg|jpeg|svg|png|bmp|ico)$/", $item->icon)) {
$ico = "<img alt=\"{$item->title}\" class=\"nav-icon\" src=\"{$item->icon}\">";
} else {
$ico = "<i class=\"nav-icon {$item->icon}\"></i>";
}
}
if (property_exists($item, "link")) {
if (!preg_match("/^http/", $item->link)) {
if (!preg_match("/^[\/\.]/", $item->link)) {
$item->link = "/crblog/" . $item->link;
}
$out .= "<a href=\"{$item->link}\" title=\"{$item->title}\" onclick=\"return app.doClick(this, event)\" class=\"nav-link\">".$ico." <p>".$item->title;
} else {
$out .= "<a href=\"{$item->link}\" title=\"{$item->title}\" onclick=\"return app.doClick(this, event)\" class='nav-link' target='_blank'><p>".$item->title;
}
} else {
$out .= "<a href=\"#\" title=\"{$item->title}\" class=\"nav-link\">$ico <p>".$item->title;
}
if (property_exists($item, "_children")) {
$out .= $arrow. "</p></a>";
$tmp = buildSitemap($item->_children, $lvl+1);
$out .= "\n\t\t\t" . $tmp ."\n";
} else {
$out .= "</p></a>";
}
$out .= "</li>\n";
}
$out .= "\n".str_repeat("\t", $lvl);
$out .= "</ul>";
return $out;
}
function getSitemap($file) {
return json_decode(file_get_contents($file));
}
function tidy($html) {
$tmpfile = "/tmp/tidy_".uniqid().".html";
file_put_contents($tmpfile, $html);
$newhtml = `cat $tmpfile | tidy -q --show-body-only yes`;
unlink($tmpfile);
return $newhtml;
}
?>