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);
54 $header{content_type} = 'text/html; charset=utf-8';
57 my @avail = qw( light dark circus mono red );
58 my %styles = map {$_ => $_} @avail;
60 if (defined( my $setstyle = $get{style} )) {
61 $style = $styles{ $setstyle };
64 my $cookie = CGI::Cookie->new(
66 -value => $setstyle || '',
67 -path => '/', # site-wide
68 -expires => $setstyle ? '+5y' : '-1d',
69 ) or die "empty object returned\n";
70 AddCookie($cookie->as_string);
71 } or warn "Unable to create style cookie: $@";
74 my $setstyle = $style;
75 $style ||= $styles{$_} for $cookie{style} || ();
83 my $mod = stat $_ or next;
84 $mod = $mod->mtime or next;
85 $lastmod = $mod if $mod gt $lastmod;
88 for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) {
89 next if str2time($_) < $lastmod;
90 $header{status} = '304 Same old';
94 $header{'Last-Modified'} = time2str($lastmod);
100 open my $cache, '<:raw', "data/$filename.json"
101 or return do "./$filename.inc.pl"; # silent fallback to original code
104 return JSON::decode_json(readline $cache);
106 if ($@ or !@data or !$data[0]) {
107 die ['Table data not found', $@ || $!];
109 if (@data == 1 and ref $data[0] eq 'HASH' and not %{$data[0]}) {
110 die ['Table data missing'];
112 return wantarray ? @data : $data[0]; # list compatibility like do does
118 unless ($meta->{nocache}) {
119 # announce and check data modification
121 $ENV{SCRIPT_FILENAME},
122 (grep { /\bShiar_/ } values %INC),
123 $meta->{data} ? @{ $meta->{data} } : (),
125 $header{'Cache-Control'} = 'max-age='.(24*60*60);
129 $meta->{charset} ||= 'utf-8';
130 $meta->{lang} ||= 'en';
132 # convert options to arrays
133 ref $_ eq 'ARRAY' or $_ = [$_]
134 for grep {$_} $meta->{raw}, $meta->{description}, $meta->{keywords};
136 # document headers before output
137 $header{content_type} = "text/html; charset=$meta->{charset}"
138 unless $PLP::sentheaders;
139 exit if $ENV{REQUEST_METHOD} eq 'HEAD';
141 unshift @{ $meta->{raw} }, (
142 '<link rel="stylesheet" type="text/css" media="all" href="/light.css?1.20">',
144 $meta->{stylesheet} = stylesheet();
146 if (my $img = $meta->{image}) {
147 my $proto = sprintf('http%s://', !!$ENV{HTTPS} && 's');
148 my $url = "$proto$ENV{HTTP_HOST}/$img";
149 push @{ $meta->{raw} }, (
150 qq(<meta property="og:image" content="$url" />),
154 my ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
156 $meta->{canonical} //= "/$file" . ($Request ne '' && "/$Request");
157 if (my $url = $meta->{canonical}) {
158 $url = "https://sheet.shiar.nl$url";
159 push @{ $meta->{raw} }, qq(<link rel="canonical" href="$url" />);
164 say '<!DOCTYPE html>';
166 $rootattr .= qq( class="s-$_") for $meta->{stylesheet} || ();
167 say qq(<html lang="$meta->{lang}"$rootattr>);
170 say sprintf '<meta http-equiv="content-type" content="%s">', $_
171 for $header{content_type};
172 say sprintf '<title>%s</title>', $meta->{title};
173 say sprintf '<meta name="description" content="%s">', EscapeHTML($_)
174 for join(' ', @{ $meta->{description} // [] }) || ();
175 say sprintf '<meta name="keywords" content="%s">', EscapeHTML($_)
176 for join(', ', @{ $meta->{keywords} // [] }) || ();
177 say '<meta name="viewport" content="width=device-width, initial-scale=1">';
178 say '<link rel="icon" type="image/png" href="/clip.png">';
179 say for map { @{$_} } $meta->{raw} || ();
180 say '<meta name="robots" content="noindex">' if $Dev;
181 say '<script src="/prefer.js"></script>';
184 say sprintf '<body id="%s">', $file;
186 # development version indicator
187 printf '<p style="%s">beta</p>', join('; ',
191 'border: 1ex solid red',
192 'border-width: 1ex 0',
194 'background: inherit',
198 # prepare trailing output
202 <a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
203 rel="source" title="Written in Perl">plp</a>
204 version <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
205 rel="vcs-git" title="Git repository">$meta->{version}</a>
206 created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
207 <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html"
208 title="Licensed under the GNU Affero General Public License, version 3"
209 rel="license">AGPLv3</a>
212 say sprintf '• %.3fs', Time::HiRes::tv_interval($Time) if $Dev and $Time;
221 my ($message, $html) = @_;
223 warn join ': ', @{$message};
224 $html = shift @{$message};
230 unless ($PLP::sentheaders) {
231 Html({nocache => 1});
232 say '<h1>Page unavailable</h1>';
234 Alert("Fatal error: $html.", @{$message});
239 my ($title, $href, $selected) = @_;
241 return $title if not $href;
242 return "<strong>$title</strong>" if $selected;
243 return sprintf '<a href="%s">%s</a>', EscapeHTML($href), $title;