source: cache syntax highlighted html
[sheet.git] / source.plp
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>';
        }