Posts Tagged with syntax

代码高亮 - 续

上次说用vim来解决post里代码高亮的问题, 然后收到了来自发芽网推荐

写博客时,没有代码高亮?
发邮件时,代码格式很乱?
有好代码,保存后很难找?

来看下效果:

C++代码, 代码高亮@代码发芽网
1 #include <stdio.h>2 int main()3 {4     printf("Hello world!");5 6     return 0;7 }

发芽网还支持code snippet的存储和搜索, 也就是类似代码snippet仓库的功能, 而事实上代码高亮也似乎只是这个功能的副产品.
不过个人不太喜欢在html里嵌入style, 所以暂时还是用vim来搞定. 不在意这个又喜欢方便的, 发芽网的确是个不错的选择

借助vim高亮代码

起因还是这几天折腾vps, 为了最大限度节约资源, 决定停用之前的syntaxhighlighter插件, 而采用静态的高亮代码. 好处嘛:
一来省掉server端每次输出content的过滤过程
二来可以减少浏览器载入并执行js的压力
三来以后再换其他blog平台, 之前的代码片段依然可以很好的展现, 而不像现在. 检查了一下发现以前的post里残留着wp的coolcode的tag, textpattern的txp_batCode的tag, 还有habari的syntqxhighlighter的tag. 除了后者, 其他的都已经不像code了
找来找去发现解决方案其实一直都在我的硬盘里 -- 借助vim的syntax脚本高亮代码, 然后通过vim的2html.vim脚本生成html代码, 然后写css就ok. 至于好处嘛, 其一可以稍微减轻server和client的压力, 第二可以不依赖于任何blog平台和插件, 能输出html就成, 第三是可以借用vim的上百个语法脚本库, 这个数量绝对是geshi, syntaxhighlighter所望尘莫及的
步骤如下:
1. 编辑 ~/.vimrc , 添加以下代码, 当然css这个随便想怎么改怎么改:

syntax onlet html_use_css = 1let html_use_encoding = "utf8"let use_xhtml = 1

2. 添加css到现有的style里:

pre.code {    overflow: auto;margin-left: 1pt;padding: 5pt 10px;color: #000;background-color: #f8f8f8;border-left: 4pt solid #6ce26c;}pre.code .Constant {    color: #000;}pre.code .Comment {    color: #008200;}pre.code .Identifier {    color: #000000;}pre.code .Label {    color: #ff0;}pre.code .Operator {    color: #ff0;font-weight: bold;}pre.code .PreProc {    color: #ff1493;}pre.code .Special {    color: #008200;}pre.code .Statement {    color: #006699;}pre.code .Title {    color: #000000;font-weight: bold;}pre.code .Type {    color: #069;text-decoration: underline;}pre.code .Underlined {    color: #208aff;text-decoration: underline;}

3. 高亮代码, 最方便的是在vim新建文件的时候就指定对应语言的扩展名, 比如.php, .c等, 可以自动高亮代码. 如果没有指定, 可以在vim手动执行高亮脚本, 例如php:

:runtime syntax/php.vim

具体的.vim脚本名可参考syntax目录
4. 生成html, 执行如下命令

:runtime syntax/2html.vim

或者

:TOhtml

5. 把生成的html里pre包含的部分copy出来, 指定pre的class为code:

<pre class="code"><!-- CODE HERE --></pre>

-----------
稍微整理了一下之前有code的post, 所以被dimlau发现07年的post又"诡异"的出现在feed里...

SyntaxHighlighter for Habari

偶尔会在post里面贴点代码出来,尽管habari已经有了现成的GeShi插件,不过个人来讲更喜欢用dp.SyntaxHighlighter来实现,所以便有了这个东东

支持的语言:

  • C++ -- cpp, c, c++
  • C# -- c#, c-sharp, csharp
  • CSS -- css
  • Delphi -- delphi, pascal
  • Java -- java
  • JavaScript -- js, jscript, javascript
  • PHP -- php
  • Python -- py, python
  • Ruby -- rb, ruby, rails, ror
  • SQL -- sql
  • VB -- vb, vb.net
  • XML/HTML -- xml, html, xhtml, xslt

使用方法:

[ sourcecode lang='language' ]code[ /sourcecode ]

说明:

1. lang属性是必须的,值可以上上述语言的任意一个
2. 使用时记得删除 [ ] 还有 sourcecode 之间的空格。

下载 SyntaxHighlighter for Habari

--------------------- 华丽的分割线 ---------------------

SyntaxHighlighter for Habari is used to highlight source code in your post.

It supports the languages below:

  • C++ -- cpp, c, c++
  • C# -- c#, c-sharp, csharp
  • CSS -- css
  • Delphi -- delphi, pascal
  • Java -- java
  • JavaScript -- js, jscript, javascript
  • PHP -- php
  • Python -- py, python
  • Ruby -- rb, ruby, rails, ror
  • SQL -- sql
  • VB -- vb, vb.net
  • XML/HTML -- xml, html, xhtml, xslt

Usage:

Insert some bbcode like this:

[ sourcecode lang='language' ]code[ /sourcecode ]

And please pay attention to:

1. lang attribute is required, the value is listed above, py, python, etc.
2. REMEMBER that there is NO space in [,] and 'sourcecode'

Download SyntaxHighlighter for Habari

Demo:

[ sourcecode lang='c']

#include ;

int main () {
  printf ("Hello World!n");
}

[/sourcecode]

最终的显示结果是


DOWNLOAD SyntaxHighlighter for Habari