index: release v1.18 with only altgr index linked
[sheet.git] / common.inc.plp
index e3316fc439a00246e5454d0f64194b3a1fb460a6..a634733cba704de18de8e56605a78a796f8109c9 100644 (file)
@@ -1,5 +1,6 @@
 <:
 use 5.014;
+use strict;
 use utf8;
 use warnings;
 no  warnings 'qw';  # you know what you doing
@@ -10,42 +11,45 @@ use File::stat 'stat';
 use HTTP::Date;
 use Encode qw( decode_utf8 );
 
+our $Dev;
+
 sub Alert {
        my ($html, $debug) = @_;
        ref $html eq 'ARRAY' or $html = [$html];
        my ($title, @lines) = @{$html};
-       $body = "<h2>$title</h2>";
+       my $body = "<h2>$title</h2>";
        $body .= "\n<p>$_</p>" for @lines;
        $body .= "\n<pre>$debug</pre>" if $Dev and $debug;
        say "<div class=error>$body</div>\n";
 }
 
+sub Abort {
+       my ($html, $code, $debug) = @_;
+       unless ($PLP::sentheaders) {
+               $header{Status} = $code || 500;
+       }
+       elsif ($Dev) {
+               ref $html eq 'ARRAY' or $html = [$html];
+               push @{$html}, "Also failed to set HTTP status <q>$code</q>"
+                       . " after output!";
+       }
+       Alert($html, $debug);
+       exit;
+}
+
 BEGIN {
        require Time::HiRes;
        our $Time = [Time::HiRes::gettimeofday()];
 
-       $PLP::ERROR = sub {
-               my ($text, $html) = @_;
-               warn $text;
-               unless ($PLP::sentheaders and $PLP::sentheaders->[0] !~ m{/PLP\.pm$}) {
-                       Html({nocache => 1});
-                       say '<h1>Page unavailable</h1>';
-               }
-               Alert("<strong>Fatal error</strong>: $html.");
-       };
-
        push @INC, '.';
 
        # user request
        our $Dev = $ENV{HTTP_HOST} =~ /\bdev\./;
-       our ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
 }
 
-our $Request = decode_utf8($ENV{PATH_INFO} =~ s{^/}{}r);
+our $Request //= decode_utf8($ENV{PATH_INFO} =~ s{^/}{}r);
 
 our $style;
-our $showkeys = !exists $get{keys} ? undef :
-       ($get{keys} ne '0' && ($get{keys} || 'always'));
 
 $header{content_type} = 'text/html; charset=utf-8';
 
@@ -72,7 +76,7 @@ sub stylesheet {
 
        return map { sprintf(
                '<link rel="%s" type="text/css" media="all" href="%s" title="%s">',
-               $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css?1.11", $_
+               $_ eq $style ? 'stylesheet' : 'alternate stylesheet', "/$_.css?1.18", $_
        ) } @avail;
 }
 
@@ -93,6 +97,24 @@ sub checkmodified {
        $header{'Last-Modified'} = time2str($lastmod);
 }
 
