因?yàn)楣揪W(wǎng)絡(luò)慢娃兽,每次想來看thinkphp5.0的手冊的時(shí)候總是要等老半天绰沥。周末在家有空就用php扒了手冊的頁面做成chm
<?php
// 扒thinkphp5.0的手冊
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];
}
}
}
// 上一頁
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);
}
// 下一頁
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动壤、選擇剛才下載下來的文件夾
3、自己調(diào)整一下順序
4酥诽、點(diǎn)擊編譯保存就會(huì)生存一個(gè)CHM文件
(注意代碼下載下來的可能不是很完善鞍泉,如果有需要的可能自己打開html修改,或者在目錄下建立一個(gè)JS文件肮帐,代碼里有提到這個(gè)JS的路徑咖驮,用JS文件來統(tǒng)一修改代碼)