Last active
August 29, 2015 14:25
-
-
Save z448/768472099bb8ea60811b to your computer and use it in GitHub Desktop.
get .js & .css sources from html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# downloads src .js and .css files from html; use if you are behind proxy and cant source external content | |
# usage | |
# ./getHtmlSrc.pl /path/to/some.html | |
# v0.2 - prints local links after src name | |
# v0.3 - archive (out.html.src.gz) then remove all .js & .css | |
# v0.4 - outputs out.html with swapped src files for local ones | |
use strict; | |
use warnings; | |
use IO::All; | |
my (@lines, $file, $content, $name, $url, $hot, @files, $nurl); | |
my $locLibDir = 'llib'; | |
my $fileOut = 'out.html'; | |
if ($ARGV[0]) { | |
$file = $ARGV[0]; | |
`rm $fileOut && touch $fileOut`; | |
} else { | |
print "give me path to file\n"; | |
exit; | |
} | |
io("$file") > $content; | |
@lines = split(/\n/, $content); | |
foreach (@lines) { | |
if (/(\<script\ src\=\"(http|\/\/)|(\<link\ rel.*\/\/.*))/) { | |
$name = $url = $nurl = $_; | |
$name =~ s/.*\/(.*?\.(js|css)).*/$1/; | |
push @files, $name; | |
$url =~ s/(.*\/\/)(.*?)(\".*)/$2/; | |
$nurl =~ s/(.*)(\/\/.*?)(\".*)/$1$name$3/; | |
$nurl =~ s/(.*)(http\:)(.*)/$1$3/; | |
print "$name -> "; | |
$hot = "curl\ \-LO\ http\:\/\/$url 1>/dev/null 2>/dev/null"; | |
`$hot`; | |
print "$nurl\n"; | |
"$nurl\n" >> io($fileOut); | |
} else { | |
"$_\n" >> io($fileOut); | |
} | |
} | |
sub archive { | |
my $toTar = join ' ', @files; | |
`tar -zcvf "$fileOut".'src.gz' $toTar`; | |
`rm $toTar`; | |
} | |
archive; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment