第一部分:模板修改
1、js部分:刪除或使用現有組件替換
2、調用百度mip文件:
head里加<link rel="stylesheet" type="text/css"href="https://mipcache.bdstatic.com/static/mipmain-v1.1.1.css">
body里加<script src="https://mipcache.bdstatic.com/static/mipmain-v1.1.2.js"></script>
3、head里加<link rel="canonical"href="{dede:global.cfg_basehost/}{dede:field name='arcurl'/}" >,通過dedecms標簽直接調用當前頁url。
4、外部通用css文件:建議將css文件中的樣式代碼嵌入<style mip-custom>…</style>中,另存為模板文件(如css.htm),用{dede:includefilename="css.htm"/}替換相關模板中的<link rel="stylesheet" type="text/css"href="…" />。
模板中的內聯css可人工進行查找替換,合并至<stylemip-custom>中。(雖在下面代碼中可以自動進行處理,但從靜態文件生成性能角度考慮,還是建議人工先將模板中的內聯樣式一次性整改好。)
注:以上操作大多可通過批量查找替換來完成,看似需要修改很多,但實際工作量并不大。
第二部分:程序文件修改
· 靜態生成移動站:
找到 /include/dedetag.class.php文件中解析模板輸出為文件的函數:
function SaveTo($filename)
{
$fp = @fopen($filename,"w")or die("DedeTag Engine Create File False");
fwrite($fp,$this->GetResult());
fclose($fp);
}
替換為(部分代碼可根據實際情況進行改動):
//路徑轉換函數文件。$content:代碼源,$feed_url:首頁,$f_url:相對路徑的目錄部分
注:該方案初步測試成功,因生成靜態文件時處理程序增加,理論上來說會對生成效率有所影響。另外,不排除存在問題的可能性,如有問題或其他想法可回帖共同研究探討。
· 默認動態移動站:
1、修改/m目錄下index.php、list.php、view.php三個php文件的編碼,改為utf-8。
2、找到 /include/dedetag.class.php文件中解析模板直接輸出的函數:
function Display()
{
echo $this->GetResult();
}
替換為:
function Display()
{
$html=str_replace('<meta charset="gb2312">','<meta charset="utf-8">',$this->GetResult()); //轉換為utf-8編碼聲明,此處源內容$this->GetResult()不需要轉編碼
echo $html;
}
如此即可以utf-8編碼輸出動態頁面,其他處理可參照上面靜態生成站點方案中的代碼。
1、js部分:刪除或使用現有組件替換
2、調用百度mip文件:
head里加<link rel="stylesheet" type="text/css"href="https://mipcache.bdstatic.com/static/mipmain-v1.1.1.css">
body里加<script src="https://mipcache.bdstatic.com/static/mipmain-v1.1.2.js"></script>
3、head里加<link rel="canonical"href="{dede:global.cfg_basehost/}{dede:field name='arcurl'/}" >,通過dedecms標簽直接調用當前頁url。
4、外部通用css文件:建議將css文件中的樣式代碼嵌入<style mip-custom>…</style>中,另存為模板文件(如css.htm),用{dede:includefilename="css.htm"/}替換相關模板中的<link rel="stylesheet" type="text/css"href="…" />。
模板中的內聯css可人工進行查找替換,合并至<stylemip-custom>中。(雖在下面代碼中可以自動進行處理,但從靜態文件生成性能角度考慮,還是建議人工先將模板中的內聯樣式一次性整改好。)
注:以上操作大多可通過批量查找替換來完成,看似需要修改很多,但實際工作量并不大。
第二部分:程序文件修改
· 靜態生成移動站:
找到 /include/dedetag.class.php文件中解析模板輸出為文件的函數:
function SaveTo($filename)
{
$fp = @fopen($filename,"w")or die("DedeTag Engine Create File False");
fwrite($fp,$this->GetResult());
fclose($fp);
}
替換為(部分代碼可根據實際情況進行改動):
//路徑轉換函數文件。$content:代碼源,$feed_url:首頁,$f_url:相對路徑的目錄部分
function relative_to_absolute($content,$protocol, $domain, $f_url) { //根目錄相對路徑(如href="/a/b.html")轉換 $new_content =preg_replace('/hrefs*=s*(['"])s*//','href=1'.$protocol.$domain.'/', $content); $new_content =preg_replace('/srcs*=s*(['"])s*//', 'src=1'.$protocol.$domain.'/',$new_content); //當前頁相對路徑(如href="a/b.html")轉換 $new_content =preg_replace('/hrefs*=s*(['"])(?!(http|https)://)/','href=1'.$protocol.$domain.$f_url,$new_content); $new_content =preg_replace('/srcs*=s*(['"])(?!(http|https)://)/','src=1'.$protocol.$domain.$f_url, $new_content); return $new_content; } function SaveTo($filename) { $fp=@fopen($filename,"w") ordie("DedeTag Engine Create File False"); if(substr($_SERVER['PHP_SELF'],-6)=='_m.php'||substr($filename,-13)=='/m/index.html'){ //跳轉適配站識別是否為移動端生成,不影響pc端的gbk編碼。移動端為獨立站點需去掉此判斷條件。 $f_url=explode('www.域名.com/m',dirname($filename));//分割路徑,獲取當前頁相對路徑的目錄部分 //如dirname($filename)得到的本地絕對路徑為D:/wwwroot/www.域名.com/m/yygk/xwzx,用網站目錄"www.域名.com/m"作為標識分割路徑,得到目錄部分"/yygk/xwzx"。 $html=$this->GetResult(); $html=$this->relative_to_absolute($html,'http://','m.域名.com',$f_url[1].'/');//相對路徑轉換絕對路徑 $html=str_replace('<metacharset="gb2312">','<metacharset="utf-8">',iconv('gbk','utf-8//ignore',$html)); //轉換為utf-8編碼聲明,fwrite會以此生成對應編碼的靜態頁面 $html=str_replace('<a','<a target="_blank" ',$html); //<a>標簽加target $html=str_replace('<img','<mip-img ',$html); //替換<img>標簽 /* 主要針對編輯器生成的內聯樣式,將內聯樣式轉換到head的style標簽中 */ if(preg_match_all('/sstyles*=s*['"](.*?)['"]/',$html,$css)){ $css0=array_unique($css[0]);//過濾重復style foreach($css0as $k => $v){ $html=str_replace($v,'class="mip_add_css_'.$k.'"',$html); //mip_add_css_為自定義樣式名前綴,可自行修改,但需避免與原有樣式名重復 $temp_name='mip_add_css_'.$k; $$temp_name=$css[1][$k]; $add_css.='.'.$temp_name.'{'.$css[1][$k]."}n"; } $html=str_replace('<stylemip-custom>',"<style mip-custom>n".$add_css,$html); } fwrite($fp, $html); }else{ //pc端執行 fwrite($fp,$this->GetResult()); } fclose($fp); } |
注:該方案初步測試成功,因生成靜態文件時處理程序增加,理論上來說會對生成效率有所影響。另外,不排除存在問題的可能性,如有問題或其他想法可回帖共同研究探討。
· 默認動態移動站:
1、修改/m目錄下index.php、list.php、view.php三個php文件的編碼,改為utf-8。
2、找到 /include/dedetag.class.php文件中解析模板直接輸出的函數:
function Display()
{
echo $this->GetResult();
}
替換為:
function Display()
{
$html=str_replace('<meta charset="gb2312">','<meta charset="utf-8">',$this->GetResult()); //轉換為utf-8編碼聲明,此處源內容$this->GetResult()不需要轉編碼
echo $html;
}
如此即可以utf-8編碼輸出動態頁面,其他處理可參照上面靜態生成站點方案中的代碼。