-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
148 lines (106 loc) · 3.54 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
require_once './vendor/autoload.php';
$version = "2.0.0 beta 5";
// Let's see if there's a search-parameter.
$parseurl = parse_url($_SERVER['REQUEST_URI']);
// Yeah, this is turning into a bit of black magic voodoo. Refactor at some point.
$prefix = dirname($parseurl['path']);
$prefix = strtr($prefix, array("/extensions" => "","/internals" => "", "/tutorial" => "", "/howto" => ""));
$request = str_replace($prefix, "", $parseurl['path']);
// Strip of beginning slash.
if (strpos($request, "/") === 0) {
$request = substr($request, 1);
}
if (empty($request) || $request == "v20" || $request == "bolt-docs" || $request == "/" ) {
header("location: ./introduction");
}
// \Dumper::dump($request);
// \Dumper::dump($prefix);
if (!file_exists("./source/".$request.".md")) {
echo "No proper name for a page in the docs. Bye!";
die();
}
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();
$menu = $yaml->parse(file_get_contents('menu.yml'));
$source = file_get_contents("./source/".$request.".md");
$source = \ParsedownExtra::instance()->text($source);
$source = Michelf\SmartyPants::defaultTransform($source);
preg_match("/<h1>(.*)<\/h1>/i", $source, $maintitle);
$maintitle = $maintitle[1];
preg_match_all("/<h2>(.*)<\/h2>/i", $source, $matches);
$submenu = array();
foreach ($matches[1] as $key => $title) {
$submenu[ makeSlug(strip_tags($title)) ] = strip_tags($title);
}
// Let's see if there's a 'q' to highlight..
/* ----- This is broken: it also replaces _inside_ image tags. Not what we want.
if (!empty($_GET['q'])) {
$q = makeSlug($_GET['q']);
$source = preg_replace_callback("/" . $q . "/i", function($matches) {
$output = sprintf("<mark id='%s'>%s</mark>",
makeSlug($matches[0]),
$matches[0]
);
return $output;
}, $source);
}
---- */
// Markup for <h1> and <h2>..
$source = preg_replace_callback("/<h([234])>(.*)<\/h([234])>/i", function($matches) {
$output = sprintf("<h%s id='%s'>%s<a href='#%s' class='anchor'>¶</a></h%s>",
$matches[1],
makeSlug($matches[2]),
$matches[2],
makeSlug($matches[2]),
$matches[1]
);
return $output;
}, $source);
$loader = new Twig_Loader_Filesystem('./view');
$twig = new Twig_Environment($loader, array(
/* 'cache' => './cache', */
));
// Add Dumper function to twig.
$dumper = new Twig_SimpleFunction(
'dump',
function ($var) { return \Dumper::dump($var, DUMPER_CAPTURE); },
array('is_safe' => array('html')
));
$twig->addFunction($dumper);
// Add markdown to twig.
$markdown = new Twig_SimpleFilter(
'markdown',
function ($content) { return \ParsedownExtra::instance()->text($content); },
array('is_safe' => array('html')
));
$twig->addFilter($markdown);
// Add slug filter to twig.
$slug = new Twig_SimpleFilter(
'slug',
function ($name) { return \URLify::filter($name); },
array('is_safe' => array('html')
));
$twig->addFilter($slug);
echo $twig->render('index.twig', array(
'title' => $maintitle,
'source' => $source,
'menu' => $menu,
'submenu' => $submenu,
'current' => $request,
'version' => $version,
'requested_page' => $request,
'prefix' => ($prefix == "/" ? "" : $prefix)
));
// ----------
/**
* Modify a string, so that we can use it for slugs. Like
* safeString, but using hyphens instead of underscores.
*
* @param string $str
* @param string $type
* @return string
*/
function makeSlug($str) {
return \URLify::filter(strip_tags($str));
}