-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate-html-docs.php
280 lines (232 loc) · 9.56 KB
/
generate-html-docs.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Code to generate HTML documents from the Markdown documents.
*
*/
require_once('vendor/autoload.php');
/**
* Extension of Parsedown class that is used to translate Markdown to HTML.
*
*/
class MyParsedown extends \Parsedown
{
protected $highlighter;
protected $minimalHighlighter;
public function __construct()
{
# Syntax highlighter for PHP
$this->highlighter = new \FSHL\Highlighter(new \FSHL\Output\Html());
$this->highlighter->setLexer(new \FSHL\Lexer\Php());
# Syntax highlighter for shell commands/scripts
$this->minimalHighlighter = new \FSHL\Highlighter(new \FSHL\Output\Html());
$this->minimalHighlighter->setLexer(new \FSHL\Lexer\Minimal());
}
/**
* Use new highlighters for code blocks.
*
* @param unknown $block
* @return unknown
*/
public function blockFencedCodeComplete($block)
{
$text = print_r($block, true);
$matches = array();
preg_match('/\[class\] => language-([a-zA-Z]+)/', $text, $matches);
if (count($matches) > 1 && $matches[1] === 'shell') {
$block['element']['text']['text'] = $this->minimalHighlighter->highlight($block['element']['text']['text']);
}
else {
$block['element']['text']['text'] = $this->highlighter->highlight($block['element']['text']['text']);
}
return $block;
}
}
/**
* Translates the specified Markdown file to HTML format and creates a file with
* the HTML translation.
*
* @param string $file the Markdown file to translate.
* @param array $files the list of all the Markdown files.
*/
function translateFile($file, $files)
{
$parsedown = new MyParsedown();
$markdown = file_get_contents($file);
$content = $parsedown->text($markdown);
$content = str_replace('<pre>', '<div class="description"><pre>', $content);
$content = str_replace('</pre>', '</pre></div>', $content);
# Create anchors for h2 and h3 tags
$content = preg_replace('/<h2>([^<]*)<\/h2>/', '<h2 id="\1">\1</h2>', $content);
$content = preg_replace('/<h3>([^<]*)<\/h3>/', '<h3 id="\1">\1</h3>', $content);
# Convert links to Markdown documents to links to HTML documents
$content = str_replace('.md">', '.html">', $content);
$html = "<!DOCTYPE html>\n" . "<html>\n" . "<head>\n" . '<meta charset="UTF-8">' . "\n"
. '<link rel="stylesheet" href="' . 'themes/apigen/theme-phpcap/src/resources/style.css">' . "\n"
. '<link rel="stylesheet" href="' . 'themes/apigen/theme-phpcap/src/resources/docstyle.css">' . "\n"
. "<title>PHPCap Documentation</title>\n"
. "</head>\n"
. "<body>\n"
. '<div id="left">'."\n"
. '<div id="menu">'."\n"
. '<div id="topmenu">'."\n"
. '<span>PHPCap Docs</span> | <a href="api/index.html">PHPCap API</a>'."\n"
. '<hr />'."\n"
. "</div>\n"
. createIndex($file, $files)
. '</div>'."\n"
. '</div>'."\n"
. '<div id="splitter"></div>'."\n"
. '<div id="right">'."\n"
. '<div id="rightInner">'."\n"
. '<div id="content">' . "\n"
. $content
. '</div>' . "\n"
. '<div id="footer">'
. "\n" . 'PHPCap documentation' . "\n" . '</div>' . "\n"
. "</div></div>\n"
. '<script src="' . 'api/resources/combined.js"></script>'. "\n"
//. '<script src="' . __DIR__ . '/docs/api/elementlist.js"></script>' . "\n"
. "</body>\n" . "</html>\n";
$outputFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).".html";
$outputFile = str_replace('docs-md', 'docs', $outputFile);
print "{$outputFile}\n";
file_put_contents($outputFile, $html);
}
/**
* Creates an index for the specified file. Code assumes
* each file has a single <h1> tag that is at the start
* of the file. The text for this tag is used as the
* name of the document represented by the file.
*
* @param string $file Markdown file for which an index is being created.
* @param array $files List of all Markdown files (i.e., the contents of the index).
*
* @return string the index in HTML format.
*/
function createIndex($file, $files)
{
$index = '';
$fileName = pathinfo($file, PATHINFO_FILENAME).".html";
if (strcmp($fileName,'index.html') === 0) {
$index = '<span id="overview">Overview</span>'."\n";
}
else {
$index .= '<a href="index.html" title="Overview"><span id="overview">Overview</span></a>'."\n";
}
$index .= "<ul>\n";
foreach ($files as $indexFile) {
$indexFileName = pathinfo($indexFile, PATHINFO_FILENAME).".html";
if (!(strcmp($indexFileName,"index.html") === 0)) {
$parsedown = new MyParsedown();
$markdown = file_get_contents($indexFile);
$content = $parsedown->text($markdown);
$html = new DOMDocument();
#------------------------------------------------------
# Get the first <h1> element value; it will be used as
# the label for the link for the document
#------------------------------------------------------
$h1 = '';
$html->loadHTML($content);
foreach($html->getElementsByTagName('h1') as $h1) {
// print("------------HTML: ".$html->saveHTML($h1));
$h1 = $h1->nodeValue;
break;
}
#---------------------------------------------
# Get the text for the h2 and h3 tags
#---------------------------------------------
$xpath = new DOMXPath($html);
$expression = '(//h2|//h3)';
$elements = $xpath->query($expression);
$headings = array();
foreach ($elements as $element) {
array_push($headings, [$element->tagName, $element->nodeValue]);
}
# if this is the index entry for the current file being processed
if (strcmp($fileName,$indexFileName) === 0) {
$index .= '<li class="active"><a href="'.$indexFileName.'">'.$h1.'</a></li>'."\n";
# if any h2 or h3 headings were found
if (count($headings) > 0) {
$index .= '<ul class="intraPage">'."\n";
$lastTag = 'h2';
foreach ($headings as $heading) {
list($tag, $text) = $heading;
if ($tag > $lastTag) {
$index .= '<ul class="intraPage">'."\n";
} elseif ($tag < $lastTag) {
$index .= "</ul>\n";
}
$index .= ' <li class="active"><a href="#'.$text.'">'.$text.'</a></li>'."\n";
$lastTag = $tag;
}
if ($lastTag > 'h2') {
$index .= "</ul>\n";
}
$index .= "</ul>\n";
}
}
else {
$index .= '<li><a href="'.$indexFileName.'">'.$h1.'</a></li>'."\n";
}
}
}
$index .= "</ul>\n";
return $index;
}
/**
* Sorts file names in alphabetical order, except that file names
* starting with 'user' (ignoring case) always sort first, and
* file names starting with 'developer' (ignoring case) always sort last.
*
* @param string $a file name 1 (possibly a full path name)
* @param string $b file name 2 (possibly a full path name)
* @return number -1 for a < b, 0 for a = b, and 1 for a > b
*/
function fileNameCompare($a, $b)
{
$comparison = 0;
# Get just the file names from the full paths
$a = basename($a);
$b = basename($b);
if (stripos($a, 'user', 0) === 0 && stripos($b, 'user', 0) !== 0) {
# if $a starts with 'user' and $b does not, sort $a first
$comparison = -1;
} elseif (stripos($a, 'user', 0) !== 0 && stripos($b, 'user', 0) === 0) {
# if $a does not start with 'user' and $b does, sort $b first
$comparison = 1;
} elseif (stripos($a, 'developer', 0) === 0 && stripos($b, 'developer', 0) !== 0) {
# if $a starts with 'developer' and $b does not, sort $a last
$comparison = 1;
} elseif (stripos($a, 'developer', 0) !== 0 && stripos($b, 'developer', 0) === 0) {
# if $a does not start with 'developer' and $b does, sort $b last
$comparison = -1;
} else {
# for cases not covered above, sort alphabetically
$comparison = strcmp($a,$b);
}
return $comparison;
}
# Set the input and output directories
$inputDirectory = __DIR__."/docs-md/";
$outputDirectory = __DIR__."/docs/";
$inputResources = $inputDirectory.'/resources/';
$outputResources = $outputDirectory.'/resources/';
# Create the html resources directory if it doesn't already exist
if (!file_exists($outputResources)) {
mkdir($outputResources);
}
# Copy the Markdown resources to the HTML resources directory
$resources = glob($inputResources . "*");
foreach ($resources as $resource) {
$dest = str_replace('docs-md', 'docs', $resource);
copy($resource, $dest);
}
# Process each Markdown file
$files = glob($inputDirectory . "*.md");
usort($files, "fileNameCompare");
foreach($files as $file)
{
print "\nTranslating\n$file\n";
translateFile( $file, $files );
}
print "\nDone.\n";