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>";
37 my $links = "<a\nhref=\"$atom\">Atom feed</a>";
39 my $q_val = $ctx->{-q_value_html};
40 if (defined $q_val && $q_val ne '') {
41 $q_val = qq(\nvalue="$q_val");
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 />} .
51 qq{<input\ntype=submit\nvalue=search />} .
55 $top = '<pre>' . $top . "\n" . $links . '</pre>';
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;
66 my $urls = 'Archives are clonable:';
67 my $ctx = $self->{ctx};
68 my $obj = $ctx->{-inbox};
69 my $desc = ascii_html($obj->description);
71 my $http = $obj->base_url($ctx->{env});
73 my %seen = ( $http => 1 );
76 foreach my $u (@{$obj->cloneurl}) {
78 $need_tor = 1 if $u =~ m!\A[^:]+://\w+\.onion/!;
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);
90 $urls .= "\n note: .onion URLs require Tor: ";
91 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
93 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
94 '<hr><pre>'.join("\n\n",
97 'Code for this site: '.
98 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
99 ).'</pre></body></html>';
104 my $nr = $self->{nr}++;
106 return _html_top($self) if $nr == 0;
108 if (my $mid = $self->{cb}) { # middle
109 $mid = $mid->($nr, $self->{ctx}) and return $mid;
112 delete $self->{cb} ? _html_end($self) : undef;