python chm制作_生成chm文档的方法
一個類和hhc.exe還有hha.dll
用于生成包含html目錄的chm項目文件 ,然后通過hhp項目文件和內容文件生成 .chm手冊。
/*?函數?listDirTree(?$dirName?=?null?)
**?功能?列出目錄下所有文件及子目錄
**?參數?$dirName?目錄名稱
**?返回?目錄結構數組?false為失敗
*/
function?listDir($dirName?=?null)?{
if?(empty($dirName))
exit("IBFileSystem:?directory?is?empty.");
if?(is_dir($dirName))?{
if?($dh?=?opendir($dirName))?{
$tree?=?array();
while?((?$file?=?readdir($dh)?)?!==?false)?{
if?($file?!=?"."?&&?$file?!=?"..")?{
$filePath?=?$dirName?.?DIRECTORY_SEPARATOR?.?$file;
if?(is_dir($filePath))?{?//為目錄,遞歸
$tree2?=listDir($filePath);
$tree?=?$tree2??array_merge($tree,$tree2):$tree;
}?else?{?//為文件,添加到當前數組
$tree[]?=?$filePath;
}
}
}
closedir($dh);
}?else?{
exit("IBFileSystem:?can?not?open?directory?$dirName.");
}
//返回當前的$tree
$tree?=?array_unique($tree);
natsort($tree);
return?$tree;
}?else?{
exit("IBFileSystem:?$dirName?is?not?a?directory.");
}
}
function?listDirTree($dirName?=?null,$remove)?{
if?(empty($dirName))
exit("IBFileSystem:?directory?is?empty.");
if?(is_dir($dirName))?{
if?($dh?=?opendir($dirName))?{
$tree?=?array();
while?((?$file?=?readdir($dh)?)?!==?false)?{
if?($file?!=?"."?&&?$file?!=?".."?&&?stripos($remove,?$file)?===?false)?{
$filePath?=?$dirName?.?DIRECTORY_SEPARATOR?.?$file;
if?(is_dir($filePath))?{?//為目錄,遞歸
$arr?=?listDirTree($filePath,$remove);
natsort($arr);
$tree[$file]?=?$arr;
}?else?{?//為文件,添加到當前數組
$tree[]?=?$filePath;
}
}
}
closedir($dh);
}?else?{
exit("IBFileSystem:?can?not?open?directory?$dirName.");
}
//返回當前的$tree
return?$tree;
}?else?{
exit("IBFileSystem:?$dirName?is?not?a?directory.");
}
}
function?cmp($a,$b){
$a?=?(int)$a;
$b?=?(int)$b;
if($a?==?$b)????return?0;
return?($a>$b)??1:-1;
}
class?chmBuilder{
//?const?version?=?0.1;
public?$chm_name;
public?$chm_path;
public?$chm_hhp;
public?$chm_hhc;
public?$chm_hhk;
public?$chm_uninclude_dirs;
public?$chm_uninclude_files;
public?$chm_image_type;
public?$chm_first_open;
public?$chm_title;
public?function?__construct($chm_name='your_chm',$chm_path='',$chm_uninclude_dirs,$chm_uninclude_files){
$this->chm_name?=?$chm_name;
$this->chm_path?=?$chm_path;
$this->chm_uninclude_dirs?=?$chm_uninclude_dirs;
$this->chm_uninclude_files?=?$chm_uninclude_files;
$this->chm_image_type?=?'Folder';
}
public?function?build(){
$this->buildHhp();
$this->buildHhc();
$this->buildHhk();
}
public?function?buildHhp(){
$manual_files?=?listDir($this->chm_path);
$files?=?implode(PHP_EOL,?$manual_files);
$this->chm_first_open?=?iconv('UTF-8',?'GB2312',?$this->chm_first_open);
$this->chm_title?=?iconv('UTF-8',?'GB2312',?$this->chm_title);
$tpl?=?<<
[OPTIONS]
Compatibility=1.1?or?later
Compiled?file={$this->chm_path}/{$this->chm_name}.chm
Contents?file={$this->chm_hhc}.hhc
COPYRIGHT=www.thinkphp.cn
Display?compile?progress=No
Default?topic={$this->chm_first_open}
Error?log?file=chm_builder.Log
Full-text?search=Yes
Index?file={$this->chm_hhk}.hhk
ImageType={$this->chm_image_type}
Language=0x804
Title={$this->chm_title}
[FILES]
{$files}
eof;
file_put_contents("{$this->chm_path}/{$this->chm_hhp}.hhp",?$tpl);
}
public?function?buildHhc(){
$list?=?array();
$file_tree?=?listDirTree($this->chm_path,"{$this->chm_hhp}?{$this->chm_uninclude_dirs}{$this->chm_uninclude_files}");
uksort($file_tree,?'cmp');
foreach?($file_tree?as?$key?=>?$value)?{
if(is_string($value)){
$title?=?explode(DIRECTORY_SEPARATOR,?$value);
$title?=?array_pop($title);
$title?=?rtrim($title,'.html');
$list[]?=?<<
eof;
}else{
$child?=?array();
foreach?($value?as?$k?=>?$val)?{
$title?=?explode(DIRECTORY_SEPARATOR,?$val);
$title?=?array_pop($title);
$title?=?rtrim($title,'.html');
$child[]?=?<<
eof;
}
$child?=?implode(PHP_EOL,?$child);
$list[]?=?<<?
{$child}
eof;
}
}
$list?=?implode(PHP_EOL,?$list);
$tpl?=?<<
HTML?PUBLIC?"-//IETF//DTD?HTML//EN">
{$list}
eof;
file_put_contents("{$this->chm_path}/{$this->chm_hhc}.hhc",?$tpl);
}
public?function?buildHhk(){
$list?=?array();
$file_tree?=?listDir($this->chm_path);
foreach?($file_tree?as?$key?=>?$value)?{
if(is_string($value)){
if(stripos($value,?'.html')){
$title?=?explode(DIRECTORY_SEPARATOR,?$value);
$title?=?array_pop($title);
$title?=?rtrim($title,'.html');
$list[]?=?<<
eof;
}
}
}
$list?=?implode(PHP_EOL,?$list);
$tpl?=?<<
HTML?PUBLIC?"-//IETF//DTD?HTML//EN">
{$list}
eof;
file_put_contents("{$this->chm_path}/{$this->chm_hhk}.hhk",?$tpl);
}
public?function?makeChm(){
if(!is_file("{$this->chm_path}/{$this->chm_hhp}.hhp"))
return?"build?error:can't?generate?*.hhp?file!";
$command?=?"hhc?{$this->chm_path}/{$this->chm_hhp}.hhp";
system($command);
if(file_exists("{$this->chm_path}/{$this->chm_name}.chm"))
return?true;
else
return?'generate?chm?failed!';
}
}
?>
使用方法,放到要生成目錄的外面 定義好路徑,手冊名,不包含目錄,不包含文件 字符串(空格分割),設置好一些屬性后, 將hhc.exe的位置加入環境變量path中,cmd 里調用 執行的index.php 可以看到生成的信息,或者錯誤
index.php
header('Content-type:text/plain;charset=utf-8');
error_reporting(E_ERROR);
ini_set('memory_limit',?'30M');
include?'chm_builder.php';
$chm?=?new?chmBuilder('ThinkPHP?manual',__DIR__.DIRECTORY_SEPARATOR.'manual','public?','.DS_Store?Thumbs.db?book.tpl');
$chm->chm_hhp?=?'index';
$chm->chm_hhc?=?'index';
$chm->chm_first_open?=?$chm->chm_path.DIRECTORY_SEPARATOR.'序言.html';
$chm->chm_hhk?=?'index';
$chm->chm_title?=?'ThinkPHP?3.1.2官方手冊';
$chm->build();
//$chm->makeChm();
?>
這個可以配合ThinkPHP Sublime 插件來生成手冊,目前排序方面有點問題,故沒集成到插件里去。目前只支持二級分類,多級的大家遞歸時tab縮進好個是就行了,用手冊里第一層目錄和單文件名作為章節,里面的文件作為子章節
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python chm制作_生成chm文档的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大连有什么特色烧烤?
- 下一篇: 新疆有哪些好的食物?