生成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,如需转载请自行联系原作者
“电子文档处理器”是一个文档批量处理、批量格式转换工具;help和chm帮助文件快速制作软件;电子书批量制作工具;批量chm反编译工具;内码转换、索引生成、批量改名软件。支持子目录及光盘直接操作,自动化程度高。欢迎使用“电子文档处理器”这一简单快捷的chm制作软件、help制作软件,“易上手”的特点让你自己动手轻松制作chm文件和help文件。 主要产品功能: 文本文件批量转换为帮助文件(txt2chm和Htm2chm:chm帮助文件制作软件,txt2help:help帮助文件制作工具),用户无须学习任何指令就可以快速地制作自己的电子书或chm帮助文件; 网页文件转为文本文件(htm2txt); 纯文本文件批量转换为网页文件,且自动生成上下文链接及目录(txt2html); 纯文本文件转换为微软网页帮助文件(htm2chmchm帮助文件制作工具); VB源文件转为网页文件(vb2html); 索引生成器(index maker); 繁简体互相转换(gb2big,big52gb); 文件改名器(file renamer)。 NAME:DIGERATI S/N:7265676E616D65 注册方法如下: 1、安装程序完成后,把注册文件 ews69181591fd96.crc 复制到“etextwizard”程序所在的目录下,注意必须在相同的目录下,请不要更改这个注册文件的文件名或文件内容。 注册成功的标志: 1、菜单上明显的“注册”项消失了; 2、“购买”按钮消失了,“关于”菜单下出现了“本软件注册给:"DIGERATI"的字样 注册文件 ews69181591fd96.crc 是采用特殊算法生成的,请千万不要修改注册文件里面的内容或文件名,这点非常重要!请务必按说明执行注册...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值