]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
view: introduce WwwStream interface
[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 $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
26         if ($obj->search) {
27                 $top = qq{<form\naction="$upfx"><pre>$top} .
28                           qq{ <input\nname=q\ntype=text />} .
29                           qq{<input\ntype=submit\nvalue=search />} .
30                           q{</pre></form>}
31         } else {
32                 $top = '<pre>' . $top . '</pre>';
33         }
34         "<html><head><title>$title</title>" .
35                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
36                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
37                 PublicInbox::Hval::STYLE .
38                 "</head><body>$top";
39 }
40
41 sub _html_end {
42         my ($self) = @_;
43         my $urls = 'Archives are clone-able:';
44         my $ctx = $self->{ctx};
45         my $obj = $ctx->{-inbox};
46         my $desc = ascii_html($obj->description);
47         my @urls = @{$obj->cloneurl};
48         my %seen = map { $_ => 1 } @urls;
49
50         # FIXME: cleanup
51         my $env = $ctx->{env};
52         my $scheme = $env->{'psgi.url_scheme'};
53         my $host_port = $env->{HTTP_HOST} ||
54                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
55         my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
56         $http = URI->new($http . $obj->{name})->canonical->as_string;
57         $seen{$http} or unshift @urls, $http;
58         if (scalar(@urls) == 1) {
59                 $urls .= " git clone --mirror $urls[0]";
60         } else {
61                 $urls .= "\n" .
62                         join("\n", map { "\tgit clone --mirror $_" } @urls);
63         }
64         my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
65         '<pre>'.join("\n",
66                 '- ' . $desc,
67                 $urls,
68                 'served with software from public-inbox: '
69                         ."<a\nhref=\"$url\">$url</a>",
70         ).'</pre></body></html>';
71 }
72
73 sub getline {
74         my ($self) = @_;
75         my $nr = $self->{nr}++;
76
77         return _html_top($self) if $nr == 0;
78
79         if (my $mid = $self->{cb}) { # middle
80                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
81         }
82
83         delete $self->{cb} ? _html_end($self) : undef;
84 }
85
86 sub close {}
87
88 1;