/usr/share/perl5/CSS/Tiny.pm is in libcss-tiny-perl 1.19-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | package CSS::Tiny;
# See POD at end for docs
use strict;
BEGIN {
require 5.004;
$CSS::Tiny::VERSION = '1.19';
$CSS::Tiny::errstr = '';
}
# Create an empty object
sub new { bless {}, shift }
# Create an object from a file
sub read {
my $class = shift;
# Check the file
my $file = shift or return $class->_error( 'You did not specify a file name' );
return $class->_error( "The file '$file' does not exist" ) unless -e $file;
return $class->_error( "'$file' is a directory, not a file" ) unless -f _;
return $class->_error( "Insufficient permissions to read '$file'" ) unless -r _;
# Read the file
local $/ = undef;
open( CSS, $file ) or return $class->_error( "Failed to open file '$file': $!" );
my $contents = <CSS>;
close( CSS );
$class->read_string( $contents )
}
# Create an object from a string
sub read_string {
my $self = ref $_[0] ? shift : bless {}, shift;
# Flatten whitespace and remove /* comment */ style comments
my $string = shift;
$string =~ tr/\n\t/ /;
$string =~ s!/\*.*?\*\/!!g;
# Split into styles
foreach ( grep { /\S/ } split /(?<=\})/, $string ) {
unless ( /^\s*([^{]+?)\s*\{(.*)\}\s*$/ ) {
return $self->_error( "Invalid or unexpected style data '$_'" );
}
# Split in such a way as to support grouped styles
my $style = $1;
my $properties = $2;
$style =~ s/\s{2,}/ /g;
my @styles = grep { s/\s+/ /g; 1; } grep { /\S/ } split /\s*,\s*/, $style;
foreach ( @styles ) { $self->{$_} ||= {} }
# 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.
This module is primarily for reading and writing simple files, and anything
we write shouldn't need to have documentation/comments. If you need
something with more power, move up to CSS.pm. With the increasing complexity
of CSS, this is becoming more common, but many situations can still live
with simple CSS files.
=head2 CSS Feature Support
C<CSS::Tiny> supports grouped styles of the form
C<this, that { color: blue }> correctly when reading, ungrouping them into
the hash structure. However, it will not restore the grouping should you
write the file back out. In this case, an entry in the original file of
the form
H1, H2 { color: blue }
would become
H1 { color: blue }
H2 { color: blue }
C<CSS::Tiny> handles nested styles of the form C<P EM { color: red }>
in reads and writes correctly, making the property available in the
form
$CSS->{'P EM'}->{color}
C<CSS::Tiny> ignores comments of the form C</* comment */> on read
correctly, however these comments will not be written back out to the
file.
=head1 CSS FILE SYNTAX
Files are written in a relatively human-orientated form, as follows:
H1 {
color: blue;
}
.this {
color: red;
font-size: 10px;
}
P EM {
color: yellow;
}
When reading and writing, all property descriptors, for example C<color>
and C<font-size> in the example above, are converted to lower case. As an
example, take the following CSS.
P {
Font-Family: Verdana;
}
To get the value C<'Verdana'> from the object C<$CSS>, you should
reference the key C<$CSS-E<gt>{P}-E<gt>{font-family}>.
=head1 METHODS
=head2 new
The constructor C<new> creates and returns an empty C<CSS::Tiny> object.
=head2 read $filename
The C<read> constructor reads a CSS stylesheet, and returns a new
C<CSS::Tiny> object containing the properties in the file.
Returns the object on success, or C<undef> on error.
=head2 read_string $string
The C<read_string> constructor reads a CSS stylesheet from a string.
Returns the object on success, or C<undef> on error.
=head2 clone
The C<clone> method creates an identical copy of an existing C<CSS::Tiny>
object.
=head2 write_string
Generates the stylesheet for the object and returns it as a string.
=head2 write
The C<write $filename> generates the stylesheet for the properties, and
writes it to disk. Returns true on success. Returns C<undef> on error.
=head2 html
The C<html> method generates the CSS, but wrapped in a C<style> HTML tag,
so that it can be dropped directly onto a HTML page.
=head2 xhtml
The C<html> method generates the CSS, but wrapped in a C<style> XHTML tag,
so that it can be dropped directly onto an XHTML page.
=head2 errstr
When an error occurs, you can retrieve the error message either from the
C<$CSS::Tiny::errstr> variable, or using the C<errstr> method.
=head1 CAVEATS
=head2 CSS Rule Order
While the order of rules in CSS is important, this is one of the features
that is sacrificed to keep things small and dependency-free. If you need
to preserve order yourself, we recommend that you upgrade to the more
powerful L<CSS> module.
If this is not possible in your case, alternatively it can be done with the
help of another module such as L<Tie::IxHash>:
my $css = CSS::Tiny->new;
tie %$css, 'Tie::IxHash';
$css->read('style.css');
Note: You will also need to remember to add the additional dependency to
your code or module in this case.
=head1 SUPPORT
Bugs should be reported via the CPAN bug tracker at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CSS-Tiny>
For other issues, or commercial enhancement or support, contact the author.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 SEE ALSO
L<CSS>, L<http://www.w3.org/TR/REC-CSS1>, L<Config::Tiny>, L<http://ali.as/>
=head1 COPYRIGHT
Copyright 2002 - 2010 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|