]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: implement generic help text
[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 $help = $upfx.'_/text/help';
35         my $atom = $ctx->{-atom} || $upfx.'new.atom';
36         my $tip = $ctx->{-html_tip} || '';
37         my $top = "<b>$desc</b>";
38         my $links = "<a\nhref=\"$help\">help</a> / ".
39                         "<a\nhref=\"$atom\">Atom feed</a>";
40         if ($obj->search) {
41                 my $q_val = $ctx->{-q_value_html};
42                 if (defined $q_val && $q_val ne '') {
43                         $q_val = qq(\nvalue="$q_val");
44                 } else {
45                         $q_val = '';
46                 }
47                 # XXX gross, for SearchView.pm
48                 my $extra = $ctx->{-extra_form_html} || '';
49                 my $action = $upfx eq '' ? './' : $upfx;
50                 $top = qq{<form\naction="$action"><pre>$top} .
51                           qq{\n<input\nname=q\ntype=text$q_val />} .
52                           $extra .
53                           qq{<input\ntype=submit\nvalue=search />} .
54                           ' ' . $links .
55                           q{</pre></form>}
56         } else {
57                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
58         }
59         "<html><head><title>$title</title>" .
60                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
61                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
62                 PublicInbox::Hval::STYLE .
63                 "</head><body>". $top . $tip;
64 }
65
66 sub _html_end {
67         my ($self) = @_;
68         my $urls = 'Archives are clonable:';
69         my $ctx = $self->{ctx};
70         my $obj = $ctx->{-inbox};
71         my $desc = ascii_html($obj->description);
72
73         my $http = $obj->base_url($ctx->{env});
74         chop $http;
75         my %seen = ( $http => 1 );
76         my @urls = ($http);
77         foreach my $u (@{$obj->cloneurl}) {
78                 next if $seen{$u};
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         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
90         if (@nntp) {
91                 $urls .= "\n\n";
92                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
93                 $urls .= ' available over NNTP:';
94                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
95         }
96         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
97                 $urls .= "\n note: .onion URLs require Tor: ";
98                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
99         }
100         my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
101         '<hr><pre>'.join("\n\n",
102                 $desc,
103                 $urls,
104                 'Code for this site: '.
105                 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
106         ).'</pre></body></html>';
107 }
108
109 sub getline {
110         my ($self) = @_;
111         my $nr = $self->{nr}++;
112
113         return _html_top($self) if $nr == 0;
114
115         if (my $mid = $self->{cb}) { # middle
116                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
117         }
118
119         delete $self->{cb} ? _html_end($self) : undef;
120 }
121
122 1;