Replaced cache.pl with htmlrescache

See the .gitattributes file.
master
Hauke D 7 years ago
parent e12f1aa25a
commit 07f72bd8dd

4
.gitattributes vendored

@ -0,0 +1,4 @@
# See http://bitbucket.org/haukex/htools/src/HEAD/htmlrescache
# Set up via: htmlrescache -cweb/_cache init
/web/*.html filter=htmlrescache
/web/**/*.html filter=htmlrescache

2
.gitignore vendored

@ -4,6 +4,6 @@
/web/emperl.js
/web/emperl.wasm
/web/emperl.data
/web/cache/
/web/_cache/
/pages/
/wiki/

@ -1,80 +0,0 @@
#!/usr/bin/env perl
use warnings;
use strict;
use feature 'state';
use FindBin;
use File::Find qw/find/;
use File::Path qw/make_path/;
use File::Spec::Functions qw/catdir catfile abs2rel rel2abs splitdir/;
use File::Basename qw/fileparse/;
use ExtUtils::MakeMaker qw/prompt/;
require File::Spec::Unix;
# A quick and dirty script for caching external resources locally
my $USE_WGET; BEGIN { $USE_WGET=0 }
use if !$USE_WGET, 'HTTP::Tiny';
my $DEBUG; BEGIN { $DEBUG=0 }
use if $DEBUG, 'Data::Dump';
my $CACHEDIR = catdir($FindBin::Bin,'web','cache');
make_path $CACHEDIR;
my @findin = @ARGV ? map {rel2abs($_)} @ARGV : catdir($FindBin::Bin,'web');
my @htmlfiles;
find({ follow=>1,
wanted=>sub {
-f && /\.html?\z/i && push @htmlfiles, $File::Find::name;
} },
@findin );
# yes, I know, parsing HTML with regexes, boo, hiss ;-)
# http://www.perlmonks.org/?node_id=1201438
my $regex = qr{
< (?:script|link) [^>]+ (?:src|href) \s* = \s*
(?<q> ["'] )
(?= https?:// (?! localhost\b | 127\.0\.0\.1\b ) )
\K
(?: (?! \k{q} | > ) . )*
(?= \k{q} )
}msx;
for my $fn (@htmlfiles) {
my $html = do { open my $fh, '<:encoding(UTF-8)', $fn or die "$fn: $!"; local $/; <$fh> };
my $count = $html =~ s/$regex/fetch_resource($fn,$&)/eg;
warn+($count||0)." replacements in $fn\n";
next unless $count;
next unless prompt("Overwrite $fn? [Yn]","y")=~/^\s*y/i;
open my $ofh, '>:encoding(UTF-8)', $fn or die "$fn: $!";
print $ofh $html;
close $ofh;
}
sub fetch_resource {
my ($file,$url) = @_;
state $http = $USE_WGET ? undef : HTTP::Tiny->new;
state %cached;
my ($cachefn) = $url =~ m{/([^/]+)\z} or die $url;
$cachefn = catfile($CACHEDIR, $cachefn);
print STDERR "$url: ";
if (not $cached{$url}) {
if ($USE_WGET) {
# Note: wget's timestamping (-N) doesn't work with -O
system('wget','-nv','-O'.$cachefn,$url)==0 or die "wget failed\n";
}
else {
my $resp = $http->mirror($url, $cachefn);
die "$resp->{status} $resp->{reason}\n" unless $resp->{success};
warn "$resp->{status} $resp->{reason}\n";
}
$cached{$url} = $cachefn;
}
else { print STDERR "already fetched\n"; }
my (undef,$path) = fileparse($file);
my $newurl = File::Spec::Unix->catdir(splitdir( abs2rel($cachefn,$path) ));
$DEBUG and dd($file, $url, $cachefn, $newurl);
return $newurl;
}

@ -25,7 +25,7 @@ You should have received a copy of the licenses along with this program.
If not, see http://perldoc.perl.org/index-licence.html
-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.2/codemirror.min.css" integrity="sha256-I8NyGs4wjbMuBSUE40o55W6k6P7tu/7G28/JGUUYCIs=" crossorigin="anonymous" />
<!--cacheable--><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.2/codemirror.min.css" integrity="sha256-I8NyGs4wjbMuBSUE40o55W6k6P7tu/7G28/JGUUYCIs=" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="emscr_ide.css" />
<style>
button,.text {
@ -56,9 +56,9 @@ pre,.code,textarea {
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.2/codemirror.min.js" integrity="sha256-uRIJ6Wfou5cTtmcCvQNA9lvsYl8sUbZXxnfG+P79ssY=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.2/mode/perl/perl.min.js" integrity="sha256-Uu9QBfi8gU6J/MzQunal8ewmY+i/BbCkBrcAXA5bcCM=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<!--cacheable--><script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.2/codemirror.min.js" integrity="sha256-uRIJ6Wfou5cTtmcCvQNA9lvsYl8sUbZXxnfG+P79ssY=" crossorigin="anonymous"></script>
<!--cacheable--><script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.2/mode/perl/perl.min.js" integrity="sha256-Uu9QBfi8gU6J/MzQunal8ewmY+i/BbCkBrcAXA5bcCM=" crossorigin="anonymous"></script>
<!--cacheable--><script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="emscr_ide.js"></script>
<script src="../webperl.js"></script>
<script>

@ -26,8 +26,7 @@ If not, see http://perldoc.perl.org/index-licence.html
##### -->
<meta name="viewport" content="width=600" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css"
integrity="sha256-oSrCnRYXvHG31SBifqP2PM1uje7SJUyX0nTwO2RJV54=" crossorigin="anonymous" />
<!--cacheable--><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css" integrity="sha256-oSrCnRYXvHG31SBifqP2PM1uje7SJUyX0nTwO2RJV54=" crossorigin="anonymous" />
<style>
body {
margin: 0.4em;
@ -103,8 +102,7 @@ textarea.samp_ta {
<script src="webperl.js"></script>
<!--script src="https://webperlcdn.zero-g.net/v0.07-beta/webperl.js"
integrity="sha256-jL8SB7St5ou4+hb0frK0k6VCQXsWQ1wolDrdU7i4juc=" crossorigin="anonymous"></script-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<!--cacheable--><script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script type="text/perl">
use warnings;

Loading…
Cancel
Save