سؤال

I'm trying to use a file all.css containing some classes and want to get a file green.css containing only green classes.

I'm using perl CSS module, any suggestions on how can i use it to search for lines that contain .green and end with { and then extract the css block ?

I'm new to perl, So far i tried to just print the selector lines matching "green" but i can't get it to work:

my $css = CSS->new( { 'parser' => 'CSS::Parse::Lite'} );

print $styleSheetPath;
$css->read_file($styleSheetPath);

 open my $fileHandle, ">>", "green.css" or die "Can't open 'green.css'\n";

 #search for lines that contain .green and end { and then extract css block 
 #and write to green.css
 serialize($css);

 sub serialize{
       my ($obj) = @_;

    for my $style (@{$obj->{styles}}){
         print join "\n ", map {$_->{name}} @{$style->{selectors}};
         if ( grep( /green/, @{$style->{selectors}} )) {
           print "green matches ";
            print $_->{name};
         }

       }
}
هل كانت مفيدة؟

المحلول

It helps to read the documentation of the software you are working with. Call the get_style_by_selector method with a .green argument to find the styles.

use CSS qw();
my $css = CSS->new;
$css->read_string('.red { clear: both; } .green { clear: both; }');
$css->get_style_by_selector('.green')->to_string;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top