-
Notifications
You must be signed in to change notification settings - Fork 3
/
mddoc
executable file
·114 lines (95 loc) · 3.06 KB
/
mddoc
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
#!/usr/bin/env php
<?php
/**
* Markdown Tree Converter
*
* Recursive converts all markdown files from a source directory to HTML
* and places them in the destination directory recreating the structure
* of the source and applying a template parser.
*/
// Composer autoloader
require_once dirname(__FILE__) . '/vendor/autoload.php';
// Deals with command-line input arguments
function usage()
{
printf(
"Usage: %s -i %s -o %s\n",
basename(__FILE__),
'/path/to/sourcedir',
'/path/to/destdir'
);
}
if (5 > $argc) {
usage();
exit;
}
$in = array_search('-i', $argv);
$src = realpath($argv[$in+1]);
if (!is_dir($src) || !is_readable($src)) {
echo "[ERROR] Invalild source directory.\n";
usage();
exit(1);
}
$out = array_search('-o', $argv);
$dest = realpath($argv[$out+1]);
if (!is_dir($dest) || !is_writeable($dest)) {
echo "[ERROR] Invalild destination directory.\n";
usage();
exit(1);
}
// Register custom read-time MarkdownFilter
stream_filter_register("markdown", "\MarkdownFilter\MarkdownFilter")
or die("Failed to register filter Markdown");
// Register custom write-time TemplateFilter
stream_filter_register("template.*", "\TemplateFilter\TemplateFilter")
or die("Failed to register filter Template");
// Load directory iterator for source
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($src),
RecursiveIteratorIterator::SELF_FIRST
);
// For every valid item
while ($it->valid()) {
// Exclude dot items (., ..)
if (!$it->isDot()) {
// If current item is a directory, the same empty directory
// is created on destination
if ($it->isDir()) {
$path = $dest . '/' . $it->getFileName();
if ((!@is_dir($path)) && !@mkdir($path, 0777, true)) {
echo "Unable to create folder {$path}\n";
exit(1);
}
}
// If current item is a markdown (*.md) file it's processed and
// saved at the coresponding destination path
if ($it->isFile() && 'md' == $it->getExtension()) {
$path = $it->key();
if (!is_readable($path)) {
echo "Unable to read file {$path}\n";
exit(2);
}
$content = file_get_contents(
'php://filter/read=markdown/resource=file://' . $path
);
if (false === $content) {
echo "Unable to read from source '" . $path . "'\n";
exit(3);
}
$pathinfo = pathinfo($dest . '/' . $it->getSubPathName());
$target = $pathinfo['dirname']
. '/' . $pathinfo['filename'] . '.html';
$result = file_put_contents(
'php://filter/write=template.' .
base64_encode(basename($path)) . '/resource=file://' . $target,
$content
);
if (false === $result) {
echo "Unable to write file '" . $target . "'\n";
exit(4);
}
}
}
$it->next();
}
exit(0);