【问题标题】:Extract specific css classes in a file提取文件中的特定 css 类
【发布时间】:2013-07-19 09:16:16
【问题描述】:

我正在尝试使用包含一些类的文件all.css,并希望获得一个仅包含绿色类的文件green.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};
         }

       }
}

【问题讨论】:

  • @innaM 编辑了我的问题

标签: css perl cpan css-parsing


【解决方案1】:

阅读您正在使用的软件的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;

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2013-02-02
    • 2014-05-13
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2011-07-25
    相关资源
    最近更新 更多