生成chm文档的方法

简介:

一个类和hhc.exe还有hha.dll
用于生成包含html目录的chm项目文件 ,然后通过hhp项目文件和内容文件生成 .chm手册。

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
<?php
     /* 函数 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  = <<<eof
[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
     <LI><OBJECT type= "text/sitemap" >
         <param name= "Name"  value= "{$title}" >
         <param name= "Local"  value= "{$value}" >
         </OBJECT>
eof;
                 } else {
                     $child  array ();
                     foreach  ( $value  as  $k  =>  $val ) {
                         $title  explode (DIRECTORY_SEPARATOR,  $val );
                         $title  array_pop ( $title );
                         $title  = rtrim( $title , '.html' );
                         $child [] = <<<eof
         <LI><OBJECT type= "text/sitemap" >
             <param name= "Name"  value= "{$title}" >
             <param name= "Local"  value= "{$val}" >
             <param name= "ImageNumber"  value= "9" >
             </OBJECT>
eof;
                     }
                     $child  = implode(PHP_EOL,  $child );
                     $list [] = <<<eof
     <LI> <OBJECT type= "text/sitemap" >
         <param name= "Name"  value= "{$key}" >
         <param name= "ImageNumber"  value= "1" >
         </OBJECT>
     <UL>   
{ $child }
     </UL>   
eof;
                 }
             }
             $list  = implode(PHP_EOL,  $list );
             $tpl  = <<<eof
<!DOCTYPE HTML PUBLIC  "-//IETF//DTD HTML//EN" >
<HTML>
<HEAD>
<meta name= "GENERATOR"  content= "yangweijie code-tech.diandian.com" >
<!-- Sitemap 1.0 -->
</HEAD><BODY>
<OBJECT type= "text/site properties" >
     <param name= "ExWindow Styles"  value= "0x200" >
     <param name= "Window Styles"  value= "0x800025" >
     <param name= "Font"  value= "MS Sans Serif,10,0" >
</OBJECT>
<UL>
{ $list }
</UL>
</BODY></HTML>
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
     <LI><OBJECT type= "text/sitemap" >
         <param name= "Name"  value= "{$title}" >
         <param name= "Local"  value= "{$value}" >
         </OBJECT>
eof;
                     }
                 }
             }
             $list  = implode(PHP_EOL,  $list );
             $tpl  = <<<eof
<!DOCTYPE HTML PUBLIC  "-//IETF//DTD HTML//EN" >
<HTML>
<HEAD>
<meta name= "GENERATOR"  content= "yangweijie code-tech.diandian.com" >
<!-- Sitemap 1.0 -->
</HEAD><BODY>
<UL>
{ $list }
</UL>
</BODY></HTML>
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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缩进好个是就行了,用手册里第一层目录和单文件名作为章节,里面的文件作为子章节










本文转自 3147972 51CTO博客,原文链接:http://blog.51cto.com/a3147972/1225251,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
JavaScript 前端开发
nodejs实现解析chm文件列表,无需转换为PDF文件格式,在线预览chm文件以及目录,不依赖任何网页端插件
nodejs实现解析chm文件列表,无需转换为PDF文件格式,在线预览chm文件以及目录,不依赖任何网页端插件
|
7月前
|
前端开发
前端实现在线预览文档(pdf、doc文件)
前端实现在线预览文档(pdf、doc文件)
|
C# Windows
C#编程学习15:应用程序帮助文档(chm/pdf)制作与C#调用
C#编程学习15:应用程序帮助文档(chm/pdf)制作与C#调用
C#编程学习15:应用程序帮助文档(chm/pdf)制作与C#调用
|
Python
Python 技术篇-利用pdfkit库实现html格式文件转换PDF文档实例演示
Python 技术篇-利用pdfkit库实现html格式文件转换PDF文档实例演示
295 0
Python 技术篇-利用pdfkit库实现html格式文件转换PDF文档实例演示
|
Python
Python 技术篇-利用Office VBA实现word文档转化为pdf文档实例演示
Python 技术篇-利用Office VBA实现word文档转化为pdf文档实例演示
313 0
Python 技术篇-利用Office VBA实现word文档转化为pdf文档实例演示
基于iTextSharp的PDF文档操作
  公司是跨境电商,需要和各种物流打交道,需要把东西交给物流,让他们发到世界各地。其中需要物流公司提供一个运单号,来追踪货物到达哪里?!   最近在和DHL物流公司(应该是个大公司)对接,取运单号的方式是调用对方提供的API,简单说,就是我们传一些发货地址和客户信息,要发的货物等,对方返回一个运单号和物流面单(就是我们淘宝快递上的面单)。
1550 0
|
Java 数据安全/隐私保护 Apache