使用php在win下生成chm文档
一个类和hhc.exe还有hha.dll
用于生成包含html目录的chm项目文件 ,然后通过hhp项目文件和内容文件生成 .chm手册。<?php <br />
/* 函数 listDirTree( $dirName = null )<br>
** 功能 列出目录下所有文件及子目录<br>
** 参数 $dirName 目录名称<br>
** 返回 目录结构数组 false为失败<br>
*/<br>
<br>
function listDir($dirName = null) {<br>
if (empty($dirName))<br>
exit("IBFileSystem: directory is empty.");<br>
if (is_dir($dirName)) {<br>
if ($dh = opendir($dirName)) {<br>
$tree = array();<br>
while (( $file = readdir($dh) ) !== false) {<br>
if ($file != "." && $file != "..") {<br>
$filePath = $dirName . DIRECTORY_SEPARATOR . $file;<br>
if (is_dir($filePath)) { //为目录,递归<br>
$tree2 =listDir($filePath);<br>
$tree = $tree2? array_merge($tree,$tree2):$tree;<br>
} else { //为文件,添加到当前数组<br>
$tree[] = $filePath;<br>
}<br>
}<br>
}<br>
closedir($dh);<br>
} else {<br>
exit("IBFileSystem: can not open directory $dirName.");<br>
}<br>
<br>
//返回当前的$tree<br>
$tree = array_unique($tree);<br>
natsort($tree);<br>
return $tree;<br>
} else {<br>
exit("IBFileSystem: $dirName is not a directory.");<br>
}<br>
}<br>
<br>
function listDirTree($dirName = null,$remove) {<br>
if (empty($dirName))<br>
exit("IBFileSystem: directory is empty.");<br>
if (is_dir($dirName)) {<br>
if ($dh = opendir($dirName)) {<br>
$tree = array();<br>
while (( $file = readdir($dh) ) !== false) {<br>
if ($file != "." && $file != ".." && stripos($remove, $file) === false) {<br>
$filePath = $dirName . DIRECTORY_SEPARATOR . $file;<br>
if (is_dir($filePath)) { //为目录,递归<br>
$arr = listDirTree($filePath,$remove);<br>
natsort($arr);<br>
$tree[$file] = $arr;<br>
} else { //为文件,添加到当前数组<br>
$tree[] = $filePath;<br>
}<br>
}<br>
}<br>
closedir($dh);<br>
} else {<br>
exit("IBFileSystem: can not open directory $dirName.");<br>
}<br>
<br>
//返回当前的$tree<br>
return $tree;<br>
} else {<br>
exit("IBFileSystem: $dirName is not a directory.");<br>
}<br>
}<br>
<br>
function cmp($a,$b){<br>
$a = (int)$a;<br>
$b = (int)$b;<br>
if($a == $b) return 0;<br>
return ($a>$b)? 1:-1;<br>
}<br>
<br>
<br>
class chmBuilder{<br>
// const version = 0.1;<br>
public $chm_name;<br>
public $chm_path;<br>
public $chm_hhp;<br>
public $chm_hhc;<br>
public $chm_hhk;<br>
public $chm_uninclude_dirs;<br>
public $chm_uninclude_files;<br>
public $chm_image_type;<br>
public $chm_first_open;<br>
public $chm_title;<br>
<br>
public function __construct($chm_name='your_chm',$chm_path='',$chm_uninclude_dirs,$chm_uninclude_files){<br>
$this->chm_name = $chm_name;<br>
$this->chm_path = $chm_path;<br>
$this->chm_uninclude_dirs = $chm_uninclude_dirs;<br>
$this->chm_uninclude_files = $chm_uninclude_files;<br>
$this->chm_image_type = 'Folder';<br>
}<br>
<br>
public function build(){<br>
$this->buildHhp();<br>
$this->buildHhc();<br>
$this->buildHhk();<br>
}<br>
<br>
public function buildHhp(){<br>
$manual_files = listDir($this->chm_path);<br>
$files = implode(PHP_EOL, $manual_files);<br>
$this->chm_first_open = iconv('UTF-8', 'GB2312', $this->chm_first_open);<br>
$this->chm_title = iconv('UTF-8', 'GB2312', $this->chm_title);<br>
$tpl =
[OPTIONS]<br>
Compatibility=1.1 or later<br>
Compiled file={$this->chm_path}/{$this->chm_name}.chm<br>
Contents file={$this->chm_hhc}.hhc<br>
COPYRIGHT=www.thinkphp.cn<br>
Display compile progress=No<br>
Default topic={$this->chm_first_open}<br>
Error log file=chm_builder.Log<br>
Full-text search=Yes<br>
Index file={$this->chm_hhk}.hhk<br>
ImageType={$this->chm_image_type}<br>
Language=0x804<br>
Title={$this->chm_title}<br>
<br>
[FILES]<br>
{$files}<br>
eof;<br>
file_put_contents("{$this->chm_path}/{$this->chm_hhp}.hhp", $tpl);<br>
}<br>
<br>
public function buildHhc(){<br>
$list = array();<br>
$file_tree = listDirTree($this->chm_path,"{$this->chm_hhp} {$this->chm_uninclude_dirs}{$this->chm_uninclude_files}");<br>
uksort($file_tree, 'cmp');<br>
foreach ($file_tree as $key => $value) {<br>
if(is_string($value)){<br>
$title = explode(DIRECTORY_SEPARATOR, $value);<br>
$title = array_pop($title);<br>
$title = rtrim($title,'.html');<br>
$list[] =
<li>
<object><br>
<param>
<br>
<param>
<br>
</object><br>
eof;<br>
}else{<br>
$child = array();<br>
foreach ($value as $k => $val) {<br>
$title = explode(DIRECTORY_SEPARATOR, $val);<br>
$title = array_pop($title);<br>
$title = rtrim($title,'.html');<br>
$child[] =
</li>
<li>
<object><br>
<param>
<br>
<param>
<br>
<param>
<br>
</object><br>
eof;<br>
}<br>
$child = implode(PHP_EOL, $child);<br>
$list[] =
</li>
<li> <object><br>
<param>
<br>
<param>
<br>
</object><br>
<ul> <br>
{$child}<br>
</ul> <br>
eof;<br>
}<br>
}<br>
$list = implode(PHP_EOL, $list);<br>
$tpl =
nbsp;HTML PUBLIC "-//IETF//DTD HTML//EN"><br>
<br>
<br>
<meta>
<br>
<!-- Sitemap 1.0 --><br>
<br>
<object><br>
<param>
<br>
<param>
<br>
<param>
<br>
</object><br>
<ul>
<br>
{$list}<br>
</ul>
<br>
<br>
eof;<br>
file_put_contents("{$this->chm_path}/{$this->chm_hhc}.hhc", $tpl);<br>
}<br>
<br>
public function buildHhk(){<br>
$list = array();<br>
$file_tree = listDir($this->chm_path);<br>
foreach ($file_tree as $key => $value) {<br>
if(is_string($value)){<br>
if(stripos($value, '.html')){<br>
$title = explode(DIRECTORY_SEPARATOR, $value);<br>
$title = array_pop($title);<br>
$title = rtrim($title,'.html');<br>
$list[] =
</li>
<li>
<object><br>
<param>
<br>
<param>
<br>
</object><br>
eof;<br>
}<br>
}<br>
}<br>
$list = implode(PHP_EOL, $list);<br>
$tpl =
nbsp;HTML PUBLIC "-//IETF//DTD HTML//EN"><br>
<br>
<br>
<meta>
<br>
<!-- Sitemap 1.0 --><br>
<br>
<ul>
<br>
{$list}<br>
</ul>
<br>
<br>
eof;<br>
file_put_contents("{$this->chm_path}/{$this->chm_hhk}.hhk", $tpl);<br>
}<br>
<br>
public function makeChm(){<br>
if(!is_file("{$this->chm_path}/{$this->chm_hhp}.hhp")) <br>
return "build error:can't generate *.hhp file!";<br>
$command = "hhc {$this->chm_path}/{$this->chm_hhp}.hhp";<br>
system($command);<br>
if(file_exists("{$this->chm_path}/{$this->chm_name}.chm"))<br>
return true;<br>
else<br>
return 'generate chm failed!';<br>
}<br>
}<br>
<br>
?></li>
使用方法,放到要生成目录的外面 定义好路径,手册名,不包含目录,不包含文件 字符串(空格分割),设置好一些属性后, 将hhc.exe的位置加入环境变量path中,cmd 里调用 执行的index.php 可以看到生成的信息,或者错误
index.php<?php <br />
header('Content-type:text/plain;charset=utf-8');<br>
error_reporting(E_ERROR);<br>
ini_set('memory_limit', '30M');<br>
include 'chm_builder.php';<br>
$chm = new chmBuilder('ThinkPHP manual',__DIR__.DIRECTORY_SEPARATOR.'manual','public ','.DS_Store Thumbs.db book.tpl');<br>
$chm->chm_hhp = 'index';<br>
$chm->chm_hhc = 'index';<br>
$chm->chm_first_open = $chm->chm_path.DIRECTORY_SEPARATOR.'序言.html';<br>
$chm->chm_hhk = 'index';<br>
$chm->chm_title = 'ThinkPHP 3.1.2官方手册';<br>
$chm->build();<br>
//$chm->makeChm();<br>
?>
这个可以配合ThinkPHP Sublime 插件来生成手册,目前排序方面有点问题,故没集成到插件里去。目前只支持二级分类,多级的大家递归时tab缩进好个是就行了,用手册里第一层目录和单文件名作为章节,里面的文件作为子章节
chm_buider.zip
( 473.81 KB 下载:281 次 )
AD:真正免费,域名+虚机+企业邮箱=0元

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

