user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/3] introduce WwwStream for per-message views
@ 2016-06-18  4:47 Eric Wong
  2016-06-18  4:47 ` [PATCH 1/3] feed: split out top-of-page generation Eric Wong
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Wong @ 2016-06-18  4:47 UTC (permalink / raw)
  To: meta

It will be considerably more work to port the rest of
the stuff over, but this is a start...

Eric Wong (3):
      feed: split out top-of-page generation
      view: introduce WwwStream interface
      view: minor tweaks to reduce long lines

 lib/PublicInbox/Feed.pm      | 36 ++++++++++--------
 lib/PublicInbox/View.pm      | 63 +++++++++++++------------------
 lib/PublicInbox/WwwStream.pm | 88 ++++++++++++++++++++++++++++++++++++++++++++
 t/view.t                     | 14 ++++++-
 4 files changed, 146 insertions(+), 55 deletions(-)


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

* [PATCH 1/3] feed: split out top-of-page generation
  2016-06-18  4:47 [PATCH 0/3] introduce WwwStream for per-message views Eric Wong
@ 2016-06-18  4:47 ` Eric Wong
  2016-06-18  4:47 ` [PATCH 2/3] view: introduce WwwStream interface Eric Wong
  2016-06-18  4:47 ` [PATCH 3/3] view: minor tweaks to reduce long lines Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2016-06-18  4:47 UTC (permalink / raw)
  To: meta

This will eventually allow us to reuse code to generate a common
header.
---
 lib/PublicInbox/Feed.pm | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 07774cb..045e495 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -109,20 +109,11 @@ sub emit_atom_thread {
 	end_feed($fh);
 }
 
