1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # HTML body stream for which yields getline+close methods
5 package PublicInbox::WwwStream;
8 use PublicInbox::Hval qw(ascii_html);
10 our $TOR_URL = 'https://www.torproject.org/';
11 our $CODE_URL = 'https://public-inbox.org/';
12 our $PROJECT = 'public-inbox';
17 my ($class, $ctx, $cb) = @_;
18 bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
22 my ($class, $ctx, $code, $cb) = @_;
23 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
24 $class->new($ctx, $cb) ]
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>";
41 my $q_val = $ctx->{-q_value_html};
42 if (defined $q_val && $q_val ne '') {
43 $q_val = qq(\nvalue="$q_val");
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 />} .
53 qq{<input\ntype=submit\nvalue=search />} .
57 $top = '<pre>' . $top . "\n" . $links . '</pre>';
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;
68 my $urls = 'Archives are clonable:';
69 my $ctx = $self->{ctx};
70 my $obj = $ctx->{-inbox};
71 my $desc = ascii_html($obj->description);
73 my $http = $obj->base_url($ctx->{env});
75 my %seen = ( $http => 1 );
77 foreach my $u (@{$obj->cloneurl}) {
80 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
82 if (scalar(@urls) == 1) {
83 $urls .= " git clone --mirror $http";
86 join("\n", map { "\tgit clone --mirror $_" } @urls);
89 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
92 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
93 $urls .= ' available over NNTP:';
94 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
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>];
100 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
101 '<hr><pre>'.join("\n\n",
104 'Code for this site: '.
105 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
106 ).'</pre></body></html>';
111 my $nr = $self->{nr}++;
113 return _html_top($self) if $nr == 0;
115 if (my $mid = $self->{cb}) { # middle
116 $mid = $mid->($nr, $self->{ctx}) and return $mid;
119 delete $self->{cb} ? _html_end($self) : undef;