《Go語言程式設計實例:Web開發中的程式碼範例》隨著網路的快速發展,Web開發已成為各行業中不可或缺的一部分。作為一門功能強大且效能優越的程式語言,Go語言在Web開發中越來越受到開發者們的青睞。本文將透過具體的程式碼範例,介紹如何利用Go語言進行Web開發,讓讀者更能理解並運用Go語言來建立自己的Web應用。 1.簡單的HTTP伺服器首先,讓我們從一個

了解Python程式設計的入門程式碼範例Python是一種簡單易學,功能強大的程式語言。對於初學者來說,了解Python程式設計的入門級程式碼範例是非常重要的。本文將為您提供一些具體的程式碼範例,幫助您快速入門。列印HelloWorldprint("HelloWorld")這是Python中最簡單的程式碼範例。 print()函數用於將指定的內容輸出

Java冒泡排序最簡單的程式碼範例冒泡排序是一種常見的排序演算法,它的基本想法是透過相鄰元素的比較和交換來將待排序序列逐步調整為有序序列。以下是一個簡單的Java程式碼範例,示範如何實作冒泡排序:publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

PHP變數儲存程式運行期間的值,對於建立動態且互動的WEB應用程式至關重要。本文將深入探討php變量,並透過10個真實的範例展示它們的實際應用。 1.儲存使用者輸入$username=$_POST["username"];$passWord=$_POST["password"];此範例從表單提交中提取使用者名稱和密碼,並將其儲存在變數中以供進一步處理。 2.設定配置值$database_host="localhost";$database_username="username";$database_pa

