CSS::Declare works with arrays instead of arrayrefs
[catagits/Web-Simple.git] / lib / CSS / Declare.pm
1 package CSS::Declare;
2
3 use strict;
4 use warnings;
5
6 use Perl6::Gather;
7
8 my $IN_SCOPE = 0;
9
10 sub import {
11   die "Can't import CSS::Declare into a scope when already compiling one that uses it"
12     if $IN_SCOPE;
13   my ($class, @args) = @_;
14   my $opts = shift(@args) if ref($args[0]) eq 'HASH';
15   my $target = $class->_find_target(0, $opts);
16   my $unex = $class->_export_tags_into($target);
17   $class->_install_unexporter($unex);
18   $IN_SCOPE = 1;
19 }
20
21 sub _find_target {
22   my ($class, $extra_levels, $opts) = @_;
23   return $opts->{into} if defined($opts->{into});
24   my $level = ($opts->{into_level} || 1) + $extra_levels;
25   return (caller($level))[0];
26 }
27
28 my @properties = qw{
29 background
30 background_color
31 border
32 border_collapse
33 border_top
34 color
35 float
36 font_family
37 font_size
38 list_style_type
39 margin
40 padding
41 };
42
43 sub _export_tags_into {
44   my ($class, $into) = @_;
45    for my $property (@properties) {
46       my $property_name = $property;
47       $property_name =~ tr/_/-/;
48       no strict 'refs';
49       *{"$into\::$property"} = sub ($) { return ($property_name => $_[0]) };
50    }
51   return sub {
52     foreach my $property (@properties) {
53       no strict 'refs';
54       delete ${"${into}::"}{$property}
55     }
56     $IN_SCOPE = 0;
57   };
58 }
59
60 sub _install_unexporter {
61   my ($class, $unex) = @_;
62   $^H |= 0x120000; # localize %^H
63   $^H{'CSS::Declare::Unex'} = bless($unex, 'CSS::Declare::Unex');
64 }
65
66 sub to_css_string {
67    my @css = @_;
68    return join q{ }, gather {
69       while (my ($selector, $declarations) = splice(@css, 0, 2)) {
70          take "$selector "._generate_declarations($declarations)
71       }
72    };
73 }
74
75 sub _generate_declarations {
76    my $declarations = shift;
77
78    return '{'.join(q{;}, gather {
79       while (my ($property, $value) = splice(@{$declarations}, 0, 2)) {
80          take "$property:$value"
81       }
82    }).'}';
83 }
84
85 package CSS::Declare::Unex;
86
87 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
88
89 1;