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