]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
wwwstream: wording/grammar tweaks in trailer
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # HTML body stream for which yields getline+close methods
5 package PublicInbox::WwwStream;
6 use strict;
7 use warnings;
8 use PublicInbox::Hval qw(ascii_html);
9 use URI;
10 use constant PI_URL => 'https://public-inbox.org/';
11
12 sub new {
13         my ($class, $ctx, $cb) = @_;
14         bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
15 }
16
17 sub response {
18         my ($class, $ctx, $code, $cb) = @_;
19         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
20           $class->new($ctx, $cb) ]
21 }
22
23 sub _html_top ($) {
24         my ($self) = @_;
25         my $ctx = $self->{ctx};
26         my $obj = $ctx->{-inbox};
27         my $desc = ascii_html($obj->description);
28         my $title = $ctx->{-title_html} || $desc;
29         my $upfx = $ctx->{-upfx} || '';
30         my $atom = $ctx->{-atom} || $upfx.'new.atom';
31         my $tip = $ctx->{-html_tip} || '';
32         my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
33         if ($obj->search) {
34                 my $q_val = $ctx->{-q_value_html};
35                 if (defined $q_val && $q_val ne '') {
36                         $q_val = qq(\nvalue="$q_val" );
37                 } else {
38                         $q_val = '';
39                 }
40                 # XXX gross, for SearchView.pm
41                 my $extra = $ctx->{-extra_form_html} || '';
42                 my $action = $upfx eq '' ? './' : $upfx;
43                 $top = qq{<form\naction="$action"><pre>$top} .
44                           qq{ <input\nname=q\ntype=text$q_val/>} .
45                           $extra .
46                           qq{<input\ntype=submit\nvalue=search />} .
47                           q{</pre></form>}
48         } else {
49                 $top = '<pre>' . $top . '</pre>';
50         }
51         "<html><head><title>$title</title>" .
52                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
53                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
54                 PublicInbox::Hval::STYLE .
55                 "</head><body>". $top . $tip;
56 }
57
58 sub _html_end {
59         my ($self) = @_;
60         my $urls = 'Archives are clonable:';
61         my $ctx = $self->{ctx};
62         my $obj = $ctx->{-inbox};
63         my $desc = ascii_html($obj->description);
64
65         my $http = $obj->base_url($ctx->{env});
66         chop $http;
67         my %seen = ( $http => 1 );
68         my @urls = ($http);
69         foreach my $u (@{$obj->cloneurl}) {
70                 next if $seen{$u};
71                 $seen{$u} = 1;
72                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
73         }
74         if (scalar(@urls) == 1) {
75                 $urls .= " git clone --mirror $http";
76         } else {
77                 $urls .= "\n" .
78                         join("\n", map { "\tgit clone --mirror $_" } @urls);
79         }
80         my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
81         '<pre>'.join("\n",
82                 '- ' . $desc,
83                 $urls,
84                 'Served with public-inbox: '.
85                 qq(git clone <a\nhref="$url">$url</a> public-inbox),
86         ).'</pre></body></html>';
87 }
88
89 sub getline {
90         my ($self) = @_;
91         my $nr = $self->{nr}++;
92
93         return _html_top($self) if $nr == 0;
94
95         if (my $mid = $self->{cb}) { # middle
96                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
97         }
98
99         delete $self->{cb} ? _html_end($self) : undef;
100 }
101
102 sub close {}
103
104 1;