-
Notifications
You must be signed in to change notification settings - Fork 63
/
gen_doc.php
116 lines (105 loc) · 2.77 KB
/
gen_doc.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
<?php
/*****************************************************
* A PHP script to parse markdown files, and generate
* HTML files.
* @author ideawu
* @link http://www.ideawu.com/
*
* Usage:
* php gen_doc.php md_dir output_dir
*****************************************************/
$input_dir = isset($argv[1])? $argv[1] : '.';
$output_dir = isset($argv[2])? $argv[2] : './output';
$input_dir = rtrim($input_dir, '/');
$output_dir = rtrim($output_dir, '/');
parse_dir($input_dir, $output_dir);
function parse_dir($input_dir, $output_dir, $base_url='.'){
if(!file_exists($output_dir)){
mkdir($output_dir);
}
$template = "$input_dir/template.php";
if(!file_exists($template)){
$template = null;
}
$files = scandir($input_dir);
foreach($files as $file){
if($file == '.' || $file == '..'){
continue;
}
$fullpath = "$input_dir/$file";
if(is_dir($fullpath)){
$new_output_dir = $output_dir . '/' . $file;
parse_dir($fullpath, $new_output_dir, "$base_url/..");
}else{
$ps = explode('.', $file);
$ext = $ps[count($ps) - 1];
if($ext == 'md'){
gen_doc($fullpath, $output_dir, $template, $base_url);
}else if(!in_array($ext, array('php'))){
copy($fullpath, "$output_dir/$file");
}
}
}
}
function gen_doc($file, $output_dir, $template=null, $base_url=''){
$name = str_replace('.md', '', basename($file));
$markdown = array(
'name' => $name,
'input_file' => $file,
'output_file' => "$output_dir/$name.html",
'title' => '',
'html' => '',
'base_url' => $base_url,
);
if(file_exists($markdown['output_file']) && filemtime($file) < filemtime($markdown['output_file'])){
echo "[skip] {$markdown['output_file']} => {$markdown['output_file']}\n";
return;
}
$in_comment = false;
$lines = file($file);
foreach($lines as $line){
if($line[0] == '`'){
$in_comment = $in_comment? false:true;
}
if($line[0] == '#' && $line[1] != '#' && !$in_comment){
$line = trim($line);
$line = trim($line, '#');
$line = trim($line);
$markdown['title'] = $line;
}
}
$cmd = "python -m markdown.__main__ -x tables -x fenced_code -x headerid $file";
exec($cmd, $result, $retval);
$markdown['html'] = join("\n", $result);
if($template){
ob_start();
include($template);
$html = ob_get_clean();
}else{
$html = default_template($markdown);
}
echo "[build] {$markdown['output_file']} => {$markdown['output_file']}\n";
file_put_contents($markdown['output_file'], $html);
}
function default_template($markdown){
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{$markdown['title']}</title>
<style type="text/css">
body{
font-size: 14px;
font-family: arial;
background: #fff;
}
</style>
</head>
<body>
{$markdown['html']}
</body>
</html>
HTML;
return $html;
}