-sub emit_html_index {
-	my ($res, $ctx) = @_;
-	my $feed_opts = get_feedopts($ctx);
-	my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
-
-	my $max = $ctx->{max} || MAX_PER_PAGE;
+sub _html_index_top {
+	my ($feed_opts, $srch) = @_;
 
 	my $title = ascii_html($feed_opts->{description} || '');
-	my ($footer, $param, $last);
-	my $state = { ctx => $ctx, seen => {}, anchor_idx => 0, fh => $fh };
-	my $srch = $ctx->{srch};
-
 	my $top = "<b>$title</b> (<a\nhref=\"new.atom\">Atom feed</a>)";
-
 	if ($srch) {
 		$top = qq{<form\naction=""><pre>$top} .
 			  qq{ <input\nname=q\ntype=text />} .
@@ -132,11 +123,24 @@ sub emit_html_index {
 		$top = '<pre>' . $top . "\n";
 	}
 
-	$fh->write("<html><head><title>$title</title>" .
-		   "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
-		   "href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
-		   PublicInbox::Hval::STYLE .
-		   "</head><body>$top");
+	"<html><head><title>$title</title>" .
+		"<link\nrel=alternate\ntitle=\"Atom feed\"\n".
+		"href=\"new.atom\"\ntype=\"application/atom+xml\"/>" .
+		PublicInbox::Hval::STYLE .
+		"</head><body>$top";
+}
+
+sub emit_html_index {
+	my ($res, $ctx) = @_;
+	my $feed_opts = get_feedopts($ctx);
+	my $fh = $res->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
+
+	my $max = $ctx->{max} || MAX_PER_PAGE;
+
+	my ($footer, $param, $last);
+	my $state = { ctx => $ctx, seen => {}, anchor_idx => 0, fh => $fh };
+	my $srch = $ctx->{srch};
+	$fh->write(_html_index_top($feed_opts, $srch));
 
 	# if the 'r' query parameter is given, it is a legacy permalink
 	# which we must continue supporting:

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

* [PATCH 2/3] view: introduce WwwStream interface
  2016-06-18  4:47 [PATCH 0/3] introduce WwwStream for per-message views Eric Wong
  2016-06-18  4:47 ` [PATCH 1/3] feed: split out top-of-page generation Eric Wong
@ 2016-06-18  4:47 ` Eric Wong
  2016-06-18  4:47 ` [PATCH 3/3] view: minor tweaks to reduce long lines Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2016-06-18  4:47 UTC (permalink / raw)
  To: meta

This will allow us to commonalize HTML generation in the future
and is the start of moving existing HTML generation to a "pull"
streaming model (from the existing "push" one).

Using the getline/close pull model is superior to the existing
$fh->write streaming as it allows us to throttle response
generation based on backpressure from slow clients.
---
 lib/PublicInbox/View.pm      | 51 +++++++++----------------
 lib/PublicInbox/WwwStream.pm | 88 ++++++++++++++++++++++++++++++++++++++++++++
 t/view.t                     | 14 ++++++-
 3 files changed, 119 insertions(+), 34 deletions(-)
 create mode 100644 lib/PublicInbox/WwwStream.pm

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 534f85e..e7e387d 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -15,6 +15,7 @@ use PublicInbox::Linkify;
 use PublicInbox::MID qw/mid_clean id_compress mid2path mid_mime/;
 use PublicInbox::MsgIter;
 use PublicInbox::Address;
+use PublicInbox::WwwStream;
 require POSIX;
 
 use constant INDENT => '  ';
@@ -22,31 +23,22 @@ use constant TCHILD => '` ';
 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
 
 # public functions: (unstable)
-# TODO: stream this, since threading is expensive but also oh-so-important
 sub msg_html {
 	my ($ctx, $mime, $footer) = @_;
-	$footer = defined($footer) ? "\n$footer" : '';
 	my $hdr = $mime->header_obj;
-	my $n = 0;
-	Plack::Util::inline_object(
-		close => sub {}, # noop
-		getline => sub {
-			my $nr = $n++;
-			if ($nr == 0) {
-				headers_to_html_header($hdr, $ctx) .
-					multipart_text_as_html($mime, '') .
-					'</pre><hr />'
-			} elsif ($nr == 1) {
-				'<pre>' .
-					html_footer($hdr, 1, $ctx) .
-					'</pre>' . msg_reply($ctx, $hdr) .
-					'<hr /><pre>'.  $footer .
-					'</pre></body></html>'
-			} else {
-				undef
-			}
+	my $tip = _msg_html_prepare($hdr, $ctx);
+	PublicInbox::WwwStream->new($ctx, sub {
+		my ($nr, undef) = @_;
+		if ($nr == 1) {
+			$tip . multipart_text_as_html($mime, '') .
+				'</pre><hr />'
+		} elsif ($nr == 2) {
+			'<pre>' . html_footer($hdr, 1, $ctx) .
+			'</pre>' . msg_reply($ctx, $hdr) . '<hr />'
+		} else {
+			undef
 		}
-	)
+	});
 }
 
 # /$INBOX/$MESSAGE_ID/#R
@@ -318,18 +310,15 @@ sub add_text_body {
 	$s;
 }
 
-sub headers_to_html_header {
+sub _msg_html_prepare {
 	my ($hdr, $ctx) = @_;
 	my $srch = $ctx->{srch} if $ctx;
 	my $atom = '';
-	my $rv = '';
-	my $upfx = '';
+	my $rv = "<pre\nid=b>"; # anchor for body start
 
 	if ($srch) {
-		$atom = qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
-			qq!href="${upfx}t.atom"\ntype="application/atom+xml"/>!;
+		$ctx->{-upfx} = '../';
 	}
-
 	my @title;
 	my $mid = $hdr->header_raw('Message-ID');
 	$mid = PublicInbox::Hval->new_msgid($mid);
@@ -352,15 +341,11 @@ sub headers_to_html_header {
 		$rv .= "$h: " . $v->as_html . "\n";
 
 	}
+	$ctx->{-title_html} = join(' - ', @title);
 	$rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
-	$rv .= "(<a\nhref=\"${upfx}raw\">raw</a>)\n";
+	$rv .= "(<a\nhref=\"raw\">raw</a>)\n";
 	$rv .= _parent_headers($hdr, $srch);
 	$rv .= "\n";
-
-	("<html><head><title>".  join(' - ', @title) . "</title>$atom".
-	 PublicInbox::Hval::STYLE .
-	 "</head><body><pre\nid=b>" . # anchor for body start
-	 $rv);
 }
 
 sub thread_skel {
diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm
new file mode 100644
index 0000000..62a4fe2
--- /dev/null
+++ b/lib/PublicInbox/WwwStream.pm
@@ -0,0 +1,88 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# HTML body stream for which yields getline+close methods
+package PublicInbox::WwwStream;
+use strict;
+use warnings;
+use PublicInbox::Hval qw(ascii_html);
+use URI;
+use constant PI_URL => 'https://public-inbox.org/README.html';
+
+sub new {
+	my ($class, $ctx, $cb) = @_;
+	bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
+}
+
+sub _html_top ($) {
+	my ($self) = @_;
+	my $ctx = $self->{ctx};
+	my $obj = $ctx->{-inbox};
+	my $desc = ascii_html($obj->description);
+	my $title = $ctx->{-title_html} || $desc;
+	my $upfx = $ctx->{-upfx} || '';
+	my $atom = $ctx->{-atom} || $upfx.'new.atom';
+	my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
+	if ($obj->search) {
+		$top = qq{<form\naction="$upfx"><pre>$top} .
+			  qq{ <input\nname=q\ntype=text />} .
+			  qq{<input\ntype=submit\nvalue=search />} .
+			  q{</pre></form>}
+	} else {
+		$top = '<pre>' . $top . '</pre>';
+	}
+	"<html><head><title>$title</title>" .
+		"<link\nrel=alternate\ntitle=\"Atom feed\"\n".
+		"href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
+		PublicInbox::Hval::STYLE .
+		"</head><body>$top";
+}
+
+sub _html_end {
+	my ($self) = @_;
+	my $urls = 'Archives are clone-able:';
+	my $ctx = $self->{ctx};
+	my $obj = $ctx->{-inbox};
+	my $desc = ascii_html($obj->description);
+	my @urls = @{$obj->cloneurl};
+	my %seen = map { $_ => 1 } @urls;
+
+	# FIXME: cleanup
+	my $env = $ctx->{env};
+	my $scheme = $env->{'psgi.url_scheme'};
+	my $host_port = $env->{HTTP_HOST} ||
+			"$env->{SERVER_NAME}:$env->{SERVER_PORT}";
+	my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
+	$http = URI->new($http . $obj->{name})->canonical->as_string;
+	$seen{$http} or unshift @urls, $http;
+	if (scalar(@urls) == 1) {
+		$urls .= " git clone --mirror $urls[0]";
+	} else {
+		$urls .= "\n" .
+			join("\n", map { "\tgit clone --mirror $_" } @urls);
+	}
+	my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
+	'<pre>'.join("\n",
+		'- ' . $desc,
+		$urls,
+		'served with software from public-inbox: '
+			."<a\nhref=\"$url\">$url</a>",
+	).'</pre></body></html>';
+}
+
+sub getline {
+	my ($self) = @_;
+	my $nr = $self->{nr}++;
+
+	return _html_top($self) if $nr == 0;
+
+	if (my $mid = $self->{cb}) { # middle
+		$mid = $mid->($nr, $self->{ctx}) and return $mid;
+	}
+
+	delete $self->{cb} ? _html_end($self) : undef;
+}
+
+sub close {}
+
+1;
diff --git a/t/view.t b/t/view.t
index 6c08599..4ce3c77 100644
--- a/t/view.t
+++ b/t/view.t
@@ -5,12 +5,24 @@ use warnings;
 use Test::More;
 use Email::MIME;
 use PublicInbox::View;
+use Plack::Util;
+
+# FIXME: make this test less fragile
+my $ctx = {
+	env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'http' },
+	-inbox => Plack::Util::inline_object(
+		name => 'test',
+		search => sub { undef },
+		cloneurl => sub {[]},
+		description => sub { '' }),
+};
+$ctx->{-inbox}->{-primary_address} = 'test@example.com';
 
 sub msg_html ($) {
 	my ($mime) = @_;
 
 	my $s = '';
-	my $body = PublicInbox::View::msg_html(undef, $mime);
+	my $body = PublicInbox::View::msg_html($ctx, $mime);
 	while (defined(my $buf = $body->getline)) {
 		$s .= $buf;
 	}

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

* [PATCH 3/3] view: minor tweaks to reduce long lines
  2016-06-18  4:47 [PATCH 0/3] introduce WwwStream for per-message views Eric Wong
  2016-06-18  4:47 ` [PATCH 1/3] feed: split out top-of-page generation Eric Wong
  2016-06-18  4:47 ` [PATCH 2/3] view: introduce WwwStream interface Eric Wong
@ 2016-06-18  4:47 ` Eric Wong
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Wong @ 2016-06-18  4:47 UTC (permalink / raw)
  To: meta

Fold addressee fields to better delimit destinations,
reduce horizontal rule <hr /> to reduce scrolling,
and use spaces to indent "git send-email" example.
---
 lib/PublicInbox/View.pm | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index e7e387d..e6d30a8 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -34,7 +34,7 @@ sub msg_html {
 				'</pre><hr />'
 		} elsif ($nr == 2) {
 			'<pre>' . html_footer($hdr, 1, $ctx) .
-			'</pre>' . msg_reply($ctx, $hdr) . '<hr />'
+			'</pre>' . msg_reply($ctx, $hdr);
 		} else {
 			undef
 		}
@@ -50,15 +50,15 @@ sub msg_reply {
 	my ($arg, $link) = mailto_arg_link($hdr);
 	push @$arg, '/path/to/YOUR_REPLY';
 
-	"<hr /><pre\nid=R>".
+	"<pre\nid=R>".
 	"You may reply publically to <a\nhref=#t>this message</a> via\n".
 	"plain-text email using any one of the following methods:\n\n" .
 	"* Save the following mbox file, import it into your mail client,\n" .
 	"  and reply-to-all from there: <a\nhref=raw>mbox</a>\n\n" .
 	"* Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,\n" .
 	"  and <b>--in-reply-to</b> switches of git-send-email(1):\n\n" .
-	"\tgit send-email \\\n\t\t" .
-	join(" \\\n\t\t", @$arg ). "\n\n" .
+	"  git send-email \\\n    " .
+	join(" \\\n    ", @$arg ). "\n\n" .
 	qq(  <a\nhref="$se_url">$se_url</a>\n\n) .
 	"* If your mail client supports setting the <b>In-Reply-To</b>" .
 	" header\n  via mailto: links, try the " .
@@ -338,7 +338,9 @@ sub _msg_html_prepare {
 				next;
 			}
 		}
-		$rv .= "$h: " . $v->as_html . "\n";
+		$v = $v->as_html;
+		$v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
+		$rv .= "$h: $v\n";
 
 	}
 	$ctx->{-title_html} = join(' - ', @title);
@@ -420,7 +422,7 @@ sub _parent_headers {
 		}
 
 		if (@refs) {
-			$rv .= 'References: '. join(' ', @refs) . "\n";
+			$rv .= 'References: '. join("\n\t", @refs) . "\n";
 		}
 	}
 	$rv;

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

end of thread, other threads:[~2016-06-18  4:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-18  4:47 [PATCH 0/3] introduce WwwStream for per-message views Eric Wong
2016-06-18  4:47 ` [PATCH 1/3] feed: split out top-of-page generation Eric Wong
2016-06-18  4:47 ` [PATCH 2/3] view: introduce WwwStream interface Eric Wong
2016-06-18  4:47 ` [PATCH 3/3] view: minor tweaks to reduce long lines 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).