+sub Data {
+       my ($filename) = @_;
+       my @data = eval {
+               open my $cache, '<:raw', "data/$filename.json"
+                       or return do "./$filename.inc.pl"; # silent fallback to original code
+               require JSON;
+               local $/; # slurp
+               return JSON::decode_json(readline $cache);
+       };
+       if ($@ or !@data or !$data[0]) {
+               die ['Table data not found', $@ || $!];
+       }
+       if (@data == 1 and ref $data[0] eq 'HASH' and not %{$data[0]}) {
+               die ['Table data missing'];
+       }
+       return wantarray ? @data : $data[0]; # list compatibility like do does
+}
+
 sub Html {
        my ($meta) = @_;
 
@@ -109,6 +131,7 @@ sub Html {
        # default fallbacks
        $meta->{stylesheet} ||= [qw( light dark circus mono red )];
        $meta->{charset} ||= 'utf-8';
+       $meta->{lang} ||= 'en';
 
        # convert options to arrays
        ref $_ eq 'ARRAY' or $_ = [$_]
@@ -117,51 +140,61 @@ sub Html {
        # document headers before output
        $header{content_type} = "text/html; charset=$meta->{charset}"
                unless $PLP::sentheaders;
+       exit if $ENV{REQUEST_METHOD} eq 'HEAD';
        unshift @{ $meta->{raw} }, stylesheet($meta->{stylesheet});
 
        push @{ $meta->{raw} }, (
                '<link rel="stylesheet" type="text/css" media="monochrome" href="/mono.css?1.11" title="light">',
        );
 
-       # optional amends
-       push @{ $meta->{raw} }, (
-               '<!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->',
-               '<!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]-->',
-               !$showkeys ? '<style> .no {visibility:hidden} </style>' :
-               $showkeys eq 'ghost' ? '<style> .no, .alias {opacity:.5} </style>' : (),
-               '<script type="text/javascript" src="/keys.js?1.6" async></script>',
-       ) if $meta->{keys};
-
-       # leading output
-       say '<!DOCTYPE html>';
-       say '<html lang="en">';
-       say '';
-       say '<head>';
-       say sprintf '<meta http-equiv="content-type" content="%s">', $_
-               for $header{content_type};
-       say sprintf '<title>%s</title>', $meta->{title};
-       say sprintf '<meta name="description" content="%s">', EscapeHTML($_)
-               for join(' ', @{ $meta->{description} }) || ();
-       say sprintf '<meta name="keywords" content="%s">', EscapeHTML($_)
-               for join(', ', @{ $meta->{keywords} }) || ();
-       say '<meta name="viewport" content="width=device-width, initial-scale=1">';
-       say '<link rel="icon" type="image/png" href="/clip.png">';
-       say for map { @{$_} } $meta->{raw} || ();
-       say '<meta name="robots" content="noindex">' if $Dev;
-       say '</head>';
-       say '';
-       say sprintf '<body id="%s">', $file;
-
-       # development version indicator
-       printf '<p style="%s">beta</p>', join('; ',
-               'position: fixed',
-               'right: 1em',
-               'opacity: .5',
-               'border: 1ex solid red',
-               'border-width: 1ex 0',
-               'z-index: 1',
-               'background: inherit',
-       ) if $Dev;
+       if (my $img = $meta->{image}) {
+               my $proto = sprintf('http%s://', !!$ENV{HTTPS} && 's');
+               my $url = "$proto$ENV{HTTP_HOST}/$img";
+               push @{ $meta->{raw} }, (
+                       qq(<meta property="og:image" content="$url" />),
+               );
+       }
+
+       my ($file) = $ENV{SCRIPT_FILENAME} =~ m{ ([^/]+) \.plp$ }x;
+
+       $meta->{canonical} //= "/$file" . ($Request ne '' && "/$Request");
+       if (my $url = $meta->{canonical}) {
+               $url = "https://sheet.shiar.nl$url";
+               push @{ $meta->{raw} }, qq(<link rel="canonical" href="$url" />);
+       }
+
+       PLP_START {
+               # leading output
+               say '<!DOCTYPE html>';
+               say qq(<html lang="$meta->{lang}">);
+               say '';
+               say '<head>';
+               say sprintf '<meta http-equiv="content-type" content="%s">', $_
+                       for $header{content_type};
+               say sprintf '<title>%s</title>', $meta->{title};
+               say sprintf '<meta name="description" content="%s">', EscapeHTML($_)
+                       for join(' ', @{ $meta->{description} // [] }) || ();
+               say sprintf '<meta name="keywords" content="%s">', EscapeHTML($_)
+                       for join(', ', @{ $meta->{keywords} // [] }) || ();
+               say '<meta name="viewport" content="width=device-width, initial-scale=1">';
+               say '<link rel="icon" type="image/png" href="/clip.png">';
+               say for map { @{$_} } $meta->{raw} || ();
+               say '<meta name="robots" content="noindex">' if $Dev;
+               say '</head>';
+               say '';
+               say sprintf '<body id="%s">', $file;
+
+               # development version indicator
+               printf '<p style="%s">beta</p>', join('; ',
+                       'position: fixed',
+                       'right: 1em',
+                       'opacity: .5',
+                       'border: 1ex solid red',
+                       'border-width: 1ex 0',
+                       'z-index: 1',
+                       'background: inherit',
+               ) if $Dev;
+       };
 
        # prepare trailing output
        PLP_END {
@@ -176,6 +209,7 @@ sub Html {
         title="Licensed under the GNU Affero General Public License, version 3"
         rel="license">AGPLv3</a>
 EOT
+               our $Time;
                say sprintf '• %.3fs', Time::HiRes::tv_interval($Time) if $Dev and $Time;
                say '</p>';
                say '';
@@ -183,12 +217,30 @@ EOT
        };
 }
 
+BEGIN {
+       $PLP::ERROR = sub {
+               my ($message, $html) = @_;
+               if (ref $message) {
+                       warn join ': ', @{$message};
+                       $html = shift @{$message};
+               }
+               else {
+                       warn $message;
+                       $message = [];
+               }
+               unless ($PLP::sentheaders) {
+                       Html({nocache => 1});
+                       say '<h1>Page unavailable</h1>';
+               }
+               Alert("Fatal error: $html.", @{$message});
+       };
+}
+
 sub showlink {
        my ($title, $href, $selected) = @_;
-       return sprintf(
-               !$href ? '%s' :
-               $selected ? '<strong>%s</strong>' : '<a href="%2$s">%s</a>',
-               EscapeHTML($title), EscapeHTML($href)
-       );
+       EscapeHTML($title);
+       return $title if not $href;
+       return "<strong>$title</strong>" if $selected;
+       return sprintf '<a href="%s">%s</a>', EscapeHTML($href), $title;
 }