[PATCHv2 5/8] gitweb: Highlight matched part of project name when searching projects
- From
Jakub Narebski <jnareb@gmail.com>
- Date
- Feb 15, 2012, 20:38 UTC
- Message-ID
- <1329338332-30358-6-git-send-email-jnareb@gmail.com>
- In-Reply-To
- <1329338332-30358-1-git-send-email-jnareb@gmail.com>
Use newly introduced esc_html_match_hl() to escape HTML and mark match with span element with 'match' class. Currently only 'path' part (i.e. project name) is highlighted; match might be on the project description.
The code makes use of the fact that defined $search_regexp means that there was search going on.
Signed-off-by: Jakub Narebski <jnareb@gmail.com> --- Introducing esc_html_match_hl() could have been put together with introduction of esc_html_hl_regions() in previos commit.
Changes from v1: * Main part of esc_html_match_hl() got split into esc_html_hl_regions(), which was introduced in previous commit.
gitweb/gitweb.perl | 18 +++++++++++++++++- 1 files changed, 17 insertions(+), 1 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 8dcd54b..5596701 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -1742,6 +1742,20 @@ sub esc_html_hl_regions { return $out; } +# highlight match (if any), and escape HTML +sub esc_html_match_hl { + my ($str, $regexp) = @_; + return esc_html($str) unless defined $regexp; + + my @matches; + while ($str =~ /$regexp/g) { + push @matches, [$-[0], $+[0]]; + } + return esc_html($str) unless @matches; + + return esc_html_hl_regions($str, 'match', @matches); +} + ## ---------------------------------------------------------------------- ## functions returning short strings @@ -5389,7 +5403,9 @@ sub git_project_list_rows { print "</td>\n"; } print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"), - -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" . + -class => "list"}, + esc_html_match_hl($pr->{'path'}, $search_regexp)) . + "</td>\n" . "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"), -class => "list", -title => $pr->{'descr_long'}}, esc_html($pr->{'descr'})) . "</td>\n" .
-- 1.7.9