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 $atom = $ctx->{-atom} || $upfx.'new.atom';
35 my $tip = $ctx->{-html_tip} || '';
36 my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
38 my $q_val = $ctx->{-q_value_html};
39 if (defined $q_val && $q_val ne '') {
40 $q_val = qq(\nvalue="$q_val" );
44 # XXX gross, for SearchView.pm
45 my $extra = $ctx->{-extra_form_html} || '';
46 my $action = $upfx eq '' ? './' : $upfx;
47 $top = qq{<form\naction="$action"><pre>$top} .
48 qq{ <input\nname=q\ntype=text$q_val/>} .
50 qq{<input\ntype=submit\nvalue=search />} .
53 $top = '<pre>' . $top . '</pre>';
55 "<html><head><title>$title</title>" .
56 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
57 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
58 PublicInbox::Hval::STYLE .
59 "</head><body>". $top . $tip;
64 my $urls = 'Archives are clonable:';
65 my $ctx = $self->{ctx};
66 my $obj = $ctx->{-inbox};
67 my $desc = ascii_html($obj->description);
69 my $http = $obj->base_url($ctx->{env});
71 my %seen = ( $http => 1 );
74 foreach my $u (@{$obj->cloneurl}) {
76 $need_tor = 1 if $u =~ m!\A[^:]+://\w+\.onion/!;
78 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
80 if (scalar(@urls) == 1) {
81 $urls .= " git clone --mirror $http";
84 join("\n", map { "\tgit clone --mirror $_" } @urls);
88 $urls .= "\n note: .onion URLs require Tor: ";
89 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
91 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
92 '<hr><pre>'.join("\n\n",
95 'Code for this site: '.
96 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
97 ).'</pre></body></html>';
102 my $nr = $self->{nr}++;
104 return _html_top($self) if $nr == 0;
106 if (my $mid = $self->{cb}) { # middle
107 $mid = $mid->($nr, $self->{ctx}) and return $mid;
110 delete $self->{cb} ? _html_end($self) : undef;