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;
18 my ($class, $ctx, $code, $cb) = @_;
19 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
20 $class->new($ctx, $cb) ]
25 my $ctx = $self->{ctx};
26 my $obj = $ctx->{-inbox};
27 my $desc = ascii_html($obj->description);
28 my $title = $ctx->{-title_html} || $desc;
29 my $upfx = $ctx->{-upfx} || '';
30 my $atom = $ctx->{-atom} || $upfx.'new.atom';
31 my $tip = $ctx->{-html_tip} || '';
32 my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
34 my $q_val = $ctx->{-q_value_html};
35 if (defined $q_val && $q_val ne '') {
36 $q_val = qq(\nvalue="$q_val" );
40 # XXX gross, for SearchView.pm
41 my $extra = $ctx->{-extra_form_html} || '';
42 $top = qq{<form\naction="$upfx"><pre>$top} .
43 qq{ <input\nname=q\ntype=text$q_val/>} .
45 qq{<input\ntype=submit\nvalue=search />} .
48 $top = '<pre>' . $top . '</pre>';
50 "<html><head><title>$title</title>" .
51 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
52 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
53 PublicInbox::Hval::STYLE .
54 "</head><body>". $top . $tip;
59 my $urls = 'Archives are clone-able:';
60 my $ctx = $self->{ctx};
61 my $obj = $ctx->{-inbox};
62 my $desc = ascii_html($obj->description);
65 my $env = $ctx->{env};
66 my $scheme = $env->{'psgi.url_scheme'};
67 my $host_port = $env->{HTTP_HOST} ||
68 "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
69 my $http = "$scheme://$host_port".($env->{SCRIPT_NAME} || '/');
70 $http = URI->new($http . $obj->{name})->canonical->as_string;
71 my %seen = ( $http => 1 );
73 foreach my $u (@{$obj->cloneurl}) {
76 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
78 if (scalar(@urls) == 1) {
79 $urls .= " git clone --mirror $http";
82 join("\n", map { "\tgit clone --mirror $_" } @urls);
84 my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
88 'served with software from public-inbox: '
89 ."<a\nhref=\"$url\">$url</a>",
90 ).'</pre></body></html>';
95 my $nr = $self->{nr}++;
97 return _html_top($self) if $nr == 0;
99 if (my $mid = $self->{cb}) { # middle
100 $mid = $mid->($nr, $self->{ctx}) and return $mid;
103 delete $self->{cb} ? _html_end($self) : undef;