forked from WhiteHouse/source-code-policy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Build.php
executable file
·71 lines (57 loc) · 1.85 KB
/
Build.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
<?php
require __DIR__ . '/vendor/autoload.php';
use Pandoc\Pandoc;
class Build {
public static $markdown_dir = __DIR__ . '/markdown';
public function __construct() {
foreach (glob(self::$markdown_dir . '/*', GLOB_ONLYDIR) as $folder) {
$this->Generate($folder,'html');
$this->Generate($folder,'docx');
$this->Generate($folder,'epub');
$this->Generate($folder,'odt');
}
}
/**
* Parses the markdown source to output clean html for publishing on web.
* @TODO provide error handling if pandoc is not installed with instructions to install
* @return boolean true if completed, false if error
*/
public function Generate($folder, $output='html') {
$content = "";
$output_directory = __DIR__ .'/'. $output;
foreach (glob($folder . '/*') as $file)
{
$content .= file_get_contents($file);
}
//start the build
echo 'Building ' . basename($folder) . '.'.$output.'... ';
//convert md to html via pandoc bin
$pandoc = new Pandoc();
$options = array(
"from" => "markdown",
"to" => $output,
"standalone" => null, //this includes the meta data in the markup
"toc" => null,
);
$generate = $pandoc->runWith($content, $options);
switch($output){
case 'html':
//tidy the output html
$tidy = new Tidy();
$options = array('indent' => true);
$tidy->parseString($generate, $options);
$tidy->cleanRepair();
// save to file
file_put_contents($output_directory . '/' . basename($folder) . '.' . $output, (string) $tidy);
break;
default:
file_put_contents($output_directory . '/' . basename($folder) . '.' . $output, (string) $generate);
break;
}
// output the work completed
echo 'done!' . PHP_EOL;
return true;
}
}
//Run the build script
new Build();