制作thinkphp5.0的手册

因为公司网络慢,每次想来看thinkphp5.0的手册的时候总是要等老半天。周末在家有空就用php扒了手册的页面做成chm

savePath = $dir;
        $this->cssPath  = $this->savePath . 'css/';
        if( ! file_exists( $this->savePath ) ){
            mkdir($this->savePath, '0777');
        }
        $this->listPath = substr($link, 0, strrpos($link, '/') + 1);
    }
    // run
    public function run(){
        $link   = 'http://www.kancloud.cn/manual/thinkphp5/118003';
        $html   = $this->getCode($link);
        $this->saveCSS($html);

        $arr    = $this->getDirLink($html);
        list($all, $link, $pid, $title) = $arr;
        $len    = count($link) - 1;
        $path   = $this->savePath;
        foreach($link as $k => $v){
            if( $pid[$k] == 0 ){
                $path = $this->savePath. iconv('UTF-8', 'gb2312//ignore', $title[$k] );
                if(!file_exists($path)){
                    mkdir($path,'0777');
                }
            }
            $html = $this->getCode($this->listPath.$v);
            $html = $this->removeCode($html, $title[$k]);

            if( $k != 0 ){
                $key = $k-1;
                $prevPage = $pid[$k] == 0 ? ('../' . $this->getPreveTitle($title, $pid, $key)) : '.';
                $prevPage .= '/'.basename($link[$key]);
                $html = $this->setPrevPage($html, $prevPage, $title[$key]);
            }
            if( $k != $len ){
                $key = $k+1;
                $nextPage = $pid[$key] == 0 ? '../' . $title[$key] : '.';
                $nextPage .= '/'.basename($link[$key]);
                $html = $this->setNextPage($html, $nextPage, $title[$key]);
            }
            $filename = $path.'/'.basename($link[$k]).'.html';
            file_put_contents($filename, iconv('UTF-8', 'gb2312//ignore', $html));
        }
    }
    // 获取上一个pid为0的title
    private function getPreveTitle($title, $pid, $k){
        for (; $k > 0; $k--) { 
            if( $pid[$k] == 0 ){
                return $title[$k];
            }
        }
    }

    // 上一页
    private function setPrevPage($html, $prevPage, $title){
        return preg_replace('/([\s\S]*)<\/a>/U','\1'.$title.'', $html);
    }

    // 下一页
    private function setNextPage($html, $nextPage, $title){
        return preg_replace('/([\s\S]*)<\/a>/U','\1'.$title.'', $html);
    }

    // 删除与替换垃圾代码
    private function removeCode($html, $title){

        $replace = [
            // 替换标题
            '/.+<\/title>/'              => '<title>'.$title.'',
            '/UTF-8/'                           => 'gb2312',
            // 替换css路径
            '/ '[\s\S]*

在命令行下运行这段代码就会生成制作chm所需要的文件,随便打开里面的一个html文件用浏览器的调试工具改改样式保存。然后用Easy CHM

Paste_Image.png

1、点击新建
2、选择刚才下载下来的文件夹
3、自己调整一下顺序
4、点击编译保存就会生存一个CHM文件

(注意代码下载下来的可能不是很完善,如果有需要的可能自己打开html修改,或者在目录下建立一个JS文件,代码里有提到这个JS的路径,用JS文件来统一修改代码)

你可能感兴趣的:(制作thinkphp5.0的手册)