您的位置:首页 > 编程语言

如何使LXR索引的代码在线阅读时语法高亮显示

2012-09-07 11:52 447 查看
LXR —— Linux Cross Refercence。Linux内核源码阅读和查询的利器之一,不用多介绍了。LXR安装后看到的源码是没有颜色的,用惯了语法高亮的编辑器,一下子看到满屏的黑白代码不免有点枯燥。于是给它装了个可以显示语法颜色的工具。

1、安装google-code-prettify

项目地址是:http://code.google.com/p/google-code-prettify/

$ wget http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css $ wget http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js 我把它们放到lxr的安装目录下面,lxr我放在web服务器的根目录,所以就

2、修改lxr/http/template-head

这是lxr默认的html头,在<head></head>标签之间加上

1
2

<link href="/lxr/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/lxr/prettify.js"></script>

注意文件路径,不在乎需要从远方服务器读取的话,也可以

1
2

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

这样也可以让google-code-prettify的维护者来自行维护这些脚本。

然后找到<body>标签:

1

<body bgcolor=white>

改为

1

<body bgcolor=white  onload='prettyPrint()'>

3、修改lxr/http/source

这是个perl脚本,查找字符串"<pre>",这有好几处,都在printfile这个子函数里面,改为

1

<pre class=\"prettyprint\">

已经好了,这样再看LXR里面的代码就可以看到漂亮的语法颜色了。效果图:



2009-05-01 Update:

上面的效果图有一个问题,就是代码的行号也被渲染高亮了。这个可以通过给行号标识加上"nocode"这个prettifier提供的class来解决。对lxr/http/lib/LXR/Common.pm进行如下修改即可:

1
23
4
5
6
7
8
9
10
1112
13

diff --git a/Common.pm b/Common.pm
index 9e23088..529d5a2 100755
--- a/Common.pm
+++ b/Common.pm
@@ -151,7 +151,7 @@ sub linetag {
$tag .= ' ' if $_[1] < 10;
$tag .= ' ' if $_[1] < 100;
$tag .= &fileref($_[1], $_[0], $_[1]).' ';
-    $tag =~ s/<a/<a name=L$_[1]/;
+    $tag =~ s/<a/<a class="nocode" name=L$_[1]/;
#    $_[1]++;
return($tag);
}

还可以在自己的css文件里面重写nocode这个class,例如加个方框啥的。

附注,上面使用的是较老版本的。发现老版本的那个解析语法的脚本prettify.js有点占资源,它会解析一大陀各语种的语法。如fs/aio.c等上千行的代码的话firefox会执行半天。需要专门针对C的语法优化一下,把其它不会用到的去掉。

新版本可以指定使用于什么语言,见下面的例子:

How do I specify the language of my code?

You don't need to specify the language since
prettyprint()
will guess. You can specify a language by specifying the language extension along with the
prettyprint
class like so:

<pre class="prettyprint lang-html">   The lang-* class specifies the language file extensions.   File extensions supported by default include     "bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html",     "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh",     "xhtml", "xml", "xsl". </pre>

You may also use the HTML 5 convention of embedding a code element inside the
PRE
and using
language-java
style classes. E.g.

<span class="tag"><pre</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"prettyprint"</span><span class="tag">><code</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span
class="atv">"language-java"</span><span class="tag">></span><span class="pln">...</span><span class="tag"></code></pre></span>

====
http://yp.oss.org.cn/software/show_resource.php?resource_id=1045
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: