5 no warnings 'qw'; # you know what you doing
6 no warnings 'uninitialized'; # save some useless checks for more legible code
7 use open ':std' => ':utf8';
11 use Encode qw( decode_utf8 );
14 my ($html, $debug) = @_;
15 ref $html eq 'ARRAY' or $html = [$html];
16 my ($title, @lines) = @{$html};
17 $body = "<h2>$title</h2>";
18 $body .= "\n<p>$_</p>" for @lines;
19 $body .= "\n<pre>$debug</pre>" if $Dev and $debug;
20 say "<div class=error>$body</div>\n";
25 our $Time = [Time::HiRes::gettimeofday()];
28 my ($text, $html) = @_;
30 unless ($PLP::sentheaders and $PLP::sentheaders->[0] !~ m{/PLP\.pm$}) {
32 say '<h1>Page unavailable</h1>';
34 Alert("<strong>Fatal error</strong>: $html.");
40 our $Dev = $ENV{HTTP_HOST} =~ /\bdev\./;
41 our ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
44 our $Request = decode_utf8($ENV{PATH_INFO} =~ s{^/}{}r);
47 our $showkeys = !exists $get{keys} ? undef :
48 ($get{keys} ne '0' && ($get{keys} || 'always'));
50 $header{content_type} = 'text/html; charset=utf-8';
54 my @avail = ref $avail eq 'ARRAY' ? @{$avail} : $avail or return;
55 my %styles = map {$_ => $_} @avail;
57 if (defined( my $setstyle = $get{style} )) {
58 $style = $styles{ $setstyle };
61 my $cookie = CGI::Cookie->new(
63 -value => $setstyle || '',
64 -path => '/', # site-wide
65 -expires => $setstyle ? '+5y' : '-1d',
66 ) or die "empty object returned\n";
67 AddCookie($cookie->as_string);
68 } or warn "Unable to create style cookie: $@";
71 $style ||= exists $cookie{style} && $styles{ $cookie{style} } || $avail[0];
74 '<link rel="%s" type="text/css" media="all" href="%s" title="%s">',
75 $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css?1.11", $_
82 my $mod = stat $_ or next;
83 $mod = $mod->mtime or next;
84 $lastmod = $mod if $mod gt $lastmod;
87 for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) {
88 next if str2time($_) < $lastmod;
89 $header{status} = '304 Same old';
93 $header{'Last-Modified'} = time2str($lastmod);
99 unless ($meta->{nocache}) {
100 # announce and check data modification
102 $ENV{SCRIPT_FILENAME},
103 (grep { /\bShiar_/ } values %INC),
104 $meta->{data} ? @{ $meta->{data} } : (),
106 $header{'Cache-Control'} = 'max-age='.(24*60*60);
110 $meta->{stylesheet} ||= [qw( light dark circus mono red )];
111 $meta->{charset} ||= 'utf-8';
113 # convert options to arrays
114 ref $_ eq 'ARRAY' or $_ = [$_]
115 for grep {$_} $meta->{raw}, $meta->{description}, $meta->{keywords};
117 # document headers before output
118 $header{content_type} = "text/html; charset=$meta->{charset}"
119 unless $PLP::sentheaders;
120 unshift @{ $meta->{raw} }, stylesheet($meta->{stylesheet});
122 push @{ $meta->{raw} }, (
123 '<link rel="stylesheet" type="text/css" media="monochrome" href="/mono.css?1.11" title="light">',
127 push @{ $meta->{raw} }, (
128 '<!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->',
129 '<!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]-->',
130 !$showkeys ? '<style> .no {visibility:hidden} </style>' :
131 $showkeys eq 'ghost' ? '<style> .no, .alias {opacity:.5} </style>' : (),
132 '<script type="text/javascript" src="/keys.js?1.6" async></script>',
137 say '<!DOCTYPE html>';
138 say '<html lang="en">';
141 say sprintf '<meta http-equiv="content-type" content="%s">', $_
142 for $header{content_type};
143 say sprintf '<title>%s</title>', $meta->{title};
144 say sprintf '<meta name="description" content="%s">', EscapeHTML($_)
145 for join(' ', @{ $meta->{description} }) || ();
146 say sprintf '<meta name="keywords" content="%s">', EscapeHTML($_)
147 for join(', ', @{ $meta->{keywords} }) || ();
148 say '<meta name="viewport" content="width=device-width, initial-scale=1">';
149 say '<link rel="icon" type="image/png" href="/clip.png">';
150 say for map { @{$_} } $meta->{raw} || ();
151 say '<meta name="robots" content="noindex">' if $Dev;
154 say sprintf '<body id="%s">', $file;
156 # development version indicator
157 printf '<p style="%s">beta</p>', join('; ',
161 'border: 1ex solid red',
162 'border-width: 1ex 0',
164 'background: inherit',
168 # prepare trailing output
172 <a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
173 rel="source" title="Written in Perl">plp</a>
174 version <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
175 rel="vcs-git" title="Git repository">$meta->{version}</a>
176 created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
177 <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html"
178 title="Licensed under the GNU Affero General Public License, version 3"
179 rel="license">AGPLv3</a>
181 say sprintf '• %.3fs', Time::HiRes::tv_interval($Time) if $Dev and $Time;
189 my ($title, $href, $selected) = @_;
192 $selected ? '<strong>%s</strong>' : '<a href="%2$s">%s</a>',
193 EscapeHTML($title), EscapeHTML($href)