From 598285f72ae22bca87d53a29651e7110a253b25b Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 25 Nov 2021 00:48:49 +0100 Subject: [PATCH] source: cache syntax highlighted html Massive speedup on repeated requests since Text::VimColor is so slow (but the only readily available option for plp syntax). --- .gitignore | 5 ++++- source.plp | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 142f46a..8815520 100644 --- a/.gitignore +++ b/.gitignore @@ -2,12 +2,15 @@ /data /word/put.js -# derived contents +# derived or compiled contents /sitemap.xml /light.css /UPDATE /plan.plp /word/*.min.js +# optional cache files +/source/*.html + # site owner tag for google webmaster tools /google????????????????.html diff --git a/source.plp b/source.plp index e645cd2..43fddb6 100644 --- a/source.plp +++ b/source.plp @@ -74,6 +74,16 @@ else { -r $source or Abort("Requested file not found", '404 source not found'); my $size = (stat $source)->[7]; + my $cachefile = "source/$source.html"; + if (-e $cachefile and (stat $cachefile)->[9] >= (stat $source)->[9]) { + say '
';
+		print ReadFile($cachefile);
+		say '
'; + exit; + } + 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; @@ -110,6 +120,7 @@ else { $line = qq(<$tag class="sy-\l$type">$line) if $tag; print $line; + print {$cache} $line if $cache; } say ''; } -- 2.30.0