如何在PHP中生成chm文档 - 面向对象网 php 学习 对象 - 面向对象技术开发

面向对象技术开发

会员投稿 投稿指南 站长资讯通告:
您的位置: 首页 > OOP语言 > PHP > 正文

如何在PHP中生成chm文档

来源: www.bianceng.cn 阅读:

一个类和hhc.exe还有hha.dll

用于生成包含html目录的chm项目文件 ,然后通过hhp项目文件和内容文件生成 .chm手册。

<?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!';
        }
    }
?>

Tags:
相关文章列表: