-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathfilter_files.php
485 lines (400 loc) · 16.5 KB
/
filter_files.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php // $Id$
/*
This file is part of the Windows Compiled HTML Help
Manual Generator of the PHP Documentation project.
The filters included in this file are to refine
the XSL generated HTML codes. Some filters may
be converted to XSL templates, but not all.
*/
if (!isset($HTML_SRC)) {
die("This script is called from make_chm.php to filter output from XSL DocBook templates");
}
// make sure our regexes work under PHP >= 5.1
ini_set('pcre.backtrack_limit', PHP_INT_MAX);
$counter = filterFiles();
// Filter XSL generated files through some refine filters
function filterFiles()
{
global $HTML_SRC, $HTML_TARGET, $INDEX_FILE, $LANGUAGE;
// How many files were processed
$counter = 0;
// Try to figure out what index file to use
if (file_exists("$HTML_SRC/index.html")) {
$INDEX_FILE = "index.html";
} else { $INDEX_FILE = "manual.html"; }
// Open the directory, and do the work on all HTML files
$handle = opendir($HTML_SRC);
while (false !== ($filename = readdir($handle))) {
// Only process html files
if (strpos($filename, ".html")) {
$counter++;
echo "\r \r";
echo "> $counter $filename";
refineFile($filename);
}
}
closedir($handle);
// Copy all the images to the target directory
exec("mkdir $HTML_TARGET\\figures");
exec("copy $HTML_SRC\\figures $HTML_TARGET\\figures /Y");
// Copy all supplemental files to the target directory
exec("copy suppfiles\\html $HTML_TARGET /Y");
// Copy all HTML Help files to the target directory too
exec("copy $HTML_SRC\\php_manual_$LANGUAGE.hh? $HTML_TARGET /Y");
// Rewrite script file to include current language and date
$script_js = join("", file("$HTML_TARGET/_script.js"));
$script_js = str_replace("LANGUAGE_HERE", $LANGUAGE, $script_js);
$script_js = str_replace("DATE_HERE", date("Y-m-d"), $script_js);
$fp = fopen("$HTML_TARGET/_script.js", "w");
fwrite($fp, $script_js);
fclose($fp);
// Rewrite HHP file to make UK English default language like in template
// prior to DocBook XSL 1.66.1. Add supplementary files to [FILES] section
// and also insert [MERGE FILES] section
$hhp_file = "$HTML_TARGET/php_manual_$LANGUAGE.hhp";
if (file_exists($hhp_file)) {
$php_hhp = join("", file($hhp_file));
// Get rid of hh autoindex "feature" and set UK English language
$php_hhp = preg_replace("|Auto Index=Yes\W+|i","",$php_hhp);
$php_hhp = str_replace("Language=0x0409 English (UNITED STATES)","Language=0x0809 English (UNITED KINGDOM)",$php_hhp);
// Capturing line delimiter
preg_match("|\[FILES\](\W+)\w|i",$php_hhp,$matches);
$delim = $matches[1];
// Building list of supplemental files. glob doesn't work in 4.3.7/8 :(
$d = dir("suppfiles/html");
$supp_files = "";
while (false !== ($entry = $d->read())) {
if ($entry != "." && $entry != ".." && !is_dir($entry))
$supp_files .= $delim.$entry;
}
$supp_files .= $delim."_index.html";
// Build list of figures
$d = dir("$HTML_TARGET/figures");
$figure_files = "";
while (false !== ($entry = $d->read())) {
if ($entry != "." && $entry != ".." && !is_dir($entry))
$figure_files .= $delim.'figures\\'.$entry;
}
// Insert [MERGE] section, figures and supplemental files
$php_hhp = preg_replace(
"|\[FILES\]((\W+)\w)|i",
"[MERGE FILES]$2php_manual_notes.chm$2$2[FILES]$figure_files$supp_files$1",
$php_hhp);
$fp = fopen($hhp_file, "w");
fwrite($fp, $php_hhp);
fclose($fp);
}
return $counter;
} // filterFiles() function end
// Refine HTML code in XSL generated files
function refineFile($filename)
{
global $HTML_SRC, $HTML_TARGET, $INDEX_FILE, $preNum;
// The number of <pre> parts is zero (used for example copy links)
$preNum = 0;
// Read in the contents of the source file
$content = join("", file("$HTML_SRC/$filename"));
//------------------------------------------------------------------
// Find page title and format it properly
preg_match('!<title>\s*(.+)</title>!Us', $content, $matched);
$page_title = $matched[1];
// Replace title with simple <title> content [shorter, without tags]
$content = preg_replace(
'!<h(\d)[^>]*>.+</h\1>!Us',
"<h1 class=\"masterheader\"><span id=\"pageTitle\">$page_title</span></h1>",
$content,
1
);
//------------------------------------------------------------------
// Additional divisions for skin support
// Adding div id="pageHeaders" instead of titlepage div
$content = preg_replace('|<div class="titlepage">|', '<div id="pageHeaders">', $content, 1);
// For headers we have several possibilities how to close div id="pageHeaders"
// and open div with id="pageText"
if (strpos($content, '<div class="refnamediv">') !== FALSE) {
// A function page
// extend pageHeaders div (former titlepage) to cover refnamediv with funcAvail,
// funcUsage and funcPurpose spans
$content = str_replace('</h1></div><div class="refnamediv">', '</h1>', $content);
// insert pageText div before first text division like refsect1, sect1 and so on
// i.e. just after former titlepage end
$content = preg_replace(
'!(</h2></div>)(<div class="([^"]+)")!i',
'\1<div id="pageText">\2',
$content,
1
);
}
// The index page
elseif ($filename == $INDEX_FILE) {
// Need to close one more div on this page before adding pageHeader end and pageText start
$content = str_replace(
"</h1></div>",
'</h1></div></div></div><div id="pageText"><div>',
$content
);
$content = str_replace("<hr></div>","</div><hr>", $content);
}
// Normal page
else {
// Remove empty wrapping divs for pageHeaders
$content = preg_replace(
'!<div id="pageHeaders">((<div>)+)(<h1.+?</h1>)((</div>)+)!is',
'<div id="pageHeaders">\3</div>',
$content,
1
);
// Insert pageText like in function page
$content = preg_replace(
'!(</h1></div>)(<div class="([^"]+)"|<p>)!is',
'\1<div id="pageText">\2',
$content,
1
);
}
// Instead of closing pageText div right before div with id="pageNotes" we delete start
// tag of top level div with class="refentry", "sect1" or any other chunkable class name.
// This div ends just where we need our pageText to end and is not overlapped by pageHeaders
$content = preg_replace(
'!<div class="[^"]+" lang="[^"]+">!i', "", $content, 1
);
// If this is the index file, correct it
if ($filename == $INDEX_FILE) {
$content = newIndex($content);
}
//------------------------------------------------------------------
// Change pre sections look (examples, screen outputs, etc).
$content = preg_replace_callback(
'!<pre class="([^"]+)">(.+)</pre>!Us',
"formatPre",
$content
);
//------------------------------------------------------------------
// Add .datatable class for tables to ease styling
$content = preg_replace('!<div class="(informal)?table".*<table !U',
'\\0class="datatable" ',$content);
//------------------------------------------------------------------
// Put <p> tags after all </ul> or </div> or </table> close tags to
// enable CSS support for those paragraphs (these break a <p>)
// BUT do not put a P after our special notes container
$content = preg_replace('!</(ul|div|table)>!Us', '</\\1><p>', $content);
$content = str_replace('<div id="pageNotes"></div><p>', '<div id="pageNotes"></div>', $content);
//------------------------------------------------------------------
// Delete duplicate <p> tags from code, unneded <p></p> parts, and
// <p> before <table> or <div> or </div> or </body> or <ul>
$content = preg_replace('!<p>\s*<p>!Us', '<p>', $content);
$content = preg_replace('!<p>\s*</p>!Us', '', $content);
$content = preg_replace('!<p>\s*<(table|div|/div|/body|ul)!Us', '<\\1', $content);
//------------------------------------------------------------------
// Drop out all the <div> and </div> tags left (no need to have them)
//$content = preg_replace('!</?div[^>]*>!Us', '', $content);
// !!! Temporary fix for XSLT output escaping problems
$content = preg_replace("!&raquo; !", "» ", $content);
$content = preg_replace("!&nbsp; !", " ", $content);
//------------------------------------------------------------------
// Write out file to HTML output directory
$fp = fopen("$HTML_TARGET/$filename", "w");
fwrite($fp, $content);
fclose($fp);
} // newFace() function end
// Make the old index look somewhat better
function newIndex ($content)
{
global $HTML_TARGET;
// Get contents we need to build the _index.html file
preg_match("!^(.+)<hr>!s", $content, $_index1);
preg_match("!</div></div>(<a id=\"user_notes\">.+</html>)!s", $content, $_index2);
// Write out the two components to form a complete file
$fp = fopen("$HTML_TARGET/_index.html", "w");
fwrite($fp, $_index1[1] . $_index2[1]);
fclose($fp);
// Drop out authors list (this is on the frontpage)
$content = preg_replace(
'!<div id="pageText"><div>.*<hr>!Us',
'<div id="pageText">',
$content
);
// Get TOC title from HTML code
preg_match(
'!<div class="toc"><p><b>(.+)</b></p>!U',
$content,
$match
);
// Put toc title into title places
$content = preg_replace(
'!<title>(.+)</title>!U',
"<title>$match[1]</title>",
$content
);
$content = preg_replace(
'!<span id="pageTitle">(.+)</span>!U',
"<span id=\"pageTitle\">$match[1]</span>",
$content
);
// Drop out small TOC title
$content = preg_replace(
'!<div class="toc"><p><b>(.+)</b></p>!U',
'<div class="toc">',
$content
);
return $content;
} // newIndex() function end
// Change pre sections look
function formatPre ()
{
// Number of <pre> sections on this page
global $preNum;
// Construct clipboard copy link
$preNum++;
$linkwdiv = '<div class="codelink"><a href="javascript:void(0);" onclick="copyExample(\''
. $preNum . '\')">copy to clipboard</a></div><div class="examplecode">';
// Replace all hard line breaks
list($pre_found) = func_get_args();
// Not a PHP example
if ($pre_found[1] != 'php') {
return $linkwdiv . '<code id="example_' . $preNum . '">' . pre2code(trim($pre_found[2])) . '</code></div><p>';
}
// Convert entities to characters for color coding
$example = str_replace(
array(">", "<", "&", """),
array(">", "<", "&", "\""),
trim($pre_found[2])
);
if (!strstr($example, "<?php")) {
$example = "<?php " . $example . " ?>";
$delimiter = FALSE;
} else {
$delimiter = TRUE;
}
// Get highlited source code
$colored_example = highlight_string($example, true);
// Strip out PHP delmiter, if we added it
if (!$delimiter) {
$colored_example = str_replace(
array (
'<font color="#0000CC"><?php </font>',
'<?php ',
'<font color="#0000CC">?></font>'
),
array ('', '', ''),
$colored_example
);
}
// Get much smaller source code by converting
// display identical things to smaller size
$colored_example = str_replace(
array("\n", "<code>", "</code>", " ", "<br />"),
array("", "", "", " ", "\n"),
$colored_example
);
// Pre container to strip out uneeded font tags
$colored_example = '<pre>' . $colored_example . '</pre>';
$colored_example = str_replace(
array('<pre><font color="#000000">', '<pre><span style="color: #000000">', '</font></pre>', '</span></pre>'),
array('', '', '', ''),
$colored_example
);
// Get color settings
$color_settings = array(
'<span class="cs">' => '<font color="' . ini_get("highlight.string") . '">',
'<span class="cc">' => '<font color="' . ini_get("highlight.comment") . '">',
'<span class="ck">' => '<font color="' . ini_get("highlight.keyword") . '">',
'<span class="cb">' => '<font color="' . ini_get("highlight.bg") . '">',
'<span class="cd">' => '<font color="' . ini_get("highlight.default") . '">',
'<span class="ch">' => '<font color="' . ini_get("highlight.html") . '">',
'</span>' => '</font>'
);
// Convert colors to classes spaned
$colored_example = str_replace(
array_values($color_settings),
array_keys($color_settings),
$colored_example
);
// Try to find function names so they can be linked
// This patterns is what we are searching for:
// <span class="cd">array_keys </span><span class="ck">(
$colored_example = links2Examples(
'!<span class="cd">([a-z0-9_]+)\s*</span><span class="ck">\(!Us',
$colored_example,
1,
'cd'
);
// control structures or other keywords, like
// exit, print with possibly something before
// and after them
// Note: I do not link if, else and the
// other control structures in, because they are
// too common, and linking them would clutter up
// the examples
$colored_example = links2Examples(
'!<span class="ck">[^<]*(<br>)?[^<]*' .
'(exit|die|echo|print|empty|isset|unset|break|continue' .
'|static|global|array)[^<]*' .
'</span>!Us',
$colored_example,
2,
'ck'
);
// Return with the converted example
return $linkwdiv . '<code id="example_' . $preNum . '">' . pre2code($colored_example) . '</code></div><p>';
} // formatPre() function end
// Convert <pre> contained code to text for the <code> container
function pre2code ($text)
{
return str_replace(
array(' ', "\n"),
array(' ', "<br>\n"),
$text
);
} // pre2code() function end
// Find string matching a regexp and make function/control
// structures/keywords links
function links2Examples($regexp, $example, $idx, $class)
{
global $HTML_SRC;
// Try to find matching text in $example
if (preg_match_all($regexp, $example, $found)) {
// This is where we store all text to replace,
// [original text and replacement text]
$replace_array = array(
0 => array(),
1 => array()
);
// Loop through all function names, and try to find a file
// for them (they can be user defined functions)
foreach ($found[$idx] as $num => $reptext) {
// The main part of this filename
$filepart = strtolower(str_replace("_", "-", $reptext));
// Possible full filenames
$files = array(
"function.$filepart.html",
"class.$filepart.html",
"control-structures.$filepart.html"
);
// Guess what should be the filename for this
foreach ($files as $filename) {
// If this file exists, then we are OK
if (@file_exists("$HTML_SRC/$filename")) {
$replace_array[0][] = $found[0][$num];
$replace_array[1][] = str_replace(
$reptext,
"<a href=\"$filename\" class=\"$class\">$reptext</a>",
$found[0][$num]
);
break;
}
}
}
// Perform string replacement on example content,
// only replace functions where we can link
$example = str_replace(
$replace_array[0],
$replace_array[1],
$example
);
}
// Maybe we modified something, maybe not.
// Return with the current $example text.
return $example;
} // links2Examples() function end
?>