Source of common.inc.plp

<:
use 5.014;
use strict;
use utf8;
use warnings;
no  warnings 'qw';  # you know what you doing
no  warnings 'uninitialized';  # save some useless checks for more legible code
use open ':std' => ':utf8';

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};
	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()];

	push @INC, '.';

	# user request
	our $Dev = $ENV{HTTP_HOST} =~ /\bdev\./;
}

our $Request //= decode_utf8($ENV{PATH_INFO} =~ s{^/}{}r);

our $style;

$header{content_type} = 'text/html; charset=utf-8';

sub stylesheet {
	my @avail = qw( light dark circus mono red );
	my %styles = map {$_ => $_} @avail;

	if (defined( my $setstyle = $get{style} )) {
		$style = $styles{ $setstyle };
		eval {
			require CGI::Cookie;
			my $cookie = CGI::Cookie->new(
				-name    => 'style',
				-value   => $setstyle || '',
				-path    => '/',  # site-wide
				-expires => $setstyle ? '+5y' : '-1d',
			) or die "empty object returned\n";
			AddCookie($cookie->as_string);
		} or warn "Unable to create style cookie: $@";
	}

	my $setstyle = $style;
	$style ||= $styles{$_} for $cookie{style} || ();
	$style ||= $avail[0];
	return $setstyle;
}

sub checkmodified {
	my $lastmod = 0;
	for (@_) {
		my $mod = stat $_ or next;
		$mod = $mod->mtime or next;
		$lastmod = $mod if $mod gt $lastmod;
	}

	for ($ENV{HTTP_IF_MODIFIED_SINCE} || ()) {
		next if str2time($_) < $lastmod;
		$header{status} = '304 Same old';
		exit;
	}

	$header{'Last-Modified'} = time2str($lastmod);
}

sub Data {
	my ($filename) = @_;
	my @data = eval {
		!exists $get{f} and 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) = @_;

	unless ($meta->{nocache}) {
		# announce and check data modification
		checkmodified(
			$ENV{SCRIPT_FILENAME},
			(grep { /\bShiar_/ } values %INC),
			$meta->{data} ? @{ $meta->{data} } : (),
		);
		$header{'Cache-Control'} = 'max-age='.(24*60*60);
	}

	# default fallbacks
	$meta->{charset} ||= 'utf-8';
	$meta->{lang} ||= 'en';

	# convert options to arrays
	ref $_ eq 'ARRAY' or $_ = [$_]
		for grep {$_} $meta->{raw}, $meta->{description}, $meta->{keywords};

	# 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} }, (
		'<link rel="stylesheet" type="text/css" media="all" href="/light.css?1.20">',
	);
	$meta->{stylesheet} = stylesheet();

	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>';
		my $rootattr = '';
		$rootattr .= qq( class="s-$_") for $meta->{stylesheet} || ();
		say qq(<html lang="$meta->{lang}"$rootattr>);
		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 '<script src="/prefer.js"></script>';
		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 {
		print <<"EOT";
<p class="footer">
	<a href="/" rel="start">sheet.shiar.nl</a>/$file.<a href="/source/$file.plp"
	 rel="source" title="Written in Perl">plp</a>
	version <a href="http://git.shiar.nl/sheet.git/history/HEAD:/$file.plp"
	 rel="vcs-git" title="Git repository">$meta->{version}</a>
	created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
	<a href="http://www.fsf.org/licensing/licenses/agpl-3.0.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 '';
		say '</html>';
	};
}

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) = @_;
	EscapeHTML($title);
	return $title if not $href;
	return "<strong>$title</strong>" if $selected;
	return sprintf '<a href="%s">%s</a>', EscapeHTML($href), $title;
}