Source of source.plp

<(common.inc.plp)><:

my $source = $Request;
my $incname = qr{ [a-z][/a-z0-9_.-]* \.(?:plp?|css|js|txt) }x;

if ($source =~ s{(?<=\Q.inc.pl\E)/jsonp?$}{} and -r $source) {
	# convert perl include to json construct
	checkmodified($source);
	eval {
		my $data = do $source or die $@ || $! || 'read error';
		require JSON;
		my $converter = JSON->new;
		$converter->indent->space_after->canonical;

		$header{content_type} = 'application/json';
		$header{'Access-Control-Allow-Origin'} = '*';
		$header{content_type} = 'text/plain' if exists $get{debug};
		print $_, '(' for $get{callback} // ();
		print $converter->encode($data);
		print     ')' for $get{callback} // ();
		return 1;
	} or do {
		$header{status} = '500 File unavailable';
		$header{content_type} = 'text/plain';
		print "Conversion failed: $@";
	};
	exit;
}

Html({
	title => "$source source code",
	version => '1.4',
	description => !$source ? 'Index of source files for this site.' : [
		"Source code of the $source file at this site,",
		"with syntax highlighted and references linked."
	],
	keywords => [qw'
		sheet cheat source code perl plp html agpl
	'],
	stylesheet => [qw'light dark mono red'],
	data => [$source =~ m{\A($incname)\z}],
});

say '';

if (not $source or -d $source) {
	PLP_START {
		print "<h1>Source files</h1>";
	};

	if ($source and $source ne 'tools') {
		Abort("Directory index not permitted", '403 source not allowed');
	}

	print "<p>Project code distributed under the AGPL. Please contribute back.</p>";
	say '<ul>';
	for (glob($source ? "$source/*" : '*.plp')) {
		say '<li>', showlink($_, "/source/$_");
	}
	say "</ul>\n";
}
else {
	my $href = showlink($source, $source =~ m{\A (\w+) \.plp \z}x && "/$1");
	PLP_START {
		say "<h1>Source of $href</h1>";
	};

	my $path = $source;
	if ($source =~ m{(?:/|^)\.}) {
		Abort("File request not permitted", '403 source not allowed');
	}
	elsif ($source =~ s{::}{/}g or !-e $source) {
		$source .= '.pm';
		for (0 .. $#INC) {
			-e ($_ = "$INC[$_]/$source") or next;
			$path = $_;
			last;
		}
	}
	-r $path or Abort("Requested file not found", '404 source not found');
	my $size = (stat $path)->[7];

	my $cachefile = "source/$source.html";
	if (-e $cachefile and (stat $cachefile)->[9] >= (stat $path)->[9]) {
		say '<pre>';
		print decode_utf8(ReadFile($cachefile));
		say '</pre>';
		exit;
	}
	-e or mkdir for $cachefile =~ s{[^/]+\z}{}r; # dirname
	open my $cache, '>', $cachefile
		or Alert("Could not save cache", "Opening $cachefile failed: $!");;

	if (my $hl = eval {
		$size < 32_768 or die 'large files take too long to parse';
		require Text::VimColor;
		Text::VimColor->VERSION(0.12)
			or die 'early versions are buggy under FastCGI';
		delete $Text::VimColor::SYNTAX_TYPE{Underlined};
		return Text::VimColor->new(
			file => $path,
			vim_options => [@Text::VimColor::VIM_OPTIONS,
				'+:set enc=utf-8',
				'+:let perl_sub_signatures=1',
			],
		)->marked;
	}) {
		my %TYPETAG = (
			Statement => 'strong',
			Error     => 'em',
			Todo      => 'em',
			PreProc   => 'strong',
		);

		say '<pre>';
		foreach (@{$hl}) {
			my ($type, $contents) = @{$_};
			$contents = decode_utf8($contents);
			my $tag = $type && ($TYPETAG{$type} || 'span');
			my $line = Text::VimColor::_xml_escape($contents);

			# link other page sources, stylesheets, and javascript
			$line =~ s{ ^(['"]?) \K ($incname) (?=\1$) }{ showlink($2, "/source/$2") }xe
				if !$type || $type eq 'Constant';
			# link relative page locations in html output
			$line =~ s{ ^(&quot;)\K (/\w{2,}) (?= (?:/\w+)* \1$) }{ showlink($2, "/source$2.plp") }xe
				if $type && $type eq 'Constant';
			# link perl module names (Xx::Xx...)
			$line =~ s{ ^\s* \K ([A-Z]\w+(?:::\w+)+) (?![^;\s]) }{ showlink($1, "/source/$1") }xe
				if !$type;
			# link generator scripts (by tools/...)
			$line =~ s{ ^.*? by\  \K (tools/\S+) }{ showlink($1, "/source/$1") }xe
				if $type && $type eq 'Comment';

			$line = qq(<$tag class="sy-\l$type">$line</$tag>) if $tag;
			print $line;
			print {$cache} $line if $cache;
		}
		say '</pre>';
	}
	else {
		say '<pre>';
		print EscapeHTML(decode_utf8(ReadFile($path)));
		say '</pre>';
	}

	say '';
}