user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 16/22] linkify: move to_html over from ViewDiff
Date: Sat, 25 Jan 2020 04:45:04 +0000
Message-ID: <20200125044510.13769-17-e@yhbt.net> (raw)
In-Reply-To: <20200125044510.13769-1-e@yhbt.net>

We use the same idiom in many places for doing two-step
linkification and HTML escaping.  Get rid of an outdated
comment in flush_quote while we're at it.
---
 lib/PublicInbox/Linkify.pm    |  2 ++
 lib/PublicInbox/View.pm       |  8 ++------
 lib/PublicInbox/ViewDiff.pm   | 31 +++++++++++++------------------
 lib/PublicInbox/ViewVCS.pm    |  6 ++----
 lib/PublicInbox/WwwListing.pm |  3 +--
 5 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/lib/PublicInbox/Linkify.pm b/lib/PublicInbox/Linkify.pm
index 77b94f56..199457af 100644
--- a/lib/PublicInbox/Linkify.pm
+++ b/lib/PublicInbox/Linkify.pm
@@ -120,4 +120,6 @@ sub linkify_mids {
 	!ge;
 }
 
+sub to_html { linkify_2($_[0], ascii_html(linkify_1(@_))) }
+
 1;
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 54a49de6..f613afb5 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -496,13 +496,10 @@ sub multipart_text_as_html {
 sub flush_quote {
 	my ($s, $l, $quot) = @_;
 
-	# show everything in the full version with anchor from
-	# short version (see above)
-	my $rv = $l->linkify_1($$quot);
+	my $rv = $l->to_html($$quot);
 
 	# we use a <span> here to allow users to specify their own
 	# color for quoted text
-	$rv = $l->linkify_2(ascii_html($rv));
 	$$quot = undef;
 	$$s .= qq(<span\nclass="q">) . $rv . '</span>'
 }
@@ -608,8 +605,7 @@ sub add_text_body { # callback for msg_iter
 			flush_diff($rv, $ctx, $l);
 		} else {
 			# regular lines, OK
-			$l->linkify_1($cur);
-			$$rv .= $l->linkify_2(ascii_html($cur));
+			$$rv .= $l->to_html($cur);
 			$cur = undef;
 		}
 	}
diff --git a/lib/PublicInbox/ViewDiff.pm b/lib/PublicInbox/ViewDiff.pm
index 10d71004..be0c0452 100644
--- a/lib/PublicInbox/ViewDiff.pm
+++ b/lib/PublicInbox/ViewDiff.pm
@@ -44,11 +44,6 @@ my $PATH_B = '"?b/.+|/dev/null';
 # cf. git diff.c :: get_compact_summary
 my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
 
