找回typecho的段前空格——转

 自从把 typecho 升级到0.8版后就一直纠结于一个不大不小的问题--吃空格,程序会把除了第一自然段以外的其它自然段前面的空格给吃掉,给排版照常困难。对于我这样有洁癖的人来说是不可容忍的。无图无真相。

  自从把 typecho 升级到0.8版后就一直纠结于一个不大不小的问题--吃空格,程序会把除了第一自然段以外的其它自然段前面的空格给吃掉,给排版照常困难。对于我这样有洁癖的人来说是不可容忍的。无图无真相,看图。
没有空格
  修改方法:找到typecho\var\Typecho\Common里的Paragraph.php这个文件,把private static function cutByBlock($text)
    {
        $space = "( | )";
        $text = str_replace("\r\n", "\n", trim($text));
        $text = preg_replace("/{$space}*\n{$space}*/is", "\n", $text);
        $text = preg_replace("/\n{2,}/", "</p><p>", $text);
        $text = nl2br($text);
        $text = preg_replace("/(<p>)?\s*<p:([0-9]{4})\/>\s*(<\/p>)?/s", "<p:\\2/>", $text);
        $text = preg_replace("/<p>{$space}*<\/p>/is", '', $text);
        return $text;
    }
替换为private static function cutByBlock($text)
    {
        $space = "( | )";
        $text = str_replace("\r\n", "<br />", trim($text));
        $text = str_replace("\n", "<br />", trim($text));
        $text = preg_replace("/{$space}*\n{$space}*/is", "\n", $text);
        $text = preg_replace("/\n{2,}/", "</p><p>", $text);
        $text = nl2br($text);
        $text = preg_replace("/(<p>)?\s*<p:([0-9]{4})\/>\s*(<\/p>)?/s", "<p:\\2/>", $text);
        $text = preg_replace("/<p>{$space}*<\/p>/is", '', $text);
        return $text;
    }

  修改完后的效果,看图
有空格
  这个问题产生的原因在于\r\n这两个的含义。\r是回车,\n是换行。\r\n才是回车换行。所以\r\n\n这两个应该要分别处理,而不能只处理\n

相关文章

发表新评论