Rumah > php教程 > php手册 > 使用php在win下生成chm文档

使用php在win下生成chm文档

WBOY
Lepaskan: 2016-06-07 11:45:28
asal
1029 orang telah melayarinya

一个类和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元

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan