第一部分:模板修改
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编码输出动态页面,其他处理可参照上面静态生成站点方案中的代码。

免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。