CSS-Tiny
view release on metacpan or search on metacpan
lib/CSS/Tiny.pm view on Meta::CPAN
# Split into properties
foreach ( grep { /\S/ } split /\;/, $properties ) {
unless ( /^\s*(\*?[\w._-]+)\s*:\s*(.*?)\s*$/ ) {
return $self->_error( "Invalid or unexpected property '$_' in style '$style'" );
}
foreach ( @styles ) { $self->{$_}->{lc $1} = $2 }
}
}
$self
}
# Copy an object, using Clone.pm if available
BEGIN { local $@; eval "use Clone 'clone';"; eval <<'END_PERL' if $@; }
sub clone {
my $self = shift;
my $copy = ref($self)->new;
foreach my $key ( keys %$self ) {
my $section = $self->{$key};
$copy->{$key} = {};
foreach ( keys %$section ) {
$copy->{$key}->{$_} = $section->{$_};
}
}
$copy;
}
END_PERL
# Save an object to a file
sub write {
my $self = shift;
my $file = shift or return $self->_error( 'No file name provided' );
# Write the file
open( CSS, '>'. $file ) or return $self->_error( "Failed to open file '$file' for writing: $!" );
print CSS $self->write_string;
close( CSS );
}
# Save an object to a string
sub write_string {
my $self = shift;
# Iterate over the styles
# Note: We use 'reverse' in the sort to avoid a special case related
# to A:hover even though the file ends up backwards and looks funny.
# See http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
my $contents = '';
foreach my $style ( reverse sort keys %$self ) {
$contents .= "$style {\n";
foreach ( sort keys %{ $self->{$style} } ) {
$contents .= "\t" . lc($_) . ": $self->{$style}->{$_};\n";
}
$contents .= "}\n";
}
return $contents;
}
# Generate a HTML fragment for the CSS
sub html {
my $css = $_[0]->write_string or return '';
return "<style type=\"text/css\">\n<!--\n${css}-->\n</style>";
}
# Generate an xhtml fragment for the CSS
sub xhtml {
my $css = $_[0]->write_string or return '';
return "<style type=\"text/css\">\n/* <![CDATA[ */\n${css}/* ]]> */\n</style>";
}
# Error handling
sub errstr { $CSS::Tiny::errstr }
sub _error { $CSS::Tiny::errstr = $_[1]; undef }
1;
__END__
=pod
=head1 NAME
CSS::Tiny - Read/Write .css files with as little code as possible
=head1 SYNOPSIS
# In your .css file
H1 { color: blue }
H2 { color: red; font-family: Arial }
.this, .that { color: yellow }
# In your program
use CSS::Tiny;
# Create a CSS stylesheet
my $CSS = CSS::Tiny->new();
# Open a CSS stylesheet
$CSS = CSS::Tiny->read( 'style.css' );
# Reading properties
my $header_color = $CSS->{H1}->{color};
my $header2_hashref = $CSS->{H2};
my $this_color = $CSS->{'.this'}->{color};
my $that_color = $CSS->{'.that'}->{color};
# Changing styles and properties
$CSS->{'.newstyle'} = { color => '#FFFFFF' }; # Add a style
$CSS->{H1}->{color} = 'black'; # Change a property
delete $CSS->{H2}; # Delete a style
# Save a CSS stylesheet
$CSS->write( 'style.css' );
# Get the CSS as a <style>...</style> tag
$CSS->html;
=head1 DESCRIPTION
C<CSS::Tiny> is a perl class to read and write .css stylesheets with as
little code as possible, reducing load time and memory overhead. CSS.pm
requires about 2.6 meg or ram to load, which is a large amount of
overhead if you only want to do trivial things.
Memory usage is normally scoffed at in Perl, but in my opinion should be
at least kept in mind.
( run in 1.207 second using v1.01-cache-2.11-cpan-364913b4093 )