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

Re: [PATCH 6/8] gitweb: Highlight interesting parts of diff

From
Michal Kiedrowicz <michal.kiedrowicz@gmail.com>
Date
Feb 13, 2012, 06:54 UTC
Message-ID
<20120213075451.1bc20885@mkiedrowicz.ivo.pl>
In-Reply-To
<m3lio8s57v.fsf@localhost.localdomain>
Jakub Narebski <jnareb@gmail.com> wrote:
Show 33 quoted lines
> Jakub Narebski <jnareb@gmail.com> writes:
> > Michał Kiedrowicz <michal.kiedrowicz@gmail.com> writes:
>  
> > > +# Highlight characters from $prefix to $suffix and escape HTML.
> > > +# $str is a reference to the array of characters.
> > > +sub esc_html_mark_range {
> > > +	my ($str, $prefix, $suffix) = @_;
> > > +
> > > +	# Don't generate empty <span> element.
> > > +	if ($prefix == $suffix + 1) {
> > > +		return esc_html(join('', @$str), -nbsp=>1);
> > > +	}
> > > +
> > > +	my $before = join('', @{$str}[0..($prefix - 1)]);
> > > +	my $marked = join('', @{$str}[$prefix..$suffix]);
> > > +	my $after = join('', @{$str}[($suffix + 1)..$#{$str}]);
> > 
> > Eeeeeek!  First you split into letters, in caller at that, then
> > join? Why not pass striung ($str suggests string not array of
> > characters), and use substr instead?
> > 
> > [Please disregard this and the next paragraph at first reading]
> > 
> > > +
> > > +	return esc_html($before, -nbsp=>1) .
> > > +		$cgi->span({-class=>'marked'}, esc_html($marked,
> > > -nbsp=>1)) .
> > > +		esc_html($after,-nbsp=>1);
> > > +}
> > 
> > Anyway I have send to git mailing list a patch series, which in one
> > of patches adds esc_html_match_hl($str, $regexp) to highlight
> > matches in a string.  

Yeah, I saw that but after seeing that they accept different arguments I decided to leave them alone.

> Your esc_html_mark_range(), after a
> > generalization, could be used as underlying "engine".
> > 
> > Something like this, perhaps (untested):

I think I'll leave it to you after merging both these series to master :)

Show 29 quoted lines
> > 
> >    # 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).
> >    sub esc_html_hl {
> >         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 = $m->[1];
> >         }
> >         $out .= esc_html(substr($str, $pos))
> >                 if ($pos < length($str));
> >    
> >         return $out;
> >    }
> 
> Actually we can accomodate both operating on string and operating on
> array of characters in a single subroutine.  Though it can be left for
> later commit, anyway...
Show 57 quoted lines
> 
>      # 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).
>      sub esc_html_hl {
>           my ($sth, $css_class, @sel) = @_;
> 
>           if (!@sel) {
>                 if (ref($sth) eq "ARRAY") {
>                         return esc_html(join('', @$sth), -nbsp=>1);
>                 } else {
>                         return esc_html($sth, -nbsp=>1);
>           }
> 
>           if (ref($sth) eq "ARRAY") {
>                 return esc_html_hl_gen($sth,
>                         sub { 
>                                 my ($arr, $from, $to) = @_;
>                                 return join('', @{$arr}[$from..$to]);
>                         },
>                         scalar @{$arr}, $css_class, @sel);
>            } else {
>                 return esc_html_hl_gen($sth,
>                         sub {
>                                 my ($str, $from, $to) = @_;
>                                 if ($to < 0) { $to += lenght($str); };
>                                 return substr($str, $from, $to -
> $from); },
>                         length($sth), $css_class, @sel);
>            }
>      }
> 
>      # Highlight selected fragments of string or array of characters
>      # with given length, using provided $extr subroutine to extract
>      # fragment (substring)
>      sub esc_html_hl_gen {
>           my ($sth, $extr, $len, $css_class, @sel) = @_;
>      
>           my $out = '';
>           my $pos = 0;
>      
>           for my $s (@sel) {
>                 $out .= esc_html($extr->($str, $pos, $s->[0]))
>                         if ($s->[0] - $pos > 0);
>                 $out .= $cgi->span({-class => $css_class},
>                                    esc_html($extr->($str, $s->[0],
> $s->[1]))); 
>                 $pos = $s->[1];
>           }
>           $out .= esc_html($extr->($str, $pos, $len))
>                   if ($pos < $len);
>      
>           return $out;
>      }
> 
> Or maybe I have read "Higher-Order Perl" one time too many ;-))))
> 
Previous: Jakub NarebskiNext: Jakub Narebski
Message 49 of 67 in “gitweb: Highlight interesting parts of diff”
  1. 0/8 gitweb: Highlight interesting parts of diffMichał Kiedrowicz, Feb 10, 2012
  2. 1/8 gitweb: Extract print_sidebyside_diff_lines()Michał Kiedrowicz, Feb 10, 2012
  3. Jakub NarebskiFeb 11, 2012
  4. Michał KiedrowiczFeb 11, 2012
  5. 2/8 gitweb: Use print_diff_chunk() for both side-by-side and inline diffsMichał Kiedrowicz, Feb 10, 2012
  6. Jakub NarebskiFeb 11, 2012
  7. Michał KiedrowiczFeb 11, 2012
  8. Michał KiedrowiczFeb 25, 2012
  9. 3/8 gitweb: Move HTML-formatting diff line back to process_diff_line()Michał Kiedrowicz, Feb 10, 2012
  10. Jakub NarebskiFeb 11, 2012
  11. 4/8 gitweb: Push formatting diff lines to print_diff_chunk()Michał Kiedrowicz, Feb 10, 2012
  12. Jakub NarebskiFeb 11, 2012
  13. Michał KiedrowiczFeb 11, 2012
  14. Michał KiedrowiczFeb 11, 2012
  15. 5/8 gitweb: Format diff lines just before printingMichał Kiedrowicz, Feb 10, 2012
  16. Jakub NarebskiFeb 11, 2012
  17. Michał KiedrowiczFeb 11, 2012
  18. 6/8 gitweb: Highlight interesting parts of diffMichał Kiedrowicz, Feb 10, 2012
  19. Jakub NarebskiFeb 10, 2012
  20. Michał KiedrowiczFeb 10, 2012
  21. Jakub NarebskiFeb 10, 2012
  22. Michał KiedrowiczFeb 10, 2012
  23. Jakub NarebskiFeb 10, 2012
  24. Jeff KingFeb 10, 2012
  25. Michal KiedrowiczFeb 14, 2012
  26. Junio C HamanoFeb 14, 2012
  27. Jeff KingFeb 14, 2012
  28. Jeff KingFeb 10, 2012
  29. Michał KiedrowiczFeb 10, 2012
  30. Jeff KingFeb 10, 2012
  31. Michał KiedrowiczFeb 10, 2012
  32. diff-highlight: Work for multiline changes tooMichał Kiedrowicz, Feb 10, 2012
  33. Jeff KingFeb 13, 2012
  34. 1/5 diff-highlight: make perl strict and warnings fatalJeff King, Feb 13, 2012
  35. 2/5 diff-highlight: don't highlight whole linesJeff King, Feb 13, 2012
  36. Michal KiedrowiczFeb 14, 2012
  37. 3/5 diff-highlight: refactor to prepare for multi-line hunksJeff King, Feb 13, 2012
  38. 4/5 diff-highlight: match multi-line hunksJeff King, Feb 13, 2012
  39. 5/5 diff-highlight: document some non-optimal casesJeff King, Feb 13, 2012
  40. Michal KiedrowiczFeb 14, 2012
  41. Junio C HamanoFeb 14, 2012
  42. Jeff KingFeb 14, 2012
  43. Junio C HamanoFeb 14, 2012
  44. Jeff KingFeb 14, 2012
  45. Michal KiedrowiczFeb 14, 2012
  46. Jakub NarebskiFeb 10, 2012
  47. Jakub NarebskiFeb 11, 2012
  48. Jakub NarebskiFeb 12, 2012
  49. Michal KiedrowiczFeb 13, 2012
  50. Jakub NarebskiFeb 13, 2012
  51. Michał KiedrowiczFeb 13, 2012
  52. Michal KiedrowiczFeb 13, 2012
  53. Jakub NarebskiFeb 13, 2012
  54. Michał KiedrowiczFeb 13, 2012
  55. Jakub NarebskiFeb 14, 2012
  56. Michał KiedrowiczFeb 14, 2012
  57. Jeff KingFeb 14, 2012
  58. Michał KiedrowiczFeb 14, 2012
  59. Jeff KingFeb 14, 2012
  60. 7/8 gitweb: Use different colors to present marked changesMichał Kiedrowicz, Feb 10, 2012
  61. Jakub NarebskiFeb 12, 2012
  62. Michal KiedrowiczFeb 13, 2012
  63. 8/8 gitweb: Highlight combined diffsMichał Kiedrowicz, Feb 10, 2012
  64. Jakub NarebskiFeb 12, 2012
  65. Michal KiedrowiczFeb 13, 2012
  66. Jakub NarebskiFeb 11, 2012
  67. Michał KiedrowiczFeb 11, 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.