6 no warnings 'qw'; # you know what you doing
7 no warnings 'uninitialized'; # save some useless checks for more legible code
8 use open ':std' => ':utf8';
10 use File::stat 'stat';
12 use Encode qw( decode_utf8 );
17 my ($html, $debug) = @_;
18 ref $html eq 'ARRAY' or $html = [$html];
19 my ($title, @lines) = @{$html};
20 my $body = "<h2>$title</h2>";
21 $body .= "\n<p>$_</p>" for @lines;
22 $body .= "\n<pre>$debug</pre>" if $Dev and $debug;
23 say "<div class=error>$body</div>\n";
27 my ($html, $code, $debug) = @_;
28 unless ($PLP::sentheaders) {
29 $header{Status} = $code || 500;
32 ref $html eq 'ARRAY' or $html = [$html];
33 push @{$html}, "Also failed to set HTTP status <q>$code</q>"
42 our $Time = [Time::HiRes::gettimeofday()];
47 our $Dev = $ENV{HTTP_HOST} =~ /\bdev\./;
50 our $Request //= decode_utf8($ENV{PATH_INFO} =~ s{^/}{}r);
53 our $showkeys //= !exists $get{keys} ? undef :
54 ($get{keys} ne '0' && ($get{keys} || 'always'));
56 $header{content_type} = 'text/html; charset=utf-8';
60 my @avail = ref $avail eq 'ARRAY' ? @{$avail} : $avail or return;
61 my %styles = map {$_ => $_} @avail;
63 if (defined( my $setstyle = $get{style} )) {
64 $style = $styles{ $setstyle };
67 my $cookie = CGI::Cookie->new(
69 -value => $setstyle || '',
70 -path => '/', # site-wide
71 -expires => $setstyle ? '+5y' : '-1d',
72 ) or die "empty object returned\n";
73 AddCookie($cookie->as_string);
74 } or warn "Unable to create style cookie: $@";
77 $style ||= exists $cookie{style} && $styles{ $cookie{style} } || $avail[0];
80 '<link rel="%s" type="text/css" media="all" href="%s" title="%s">',
81 $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css?1.12", $_
88 my $mod = stat $_ or next;
89 $mod = $mod->mtime or next;
90 $lastmod = $mod if $mod gt $lastmod;
93 for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) {
94 next if str2time($_) < $lastmod;
95 $header{status} = '304 Same old';
99 $header{'Last-Modified'} = time2str($lastmod);
105 unless ($meta->{nocache}) {
106 # announce and check data modification
108 $ENV{SCRIPT_FILENAME},
109 (grep { /\bShiar_/ } values %INC),
110 $meta->{data} ? @{ $meta->{data} } : (),
112 $header{'Cache-Control'} = 'max-age='.(24*60*60);
116 $meta->{stylesheet} ||= [qw( light dark circus mono red )];
117 $meta->{charset} ||= 'utf-8';
119 # convert options to arrays
120 ref $_ eq 'ARRAY' or $_ = [$_]
121 for grep {$_} $meta->{raw}, $meta->{description}, $meta->{keywords};
123 # document headers before output
124 $header{content_type} = "text/html; charset=$meta->{charset}"
125 unless $PLP::sentheaders;
126 unshift @{ $meta->{raw} }, stylesheet($meta->{stylesheet});
128 push @{ $meta->{raw} }, (
129 '<link rel="stylesheet" type="text/css" media="monochrome" href="/mono.css?1.11" title="light">',
133 push @{ $meta->{raw} }, (
134 '<!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->',
135 '<!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]-->',
136 !$showkeys ? '<style> .no {visibility:hidden} </style>' :
137 $showkeys eq 'ghost' ? '<style> .no, .alias {opacity:.5} </style>' : (),
138 '<script type="text/javascript" src="/keys.js?1.6" async></script>',
141 my ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
145 say '<!DOCTYPE html>';
146 say '<html lang="en">';
149 say sprintf '<meta http-equiv="content-type" content="%s">', $_
150 for $header{content_type};
151 say sprintf '<title>%s</title>', $meta->{title};
152 say sprintf '<meta name="description" content="%s">', EscapeHTML($_)
153 for join(' ', @{ $meta->{description} // [] }) || ();
154 say sprintf '<meta name="keywords" content="%s">', EscapeHTML($_)
155 for join(', ', @{ $meta->{keywords} // [] }) || ();
156 say '<meta name="viewport" content="width=device-width, initial-scale=1">';
157 say '<link rel="icon" type="image/png" href="/clip.png">';
158 say for map { @{$_} } $meta->{raw} || ();
159 say '<meta name="robots" content="noindex">' if $Dev;
162 say sprintf '<body id="%s">', $file;
164 # development version indicator
165 printf '<p style="%s">beta</p>', join('; ',
169 'border: 1ex solid red',
170 'border-width: 1ex 0',
172 'background: inherit',
176 # prepare trailing output
180 <a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
181 rel="source" title="Written in Perl">plp</a>
182 version <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
183 rel="vcs-git" title="Git repository">$meta->{version}</a>
184 created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
185 <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html"
186 title="Licensed under the GNU Affero General Public License, version 3"
187 rel="license">AGPLv3</a>
190 say sprintf '• %.3fs', Time::HiRes::tv_interval($Time) if $Dev and $Time;
199 my ($text, $html) = @_;
201 unless ($PLP::sentheaders) {
202 Html({nocache => 1});
203 say '<h1>Page unavailable</h1>';
205 Alert("<strong>Fatal error</strong>: $html.");
210 my ($title, $href, $selected) = @_;
213 $selected ? '<strong>%s</strong>' : '<a href="%2$s">%s</a>',
214 EscapeHTML($title), EscapeHTML($href)