forked from lszzy/kindle-codebook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkindle-codebook.sh
executable file
·108 lines (100 loc) · 2.59 KB
/
kindle-codebook.sh
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
#!/usr/bin/php
<?php
//参数检查
if (!$argv || count($argv) < 2) {
exit("format: kindle-codebook.sh input [output] [skips]\nexample: kindle-codebook input/ output.html \".svn,.git\"\n");
}
//安全检查
$input = $argv[1];
$output = !empty($argv[2]) ? $argv[2] : basename($argv[1]) . '.html';
$skips = !empty($argv[3]) ? explode(',', $argv[3]) : array();
if (!file_exists($input)) {
exit('input files not exist\n');
}
if (file_exists($output)) {
exit('output file exist\n');
}
//扫描目录
function scanFiles($target)
{
$dir = rtrim($target, '/\\');
$files = array();
//扫描目录,兼容scandir
if (function_exists('scandir')) {
$subs = scandir($dir);
} else {
$subs = array();
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
$subs[] = $file;
}
closedir($handle);
sort($subs);
}
$subs = array_diff($subs, array('.', '..'));
foreach ($subs as $sub) {
$files[] = $dir . '/' . $sub;
}
return $files;
}
//递归扫描目录文件列表
function scanDirs($target, $skips)
{
$result = array();
$files = scanFiles($target);
$dirs = array();
//排序并过滤,优先文件
foreach ($files as $file) {
$skip = false;
foreach ($skips as $value) {
if (strpos($file, $value) !== false) {
$skip = true;
break;
}
}
if ($skip) {
continue;
}
if (is_file($file)) {
$result[] = $file;
} else {
$dirs[] = $file;
}
}
//再目录
foreach ($dirs as $dir) {
$result = array_merge($result, scanDirs($dir, $skips));
}
return $result;
}
//获取文件列表
$files = is_dir($input) ? scanDirs($input, $skips) : array($input);
$title = basename($input);
$html = '<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>' . $title . '</title>
</head>
<body>
<h1>' . $title . '</h1>
';
$content = '';
//目录
foreach ($files as $file) {
$anchor = str_replace(array('/', '\\', '.'), '-', $file);
$content .= "<a id='catalog-$anchor' href='#{$anchor}'>{$file}</a>\n";
}
//内容
foreach ($files as $file) {
$anchor = str_replace(array('/', '\\', '.'), '-', $file);
$text = highlight_file($file, true);
$content .= "<h3 id='{$anchor}'><a href='#catalog-$anchor'>$file</a></h3>";
$content .= "<p>" . $text . "</p>\n";
}
$html .= nl2br($content) . '
</body>
</html>';
$status = file_put_contents($output, $html);
exit($status ? "success!\n" : "fail!\n");
?>