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

[PATCHv2 4/8] gitweb: Introduce esc_html_hl_regions

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

The esc_html_hl_regions() subroutine added in this commit is meant to highlight in a given string a list of regions (given as a list of [ beg, end ] pairs of positions in string), using HTML <span> element with given class.

It is to be used in next commit for highlighting matched part in project search.

Implementation and enhancement notes:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Currently esc_html_hl_regions() subroutine doesn't accept any
  parameters, like esc_html() does.  We might want for example to
  pass  nbsp=>1  to it.
  It can easily be done with the following code:
    my %opts = grep { ref($_) ne "ARRAY" } @sel;
    @sel     = grep { ref($_) eq "ARRAY" } @sel;
  This allow adding parameters after or before regions, e.g.:
    esc_html_hl_regions("foo bar", "mark", [ 0, 3 ], -nbsp => 1);
* esc_html_hl_regions() escapes like esc_html(); if we wanted to
  highlight with esc_path(), we could pass subroutine reference
  to now named esc_gen_hl_regions().
    esc_html_hl_regions("foo bar", "mark", \&esc_path, [ 0, 3 ]);
  Note that this way we can handle -nbsp=>1 case automatically,
  e.g.
    esc_html_hl_regions("foo bar", "mark",
                        sub { esc_html(@_, -nbsp=>1) },
                        [ 0, 3 ]);
* Alternate solution for highlighting region of a string would be to
  use the idea that strings are to be HTML-escaped, and references to
  scalars are HTML (like in the idea for generic committags).
  This would require modifying gitweb code or esc_html to get list of
  fragments, e.g.:
    esc_html(\'<span class="mark">', 'foo', \'</span>', ' bar',
             { -nbsp => 1 });
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
This commit is here only for the notes in the commit message,
otherwise it could have been squashed with the next commit.

Note that we actually end up using first enhancement in notes section of the above commit message.

This patch is new; it was not present in v1
 gitweb/gitweb.perl |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index cc2bd6d..8dcd54b 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1715,6 +1715,33 @@ sub chop_and_escape_str {
 	}
 }
 
+# Highlight selected fragments of string, using given CSS class,
+# and escape HTML.  It is assumed that fragments do not overlap.
+# Regions are passed as list of pairs (array references).
+#
+# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns
+# '<span class="mark">foo</span>bar'
+sub esc_html_hl_regions {
+	my ($str, $css_class, @sel) = @_;
+	return esc_html($str) unless @sel;
+
+	my $out = '';
+	my $pos = 0;
+
+	for my $s (@sel) {
+		$out .= esc_html(substr($str, $pos, $s->[0] - $pos))
+			if ($s->[0] - $pos > 0);
+		$out .= $cgi->span({-class => $css_class},
+		                   esc_html(substr($str, $s->[0], $s->[1] - $s->[0])));
+
+		$pos = $s->[1];
+	}
+	$out .= esc_html(substr($str, $pos))
+		if ($pos < length($str));
+
+	return $out;
+}
+
 ## ----------------------------------------------------------------------
 ## functions returning short strings
 
-- 
1.7.9
Previous: Junio C HamanoNext: Jakub Narebski
Message 8 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.