2011年10月31日星期一

blogger的代码加亮

作为一个程序员,没有代码加亮的世界是多么苍白。在上一篇博客中,我利用vim的代码html转换功能来美化代码部分。这个办法实在太笨了,它花了我不少时间来编辑上一篇博客,以至于让我失去了写博客的兴趣。

最近,我无意中发现了一篇如何利用google-code-prettify来加亮代码的文章——Code highlighting on blogger。该文章并没有讲述不同语言的加亮方法,官方网站的README描述了相关解决办法。

我马上将其应用在前一篇博客中,然而默认效果并不令我满意。由于我长期使用vim的黑底系列的颜色方案,我需要一种类似的代码颜色方案来美化博客。

让人欣喜的是官方网站的themes gallery正好提供了我需要的颜色方案sunburst,这只需要简单将上文提及的
http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css
替换为
http://google-code-prettify.googlecode.com/svn/trunk/styles/sunburst.css
另外,我还专门为我的博客换了主题,每切换一次主题需要我重新将代码加亮的相关css和js重新手动加上。为此我用sed一个脚本来简化这个过程,具体内容如下:
#!/bin/sed -f

/<\/head>/i \
<link href='http://google-code-prettify.googlecode.com/svn/trunk/styles/sunburst.css' rel='stylesheet' type='text/css'/> \
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js' type='text/javascript'/>

/<body[[:blank:]]/ {
s/>/ onload='prettyPrint()'>/
}
具体的使用办法为:
  1. 在Design页面中,下载当前的template
  2. 利用上述脚本处理下载的template,从而生成新的template
  3. 在Design页面上传新的template

没有评论: