Files
digiproof/mksingleton.pl

147 lines
3.6 KiB
Perl
Raw Normal View History

2013-09-12 23:13:49 +02:00
#!/usr/bin/perl
#
# This file is part of the digital testament management program
# DigiProof.
#
# By accessing this software, DigiProof, you are duly informed
# of and agree to be bound by the conditions described below in this
# notice:
#
# This software product, DigiProof, is developed by T. Linden and
# copyrighted (C) 2013 by T. Linden, with all rights reserved.
#
# There is no charge for DigiProof software. You can redistribute
# it and/or modify it under the terms of the GNU General Public
# License, which is incorporated by reference herein.
#
# DigiProof is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS,
# OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that
# the use of it will not infringe on any third party's intellectual
# property rights.
#
# You should have received a copy of the GNU General Public License
# along with DigiProof. Copies can also be obtained from:
#
# http://www.gnu.org/licenses/gpl-2.0.html
#
# or by writing to:
#
# Free Software Foundation, Inc.
# Inc., 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301
# USA
#
# Or contact:
#
# "T. Linden" <tlinden@cpan.org>
#
# The sourcecode can be found on:
#
2025-12-13 21:03:30 +01:00
# https://codeberg.org/scip/digiproof
#
#
# Used to build digiproof-prod.html and digiproof-dev.html
# from source files. It just creates a single html file from
# everything. JS and CSS will bin minified, comments removed,
# src-file and git hash added.
2013-09-12 23:13:49 +02:00
use Data::Dumper;
use JavaScript::Minifier qw(minify);
use CSS::Compressor qw( css_compress );
my ($type, $index) = @ARGV;
2013-09-12 23:13:49 +02:00
if (!$index) {
print STDERR "Usage: $0 <prod|dev> <index.html>\n";
2013-09-12 23:13:49 +02:00
exit 1;
}
my $out = "digiproof-$type.html";
2013-09-12 23:13:49 +02:00
print STDERR "open index\n";
open I, "<$index" or die "Could not open index: $index $!\n";
my @source = <I>;
close I;
print STDERR "open $out\n";
open OUT, ">$out" or die "Could not open output $out: $!\n";
select OUT;
print STDERR "enter loop\n";
foreach (@source) {
chomp;
next if(/^\s*$/);
if (/rel="stylesheet" href="([^"]*)"/) {
my $cssfile = $1;
my $media = "";
if (/media="([^"]*)"/) {
$media = qq( media="$1" );
}
print STDERR "Inserting $cssfile\n";
print qq(<style type="text/css" $media>\n);
print &fetch($cssfile);
print qq(</style>\n);
}
elsif (/script src="([^"]*)"/) {
my $jsfile = $1;
if ($jsfile =~ /digiproof/) {
$jsfile = "js/digiproof_$type.js";
}
2013-09-12 23:13:49 +02:00
print STDERR "Inserting $jsfile\n";
print qq(<script type="text/javascript">\n);
print &fetch($jsfile);
print qq(</script>\n);
}
elsif (/GITHASH/) {
my $hash = &githash($index);
print " -- Source: $index, Githash: $hash\n";
}
2013-09-12 23:13:49 +02:00
else {
print qq($_\n);
}
}
sub fetch {
my $file = shift;
open F, "<$file" or die "Could not open index: $file $!\n";
my $hash = &githash($file);
2013-09-12 23:13:49 +02:00
if($file =~ /\.js/) {
my $js;
my $from = "/* js from $file ($hash) */\n";
if ($type eq 'prod') {
$js = minify(input => *F);
# ember.js (and probably others) contain strings which contain </script>
# which leads to rendering failures, those will be fixed here
$js =~ s/<\/script>/' + Slash + 'script>/g;
close F;
return $from . $js;
}
else {
return $from . join '', <F>;
close F;
}
2013-09-12 23:13:49 +02:00
}
else {
my $from = "/* css from $file ($hash) */\n";
2013-09-12 23:13:49 +02:00
my $src = join "", <F>;
close F;
if ($prod) {
return $from . $src;
}
else {
return $from . css_compress($src);
}
2013-09-12 23:13:49 +02:00
}
}
sub githash {
my $file = shift;
my $hash = `git hash-object $file`;
chomp $hash;
return $hash;
}