]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www_stream: linkify cloneurl entries if they're HTTP/HTTPS
[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
48         # FIXME: cleanup
49         my $env = $ctx->{env};
50         my $scheme = $env->{'psgi.url_scheme'};
51         my $host_port = $env->{HTTP_HOST} ||
52                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
53         my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
54         $http = URI->new($http . $obj->{name})->canonical->as_string;
55         my %seen = ( $http => 1 );
56         my @urls = ($http);
57         foreach my $u (@{$obj->cloneurl}) {
58                 next if $seen{$u};
59                 $seen{$u} = 1;
60                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
61         }
62         if (scalar(@urls) == 1) {
63                 $urls .= " git clone --mirror $http";
64         } else {
65                 $urls .= "\n" .
66                         join("\n", map { "\tgit clone --mirror $_" } @urls);
67         }
68         my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
69         '<pre>'.join("\n",
70                 '- ' . $desc,
71                 $urls,
72                 'served with software from public-inbox: '
73                         ."<a\nhref=\"$url\">$url</a>",
74         ).'</pre></body></html>';
75 }
76
77 sub getline {
78         my ($self) = @_;
79         my $nr = $self->{nr}++;
80
81         return _html_top($self) if $nr == 0;
82
83         if (my $mid = $self->{cb}) { # middle
84                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
85         }
86
87         delete $self->{cb} ? _html_end($self) : undef;
88 }
89
90 sub close {}
91
92 1;