source: cache syntax highlighted html
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 24 Nov 2021 23:48:49 +0000 (00:48 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 6 Dec 2021 11:07:29 +0000 (12:07 +0100)
Massive speedup on repeated requests since Text::VimColor is so slow
(but the only readily available option for plp syntax).

.gitignore
source.plp

index 142f46ae6992d47b00cafd3a1b295b7e3cce3784..88155205ca944855a90622a68770ee22cdeee44b 100644 (file)
@@ -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
index e645cd25e7167c60f844bb7c7e6f3c21054815d8..43fddb635f7e340801a89525f61861bdb69a6a22 100644 (file)
@@ -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 '<pre>';
+               print ReadFile($cachefile);
+               say '</pre>';
+               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</$tag>) if $tag;
                        print $line;
+                       print {$cache} $line if $cache;
                }
                say '</pre>';
        }