-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
174 lines (129 loc) · 4.35 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
use Symfony\Component\Yaml\Parser;
require_once './vendor/autoload.php';
$version = "2.2.16";
// 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" => "", "/storage" => ""));
$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");
die();
}
// Setup the parser and read the versions
$yaml = new Parser();
$versions = $yaml->parse(file_get_contents('versions.yml'));
// Determine if we're on 'docs' or on 'manual'
$hostname = $_SERVER['SERVER_NAME'];
if (strpos($hostname, 'manual') !== false) {
$sourcefolder = './source_manual/';
$menufile = 'menu_manual.yml';
$sitetitle = 'Bolt user manual';
} else {
if (in_array(ltrim($prefix, '/'), $versions)) {
$sourcefolder = './version'.$prefix.'/source_docs/';
$menufile = './version'.$prefix.'/menu_docs.yml';
$sitetitle = 'Bolt documentation';
$linkPrefix = $prefix;
$prefix = '';
} else {
$sourcefolder = './source_docs/';
$menufile = 'menu_docs.yml';
$sitetitle = 'Bolt documentation';
}
}
if (!file_exists($sourcefolder . $request . ".md")) {
header("HTTP/1.0 404 Not Found");
echo "No proper name for a page in the docs. Bye!";
die();
}
$menu = $yaml->parse(file_get_contents($menufile));
$source = file_get_contents($sourcefolder . $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 dump($var); },
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,
'sitetitle' => $sitetitle,
'source' => $source,
'menu' => $menu,
'submenu' => $submenu,
'current' => $request,
'version' => $version,
'requested_page' => $request,
'prefix' => ($prefix == "/" ? "" : $prefix),
'linkPrefix' => ($linkPrefix == "/" ? "" : $linkPrefix)
));
// ----------
/**
* 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));
}