使用 TextMate 大大加快了创建博客文章的速度。我创建了一堆博客格式化命令来对帖子执行各种操作,包括添加段落标签、格式化标题和基于字符串模板生成内容。我最喜欢的一个是 PHP 函数,它使用 PRE 标记对(你的日常用语)代码进行 html 实体化。
PHP
function replace_angles($matches) {
return str_replace($matches[1],htmlentities($matches[1]),$matches[0]);
}
$file = file_get_contents('php://stdin');
$file = preg_replace_callback('/<pre.*?>(.*?)<\/pre>/imsu',replace_angles, $file);
echo $file;
第一步是读入缓冲区的内容。下一步是使用 PHP 提取 PRE 代码并将其替换为 htmlentities(code)。任务完成!我经常在我的博客中使用它——也许你也可以。
