user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] reduce "PublicInbox::Hval->new_oneline" use
@ 2016-03-12  7:25 Eric Wong
  2016-03-12  7:38 ` [PATCH 2/1] feed: fix brain farts in new_oneline removal Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2016-03-12  7:25 UTC (permalink / raw)
  To: meta

It's probably a bad idea to strip extraneous whitespace
from some headers as an extra space may convey useful
information.

Newlines don't seem to be preserved by Email::MIME or
Email::Simple anyways, so there's no danger in breaking
formatting.
---
 lib/PublicInbox/Feed.pm       | 20 ++++++++------------
 lib/PublicInbox/Hval.pm       |  2 ++
 lib/PublicInbox/SearchView.pm | 23 ++++++++++-------------
 lib/PublicInbox/View.pm       | 23 +++++++++--------------
 4 files changed, 29 insertions(+), 39 deletions(-)

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 4740853..e4831f6 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -8,7 +8,7 @@ use warnings;
 use Email::Address;
 use Email::MIME;
 use Date::Parse qw(strptime);
-use PublicInbox::Hval;
+use PublicInbox::Hval qw/ascii_html/;
 use PublicInbox::Git;
 use PublicInbox::View;
 use PublicInbox::MID qw/mid_clean mid2path/;
@@ -38,8 +38,9 @@ sub generate_html_index {
 
 sub title_tag {
 	my ($title) = @_;
+	$title =~ tr/\t\n / /s; # squeeze spaces
 	# try to avoid the type attribute in title:
-	$title = PublicInbox::Hval->new_oneline($title)->as_html;
+	$title =~ ascii_html($title);
 	my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
 	"<title$type>$title</title>";
 }
@@ -117,7 +118,6 @@ sub emit_html_index {
 	my $feed_opts = get_feedopts($ctx);
 
 	my $title = $feed_opts->{description} || '';
-	$title = PublicInbox::Hval->new_oneline($title)->as_html;
 	my ($footer, $param, $last);
 	my $state = { ctx => $ctx, seen => {}, anchor_idx => 0 };
 	my $srch = $ctx->{srch};
@@ -295,11 +295,6 @@ sub get_feedopts {
 	\%rv;
 }
 
-sub mime_header {
-	my ($mime, $name) = @_;
-	PublicInbox::Hval->new_oneline($mime->header($name))->raw;
-}
-
 sub feed_updated {
 	my ($date, $ts) = @_;
 	my @t = eval { strptime($date) } if defined $date;
@@ -328,14 +323,15 @@ sub add_to_feed {
 	my $date = $header_obj->header('Date');
 	my $updated = feed_updated($date);
 
-	my $title = mime_header($header_obj, 'Subject') or return 0;
+	my $title = $header_obj->header('Subject');
+	defined $title or return 0;
 	$title = title_tag($title);
 
-	my $from = mime_header($header_obj, 'From') or return 0;
+	my $from = $header_obj->header('From') or return 0;
 	my @from = Email::Address->parse($from) or return 0;
-	my $name = PublicInbox::Hval->new_oneline($from[0]->name)->as_html;
+	my $name = ascii_html($from[0]->name);
 	my $email = $from[0]->address;
-	$email = PublicInbox::Hval->new_oneline($email)->as_html;
+	$email = ascii_html($email);
 
 	if (delete $feed_opts->{emit_header}) {
 		$fh->write(atom_header($feed_opts, $title) . $updated);
diff --git a/lib/PublicInbox/Hval.pm b/lib/PublicInbox/Hval.pm
index a455884..70bae7c 100644
--- a/lib/PublicInbox/Hval.pm
+++ b/lib/PublicInbox/Hval.pm
@@ -9,6 +9,8 @@ use warnings;
 use Encode qw(find_encoding);
 use URI::Escape qw(uri_escape_utf8);
 use PublicInbox::MID qw/mid_clean/;
+use base qw/Exporter/;
+our @EXPORT_OK = qw/ascii_html/;
 
 # for user-generated content (UGC) which may have excessively long lines
 # and screw up rendering on some browsers.  This is the only CSS style
diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm
index 36522a3..8283c1a 100644
--- a/lib/PublicInbox/SearchView.pm
+++ b/lib/PublicInbox/SearchView.pm
@@ -6,7 +6,7 @@ package PublicInbox::SearchView;
 use strict;
 use warnings;
 use PublicInbox::SearchMsg;
-use PublicInbox::Hval;
+use PublicInbox::Hval qw/ascii_html/;
 use PublicInbox::View;
 use PublicInbox::MID qw(mid2path mid_clean mid_mime);
 use Email::MIME;
@@ -68,13 +68,12 @@ sub dump_mset {
 		my $rank = sprintf("%${pad}d", $m->get_rank + 1);
 		my $pct = $m->get_percent;
 		my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
-		my $s = PublicInbox::Hval->new_oneline($smsg->subject);
-		my $f = $smsg->from_name;
-		$f = PublicInbox::Hval->new_oneline($f)->as_html;
+		my $s = ascii_html($smsg->subject);
+		my $f = ascii_html($smsg->from_name);
 		my $ts = PublicInbox::View::fmt_ts($smsg->ts);
 		my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
 		$$res .= qq{$rank. <b><a\nhref="$mid/">}.
-			$s->as_html . "</a></b>\n";
+			$s . "</a></b>\n";
 		$$res .= "$pfx  - by $f @ $ts UTC [$pct%]\n\n";
 	}
 }
@@ -84,7 +83,7 @@ sub err_txt {
 	my $u = '//xapian.org/docs/queryparser.html';
 	$u = PublicInbox::Hval::prurl($ctx->{cgi}->{env}, $u);
 	$err =~ s/^\s*Exception:\s*//; # bad word to show users :P
-	$err = PublicInbox::Hval->new_oneline($err)->as_html;
+	$err = ascii_html($err);
 	"\n\nBad query: <b>$err</b>\n" .
 		qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
 }
@@ -222,9 +221,7 @@ sub foot {
 
 sub html_start {
 	my ($q, $ctx) = @_;
-	my $query = PublicInbox::Hval->new_oneline($q->{q});
-
-	my $qh = $query->as_html;
+	my $qh = ascii_html($q->{'q'});
 	my $A = $q->qs_html(x => 'A', r => undef);
 	my $res = '<html><head>' . PublicInbox::Hval::STYLE .
 		"<title>$qh - search results</title>" .
@@ -235,8 +232,8 @@ sub html_start {
 
 	$res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
 	if (my $x = $q->{x}) {
-		my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
-		$res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
+		$x = ascii_html($x);
+		$res .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
 	}
 
 	$res .= qq{<input\ntype=submit\nvalue=search /></form>};
@@ -247,7 +244,7 @@ sub adump {
 	my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
 	my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
 	my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
-	my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
+	my $x = ascii_html($q->{'q'});
 	$x = qq{$x - search results};
 	$feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
 	$feed_opts->{url} .= '?'. $q->qs_html(x => undef);
@@ -289,7 +286,7 @@ sub qs_html {
 		$self = $tmp;
 	}
 
-	my $q = PublicInbox::Hval->new_oneline($self->{q})->as_href;
+	my $q = PublicInbox::Hval->new($self->{'q'})->as_href;
 	$q =~ s/%20/+/g; # improve URL readability
 	my $qs = "q=$q";
 
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 3522bf4..4058bee 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -11,7 +11,7 @@ use Date::Parse qw/str2time/;
 use Encode qw/find_encoding/;
 use Encode::MIME::Header;
 use Email::MIME::ContentType qw/parse_content_type/;
-use PublicInbox::Hval;
+use PublicInbox::Hval qw/ascii_html/;
 use PublicInbox::Linkify;
 use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/;
 require POSIX;
@@ -22,8 +22,6 @@ use constant MAX_TRUNC_LEN => 72;
 use constant T_ANCHOR => '#u';
 use constant INDENT => '  ';
 
-*ascii_html = *PublicInbox::Hval::ascii_html;
-
 my $enc_utf8 = find_encoding('UTF-8');
 
 # public functions:
@@ -50,10 +48,9 @@ sub msg_reply {
 	$s = '(no subject)' if (!defined $s) || ($s eq '');
 	my $f = $hdr->header('From');
 	$f = '' unless defined $f;
-	$s = PublicInbox::Hval->new_oneline($s);
 	my $mid = $hdr->header_raw('Message-ID');
 	$mid = PublicInbox::Hval->new_msgid($mid);
-	my $t = $s->as_html;
+	my $t = ascii_html($s);
 	my $se_url =
 	 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
 
@@ -121,18 +118,18 @@ sub index_entry {
 	$seen->{$id} = "#$id"; # save the anchor for children, later
 
 	my $mid = PublicInbox::Hval->new_msgid($mid_raw);
-	my $from = PublicInbox::Hval->new_oneline($hdr->header('From'))->raw;
+	my $from = $hdr->header('From');
 	my @from = Email::Address->parse($from);
 	$from = $from[0]->name;
 
-	$from = PublicInbox::Hval->new_oneline($from)->as_html;
-	$subj = PublicInbox::Hval->new_oneline($subj)->as_html;
 	my $root_anchor = $state->{root_anchor} || '';
 	my $path = $root_anchor ? '../../' : '';
 	my $href = $mid->as_href;
 	my $irt = in_reply_to($hdr);
 	my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
 
+	$from = ascii_html($from);
+	$subj = ascii_html($subj);
 	if ($srch) {
 		my $t = $ctx->{flat} ? 'T' : 't';
 		$subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
@@ -414,7 +411,7 @@ sub headers_to_html_header {
 	foreach my $h (qw(From To Cc Subject Date)) {
 		my $v = $hdr->header($h);
 		defined($v) && ($v ne '') or next;
-		$v = PublicInbox::Hval->new_oneline($v);
+		$v = PublicInbox::Hval->new($v);
 
 		if ($h eq 'From') {
 			my @from = Email::Address->parse($v->raw);
@@ -582,7 +579,7 @@ sub html_footer {
 		my $p = $ctx->{parent_msg};
 		my $next = $ctx->{next_msg};
 		if ($p) {
-			$p = PublicInbox::Hval->new_oneline($p);
+			$p = PublicInbox::Hval->new_msgid($p);
 			$p = $p->as_href;
 			$irt = "<a\nhref=\"$upfx$p/\">parent</a> ";
 		} else {
@@ -626,8 +623,7 @@ sub thread_html_head {
 	my ($cb, $header, $state) = @_;
 	$$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
 
-	my $s = PublicInbox::Hval->new_oneline($header->header('Subject'));
-	$s = $s->as_html;
+	my $s = ascii_html($header->header('Subject'));
 	$$cb->write("<html><head><title>$s</title>".
 		qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
 		qq!href="../t.atom"\ntype="application/atom+xml"/>! .
@@ -781,9 +777,8 @@ sub _inline_header {
 
 	my $cur = $state->{cur};
 	my $mid = mid_clean($hdr->header_raw('Message-ID'));
-	my $f = $hdr->header('X-PI-From');
+	my $f = ascii_html($hdr->header('X-PI-From'));
 	my $d = _msg_date($hdr);
-	$f = PublicInbox::Hval->new_oneline($f)->as_html;
 	my $pfx = ' ' . $d . ' ' . indent_for($level);
 	my $attr = $f;
 	$state->{first_level} ||= $level;
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/1] feed: fix brain farts in new_oneline removal
  2016-03-12  7:25 [PATCH] reduce "PublicInbox::Hval->new_oneline" use Eric Wong
@ 2016-03-12  7:38 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2016-03-12  7:38 UTC (permalink / raw)
  To: meta

Ugh...

Fixes: 476fc666c223 (reduce "PublicInbox::Hval->new_oneline" use)
---
 lib/PublicInbox/Feed.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index e4831f6..ec6925d 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -40,7 +40,7 @@ sub title_tag {
 	my ($title) = @_;
 	$title =~ tr/\t\n / /s; # squeeze spaces
 	# try to avoid the type attribute in title:
-	$title =~ ascii_html($title);
+	$title = ascii_html($title);
 	my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
 	"<title$type>$title</title>";
 }
@@ -117,7 +117,7 @@ sub emit_html_index {
 	my $max = $ctx->{max} || MAX_PER_PAGE;
 	my $feed_opts = get_feedopts($ctx);
 
-	my $title = $feed_opts->{description} || '';
+	my $title = ascii_html($feed_opts->{description} || '');
 	my ($footer, $param, $last);
 	my $state = { ctx => $ctx, seen => {}, anchor_idx => 0 };
 	my $srch = $ctx->{srch};
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-03-12  7:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-12  7:25 [PATCH] reduce "PublicInbox::Hval->new_oneline" use Eric Wong
2016-03-12  7:38 ` [PATCH 2/1] feed: fix brain farts in new_oneline removal Eric Wong

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).