前一段时间,使用lxr 搭建了 android源码索引平台,具体搭建过程参考另一篇blog:LXR 索引Android 源码
平台在逐步完善中,界面简单可依赖,并加入了用户单点登录、用户权限设定、平台访问计数、搜索关键字高亮显示等功能。
来说说关键字在源码中高亮显示的具体实现,这部分的开发,修改了lxr 底层lib 库以及ident文件,思想是对在url 中加入关键字信息,在点击link 后,跳转到源码文件,可以在url中获得关键字,实现高亮,具体实现如下:
ident 文件的修改,有“# beaver add(update)的行”:
sub refexpand {
my $templ = shift;
my $ret = '';
my @refs = $index->symdeclarations($identifier, $releaseid);
my $file_hits = 0;
my $last_file;
my $def;
foreach my $def (@refs) {
# my ($file, $line, $type, $rel) = @$def;
my ($file, $line, $type, $rel, $i) = @$def; # beaver add
$i = $identifier; # beaver add
$file_hits++ if $file ne $last_file;
$last_file = $file;
$rel &&= "(member of " . idref($rel, "search-member", $rel) . ")";
$ret .= expandtemplate(
$templ,
(
file => sub { $file },
line => sub { $line },
type => sub { $type },
rel => sub { $rel },
[color=red]identifier => sub {$i},[/color]
fileref => sub {
fileref("$file, line $line", "search-decl", $file, $line, $i); # beaver update
}
)
);
# print("<span class=\"search-li1\"> $type_names{$type} in ".
# fileref("$file, line $line", "search-decl",
# $file, $line).
# " $rel</span>\n");
}
$declare_hits = "<div style='margin-left:30px'>" . scalar @refs . " declarations in $file_hits files.</div>";
return $ret;
}
sub usesexpand {
my $templ = shift;
my $ret = '';
my @uses = $index->symreferences($identifier, $releaseid);
my $file_hits = 0;
my $last_file;
foreach my $ref (sort { $$a[0] cmp $$b[0] } @uses) {
# my ($file, $line) = @$ref;
my ($file, $line ,$i) = @$ref; #beaver add
$i = $identifier; #beaver add
$file_hits++ if $file ne $last_file;
$last_file = $file;
$ret .= expandtemplate(
$templ,
(
file => sub { $file },
line => sub { $line },
[color=red]identifier => sub {$i}, # beaver add[/color]
fileref => sub {
fileref("$file, line $line", "search-ref", $file, $line, $i); # beaver update
}
)
);
}
$reference_hits = "<div style='margin-left:30px'>" . scalar @uses . " references in $file_hits files.</div>";
return $ret;
}
...
修改 lxr/lib/LXR/Commom.pm文件中fileref 函数:
sub fileref {
my ($desc, $css, $path, $line, $identifier, @args) = @_; # beaver update
# jwz: URL-quote any special characters.
$path =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
if ($line > 0 && length($line) < 3) {
$line = ('0' x (3 - length($line))) . $line;
}
return ("<a class='$css' href=\"$config->{virtroot}/source$path"
.(&urlargs(@args) ne '' ? &urlargs(@args)."&" : "?")
. "i=$identifier" # beaver add
. ($line > 0 ? "#$line" : "")
. "\"\>$desc</a>");
...
}
修改以上两个文件,实现搜索的关键字在源码中高亮显示,截图如下所示,分别为输入关键字-》显示搜索结果-》点击list查看源码: