提取文件中的特定css类



我正在尝试使用一个包含某些类的文件all.css,并希望获得一个仅包含绿色类的文件green.css

我使用的是perl CSS模块,关于如何使用它搜索包含.green并以{结尾的行,然后提取css块,有什么建议吗?

我是perl的新手,到目前为止,我试图只打印与"绿色"匹配的选择器行,但我无法使其工作:

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};
         }
       }
}

阅读正在使用的软件的文档会有所帮助。使用.green参数调用get_style_by_selector方法以查找样式。

use CSS qw();
my $css = CSS->new;
$css->read_string('.red { clear: both; } .green { clear: both; }');
$css->get_style_by_selector('.green')->to_string;

最新更新