git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv2 8/8] gitweb: Use esc_html_match_hl() in 'grep' search

From
Jakub Narebski <jnareb@gmail.com>
Date
Feb 15, 2012, 20:38 UTC
Message-ID
<1329338332-30358-9-git-send-email-jnareb@gmail.com>
In-Reply-To
<1329338332-30358-1-git-send-email-jnareb@gmail.com>

Use esc_html_match_hl() in git_search_files() i.e. subroutine that implements "grep" search, instead of custom code which highlighted only one, last match.

This required enhancing esc_html_match_hl() to accept -nbsp=>1 and pass it down to esc_html().

Note that line is untabified (tabs turned into spaces) before highlighting match, which means that highlighting won't work e.g. for matching tab character "\t" explicitly; but this issue was present before this commit, and is not that easy to fix.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
This patch was not present in previous (v1) version of this patch
series.
The 'grep' search was chosen from other searches because of the
following reasons:
* 'pickaxe' search does not show matches in diff, only filenames.
* 'commit' search shortens leading text at beginning, one last match
  in the middle, and trailing text at the end; anyway I think this
  search should be rewritten to show just "log"-like view with match
  highlighting.
 gitweb/gitweb.perl |   34 ++++++++++++++--------------------
 1 files changed, 14 insertions(+), 20 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index a2e2023..36a8cca 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1723,20 +1723,22 @@ sub chop_and_escape_str {
 # '<span class="mark">foo</span>bar'
 sub esc_html_hl_regions {
 	my ($str, $css_class, @sel) = @_;
-	return esc_html($str) unless @sel;
+	my %opts = grep { ref($_) ne 'ARRAY' } @sel;
+	@sel     = grep { ref($_) eq 'ARRAY' } @sel;
+	return esc_html($str, %opts) unless @sel;
 
 	my $out = '';
 	my $pos = 0;
 
 	for my $s (@sel) {
-		$out .= esc_html(substr($str, $pos, $s->[0] - $pos))
+		$out .= esc_html(substr($str, $pos, $s->[0] - $pos), %opts)
 			if ($s->[0] - $pos > 0);
 		$out .= $cgi->span({-class => $css_class},
-		                   esc_html(substr($str, $s->[0], $s->[1] - $s->[0])));
+		                   esc_html(substr($str, $s->[0], $s->[1] - $s->[0]), %opts));
 
 		$pos = $s->[1];
 	}
-	$out .= esc_html(substr($str, $pos))
+	$out .= esc_html(substr($str, $pos), %opts)
 		if ($pos < length($str));
 
 	return $out;
@@ -1744,23 +1746,23 @@ sub esc_html_hl_regions {
 
 # highlight match (if any), and escape HTML
 sub esc_html_match_hl {
-	my ($str, $regexp) = @_;
-	return esc_html($str) unless defined $regexp;
+	my ($str, $regexp, %opts) = @_;
+	return esc_html($str, %opts) unless defined $regexp;
 
-	return esc_html_match_hl_chopped($str, undef, $regexp);
+	return esc_html_match_hl_chopped($str, undef, $regexp, %opts);
 }
 
 
 # highlight match (if any) of shortened string, and escape HTML
 sub esc_html_match_hl_chopped {
-	my ($str, $chopped, $regexp) = @_;
-	return esc_html(defined $chopped ? $chopped : $str) unless defined $regexp;
+	my ($str, $chopped, $regexp, %opts) = @_;
+	return esc_html(defined $chopped ? $chopped : $str, %opts) unless defined $regexp;
 
 	my @matches;
 	while ($str =~ /$regexp/g) {
 		push @matches, [$-[0], $+[0]];
 	}
-	return esc_html(defined $chopped ? $chopped : $str) unless @matches;
+	return esc_html(defined $chopped ? $chopped : $str, %opts) unless @matches;
 
 	# filter matches so that we mark chopped string, if it is present
 	if (defined $chopped) {
@@ -1788,7 +1790,7 @@ sub esc_html_match_hl_chopped {
 		@matches = @filtered;
 	}
 
-	return esc_html_hl_regions($str, 'match', @matches);
+	return esc_html_hl_regions($str, 'match', @matches, %opts);
 }
 
 ## ----------------------------------------------------------------------
@@ -6070,15 +6072,7 @@ sub git_search_files {
 			print "<div class=\"binary\">Binary file</div>\n";
 		} else {
 			$ltext = untabify($ltext);
-			if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
-				$ltext = esc_html($1, -nbsp=>1);
-				$ltext .= '<span class="match">';
-				$ltext .= esc_html($2, -nbsp=>1);
-				$ltext .= '</span>';
-				$ltext .= esc_html($3, -nbsp=>1);
-			} else {
-				$ltext = esc_html($ltext, -nbsp=>1);
-			}
+			$ltext = esc_html_match_hl($ltext, qr/$search_regexp/i, -nbsp=>1);
 			print "<div class=\"pre\">" .
 				$cgi->a({-href => $file_href.'#l'.$lno,
 				        -class => "linenr"}, sprintf('%4i', $lno)) .
-- 
1.7.9
Previous: Jakub NarebskiNext: Junio C Hamano
Message 12 of 16 in “[PATCHv2 0/8] gitweb: Faster and improved project search”
  1. Jakub NarebskiFeb 15, 2012
  2. 1/8 gitweb: Refactor checking if part of project info need fillingJakub Narebski, Feb 15, 2012
  3. 2/8 gitweb: Option for filling only specified info in fill_project_list_infoJakub Narebski, Feb 15, 2012
  4. Junio C HamanoFeb 20, 2012
  5. Jakub NarebskiFeb 22, 2012
  6. 3/8 gitweb: Faster project searchJakub Narebski, Feb 15, 2012
  7. Junio C HamanoFeb 20, 2012
  8. 4/8 gitweb: Introduce esc_html_hl_regionsJakub Narebski, Feb 15, 2012
  9. 5/8 gitweb: Highlight matched part of project name when searching projectsJakub Narebski, Feb 15, 2012
  10. 6/8 gitweb: Highlight matched part of project description when searching projectsJakub Narebski, Feb 15, 2012
  11. 7/8 gitweb: Highlight matched part of shortened project descriptionJakub Narebski, Feb 15, 2012
  12. 8/8 gitweb: Use esc_html_match_hl() in 'grep' searchJakub Narebski, Feb 15, 2012
  13. Junio C HamanoFeb 16, 2012
  14. Jakub NarebskiFeb 16, 2012
  15. Junio C HamanoFeb 20, 2012
  16. Jakub NarebskiFeb 22, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.