r69239 - in /trunk/libhtml-packer-perl: ./ debian/ inc/ lib/HTML/ t/ t/html/
jawnsy-guest at users.alioth.debian.org
jawnsy-guest at users.alioth.debian.org
Mon Feb 21 15:34:14 UTC 2011
Author: jawnsy-guest
Date: Mon Feb 21 15:33:57 2011
New Revision: 69239
URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=69239
Log:
integrate new upstream version to trunk
Added:
trunk/libhtml-packer-perl/.gitignore
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/.gitignore
trunk/libhtml-packer-perl/inc/
- copied from r69238, branches/upstream/libhtml-packer-perl/current/inc/
trunk/libhtml-packer-perl/t/html/s4-expected.html
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/t/html/s4-expected.html
trunk/libhtml-packer-perl/t/html/s4.html
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/t/html/s4.html
trunk/libhtml-packer-perl/t/html/s5-expected.html
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/t/html/s5-expected.html
trunk/libhtml-packer-perl/t/html/s5.html
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/t/html/s5.html
trunk/libhtml-packer-perl/t/html/s6-expected.html
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/t/html/s6-expected.html
trunk/libhtml-packer-perl/t/html/s6.html
- copied unchanged from r69238, branches/upstream/libhtml-packer-perl/current/t/html/s6.html
Removed:
trunk/libhtml-packer-perl/META.yml
Modified:
trunk/libhtml-packer-perl/.cvsignore
trunk/libhtml-packer-perl/Changes
trunk/libhtml-packer-perl/MANIFEST
trunk/libhtml-packer-perl/Makefile.PL
trunk/libhtml-packer-perl/README
trunk/libhtml-packer-perl/debian/changelog
trunk/libhtml-packer-perl/lib/HTML/Packer.pm
trunk/libhtml-packer-perl/t/01-io.t
Modified: trunk/libhtml-packer-perl/.cvsignore
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/.cvsignore?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/.cvsignore (original)
+++ trunk/libhtml-packer-perl/.cvsignore Mon Feb 21 15:33:57 2011
@@ -1,10 +1,15 @@
-blib*
-Makefile
-Makefile.old
-Build
-_build*
-pm_to_blib*
-*.tar.gz
-.lwpcookies
-HTML-Packer-*
-cover_db
+/blib*
+/Makefile
+/Makefile.old
+/Makefile.bak
+/MANIFEST.bak
+/META.yml
+/MYMETA.yml
+/Build
+/_build*
+/pm_to_blib*
+/*.tar.gz
+/HTML-Packer-*
+/t/html/*-got.html
+/inc
+.DS_Store
Modified: trunk/libhtml-packer-perl/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/Changes?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/Changes (original)
+++ trunk/libhtml-packer-perl/Changes Mon Feb 21 15:33:57 2011
@@ -1,14 +1,43 @@
Revision history for HTML-Packer
-0.1 2009-02-09
- First version, released on an unsuspecting world.
+1.000 2011-01-17
+ - Changed versioning.
+ - Raised major version due to changes in versioning.
+ - Changed requirements to Regexp::RegGrp 1.000 due to changes in versioning.
-0.2 2009-02-10
- Added some tests
-
-0.3 2009-02-11
- Typo fix in documentation
-
-0.4 2009-02-15
- - Another embarrassing typo fix in documentation
- - Updated README file
+0.05_06 2011-01-05
+ - Changed requirements to Regexp::RegGrp 0.04
+
+0.05_05 2010-12-03
+ - Added no_cdata option.
+
+0.05_04 2010-10-05
+ - Changed required perl version to 5.8.9.
+
+0.05_03 2010-09-21
+ - Added option to switch off compression via comment.
+ - Added support for conditional comments.
+ - Changed requirements to Regexp::RegGrp 0.0203
+ - Added tests.
+
+0.05_02 2010-09-13
+ - Changed requirements to Regexp::RegGrp 0.0201
+
+0.05_01
+ - Moved regexp grouping stuff to Regexp::RegGrp.
+ - Added object-oriented interface.
+ - Switched tabs to soft tabs.
+ - Switched to Module::Install.
+
+0.4 2009-02-15
+ - Another embarrassing typo fix in documentation
+ - Updated README file
+
+0.3 2009-02-11
+ - Typo fix in documentation
+
+0.2 2009-02-10
+ - Added some tests
+
+0.1 2009-02-09
+ - First version, released on an unsuspecting world.
Modified: trunk/libhtml-packer-perl/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/MANIFEST?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/MANIFEST (original)
+++ trunk/libhtml-packer-perl/MANIFEST Mon Feb 21 15:33:57 2011
@@ -1,5 +1,17 @@
.cvsignore
+.gitignore
Changes
+inc/Module/AutoInstall.pm
+inc/Module/Install.pm
+inc/Module/Install/AutoInstall.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Include.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
lib/HTML/Packer.pm
Makefile.PL
MANIFEST
@@ -12,5 +24,10 @@
t/html/s2.html
t/html/s3-expected.html
t/html/s3.html
+t/html/s4-expected.html
+t/html/s4.html
+t/html/s5-expected.html
+t/html/s5.html
+t/html/s6-expected.html
+t/html/s6.html
t/pod.t
-META.yml Module meta-data (added by MakeMaker)
Modified: trunk/libhtml-packer-perl/Makefile.PL
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/Makefile.PL?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/Makefile.PL (original)
+++ trunk/libhtml-packer-perl/Makefile.PL Mon Feb 21 15:33:57 2011
@@ -1,20 +1,15 @@
-use 5.008;
use strict;
use warnings;
-use ExtUtils::MakeMaker;
-WriteMakefile(
- NAME => 'HTML::Packer',
- AUTHOR => 'Merten Falk <nevesenin at cpan.org>',
- VERSION_FROM => 'lib/HTML/Packer.pm',
- ABSTRACT_FROM => 'lib/HTML/Packer.pm',
- ($ExtUtils::MakeMaker::VERSION >= 6.3002
- ? ('LICENSE'=> 'perl')
- : ()),
- PL_FILES => {},
- PREREQ_PM => {
- 'Test::More' => 0,
- },
- dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
- clean => { FILES => 'HTML-Packer-*' },
-);
+use inc::Module::Install;
+
+all_from 'lib/HTML/Packer.pm';
+test_requires 'Test::More' => 0;
+requires 'Regexp::RegGrp' => "1.000";
+
+repository 'http://github.com/nevesenin/html-packer-perl';
+
+clean_files 't/html/s?-got.html';
+
+auto_install();
+WriteAll();
Modified: trunk/libhtml-packer-perl/README
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/README?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/README (original)
+++ trunk/libhtml-packer-perl/README Mon Feb 21 15:33:57 2011
@@ -1,24 +1,13 @@
HTML-Packer
-
-The README is used to introduce the module and provide instructions on
-how to install the module, any machine dependencies it may have (for
-example C compilers and installed libraries) and any other information
-that should be provided before the module is installed.
-
-A README file is required for CPAN modules since CPAN extracts the README
-file from a module distribution so that people browsing the archive
-can use it to get an idea of the module's uses. It is usually a good idea
-to provide version information here so that people can decide whether
-fixes for the module are worth downloading.
INSTALLATION
To install this module, run the following commands:
- perl Makefile.PL
- make
- make test
- make install
+ perl Makefile.PL
+ make
+ make test
+ make install
To use compression for JavaScript parts install JavaScript::Packer.
To use compression for CSS parts install CSS::Packer.
@@ -47,7 +36,7 @@
COPYRIGHT AND LICENCE
-Copyright (C) 2008 Merten Falk
+Copyright (C) 2008 - 2011 Merten Falk
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Modified: trunk/libhtml-packer-perl/debian/changelog
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/debian/changelog?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/debian/changelog (original)
+++ trunk/libhtml-packer-perl/debian/changelog Mon Feb 21 15:33:57 2011
@@ -1,9 +1,13 @@
-libhtml-packer-perl (0.4-2) UNRELEASED; urgency=low
+libhtml-packer-perl (1.000-1) UNRELEASED; urgency=low
+ [ gregor herrmann ]
* debian/control: Changed: (build-)depend on perl instead of perl-
modules.
- -- gregor herrmann <gregoa at debian.org> Fri, 23 Oct 2009 02:25:11 +0200
+ [ Ernesto Hernández-Novich (USB) ]
+ * New upstream release
+
+ -- Jonathan Yu <jawnsy at cpan.org> Mon, 21 Feb 2011 10:56:47 -0500
libhtml-packer-perl (0.4-1) unstable; urgency=low
Modified: trunk/libhtml-packer-perl/lib/HTML/Packer.pm
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/lib/HTML/Packer.pm?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/lib/HTML/Packer.pm (original)
+++ trunk/libhtml-packer-perl/lib/HTML/Packer.pm Mon Feb 21 15:33:57 2011
@@ -1,142 +1,345 @@
package HTML::Packer;
-use 5.008;
+use 5.008009;
use strict;
use warnings;
use Carp;
-
-use vars qw/$VERSION $PLACEHOLDER/;
-
-$VERSION = '0.4';
-
-$PLACEHOLDER = 'hp_';
+use Regexp::RegGrp;
# -----------------------------------------------------------------------------
+our $VERSION = '1.000';
+
+our @TAGS = (
+ 'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 'button', 'cite',
+ 'del', 'dfn', 'em', 'font', 'i', 'input', 'ins', 'kbd', 'label', 'q',
+ 's', 'samp', 'select', 'small', 'strike', 'strong', 'sub', 'sup', 'u', 'var'
+);
+
+# Some regular expressions are from HTML::Clean
+
+our $COMMENT = '((?>\s*))(<!--(?:(?:[^#\[]|(?! google_ad_section_)).*?)?-->)((?>\s*))';
+
+our $PACKER_COMMENT = '<!--\s*HTML::Packer\s*(\w+)\s*-->';
+
+our $DOCTYPE = '<\!DOCTYPE[^>]*>';
+
+our $DONT_CLEAN = '(<\s*(pre|code|textarea|script|style)[^>]*>)(.*?)(<\s*\/\2[^>]*>)';
+
+our $WHITESPACES = [
+ {
+ regexp => qr/^\s*/s,
+ replacement => ''
+ },
+ {
+ regexp => qr/\s*$/s,
+ replacement => ''
+ },
+ {
+ regexp => '^\s*',
+ replacement => '',
+ modifier => 'm'
+ },
+ {
+ regexp => '\s*$',
+ replacement => '',
+ modifier => 'm'
+ },
+ {
+ regexp => qr/(?<=>)[^<>]*(?=<)/sm,
+ replacement => sub {
+ my $match = $_[0]->{match};
+
+ $match =~ s/[^\S\n]{2,}/ /sg;
+ $match =~ s/\s*\n+\s*/\n/sg;
+
+ return $match;
+ }
+ },
+ {
+ regexp => '<\s*(\/)?\s*',
+ replacement => sub {
+ return sprintf( '<%s', $_[0]->{submatches}->[0] );
+ },
+ modifier => 's'
+ },
+ {
+ regexp => '\s*(\/)?\s*>',
+ replacement => sub {
+ return sprintf( '%s>', $_[0]->{submatches}->[0] );
+ },
+ modifier => 's'
+ }
+];
+
+our $NEWLINES_TAGS = [
+ {
+ regexp => '(\s*)(<\s*\/?\s*(?:' . join( '|', @TAGS ) . ')[^>]*>)(\s*)',
+ replacement => sub {
+ return sprintf( '%s%s%s', $_[0]->{submatches}->[0] ? ' ' : '', $_[0]->{submatches}->[1], $_[0]->{submatches}->[2] ? ' ' : '' );
+ },
+ modifier => 'is'
+ }
+];
+
+our $NEWLINES = [
+ {
+ regexp => '(.)\n(.)',
+ replacement => sub {
+ my ( $pre, $post ) = @{$_[0]->{submatches}};
+
+ my $ret;
+
+ if ( $pre eq '>' or $post eq '<' ) {
+ $ret = $pre . $post;
+ }
+ elsif ( $pre =~ /[\w-]/ and $post =~ /[\w-]/ ) {
+ $ret = $pre . ' ' . $post;
+ }
+ else {
+ $ret = $pre . $post;
+ }
+
+ return $ret;
+ }
+ }
+];
+
+sub init {
+ my $class = shift;
+ my $self = {};
+
+ eval {
+ require JavaScript::Packer;
+ };
+ $self->{can_do_javascript} = $@ ? 0 : 1;
+ $self->{javascript_packer} = undef;
+ eval {
+ require CSS::Packer;
+ };
+ $self->{can_do_stylesheet} = $@ ? 0 : 1;
+ $self->{css_packer} = undef;
+
+ $self->{whitespaces}->{reggrp_data} = $WHITESPACES;
+ $self->{newlines}->{reggrp_data} = $NEWLINES;
+ $self->{newlines_tags}->{reggrp_data} = $NEWLINES_TAGS;
+ $self->{global}->{reggrp_data} = [
+ {
+ regexp => $DOCTYPE,
+ replacement => sub {
+ return '<!--~' . $_[0]->{store_index} . '~-->';
+ },
+ store => sub {
+ my $doctype = $_[0]->{match};
+
+ $doctype =~ s/\s+/ /gsm;
+
+ return $doctype;
+ }
+ },
+ {
+ regexp => $COMMENT,
+ replacement => sub {
+ my $opts = $_[0]->{opts} || {};
+ my $remove_comments = _get_opt( $opts, 'remove_comments' );
+ my $remove_newlines = _get_opt( $opts, 'remove_newlines' );
+
+ return $remove_comments ? (
+ $remove_newlines ? ' ' : (
+ ( $_[0]->{submatches}->[0] =~ /\n/s or $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : ''
+ )
+ ) : '<!--~' . $_[0]->{store_index} . '~-->';
+ },
+ store => sub {
+ my $opts = $_[0]->{opts} || {};
+ my $remove_comments = _get_opt( $opts, 'remove_comments' );
+ my $remove_newlines = _get_opt( $opts, 'remove_newlines' );
+
+ my $ret = $remove_comments ? '' : (
+ ( ( not $remove_newlines and $_[0]->{submatches}->[0] =~ /\n/s ) ? "\n" : '' ) .
+ $_[0]->{submatches}->[1] .
+ ( ( not $remove_newlines and $_[0]->{submatches}->[2] =~ /\n/s ) ? "\n" : '' )
+ );
+
+ return $ret;
+ }
+ },
+ {
+ regexp => $DONT_CLEAN,
+ replacement => sub {
+ return '<!--~' . $_[0]->{store_index} . '~-->';
+ },
+ store => sub {
+ my ( $opening, undef, $content, $closing ) = @{$_[0]->{submatches}};
+ my $opts = $_[0]->{opts} || {};
+
+ if ( $content ) {
+ if ( $opening =~ /<\s*script[^>]*(?:java|ecma)script[^>]*>/ and $self->{javascript_packer} ) {
+ my $do_javascript = _get_opt( $opts, 'do_javascript' );
+ if ( $do_javascript ) {
+ my $no_cdata = _get_opt( $opts, 'no_cdata' );
+ $self->{javascript_packer}->minify( \$content, { compress => $do_javascript } );
+ unless ( $no_cdata ) {
+ $content = '/*<![CDATA[*/' . $content . '/*]]>*/';
+ }
+ }
+ }
+ elsif ( $opening =~ /<\s*style[^>]*text\/css[^>]*>/ and $self->{css_packer} ) {
+ my $do_stylesheet = _get_opt( $opts, 'do_stylesheet' );
+ if ( $do_stylesheet ) {
+ $self->{css_packer}->minify( \$content, { compress => $do_stylesheet } );
+ $content = "\n" . $content if ( $do_stylesheet eq 'pretty' );
+ }
+ }
+ }
+ else {
+ $content = '';
+ }
+
+ # I don't like this, but
+ # $self->{whitespaces}->{reggrp}->exec( \$opening );
+ # will not work. It isn't initialized jet.
+ # If someone has a better idea, please let me know
+ $self->_process_wrapper( 'whitespaces', \$opening );
+ $self->_process_wrapper( 'whitespaces', \$closing );
+
+ return $opening . $content . $closing;
+ },
+ modifier => 'ism'
+ }
+ ];
+
+ map {
+ $self->{$_}->{reggrp} = Regexp::RegGrp->new( { reggrp => $self->{$_}->{reggrp_data} } );
+ } ( 'newlines', 'newlines_tags', 'whitespaces' );
+
+ $self->{global}->{reggrp} = Regexp::RegGrp->new(
+ {
+ reggrp => $self->{global}->{reggrp_data},
+ restore_pattern => qr/<!--~(\d+)~-->/
+ }
+ );
+
+ bless( $self, $class );
+
+ return $self;
+}
+
sub minify {
-# Some regular expressions are from HTML::Clean
- my ( $scalarref, $opts ) = @_;
-
- if ( ref( $scalarref ) ne 'SCALAR' ) {
- carp( 'First argument must be a scalarref!' );
- return '';
- }
-
- return '' if ( ${$scalarref} eq '' );
-
- if ( ref( $opts ) ne 'HASH' ) {
- carp( 'Second argument must be a hashref of options! Using defaults!' ) if ( $opts );
- $opts = {
- 'remove_comments' => 0,
- 'remove_newlines' => 0,
- 'do_javascript' => '', # minify, shrink, base62
- 'do_stylesheet' => '', # pretty, minify
- };
- }
- else {
- $opts->{'remove_comments'} = $opts->{'remove_comments'} ? 1 : 0;
- $opts->{'remove_newlines'} = $opts->{'remove_newlines'} ? 1 : 0;
- $opts->{'do_javascript'} = grep( $opts->{'do_javascript'}, ( 'minify', 'shrink', 'base62' ) ) ? $opts->{'do_javascript'} : '';
- $opts->{'do_stylesheet'} = grep( $opts->{'do_stylesheet'}, ( 'minify', 'pretty' ) ) ? $opts->{'do_stylesheet'} : '';
- }
-
- if ( $opts->{'do_javascript'} ) {
- eval( 'require JavaScript::Packer;' );
-
- if ( $@ ) {
- $opts->{'do_javascript'} = '';
- }
- }
- if ( $opts->{'do_stylesheet'} ) {
- eval( 'require CSS::Packer;' );
-
- if ( $@ ) {
- $opts->{'do_stylesheet'} = '';
- }
- }
-
- ${$scalarref} =~ s/<!--~\Q$PLACEHOLDER\E\d+~-->//gsm;
- ${$scalarref} =~ s/<!--([^#].*?)?-->//gsm if ( $opts->{'remove_comments'} );
-
- my $unclean = {};
-
- my $_replace_unclean = sub {
- my ( $opening, $content, $closing ) = @_;
-
- return '' unless ( $opening );
-
- my $key = $PLACEHOLDER . scalar( keys( %$unclean ) );
-
- if ( not $content and not $closing ) {
- $unclean->{$key} = $opening;
- }
- else {
- if ( $content ) {
- if ( $opening =~ /<\s*script[^>]*(?:java|ecma)script[^>]*>/ and $opts->{'do_javascript'} ) {
- JavaScript::Packer::minify( \$content, { 'compress' => $opts->{'do_javascript'} } );
- $content = '/*<![CDATA[*/' . $content . '/*]]>*/';
- }
- elsif ( $opening =~ /<\s*style[^>]*text\/css[^>]*>/ and $opts->{'do_stylesheet'} ) {
- CSS::Packer::minify( \$content, { 'compress' => $opts->{'do_stylesheet'} } );
- }
- }
- else {
- $content = '';
- }
- $opening =~ s/[^\S\n]{2,}/ /msg;
- $opening =~ s/\s+>/>/sgm;
- $opening =~ s/<\s+/</sgm;
- $closing =~ s/[^\S\n]{2,}/ /msg;
- $closing =~ s/\s+>/>/sgm;
- $closing =~ s/<\s+/</sgm;
- $closing =~ s/<\/\s+/<\//sgm;
-
- $unclean->{$key} = $opening . $content . $closing;
- }
-
- return '<!--~' . $key . '~-->';
- };
-
- ${$scalarref} =~ s/(<\!DOCTYPE[^>]*>)/&$_replace_unclean( $& )/xmse;
-
- ${$scalarref} =~ s/(<\s*(pre|code|textarea|script|style)[^>]*>)(.*?)(<\s*\/\2[^>]*>)/&$_replace_unclean( $1, $3, $4 )/gmsie;
-
- my @tags = (
- 'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 'button', 'cite',
- 'del', 'dfn', 'em', 'font', 'i', 'input', 'ins', 'kbd', 'label', 'q',
- 's', 'samp', 'select', 'small', 'strike', 'strong', 'sub', 'sup', 'u', 'var'
- );
-
- ${$scalarref} =~ s/^\s*//sg;
- ${$scalarref} =~ s/\s*$//sg;
- ${$scalarref} =~ s/[^\S\n]*$//smg;
- ${$scalarref} =~ s/^[^\S\n]*//smg;
- ${$scalarref} =~ s/[^\S\n]*\n/\n/sg;
- ${$scalarref} =~ s/[^\S\n]{2,}/ /sg;
- ${$scalarref} =~ s/\n{2,}/\n/sg;
- ${$scalarref} =~ s/\s+>/>/sg;
- ${$scalarref} =~ s/<\s+/</sg;
- ${$scalarref} =~ s/<\/\s+/<\//sg;
-
- if ( $opts->{'remove_newlines'} ) {
- foreach ( @tags ) {
- ${$scalarref} =~ s/[^\S]+(<\s*\/?\s*\Q$_\E( [^>]*)?>)/ $1/ismg;
- ${$scalarref} =~ s/(<\s*\/?\s*\Q$_\E( [^>]*)?>)[^\S]+/$1 /ismg;
- }
-
- ${$scalarref} =~ s/>\n</></g;
- ${$scalarref} =~ s/([^>])\n</$1</g;
- ${$scalarref} =~ s/>\n([^<])/>$1/g;
- ${$scalarref} =~ s/(\w)\n(\w)/$1 $2/g;
- ${$scalarref} =~ s/([^>])\n([^>])/$1 $2/g;
- ${$scalarref} =~ s/\n//g;
- }
-
- ${$scalarref} =~ s/<!--~(\Q$PLACEHOLDER\E\d+)~-->/$unclean->{$1}/gsme;
-
- ${$scalarref} =~ s/[^\S\n]*(<!--([^#].*?)?-->)[^\S\n]*/$1/gsm unless ( $opts->{'remove_comments'} );
+ my ( $self, $input, $opts );
+
+ unless (
+ ref( $_[0] ) and
+ ref( $_[0] ) eq __PACKAGE__
+ ) {
+ $self = __PACKAGE__->init();
+
+ shift( @_ ) unless ( ref( $_[0] ) );
+
+ ( $input, $opts ) = @_;
+ }
+ else {
+ ( $self, $input, $opts ) = @_;
+ }
+
+ if ( ref( $input ) ne 'SCALAR' ) {
+ carp( 'First argument must be a scalarref!' );
+ return undef;
+ }
+
+ my $html = \'';
+ my $cont = 'void';
+
+ if ( defined( wantarray ) ) {
+ my $tmp_input = ref( $input ) ? ${$input} : $input;
+
+ $html = \$tmp_input;
+ $cont = 'scalar';
+ }
+ else {
+ $html = ref( $input ) ? $input : \$input;
+ }
+
+ if ( $self->{can_do_javascript} and not $self->{javascript_packer_isset} ) {
+ $self->{javascript_packer} = eval {
+ JavaScript::Packer->init();
+ };
+ $self->{javascript_packer_isset} = 1;
+ }
+
+ if ( $self->{can_do_stylesheet} and not $self->{css_packer_isset} ) {
+ $self->{css_packer} = eval {
+ CSS::Packer->init();
+ };
+ $self->{css_packer_isset} = 1;
+ }
+
+ if ( ref( $opts ) ne 'HASH' ) {
+ carp( 'Second argument must be a hashref of options! Using defaults!' ) if ( $opts );
+ $opts = {
+ remove_comments => 0,
+ remove_newlines => 0,
+ do_javascript => '', # minify, shrink, base62
+ do_stylesheet => '', # pretty, minify
+ no_compress_comment => 0,
+ no_cdata => 0
+ };
+ }
+ else {
+ $opts->{remove_comments} = $opts->{remove_comments} ? 1 : 0;
+ $opts->{remove_newlines} = $opts->{remove_newlines} ? 1 : 0;
+ $opts->{do_javascript} = (
+ grep( $opts->{do_javascript}, ( 'minify', 'shrink', 'base62' ) ) &&
+ $self->{javascript_packer}
+ ) ? $opts->{do_javascript} : '';
+
+ $opts->{do_stylesheet} = (
+ grep( $opts->{do_stylesheet}, ( 'minify', 'pretty' ) ) &&
+ $self->{css_packer}
+ ) ? $opts->{do_stylesheet} : '';
+
+ $opts->{no_compress_comment} = $opts->{no_compress_comment} ? 1 : 0;
+ $opts->{no_cdata} = $opts->{no_cdata} ? 1 : 0;
+ }
+
+ if ( not $opts->{no_compress_comment} and ${$html} =~ /$PACKER_COMMENT/s ) {
+ my $compress = $1;
+ if ( $compress eq '_no_compress_' ) {
+ return ( $cont eq 'scalar' ) ? ${$html} : undef;
+ }
+ }
+
+ $self->{global}->{reggrp}->exec( $html, $opts );
+ $self->{whitespaces}->{reggrp}->exec( $html, $opts );
+ if ( $opts->{remove_newlines} ) {
+ $self->{newlines_tags}->{reggrp}->exec( $html );
+ $self->{newlines}->{reggrp}->exec( $html );
+ }
+
+ $self->{global}->{reggrp}->restore_stored( $html );
+
+ return ${$html} if ( $cont eq 'scalar' );
}
+sub _get_opt {
+ my ( $opts_hash, $opt ) = @_;
+
+ $opts_hash ||= {};
+ $opt ||= '';
+
+ my $ret = '';
+
+ $ret = $opts_hash->{$opt} if ( defined( $opts_hash->{$opt} ) );
+
+ return $ret;
+}
+
+sub _process_wrapper {
+ my ( $self, $reg_name, $in, $opts ) = @_;
+
+ $self->{$reg_name}->{reggrp}->exec( $in, $opts );
+}
+
1;
__END__
@@ -147,21 +350,27 @@
=head1 VERSION
-Version 0.4
+Version 1.000
+
+=head1 DESCRIPTION
+
+A HTML Compressor.
=head1 SYNOPSIS
use HTML::Packer;
+ my $packer = HTML::Packer->init();
+
+ $packer->minify( $scalarref, $opts );
+
+To return a scalar without changing the input simply use (e.g. example 2):
+
+ my $ret = $packer->minify( $scalarref, $opts );
+
+For backward compatibility it is still possible to call 'minify' as a function:
+
HTML::Packer::minify( $scalarref, $opts );
-
-=head1 DESCRIPTION
-
-A HTML Compressor.
-
-=head1 FUNCTIONS
-
-=head2 HTML::Packer::minify( $scalarref, $opts );
First argument must be a scalarref of HTML-Code.
Second argument must be a hashref of options. Possible options are
@@ -188,6 +397,14 @@
Default is no compression for CSS.
This option only takes effect if L<CSS::Packer> is installed.
+=item no_compress_comment
+
+If not set to a true value it is allowed to set a HTML comment that prevents the input being packed.
+
+ <!-- HTML::Packer _no_compress_ -->
+
+Is set by default.
+
=back
=head1 AUTHOR
@@ -208,7 +425,7 @@
=head1 COPYRIGHT & LICENSE
-Copyright 2009 Merten Falk, all rights reserved.
+Copyright 2009 - 2011 Merten Falk, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Modified: trunk/libhtml-packer-perl/t/01-io.t
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libhtml-packer-perl/t/01-io.t?rev=69239&op=diff
==============================================================================
--- trunk/libhtml-packer-perl/t/01-io.t (original)
+++ trunk/libhtml-packer-perl/t/01-io.t Mon Feb 21 15:33:57 2011
@@ -4,60 +4,146 @@
use Test::More;
-my $not = 3;
+my $js_input = <<EOT;
+
+<script type="javascript">
+
+
+
+ alert('test');</script>
+
+<a href="/" >link
+
+1 < /a>
+
+
+<!-- comment -->
+
+ < a href="/"> link 2
+ < / a >
+
+
+
+EOT
+
+my $css_input = <<EOT;
+
+
+ <style type="text/css">
+
+ foo {
+ asdf:asdf;
+ ew:12;
+ }
+</style>
+
+<a href="/" >link
+
+1 < /a>
+
+
+<!-- comment -->
+
+ < a href="/"> link 2
+ < / a >
+
+
+EOT
+
+my $js_expected_comp = '<script type="javascript">/*<![CDATA[*/alert(\'test\');/*]]>*/</script><a href="/">link 1 </a> <a href="/"> link 2 </a>';
+my $js_expected_nocdata = '<script type="javascript">alert(\'test\');</script><a href="/">link 1 </a> <a href="/"> link 2 </a>';
+my $js_expected_nocomp = '<script type="javascript">' . "\n\n\n\n" . ' alert(\'test\');</script><a href="/">link 1 </a> <a href="/"> link 2 </a>';
+
+my $css_expected_comp = '<style type="text/css">' . "\nfoo{\nasdf:asdf;\new:12;\n}\n" . '</style><a href="/">link 1 </a> <a href="/"> link 2 </a>';
+my $css_expected_nocomp = '<style type="text/css">' . "\n\n foo {\n asdf:asdf;\n ew:12;\n }\n" . '</style><a href="/">link 1 </a> <a href="/"> link 2 </a>';
+
+my $not = 9;
SKIP: {
- eval( 'use HTML::Packer;' );
-
- skip( 'HTML::Packer not installed!', $not ) if ( $@ );
-
- plan tests => $not;
-
- minTest( 's1' );
- minTest( 's2', { 'remove_newlines' => 1 } );
- minTest( 's3', { 'remove_comments' => 1 } );
+ eval( 'use HTML::Packer;' );
+
+ skip( 'HTML::Packer not installed!', $not ) if ( $@ );
+
+ plan tests => $not;
+
+ minTest( 's1', undef, 'Test without opts.' );
+ minTest( 's2', { remove_newlines => 1 }, 'Test remove_newlines.' );
+ minTest( 's3', { remove_comments => 1 }, 'Test remove_comments.' );
+ minTest( 's4', { remove_comments => 1, remove_newlines => 1 }, 'Test remove_newlines and remove_comments.' );
+ minTest( 's5', { remove_comments => 1, remove_newlines => 1 }, 'Test _no_compress_ comment.' );
+ minTest( 's6', { remove_comments => 1, remove_newlines => 1, no_compress_comment => 1 }, 'Test _no_compress_ comment with no_compress_comment option.' );
+
+ my $packer = HTML::Packer->init();
+ my $js_comp_input = $js_input;
+ my $js_cdata_input = $js_input;
+ $packer->minify( \$js_comp_input, { remove_comments => 1, remove_newlines => 1, do_javascript => 'minify' } );
+ $packer->minify( \$js_cdata_input, { remove_comments => 1, remove_newlines => 1, do_javascript => 'minify', no_cdata => 1 } );
+
+ eval( 'require JavaScript::Packer' );
+ if ( $@ ) {
+ is( $js_comp_input, $js_expected_nocomp, 'Test do_javascript. JavaScript::Packer not installed.' );
+ is( $js_cdata_input, $js_expected_nocomp, 'Test do_javascript 2. JavaScript::Packer not installed.' );
+ }
+ else {
+ is( $js_comp_input, $js_expected_comp, 'Test do_javascript. JavaScript::Packer installed.' );
+ is( $js_cdata_input, $js_expected_nocdata, 'Test do_javascript 2. JavaScript::Packer installed.' );
+ }
+
+ $packer->minify( \$css_input, { remove_comments => 1, remove_newlines => 1, do_stylesheet => 'pretty' } );
+
+ eval( 'require CSS::Packer' );
+ if ( $@ ) {
+ is( $css_input, $css_expected_nocomp, 'Test do_stylesheet. CSS::Packer not installed.' );
+ }
+ else {
+ is( $css_input, $css_expected_comp, 'Test do_stylesheet. CSS::Packer installed.' );
+ }
}
sub filesMatch {
- my $file1 = shift;
- my $file2 = shift;
- my $a;
- my $b;
-
- while (1) {
- $a = getc($file1);
- $b = getc($file2);
-
- if (!defined($a) && !defined($b)) { # both files end at same place
- return 1;
- }
- elsif (
- !defined($b) || # file2 ends first
- !defined($a) || # file1 ends first
- $a ne $b
- ) { # a and b not the same
- return 0;
- }
- }
+ my $file1 = shift;
+ my $file2 = shift;
+ my $a;
+ my $b;
+
+ while (1) {
+ $a = getc($file1);
+ $b = getc($file2);
+
+ if (!defined($a) && !defined($b)) { # both files end at same place
+ return 1;
+ }
+ elsif (
+ !defined($b) || # file2 ends first
+ !defined($a) || # file1 ends first
+ $a ne $b
+ ) { # a and b not the same
+ return 0;
+ }
+ }
}
sub minTest {
- my $filename = shift;
- my $opts = shift || {};
-
- open(INFILE, 't/html/' . $filename . '.html') or die("couldn't open file");
- open(GOTFILE, '>t/html/' . $filename . '-got.html') or die("couldn't open file");
-
- my $html = join( '', <INFILE> );
- HTML::Packer::minify( \$html, $opts );
- print GOTFILE $html;
- close(INFILE);
- close(GOTFILE);
-
- open(EXPECTEDFILE, 't/html/' . $filename . '-expected.html') or die("couldn't open file");
- open(GOTFILE, 't/html/' . $filename . '-got.html') or die("couldn't open file");
- ok(filesMatch(GOTFILE, EXPECTEDFILE));
- close(EXPECTEDFILE);
- close(GOTFILE);
+ my $filename = shift;
+ my $opts = shift || {};
+ my $message = shift || '';
+
+ open(INFILE, 't/html/' . $filename . '.html') or die("couldn't open file");
+ open(GOTFILE, '>t/html/' . $filename . '-got.html') or die("couldn't open file");
+
+ my $html = join( '', <INFILE> );
+
+ my $packer = HTML::Packer->init();
+
+ $packer->minify( \$html, $opts );
+ print GOTFILE $html;
+ close(INFILE);
+ close(GOTFILE);
+
+ open(EXPECTEDFILE, 't/html/' . $filename . '-expected.html') or die("couldn't open file");
+ open(GOTFILE, 't/html/' . $filename . '-got.html') or die("couldn't open file");
+ ok(filesMatch(GOTFILE, EXPECTEDFILE), $message );
+ close(EXPECTEDFILE);
+ close(GOTFILE);
}
More information about the Pkg-perl-cvs-commits
mailing list