-sub to_html ($$) {
-	$_[0]->linkify_1($_[1]);
-	$_[0]->linkify_2(ascii_html($_[1]));
-}
-
 # link to line numbers in blobs
 sub diff_hunk ($$$$) {
 	my ($dctx, $spfx, $ca, $cb) = @_;
@@ -107,7 +102,7 @@ sub anchor0 ($$$$$) {
 		my $spaces = ($orig =~ s/( +)\z//) ? $1 : '';
 		$$dst .= " <a\nid=i$attr\nhref=#$attr>" .
 			ascii_html($orig) . '</a>' . $spaces .
-			to_html($linkify, $rest);
+			$linkify->to_html($rest);
 		return 1;
 	}
 	undef;
@@ -116,7 +111,7 @@ sub anchor0 ($$$$$) {
 sub anchor1 ($$$$$) {
 	my ($dst, $ctx, $linkify, $pb, $s) = @_;
 	my $attr = to_attr($ctx->{-apfx}.$pb) or return;
-	my $line = to_html($linkify, $s);
+	my $line = $linkify->to_html($s);
 
 	my $ok = delete $ctx->{-anchors}->{$attr};
 
@@ -158,7 +153,7 @@ sub flush_diff ($$$) {
 			} elsif ($state2class[$state]) {
 				to_state($dst, $state, DSTATE_CTX);
 			}
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ /^-- $/) { # email signature begins
 			$state == DSTATE_INIT or
 				to_state($dst, $state, DSTATE_INIT);
@@ -178,53 +173,53 @@ sub flush_diff ($$$) {
 					uri_escape_utf8($pa, UNSAFE);
 			}
 			anchor1($dst, $ctx, $linkify, $pb, $s) and next;
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ s/^(index $OID_NULL\.\.)($OID_BLOB)\b//o) {
 			$$dst .= $1 . oid($dctx, $spfx, $2);
 			$dctx = { Q => '' };
-			$$dst .= to_html($linkify, $s) ;
+			$$dst .= $linkify->to_html($s) ;
 		} elsif ($s =~ s/^index ($OID_BLOB)(\.\.$OID_NULL)\b//o) {
 			$$dst .= 'index ' . oid($dctx, $spfx, $1) . $2;
 			$dctx = { Q => '' };
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ /^index ($OID_BLOB)\.\.($OID_BLOB)/o) {
 			$dctx->{oid_a} = $1;
 			$dctx->{oid_b} = $2;
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ s/^@@ (\S+) (\S+) @@//) {
 			$$dst .= '</span>' if $state2class[$state];
 			$$dst .= qq(<span\nclass="hunk">);
 			$$dst .= diff_hunk($dctx, $spfx, $1, $2);
 			$$dst .= '</span>';
 			$state = DSTATE_CTX;
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ m!^--- (?:$PATH_A)!o ||
 		         $s =~ m!^\+{3} (?:$PATH_B)!o)  {
 			# color only (no oid link) if missing dctx->{oid_*}
 			$state <= DSTATE_STAT and
 				to_state($dst, $state, DSTATE_HEAD);
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ /^\+/) {
 			if ($state != DSTATE_ADD && $state > DSTATE_STAT) {
 				to_state($dst, $state, DSTATE_ADD);
 			}
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} elsif ($s =~ /^-/) {
 			if ($state != DSTATE_DEL && $state > DSTATE_STAT) {
 				to_state($dst, $state, DSTATE_DEL);
 			}
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		# ignore the following lines in headers:
 		} elsif ($s =~ /^(?:dis)similarity index/ ||
 			 $s =~ /^(?:old|new) mode/ ||
 			 $s =~ /^(?:deleted|new) file mode/ ||
 			 $s =~ /^(?:copy|rename) (?:from|to) / ||
 			 $s =~ /^(?:dis)?similarity index /) {
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		} else {
 			$state <= DSTATE_STAT or
 				to_state($dst, $state, DSTATE_INIT);
-			$$dst .= to_html($linkify, $s);
+			$$dst .= $linkify->to_html($s);
 		}
 	}
 	@$diff = ();
diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm
index fdca70cb..77b3ca28 100644
--- a/lib/PublicInbox/ViewVCS.pm
+++ b/lib/PublicInbox/ViewVCS.pm
@@ -90,8 +90,7 @@ sub show_other_result ($$) {
 	}
 	my $l = PublicInbox::Linkify->new;
 	utf8::decode($$bref);
-	$l->linkify_1($$bref);
-	$$bref = '<pre>'. $l->linkify_2(ascii_html($$bref));
+	$$bref = '<pre>'. $l->to_html($$bref);
 	$$bref .= '</pre><hr>' . $$logref;
 	html_page($ctx, 200, $bref);
 }
@@ -125,9 +124,8 @@ sub solve_result {
 
 	my $ref = ref($res);
 	my $l = PublicInbox::Linkify->new;
-	$l->linkify_1($log);
 	$log = '<pre>debug log:</pre><hr /><pre>' .
-		$l->linkify_2(ascii_html($log)) . '</pre>';
+		$l->to_html($log) . '</pre>';
 
 	$res or return html_page($ctx, 404, \$log);
 	$ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
diff --git a/lib/PublicInbox/WwwListing.pm b/lib/PublicInbox/WwwListing.pm
index 8d610037..37ffffc1 100644
--- a/lib/PublicInbox/WwwListing.pm
+++ b/lib/PublicInbox/WwwListing.pm
@@ -111,8 +111,7 @@ sub html ($$) {
 
 		my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
 		my $l = PublicInbox::Linkify->new;
-		$l->linkify_1($tmp);
-		$out = '<pre>'.$l->linkify_2(ascii_html($tmp)).'</pre><hr>';
+		$out = '<pre>'.$l->to_html($tmp).'</pre><hr>';
 	}
 	$out = "<html><head><title>$title</title></head><body>" . $out;
 	$out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .

  parent reply index

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-25  4:44 [PATCH 00/22] HTML display cleanups, fixes, speedups Eric Wong
2020-01-25  4:44 ` [PATCH 01/22] www*stream: favor \&close instead of *close Eric Wong
2020-01-25  4:44 ` [PATCH 02/22] www: use "skel" terminology consistently Eric Wong
2020-01-25  4:44 ` [PATCH 03/22] view: improve readability around walk_thread Eric Wong
2020-01-25  4:44 ` [PATCH 04/22] searchview: keep $noop sub private to the package Eric Wong
2020-01-25  4:44 ` [PATCH 05/22] view: reduce parameters for html_footer Eric Wong
2020-01-25  4:44 ` [PATCH 06/22] view: thread_skel: drop constant tpfx parameter Eric Wong
2020-01-25  4:44 ` [PATCH 07/22] view: simplify duplicate Message-ID handling Eric Wong
2020-01-25  4:44 ` [PATCH 08/22] wwwstream: discard single-use $ctx fields after use Eric Wong
2020-01-25  4:44 ` [PATCH 09/22] view: start performing buffering into {obuf} Eric Wong
2020-01-25  4:44 ` [PATCH 10/22] t/plack.t: modernize and unindent Eric Wong
2020-01-25  4:44 ` [PATCH 11/22] init: use Import::run_die instead of system() Eric Wong
2020-01-25  4:45 ` [PATCH 12/22] tests: move the majority of t/view.t into t/plack.t Eric Wong
2020-01-25  4:45 ` [PATCH 13/22] xt/perf-msgview: switch to multipart_text_as_html Eric Wong
2020-01-25  4:45 ` [PATCH 14/22] view: inline and eliminate msg_html Eric Wong
2020-01-25  4:45 ` [PATCH 15/22] linkify: compile $LINK_RE once Eric Wong
2020-01-25  4:45 ` Eric Wong [this message]
2020-01-25  4:45 ` [PATCH 17/22] searchidx: skip filenames on "diff --git ..." Eric Wong
2020-01-25  4:45 ` [PATCH 18/22] searchidx: don't assume "a/" and "b/" as prefixes Eric Wong
2020-01-25  4:45 ` [PATCH 19/22] viewdiff: add "b=" param with non-standard diff prefix Eric Wong
2020-01-25  4:45 ` [PATCH 20/22] viewdiff: add "b=" param when missing "diff --git" line Eric Wong
2020-01-25  4:45 ` [PATCH 21/22] viewdiff: use autovivification for long_path hash Eric Wong
2020-01-25  4:45 ` [PATCH 22/22] viewdiff: rewrite and simplify 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: https://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=20200125044510.13769-17-e@yhbt.net \
    --to=e@yhbt.net \
    --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

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git