結城浩のはてなブログ

ふと思いついたことをパタパタと書いてます。

yaml2css.pl

CSS JSON (JavaScript)CSS YAML (Perl)を見て、後者のほうにCSSJSON-INHERIT-SELECTORを入れました。

#!/usr/bin/perl
use strict;
use warnings;
use YAML;

local $/;
print yaml2css(<>);

sub yaml2css {
    my $yaml = YAML::Load(shift);
    my $css;
    foreach my $selector (keys %$yaml) {
        $css .= sprintf "%s {\n", $selector;
        foreach my $property (keys %{$yaml->{$selector}}) {
            if ($property eq 'CSSJSON-INHERIT-SELECTOR') {
                my $copied_selector = $yaml->{$selector}->{$property};
                for my $copied_property (keys %{$yaml->{$copied_selector}}) {
                    $css .= sprintf "\t%s: %s;\n", $copied_property, $yaml->{$copied_selector}->{$copied_property};
                }
            } else {
                $css .= sprintf "\t%s: %s;\n", $property, $yaml->{$selector}->{$property};
            }
        }
        $css .= "}\n";
    }
    return $css;
}

1;

# This script is based on
# http://naoya.g.hatena.ne.jp/naoya/20061109/1163067893
# http://www.featureblend.com/css-json.html
# http://nyarla.net/blog/css-yaml