LXR 搜索关键字高亮显示

前一段时间,使用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查看源码:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值