user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/4] viewdiff: diffstat links to diff anchors
Date: Fri,  1 Feb 2019 08:55:06 +0000	[thread overview]
Message-ID: <20190201085508.24003-3-e@80x24.org> (raw)
In-Reply-To: <20190201085508.24003-1-e@80x24.org>

This can be helpful for reviewing larger patches which span
across several files on the permalink (/$MESSAGE_ID/) HTML
page.

More work will be needed to get this working for the /T/ and /t/
pages which show multiple emails, as the filename-based anchors
will conflict at the moment.
---
 lib/PublicInbox/ViewDiff.pm | 51 +++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 8 deletions(-)

diff --git a/lib/PublicInbox/ViewDiff.pm b/lib/PublicInbox/ViewDiff.pm
index a804568..38cb5a1 100644
--- a/lib/PublicInbox/ViewDiff.pm
+++ b/lib/PublicInbox/ViewDiff.pm
@@ -12,11 +12,11 @@ use warnings;
 use base qw(Exporter);
 our @EXPORT_OK = qw(flush_diff);
 use URI::Escape qw(uri_escape_utf8);
-use PublicInbox::Hval qw(ascii_html);
+use PublicInbox::Hval qw(ascii_html to_attr from_attr);
 use PublicInbox::Git qw(git_unquote);
 
 sub DSTATE_INIT () { 0 }
-sub DSTATE_STAT () { 1 } # TODO
+sub DSTATE_STAT () { 1 }
 sub DSTATE_HEAD () { 2 } # /^diff --git /, /^index /, /^--- /, /^\+\+\+ /
 sub DSTATE_CTX () { 3 } # /^ /
 sub DSTATE_ADD () { 4 } # /^\+/
@@ -75,14 +75,47 @@ sub to_state ($$$) {
 	$$dst .= qq(<span\nclass="$class">);
 }
 
+sub anchor0 ($$$$$) {
+	my ($dst, $anchors, $linkify, $fn, $rest) = @_;
+	if (my $attr = to_attr($fn)) {
+		$anchors->{$attr} = 1;
+		$$dst .= " <a\nhref=#$attr>" .
+			ascii_html($fn) . '</a>'.
+			to_html($linkify, $rest);
+		return 1;
+	}
+	undef;
+}
+
+sub anchor1 ($$$$$) {
+	my ($dst, $anchors, $linkify, $pb, $s) = @_;
+	my $attr = to_attr($pb) or return;
+	my $line = to_html($linkify, $s);
+
+	if (delete $anchors->{$attr} && $line =~ s/^diff //) {
+		$$dst .= "<a\nhref=#ds\nid=$attr>diff</a> ".$line;
+		return 1;
+	}
+	undef
+}
+
 sub flush_diff ($$$$) {
 	my ($dst, $spfx, $linkify, $diff) = @_;
 	my $state = DSTATE_INIT;
 	my $dctx = { Q => '' }; # {}, keys: oid_a, oid_b, path_a, path_b
+	my $anchors = {}; # attr => filename
 
 	foreach my $s (@$diff) {
-		if ($s =~ /^ /) {
-			if ($state2class[$state]) {
+		if ($s =~ /^---$/) {
+			to_state($dst, $state, DSTATE_STAT);
+			$$dst .= "<span\nid=ds>" . $s . '</span>';
+		} elsif ($s =~ /^ /) {
+			# works for common cases, but not weird/long filenames
+			if ($state == DSTATE_STAT &&
+					$s =~ /^ (\S+)(\s+\|.*\z)/s) {
+				anchor0($dst, $anchors, $linkify, $1, $2)
+					and next;
+			} elsif ($state2class[$state]) {
 				to_state($dst, $state, DSTATE_CTX);
 			}
 			$$dst .= to_html($linkify, $s);
@@ -103,6 +136,8 @@ sub flush_diff ($$$$) {
 					$dctx->{Q} .=
 					     "&a=".uri_escape_utf8($pa, UNSAFE);
 				}
+				anchor1($dst, $anchors, $linkify, $pb, $s)
+					and next;
 			}
 			$$dst .= to_html($linkify, $s);
 		} elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
@@ -127,16 +162,16 @@ sub flush_diff ($$$$) {
 		} elsif ($s =~ m!^--- $PATH_A! ||
 		         $s =~ m!^\+{3} $PATH_B!)  {
 			# color only (no oid link)
-			$state == DSTATE_INIT and
+			$state <= DSTATE_STAT and
 				to_state($dst, $state, DSTATE_HEAD);
 			$$dst .= to_html($linkify, $s);
 		} elsif ($s =~ /^\+/) {
-			if ($state != DSTATE_ADD && $state != DSTATE_INIT) {
+			if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
 				to_state($dst, $state, DSTATE_ADD);
 			}
 			$$dst .= to_html($linkify, $s);
 		} elsif ($s =~ /^-/) {
-			if ($state != DSTATE_DEL && $state != DSTATE_INIT) {
+			if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
 				to_state($dst, $state, DSTATE_DEL);
 			}
 			$$dst .= to_html($linkify, $s);
@@ -148,7 +183,7 @@ sub flush_diff ($$$$) {
 			 $s =~ /^(?:dis)?similarity index /) {
 			$$dst .= to_html($linkify, $s);
 		} else {
-			$state == DSTATE_INIT or
+			$state <= DSTATE_STAT or
 				to_state($dst, $state, DSTATE_INIT);
 			$$dst .= to_html($linkify, $s);
 		}
-- 
EW


  parent reply	other threads:[~2019-02-01  8:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-01  8:55 [PATCH 0/4] viewvcs: Atom bugfix + diffstat anchors Eric Wong
2019-02-01  8:55 ` [PATCH 1/4] hval: routines for attribute escaping Eric Wong
2019-02-01  8:55 ` Eric Wong [this message]
2019-02-01  8:55 ` [PATCH 3/4] view: diffstat anchors for multi-message/attachment views Eric Wong
2019-02-01  8:55 ` [PATCH 4/4] view: fix broken hunk header hrefs in Atom feeds Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190201085508.24003-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).