-
Notifications
You must be signed in to change notification settings - Fork 2
/
marksite.php
408 lines (336 loc) · 11.9 KB
/
marksite.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/*
* Marksite - A text-to-html conversion framework for web builders
* Copyright (C) 2011 - 2012 Yao Wei.
* This work is licensed under GNU General Public License version 3.
* See COPYING for details.
*/
// include
include_once "config.php";
include_once "markdown.php";
class Marksite_Parser
{
// menu buffer
var $menu;
// array indicating current position
var $current = array();
// blocks
var $blocks = array();
// manifest for appcache
var $manifest = array('static' => '',
'parsed' => '',
'md5summings' => '');
// recursive delete from
// http://php.net/manual/en/class.recursivedirectoryiterator.php
function empty_dir($dir)
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $path) {
if ($path->isDir()) {
rmdir($path->__toString());
} else {
unlink($path->__toString());
}
}
rmdir($dir);
}
function copy_files($src, $dst)
{
$ignored_files = array("php", "markdown", "html", "md");
$ignored_files_re = implode("|", $ignored_files);
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($src),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $path) {
$src_path = $path->__toString();
$quoted_src = preg_quote($src, '/'); // escape quote
$clean_path = preg_replace("/^$quoted_src/","",$src_path);
$dst_path = preg_replace("/^$quoted_src/",$dst,$src_path);
// don't copy hidden files and prohibited files
if (
!preg_match(
"/(\/\.[^\/\.]|\.($ignored_files_re)\$)/",
$src_path
)
) {
if ($path->isDir()) {
if( !file_exists($dst_path) ) {
mkdir($dst_path);
}
} else {
copy($src_path, $dst_path);
print("* $src_path\n");
$this->manifest['md5summings'] .= md5_file($dst_path);
$this->manifest['static'] .= "$clean_path\n";
}
}
}
}
function parse()
{
if (is_dir(MARKSITE_DST_PATH)) {
$this->empty_dir(MARKSITE_DST_PATH);
}
if (mkdir(MARKSITE_DST_PATH)) {
print("## Generating Menu\n");
$this->menu = $this->prepare_menu("");
print("\n");
if (is_dir(MARKSITE_BLOCKS_PATH)) {
print("## Generating Blocks\n");
$this->prepare_blocks(MARKSITE_BLOCKS_PATH);
print("\n");
}
print("## Generating Contents\n");
$this->generate_contents("");
print("\n");
print("## Copying Files\n");
$this->copy_files(MARKSITE_SRC_PATH, MARKSITE_DST_PATH);
print("\n");
print("## Writing Manifest\n");
$this->write_manifest();
print("\n");
print("**All Done!**\n");
print("\n");
} else {
die("Cannot create new directory: ".MARKSITE_DST_PATH);
}
}
function prepare_menu($dir)
{
include MARKSITE_SRC_PATH."$dir"."info.php";
$menu = array();
$uri_before = MARKSITE_ABSOLUTE_PATH.$dir;
// prevent menu_hidden not set
if ( !isset($menu_hidden) ) {
$menu_hidden = array();
}
foreach ( $contents as $file => $title ) {
// skip hidden one
if ( in_array( $file, $menu_hidden ) ) {
continue;
}
$src_file = MARKSITE_SRC_PATH.$dir.$file;
if (preg_match("/^http:\/\//", $file)) {
$menu[$file] = array(
'uri' => $file,
'title' => $title
);
} elseif (is_dir($src_file)) {
$menu[$file] = array(
'uri' => $uri_before.$file."/",
'title' => $title,
'menu' => $this->prepare_menu($dir.$file."/"),
);
} elseif ( file_exists("$src_file.markdown")
|| file_exists("$src_file.md")
|| file_exists("$src_file.php")
|| file_exists("$src_file.html")
) {
$menu[$file] = array(
'uri' => $uri_before.$file.".html",
'title' => $title,
);
}
}
return $menu;
}
function has_menu($level)
{
return $level < count($this->current);
}
function menu($level, $depth=1)
{
// prevent error
if (!$this->has_menu($level)) {
return "";
}
$target_menu = $this->menu;
for ($i = 0; $i < $level; $i++) {
$target_menu = $target_menu[$this->current[$i]]['menu'];
}
return $this->menu_recursion($target_menu, $level, $depth, true);
}
function menu_recursion($target_menu, $level, $depth,
$is_current = false
) {
$output = "";
foreach ($target_menu as $file => $menuitem) {
$uri = $menuitem['uri'];
$title = $menuitem['title'];
$is_current_next = false;
if ($is_current && $file == $this->current[$level]) {
$is_current_next = true;
//ancestor: current level, and not index
if ( $level < count($this->current)-1
&& $this->current[$level+1] != "index"
) {
$output .= "<li class=\"current-ancestor\">";
} else {
$output .= "<li class=\"current\">";
}
} else {
$output .= "<li>";
}
$output .= "<a href=\"$uri\">$title</a>";
if ( $depth>1
&& isset($menuitem['menu'])
&& !empty($menuitem['menu'])
) {
$output .= "\n<ul>\n";
$output .= $this->menu_recursion($menuitem['menu'], $level+1, $depth-1, $is_current_next);
$output .= "</ul>\n";
}
$output .= "</li>\n";
}
return $output;
}
function write_themed($dst_file, $title, $contents)
{
// some usable variable for theme
$home_path = MARKSITE_ABSOLUTE_PATH;
if ($page_output = fopen("$dst_file.html", "c")) {
// starting output buffer
ob_start();
// run theme, generate content
include MARKSITE_THEME_PATH."page.php";
// get output
$themed_contents = ob_get_contents();
// clean buffer
ob_end_clean();
// write contents
fwrite($page_output, $themed_contents);
fclose($page_output);
} else {
die("Cannot output page to: $dst_file.html");
}
}
function generate_contents($dir)
{
// read directory information
// "Title" => "filename" (without postfix)
//
// $contents = Array(
// "index" => "Home",
// "aboutme" => "About Me"
// );
//
include MARKSITE_SRC_PATH.$dir."info.php";
foreach ( $contents as $file => $title ) {
if (preg_match("/^http:\/\//", $file)) {
continue;
}
$act_file = $dir.$file;
$src_file = MARKSITE_SRC_PATH.$act_file;
$dst_file = MARKSITE_DST_PATH.$act_file;
// indicating where i am
array_push($this->current, $file);
if (is_dir($src_file)) {
if (mkdir($dst_file)) {
$this->generate_contents($dir.$file."/");
} else {
die("Cannot create new directory: $dst_file");
}
} elseif (file_exists($src_file)) {
print("* $src_file (copy)\n");
copy($src_file, $dst_file);
$this->manifest['md5summings'] .= md5_file($dst_file);
$this->manifest['static'] .= "$act_file\n";
} else {
$contents = $this->generate_context($src_file);
$this->write_themed($dst_file, $title, $contents);
$this->manifest['md5summings'] .= md5_file("$dst_file.html");
if ($file == 'index') {
$this->manifest['parsed'] .= "./$dir\n";
} else {
$this->manifest['parsed'] .= "./$act_file.html\n";
}
}
array_pop($this->current);
}
}
function prepare_blocks($dir) {
$dir_handler = opendir($dir);
while (($filename = readdir($dir_handler)) !== false) {
if (preg_match("/^\./", $filename)) {
continue;
}
$block_name = preg_replace("/\.([^\.]*)$/","",$filename);
$src_file = $dir.$block_name;
$this->block[$block_name] = $this->generate_context($src_file);
}
}
function generate_context($src_file)
{
$contents = "";
if ( file_exists("$src_file.md")
&& $page = fopen("$src_file.md", "r")
) {
print("* $src_file.md\n");
// read file, convert it from Markdown to HTML
$size = filesize("$src_file.md");
if ($size > 0) {
$contents = Markdown(fread($page, $size));
}
fclose($page);
} elseif (file_exists("$src_file.markdown")
&& $page = fopen("$src_file.markdown", "r")
) {
print("* $src_file.markdown\n");
// read file, convert it from Markdown to HTML
$size = filesize("$src_file.markdown");
if ($size > 0) {
$contents = Markdown(fread($page, $size));
}
fclose($page);
} elseif (file_exists("$src_file.php")) {
print("* $src_file.php\n");
ob_start();
include "$src_file.php";
$contents = ob_get_contents();
ob_end_clean();
} elseif (file_exists("$src_file.html")
&& $page = fopen("$src_file.html", "r")
) {
print("* $src_file.html\n");
// read file
$size = filesize("$src_file.html");
if ($size > 0) {
$contents = fread($page, $size);
}
} else {
print("* Warning: $src_file.{markdown|md|php|html} does not exist.\n");
}
return $contents;
}
function write_manifest() {
if (MARKSITE_APPCACHE == 'none') {
return;
}
$manifest_path = MARKSITE_DST_PATH."site.appcache";
$manifest_file = fopen($manifest_path, "x");
if ($manifest_file) { // the file does not exist before write.
fwrite($manifest_file, "CACHE MANIFEST\n");
} else {
$manifest_file = fopen($manifest_path, "a");
}
$md5sum = md5($this->manifest['md5summings']);
fwrite($manifest_file, "\n# MARKSITE APPCACHE $md5sum\n");
fwrite($manifest_file, "CACHE:\n");
switch (MARKSITE_APPCACHE) {
case 'all':
fwrite($manifest_file, "\n# STATIC CONTENTS\n");
fwrite($manifest_file, $this->manifest['static']);
// not breaked here.
case 'parsed':
fwrite($manifest_file, "\n# PARSED CONTENTS\n");
fwrite($manifest_file, $this->manifest['parsed']);
}
fclose($manifest_file);
}
}
$marksite = new Marksite_Parser;
$marksite->parse();