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