華為雲端邊緣運算對接指南:Java程式碼範例快速實現介面隨著物聯網技術的快速發展和邊緣運算的興起,越來越多的企業開始關注邊緣運算的應用。華為雲端提供了邊緣運算服務,為企業提供了高可靠的運算資源和便利的開發環境,使得邊緣運算應用更容易實現。本文將介紹如何透過Java程式碼快速實現華為雲端邊緣運算的介面。首先,我們需要準備好開發環境。確保你已經安裝了Java開發工具包(

如何使用PHP編寫庫存管理系統中的庫存分倉管理功能碼庫存管理是許多企業中不可或缺的一部分。對於擁有多個倉庫的企業來說,庫存分倉管理功能尤其重要。透過合理管理和追蹤庫存,企業可以實現不同倉庫之間的庫存調撥,優化營運成本,改善協同效率。本文將介紹如何使用PHP編寫庫存分倉管理功能的程式碼,並為您提供相關的程式碼範例。一、建立資料庫在開始編寫庫存分倉管理功能的程式碼之

標題:從入門到精通:Go語言中常用資料結構的程式碼實作資料結構在程式設計中起著至關重要的作用,它是程式設計的基礎。在Go語言中,有許多常用的資料結構,掌握這些資料結構的實作方式對於成為優秀的程式設計師至關重要。本文將介紹Go語言中常用的資料結構,並給出對應的程式碼範例,幫助讀者從入門到精通這些資料結構。 1.數組(Array)數組是一種基本的資料結構,是一組相同類型

Java選擇排序法程式碼編寫指南及範例選擇排序是一種簡單直觀的排序演算法,其想法是每次從未排序的元素中選擇最小(或最大)的元素進行交換,直到所有元素排序完成。本文將提供選擇排序的程式碼編寫指南,並附上具體的Java範例程式碼。演算法原理選擇排序的基本原理是將待排序數組分為已排序和未排序兩部分,每次從未排序部分選擇最小(或最大)的元素,將其放到已排序部分的末尾。重複上述
