-
Notifications
You must be signed in to change notification settings - Fork 127
/
generate
executable file
·91 lines (77 loc) · 2.35 KB
/
generate
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
#!/usr/bin/env php
<?php
/**
* Script for generating PDF and FAQ HTML files from the PHP.earth docs repository
* and testing on Travis. It uses the Docker Pandoc image.
*/
$docsDir = realpath(__DIR__.'/..');
/**
* Function to get files from given index.
*
* @param string $index Index file to get Markdown files from
*/
function getFiles(string $index): array
{
$curDir = realpath(__DIR__.'/..');
$files = [];
$content = file_get_contents($curDir.'/'.$index);
preg_match_all('/\[.+\]\(((?!http)\/.+)(\).*)/i', $content, $matches);
foreach ($matches[1] as $file) {
if (is_dir($curDir.'/'.$file)) {
$files = array_merge($files, getFiles($file.'/README.md'));
} else if (is_file($curDir.'/'.$file)) {
$files[] = 'docs'.$file;
}
}
return $files;
}
/**
* Generate faq.html file for using in Facebook group
*/
echo $faqHtmlOutput = shell_exec('
docker run -it --rm \
-v '.$docsDir.':/opt/docs \
-w /opt/docs \
phpearth/pandoc pandoc \
--pdf-engine=xelatex \
faq/README.md \
-o /opt/docs/.github/faq.html
docker run -it --rm \
-v '.$docsDir.':/opt/docs \
-w /opt/docs \
phpearth/pandoc chmod a+rw /opt/docs/.github/faq.html
');
// Replace internal links with absolute online links
$content = file_get_contents($docsDir.'/.github/faq.html');
$content = preg_replace('/(href\=\")((?!http)\/.+(?<!\.md))(\.md\"\>|\"\>)/i', '${1}'.'https://docs.php.earth'.'${2}">', $content);
file_put_contents($docsDir.'/.github/faq.html', $content, LOCK_EX);
/**
* Generate php-earth.pdf file
*/
$files = implode(' ', getFiles('README.md'));
echo $pdfOutput = shell_exec('
docker run -it \
-v '.$docsDir.':/opt/docs \
--name php-earth-pandoc \
phpearth/pandoc pandoc \
--verbose \
--toc \
--toc-depth=1 \
--pdf-engine=xelatex \
--top-level-division=default \
-M documentclass=report \
-M title="PHP.earth documentation" \
-M author="PHP.earth" \
-M date="Generated on `date +%Y-%m-%d`" \
-M mainfont="Lato" \
-M geometry="margin=1in" \
'.$files.' \
-o /opt/docs/.github/php-earth.pdf
');
echo "php-earth.pdf created\n";
$exitCode = shell_exec('docker inspect -f {{.State.ExitCode}} php-earth-pandoc');
if (strpos($pdfOutput, '[WARNING]') !== false) {
$exitCode = 2;
}
shell_exec('docker rm php-earth-pandoc');
exit($exitCode);