Skip to content

Commit abd146f

Browse files
committed
make_chm: switch to zh and add helper script
1 parent ab24011 commit abd146f

File tree

2 files changed

+97
-48
lines changed

2 files changed

+97
-48
lines changed

make_chm.php

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ function buildChm( $cpp = true )
2929
//for debuging set false to aviod recreation of xhtml files.
3030
$processFiles = true;
3131

32-
$sourceDir = $scriptDir.DIRECTORY_SEPARATOR ."en";
33-
$commonDir = $scriptDir.DIRECTORY_SEPARATOR ."common";
32+
$sourceDir = $scriptDir.DIRECTORY_SEPARATOR ."zh";
33+
$commonDir = $scriptDir.DIRECTORY_SEPARATOR ."common";
3434
$cssDir = $scriptDir.DIRECTORY_SEPARATOR ."css";
35-
35+
3636
$keywordsFiles = array();
3737
$errorsFiles = array();
3838
$treeFiles = array();
@@ -46,11 +46,11 @@ function buildChm( $cpp = true )
4646
$processedFiles = 0;
4747
$noTitleLinks = 0;
4848
$colspans = 0;
49-
50-
foreach( $arrFiles as $file )
51-
{
49+
50+
foreach( $arrFiles as $file )
51+
{
5252
$filelow = strtolower( $file );
53-
$relativename = substr( $filelow, strlen( $sourceDir ) + 1 );
53+
$relativename = substr( $filelow, strlen( $sourceDir ) + 1 );
5454

5555
// current file
5656
//echo( $relativename."\n" );
@@ -68,7 +68,7 @@ function buildChm( $cpp = true )
6868
if ( count( $patharr ) > 1 )
6969
{
7070
array_pop( $patharr );
71-
foreach( $patharr as $pathelem )
71+
foreach( $patharr as $pathelem )
7272
{
7373
if ( strlen($parent_id) > 0 )
7474
{
@@ -78,7 +78,7 @@ function buildChm( $cpp = true )
7878
{
7979
$parent_id = $pathelem;
8080
}
81-
}
81+
}
8282
}
8383

8484
// document id and parent document id
@@ -94,7 +94,7 @@ function buildChm( $cpp = true )
9494

9595
// load xhtml and fix tags
9696
$dom = new DomDocument('1.0', 'UTF-8');
97-
97+
9898
$dom->loadHTMLFile( $targetfile );
9999

100100
if ( $processFiles )
@@ -107,20 +107,20 @@ function buildChm( $cpp = true )
107107
$nodes = getNodesByName( $dom, "link");
108108
foreach( $nodes as $e )
109109
{
110-
if( $e->hasAttribute("href"))
110+
if( $e->hasAttribute("href"))
111111
{
112112
$e->setAttribute("href", basename( $e->getAttribute("href")));
113113
}
114114

115-
if( $e->getAttribute("rel") != "stylesheet" )
115+
if( $e->getAttribute("rel") != "stylesheet" )
116116
{
117117
$e->parentNode->removeChild( $e );
118118
}
119119
}
120120

121121
// remove divs with internal info > 1mb
122122
$nodes = getNodesByName( $dom, "div");
123-
foreach( $nodes as $e )
123+
foreach( $nodes as $e )
124124
{
125125
if( $e->getAttribute("class") == "printfooter" || strpos( $e->getAttribute("class"), "catlinks" ) !== false || strpos( $e->getAttribute("class"), "coliru-btn" ) !== false )
126126
{
@@ -141,14 +141,14 @@ function buildChm( $cpp = true )
141141

142142
// fix links to other pages
143143
$nodes = array_merge( getNodesByName( $dom, "a"), getNodesByName( $dom, "area"));
144-
foreach( $nodes as $e )
144+
foreach( $nodes as $e )
145145
{
146146
if( $e->hasAttribute("title"))
147147
{
148148
$href = $e->getAttribute("href");
149149
if ( strpos( $href, "http") === false )
150150
{
151-
$new_value = str_replace( "/" , "-", $e->getAttribute("title"));
151+
$new_value = str_replace( "/" , "-", $e->getAttribute("title"));
152152
// fix bad titles where space is used instead of _
153153
$new_value = str_replace( " " , "_", $new_value);
154154

@@ -179,7 +179,7 @@ function buildChm( $cpp = true )
179179

180180
$fref_folders = explode( "/" , $hrefname );
181181

182-
foreach ($fref_folders as $frf)
182+
foreach ($fref_folders as $frf)
183183
{
184184
$relative_folders[] = $frf;
185185
}
@@ -192,18 +192,18 @@ function buildChm( $cpp = true )
192192

193193
// remove unwanted colspan="5", about 4mb of text!
194194
$nodes = getNodesByName( $dom, "td");
195-
foreach( $nodes as $e )
195+
foreach( $nodes as $e )
196196
{
197197
if( $e->hasAttribute("colspan") && $e->getAttribute("colspan") == 5 )
198198
{
199199
$e->removeAttribute("colspan");
200200
$colspans++;
201201
}
202202
}
203-
203+
204204
// fix links to images
205205
$nodes = getNodesByName( $dom, "img");
206-
foreach( $nodes as $e )
206+
foreach( $nodes as $e )
207207
{
208208
if( $e->hasAttribute("src"))
209209
{
@@ -217,7 +217,7 @@ function buildChm( $cpp = true )
217217
$h1title = $dom->getElementById("firstHeading");
218218
if ( $h1title )
219219
{
220-
foreach( $h1title->childNodes as $h1n )
220+
foreach( $h1title->childNodes as $h1n )
221221
{
222222
$document_title .= $h1n->textContent;
223223
}
@@ -254,8 +254,8 @@ function buildChm( $cpp = true )
254254
$keywords = $pre_keywords;
255255
}
256256
foreach ( $keywords as $keywrd )
257-
{
258-
$keywordsFiles[trim($keywrd)][] = 'chmhelp\\'.$id.".html";
257+
{
258+
$keywordsFiles[trim($keywrd)][] = 'chmhelp\\'.$id.".html";
259259
}
260260

261261
// building toc tree
@@ -318,19 +318,19 @@ function buildChm( $cpp = true )
318318
$metadata .= "\t<li><object type=\"text/sitemap\">\n";
319319
$metadata .= "\t\t<param name=\"Name\" value=\"".$kwf."\">\n";
320320
foreach ($keywordsFiles[$kwf] as $file)
321-
{
321+
{
322322
$metadata .= "\t\t<param name=\"Local\" value=\"".$file."\">\n";
323323
}
324-
$metadata .= "\t</object>\n";
324+
$metadata .= "\t</object>\n";
325325
}
326326

327327
$metadata .= "\n</ul></body></html>";
328328
file_put_contents( $scriptDir.DIRECTORY_SEPARATOR ."cppreference.hhk", $metadata );
329329

330330
// building project file
331331
echo( "building project cppreference.hhp ...\n");
332-
$metadata = "[OPTIONS]\nCompatibility=1.1 or later\nCompiled file=cppreference.chm\nContents file=cppreference.hhc\nDefault Window=default\nDefault topic=chmhelp\index.html\nDisplay compile progress=No\nFull-text search=Yes\nIndex file=cppreference.hhk\nLanguage=0x409 English (United States)\nTitle=C / C++ Documentation from cppreference.com\n[WINDOWS]\n";
333-
$metadata .= 'default="C / C++ Documentation from cppreference.com","cppreference.hhc","cppreference.hhk","chmhelp\index.html","chmhelp\index.html",,,,,0x62520,,0x301e,[0,32,800,592],0x1020000,,,,,,0'."\n[FILES]\n";
332+
$metadata = "[OPTIONS]\nCompatibility=1.1 or later\nCompiled file=cppreference.chm\nContents file=cppreference.hhc\nDefault Window=default\nDefault topic=chmhelp\index.html\nDisplay compile progress=No\nFull-text search=Yes\nIndex file=cppreference.hhk\nLanguage=0x804 中文(简体,中国)\nTitle=Title=C / C++ 参考文档\n[WINDOWS]\n";
333+
$metadata .= 'default="C / C++ 参考文档","cppreference.hhc","cppreference.hhk","chmhelp\index.html","chmhelp\index.html",,,,,173408,250,0,[0,32,1080,700],0x0000000,,,,,0,0'."\n[FILES]\n";
334334

335335
buildListOfFiles( $treeFiles, $metadata );
336336

@@ -342,7 +342,7 @@ function buildChm( $cpp = true )
342342
dirToArray( $arrFiles, $commonDir, "." );
343343
echo( "copying images and styles ...\n");
344344
echo( "files: ".count( $arrFiles )."\n");
345-
foreach( $arrFiles as $file )
345+
foreach( $arrFiles as $file )
346346
{
347347
if ( endsWith( $file, '.css' ))
348348
{
@@ -367,17 +367,17 @@ function buildChm( $cpp = true )
367367
dirToArray( $arrFiles, $cssDir, ".css" );
368368
echo( "copying new styles ...\n");
369369
echo( "files: ".count( $arrFiles )."\n");
370-
foreach( $arrFiles as $file )
370+
foreach( $arrFiles as $file )
371371
{
372372
copy( $file, $targetDir.DIRECTORY_SEPARATOR.basename( $file ));
373373
echo( "file copied: ".basename( $file )."\n");
374374
gc_collect_cycles();
375-
}
375+
}
376376
}
377377

378378
function buildTree(&$array, &$metadata, $level )
379379
{
380-
foreach( $array as $object )
380+
foreach( $array as $object )
381381
{
382382
if ( count( $object->childrens ) > 0 )
383383
{
@@ -392,7 +392,7 @@ function buildTree(&$array, &$metadata, $level )
392392
$metadata .= '<li><object type="text/sitemap"><param name="Name" value="'.htmlspecialchars( $object->title ).'"><param name="Local" value="'.$object->url.'"></object>'."\n";
393393

394394
if ( count( $object->childrens ) > 0 )
395-
{
395+
{
396396
buildTree( $object->childrens, $metadata, ( $level ));
397397
$level--;
398398
insertTabs( $metadata, $level );
@@ -411,7 +411,7 @@ function insertTabs( &$metadata, $level )
411411

412412
function buildListOfFiles(&$array, &$metadata )
413413
{
414-
foreach( $array as $object )
414+
foreach( $array as $object )
415415
{
416416
$metadata .= $object->url."\n";
417417
if ( count( $object->childrens ) > 0 )
@@ -491,18 +491,18 @@ function deleteNode( $nodeName )
491491
}
492492

493493
function dirToArray(&$files, $dir, $ext)
494-
{
495-
$cdir = scandir($dir);
496-
foreach ($cdir as $key => $value)
497-
{
498-
if (!in_array($value,array(".","..")))
499-
{
500-
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
501-
{
494+
{
495+
$cdir = scandir($dir);
496+
foreach ($cdir as $key => $value)
497+
{
498+
if (!in_array($value,array(".","..")))
499+
{
500+
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
501+
{
502502
dirToArray($files, $dir . DIRECTORY_SEPARATOR . $value, $ext);
503-
}
504-
else
505-
{
503+
}
504+
else
505+
{
506506
if ( strpos( $value, $ext ) !== false )
507507
{
508508
if ( strpos( $value, "\\" ) === 0 )
@@ -511,11 +511,11 @@ function dirToArray(&$files, $dir, $ext)
511511
}
512512
else
513513
{
514-
$files[] = $dir."\\".$value;
514+
$files[] = $dir."\\".$value;
515515
}
516516
}
517-
}
518-
}
519-
}
520-
}
517+
}
518+
}
519+
}
520+
}
521521
?>

make_chm.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
for i in "$@"
3+
do
4+
case $i in
5+
PHP=*)
6+
PHP="${i#*=}"
7+
shift # past argument=value
8+
;;
9+
ICONV=*)
10+
ICONV="${i#*=}"
11+
shift # past argument=value
12+
;;
13+
HHC=*)
14+
HHC="${i#*=}"
15+
shift # past argument=value
16+
;;
17+
*)
18+
# unknown option
19+
;;
20+
esac
21+
done
22+
23+
PHP="${PHP:-$(which php)}"
24+
ICONV="${ICONV:-$(which iconv)}"
25+
HHC="${HHC:-$(which hhc)}"
26+
CPUS="$(cat /proc/cpuinfo | grep -c '^processor')"
27+
28+
# 在执行下一步之前,移动reference或output目录下文件到当前目录
29+
rm -rf chmhelp
30+
31+
# 防止loadHTMLFile乱码
32+
find -iname '*.html' | xargs -P "${CPUS}" sed -i 's/<head>/<head><meta charset="utf-8">/ig'
33+
34+
# 生成用于打包chm的文件
35+
"${PHP}" make_chm.php
36+
37+
# 设置ie兼容性,使样式正确显示
38+
cd chmhelp
39+
find -iname '*.html' | xargs -P "${CPUS}" sed -i 's/<head>/<head><meta http-equiv="x-ua-compatible" content="ie=edge">/ig'
40+
cd ..
41+
42+
mkdir -p chm_temp
43+
for i in cppreference.{hhc,hhk,hhp}; do
44+
sed -i '/chmhelp\\首页.html/d' "${i}"
45+
mv "${i}" "chm_temp/${i}"
46+
"${ICONV}" -c -f UTF-8 -t GB18030 "chm_temp/${i}" > "${i}"
47+
done
48+
49+
"${HHC}" cppreference.hhp

0 commit comments

Comments
 (0)