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

[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
Previous: Jakub NarebskiNext: Jakub Narebski
Message 9 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.