forked from d3designs/mdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdocs.php
166 lines (124 loc) · 3.41 KB
/
mdocs.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
<?php
/**
* Enable this when debugging:
*/
error_reporting(E_ALL);
/**
* @todo Add a "clean url" (mod_rewrite) option to the Menu class
*/
/**
* SETTINGS
*/
$config = new stdClass();
/**
* The folder where all of the Markdown documentation files are stored.
*/
$config->path = 'docs/';
/**
* The Markdown file extension used for all the documentation files.
*/
$config->extension = '.md';
/**
* The file to load, when no file is specified
*/
$config->index = 'Core/Core';
/**
* Default Syntax Highlighting Language
* @see /assets/geshi/ for complete list.
*/
$config->language = 'javascript';
/**
* The highest/lowest header level to include in the Table of Contents
* You shouldn't need to change these.
*/
$config->maxlevel = 1;
$config->minlevel = 2;
/**
* Ignore files in the root of Docs?
*/
$config->ignore = true;
/**
* @todo Possibly move this ugly logic code into a class or function
*/
// Determine the file to load:
$file = (isset($_GET['file']) && !empty($_GET['file']))? clean_filename($_GET['file']) : $config->index;
$config->filename = $config->path.$file.$config->extension;
if (!(file_exists($config->filename) && is_readable($config->filename))){
/**
* @todo Possibly show a 404 page
*/
die('ERROR: Document not found.');
}
$doc = file_get_contents($config->filename);
// Get the classes:
require_once 'assets/markdown.php';
require_once 'assets/markdown.mdocs.php';
require_once 'assets/toc.php';
require_once 'assets/menu.php';
require_once 'assets/geshi.php';
require_once 'assets/geshi.mdocs.php';
// Initialize Markdown
$markdown = new MarkdownExtra_Parser_mDocs();
$markdown->maxlevel = & $config->maxlevel;
$markdown->minlevel = & $config->minlevel;
// Initialize Table of Contents
$toc = new TOC();
$toc->content = & $doc;
$toc->maxlevel = & $config->maxlevel;
$toc->minlevel = & $config->minlevel;
$toc->delimiter = ':';
$toc->trim = '$ ';
// Initialize Docs Menu
$menu = new menu();
$menu->dir = & $config->path;
$menu->filetype = & $config->extension;
// Initialize GeSHi (Syntax Highlighting)
$geshi = new GeSHi_mDocs();
$geshi->default_language = & $config->language;
// Apply Markdown Syntax:
$doc = $markdown->transform($doc);
// Apply GeSHi Syntax Highlighting:
$doc = $geshi->parse_codeblocks($doc);
// Create Menu:
$menu->generate($config->ignore);
// Create TOC:
$toc->generate();
/**
* Helper Functions:
* @todo Possibly move this to a separate file
*/
/**
* Clean Filename Path
*
* Yes, it's overkill, but why not, it's fun!
* This function allows only the following characters to pass:
* A-Z a-z 0-9 - _ . /
*
* It also goes into great detail to make sure that troublesome
* slash and period characters are not abused by would-be hackers.
* So because of this, you can't read any file or folder that starts
* or ends with a period, but then again, you shouldn't have public
* files named like that in the first place, right?
*
* @param string $file
* @return string $filename
*/
function clean_filename($filename='')
{
$patern[] = '/[^\w\.\-\_\/]/';
$replacement[] = '';
$patern[] = '/\.+/';
$replacement[] = '.';
$patern[] = '/\/+/';
$replacement[] = '/';
$patern[] = '/(\/\.|\.\/)/';
$replacement[] = '/';
$patern[] = '/\.+/';
$replacement[] = '.';
$patern[] = '/\/+/';
$replacement[] = '/';
$filename = preg_replace($patern,$replacement,$filename);
$filename = trim($filename,' ./\\');
return $filename;
}
?>