View on GitHub

CSS-Writer-raku

AST Serializer; compatible with CSS::Grammar and CSS::Module

[Raku CSS Project] / [CSS-Writer]

CSS-Writer-raku

AST writer/serializer module. Compatible with CSS:Module and CSS::Grammar.

Examples

Serialize a declaration (ruleset); converting named colors to RGB masks

use CSS::Writer;
my CSS::Writer $css-writer .= new( :!pretty, :color-values, :color-masks );
say $css-writer.write(
    :ruleset{
        :selectors[ :selector[ { :simple-selector[ { :element-name<h1> } ] } ] ],
        :declarations[
             { :ident<font-size>, :expr[ :pt(12) ] },
             { :ident<color>,     :expr[ :ident<white> ] },
             { :ident<z-index>,   :expr[ :num(-9) ] },
            ],
    });

# output: h1 { font-size:12pt; color:#FFF; z-index:-9; }

Tidy and reduce size of CSS

use CSS::Writer;
use CSS::Grammar::CSS3;

sub parse-stylesheet($css) {
    use CSS::Grammar::CSS3;
    use CSS::Grammar::Actions;
    my CSS::Grammar::Actions $actions .= new;
    CSS::Grammar::CSS3.parse($css, :$actions)
       or die "unable to parse: $css";

    return $/.ast
}

my CSS::Writer $css-writer .= new( :!pretty );
my $stylesheet = parse-stylesheet( 'H1{  cOlor: RED; z-index  : -3}' );

say $css-writer.write( $stylesheet );

# output: h1 { color:red; z-index:-3; }

Writer Options

Usage Notes