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 $tip = $ctx->{-html_tip} || '';
26 my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
28 my $q_val = $ctx->{-q_value_html};
29 if (defined $q_val && $q_val ne '') {
30 $q_val = qq(\nvalue="$q_val" );
34 # XXX gross, for SearchView.pm
35 my $extra = $ctx->{-extra_form_html} || '';
36 $top = qq{<form\naction="$upfx"><pre>$top} .
37 qq{ <input\nname=q\ntype=text$q_val/>} .
39 qq{<input\ntype=submit\nvalue=search />} .
42 $top = '<pre>' . $top . '</pre>';
44 "<html><head><title>$title</title>" .
45 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
46 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
47 PublicInbox::Hval::STYLE .
48 "</head><body>". $top . $tip;
53 my $urls = 'Archives are clone-able:';
54 my $ctx = $self->{ctx};
55 my $obj = $ctx->{-inbox};
56 my $desc = ascii_html($obj->description);
59 my $env = $ctx->{env};
60 my $scheme = $env->{'psgi.url_scheme'};
61 my $host_port = $env->{HTTP_HOST} ||
62 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
63 my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
64 $http = URI->new($http . $obj->{name})->canonical->as_string;
65 my %seen = ( $http => 1 );
67 foreach my $u (@{$obj->cloneurl}) {
70 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
72 if (scalar(@urls) == 1) {
73 $urls .= " git clone --mirror $http";
76 join("\n", map { "\tgit clone --mirror $_" } @urls);
78 my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
82 'served with software from public-inbox: '
83 ."<a\nhref=\"$url\">$url</a>",
84 ).'</pre></body></html>';
89 my $nr = $self->{nr}++;
91 return _html_top($self) if $nr == 0;
93 if (my $mid = $self->{cb}) { # middle
94 $mid = $mid->($nr, $self->{ctx}) and return $mid;
97 delete $self->{cb} ? _html_end($self) : undef;