制作thinkphp5.0的手冊(cè)

因?yàn)楣揪W(wǎng)絡(luò)慢,每次想來(lái)看thinkphp5.0的手冊(cè)的時(shí)候總是要等老半天。周末在家有空就用php扒了手冊(cè)的頁(yè)面做成chm

<?php 

// 扒thinkphp5.0的手冊(cè)

class thinkphp
{
    // 保存目錄
    private $savePath = '';
    // 遠(yuǎn)程連接路徑
    private $listPath = '';
    // css保存路徑
    private $cssPath  = '';
    // 初始化  
      function __construct($dir, $link){
        $this->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));
        }
    }
    // 獲取上一個(gè)pid為0的title
    private function getPreveTitle($title, $pid, $k){
        for (; $k > 0; $k--) { 
            if( $pid[$k] == 0 ){
                return $title[$k];
            }
        }
    }

    // 上一頁(yè)
    private function setPrevPage($html, $prevPage, $title){
        return preg_replace('/<span class="jump-up">([\s\S]*)<a><\/a>/U','<span class="jump-up">\1<a href="'.$prevPage.'.html">'.$title.'</a>', $html);
    }

    // 下一頁(yè)
    private function setNextPage($html, $nextPage, $title){
        return preg_replace('/<span class="jump-down">([\s\S]*)<a><\/a>/U','<span class="jump-down">\1<a href="'.$nextPage.'.html">'.$title.'</a>', $html);
    }

    // 刪除與替換垃圾代碼
    private function removeCode($html, $title){

        $replace = [
            // 替換標(biāo)題
            '/<title>.+<\/title>/'              => '<title>'.$title.'</title>',
            '/UTF-8/'                           => 'gb2312',
            // 替換css路徑
            '/<link rel="stylesheet" href="\/\/static\.kancloud\.cn\/Static\/.*\/.*\/(.+)\.css\?v=\d+"/U' => '<link rel="stylesheet" href="../css/\1.css"',
            // 刪除導(dǎo)航
            '/<div class="manual-head">[\s\S]*<div class="manual-right">/'                      => '<div class="manual-body"><div class="manual-right">',
            // 刪除字體分享
            '/<div class="head\-tool">[\s\S]*<h1><\/h1>/U'                                      => '<h1>'.$title.'</h1>',
            // 刪除低版本瀏覽器提示
            '/<\!\-\-\[if lte IE 8\]>[\s\S]*<\!\[endif\]\-\->/U'                                => '',
            // 刪除JS代碼替換為自己的JS方便統(tǒng)一管理代碼
            '/<div class="think\-loading loading\-gear loading\-active">[\s\S]*<\/script>/'     => '<script src="./js/common.js"></script>',
            // 替換站內(nèi)連接
            '/<a href="(\d+)">/'                                                                => '<a href="./\1.html">',
        ];
        $html = preg_replace(array_keys($replace), array_values($replace), $html);
        return $html;
    }

    // 保存樣式
    private function saveCSS($html){
        preg_match_all('/<link rel="stylesheet" href="(.+)\?v=\d+">/U', $html, $match);
        if( isset($match[1]) ){
            if(!file_exists($this->cssPath)){
                mkdir($this->cssPath, '0777');
            }
            foreach($match[1] as $v){
                $css = $this->getCode('http:'.$v);
                file_put_contents($this->cssPath . basename($v), file_get_contents('http:'.$v));
            }
        }
    }

    // 獲取目錄連接
    private function getDirLink($html){
        preg_match_all('/<a href="\/manual\/thinkphp5\/(\d+)" data-pid="(\d+)" data-disable="0" data-id="\1">(.+)<\/a>/U', $html, $match);
        return $match;
    }

    // curl獲取遠(yuǎn)程代碼
    private function getCode($link){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $link);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, ' Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36');
        $html = curl_exec($ch);
        curl_close($ch);
        return $html;
    }
}

$class = new thinkphp('./chm/', 'http://www.kancloud.cn/manual/thinkphp5/118003');
$class->run();

?>

在命令行下運(yùn)行這段代碼就會(huì)生成制作chm所需要的文件,隨便打開里面的一個(gè)html文件用瀏覽器的調(diào)試工具改改樣式保存。然后用Easy CHM

Paste_Image.png

1、點(diǎn)擊新建
2、選擇剛才下載下來(lái)的文件夾
3、自己調(diào)整一下順序
4、點(diǎn)擊編譯保存就會(huì)生存一個(gè)CHM文件

(注意代碼下載下來(lái)的可能不是很完善,如果有需要的可能自己打開html修改,或者在目錄下建立一個(gè)JS文件,代碼里有提到這個(gè)JS的路徑,用JS文件來(lái)統(tǒng)一修改代碼)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容