在一个文件中提取特定的css类

问题描述:

我试图使用一个文件all.css包含一些类,并且想要获得仅包含绿色类的文件green.css在一个文件中提取特定的css类

我使用perlCSS模块,我如何能使用它的任何建议,以搜索包含.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}; 
     } 

     } 
} 
+0

@innaM编辑了我的问题 –

它有助于阅读的的documentation您正在使用的软件。使用.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;