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