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 use constant PI_URL => 'https://public-inbox.org/README.html';
13 my ($class, $ctx, $cb) = @_;
14 bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
19 my $ctx = $self->{ctx};
20 my $obj = $ctx->{-inbox};
21 my $desc = ascii_html($obj->description);
22 my $title = $ctx->{-title_html} || $desc;
23 my $upfx = $ctx->{-upfx} || '';
24 my $atom = $ctx->{-atom} || $upfx.'new.atom';
25 my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
27 $top = qq{<form\naction="$upfx"><pre>$top} .
28 qq{ <input\nname=q\ntype=text />} .
29 qq{<input\ntype=submit\nvalue=search />} .
32 $top = '<pre>' . $top . '</pre>';
34 "<html><head><title>$title</title>" .
35 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
36 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
37 PublicInbox::Hval::STYLE .
43 my $urls = 'Archives are clone-able:';
44 my $ctx = $self->{ctx};
45 my $obj = $ctx->{-inbox};
46 my $desc = ascii_html($obj->description);
49 my $env = $ctx->{env};
50 my $scheme = $env->{'psgi.url_scheme'};
51 my $host_port = $env->{HTTP_HOST} ||
52 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
53 my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
54 $http = URI->new($http . $obj->{name})->canonical->as_string;
55 my %seen = ( $http => 1 );
57 foreach my $u (@{$obj->cloneurl}) {
60 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
62 if (scalar(@urls) == 1) {
63 $urls .= " git clone --mirror $http";
66 join("\n", map { "\tgit clone --mirror $_" } @urls);
68 my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
72 'served with software from public-inbox: '
73 ."<a\nhref=\"$url\">$url</a>",
74 ).'</pre></body></html>';
79 my $nr = $self->{nr}++;
81 return _html_top($self) if $nr == 0;
83 if (my $mid = $self->{cb}) { # middle
84 $mid = $mid->($nr, $self->{ctx}) and return $mid;
87 delete $self->{cb} ? _html_end($self) : undef;