File: | blib/lib/CSS/Adaptor/Pretty.pm |
Coverage: | 98.1% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package CSS::Adaptor::Pretty; | |||||
2 | ||||||
3 | 2 2 2 | 12 4 12 | use strict; | |||
4 | 2 2 2 | 13 5 15 | use warnings; | |||
5 | ||||||
6 | 2 2 2 | 16 5 15 | use base 'CSS::Adaptor'; | |||
7 | ||||||
8 | ||||||
9 | sub format_stylesheet { | |||||
10 | 1 | 4 | my ($self, $stylesheet) = @_; | |||
11 | ||||||
12 | 1 | 2 | my @blobs; | |||
13 | ||||||
14 | 1 1 | 2 4 | for (@{$stylesheet->{items}}){ | |||
15 | ||||||
16 | 2 | 12 | push @blobs, $self->format_atrule($_) if ref $_ eq 'CSS::AtRule'; | |||
17 | 2 | 11 | push @blobs, $self->format_ruleset($_) if ref $_ eq 'CSS::Ruleset'; | |||
18 | } | |||||
19 | ||||||
20 | 1 | 12 | return join "\n", @blobs; | |||
21 | } | |||||
22 | ||||||
23 | sub format_ruleset { | |||||
24 | 1 | 2 | my ($self, $ruleset) = @_; | |||
25 | ||||||
26 | 1 | 3 | my $longest = 0; | |||
27 | ||||||
28 | 1 1 | 1 4 | for (@{$ruleset->{declarations}}){ | |||
29 | 1 | 3 | my $p = $_->{property}; | |||
30 | 1 | 8 | $p =~ s/^\s*(.*?)\s*$/$1/; | |||
31 | 1 | 6 | $longest = length $p if length $p > $longest; | |||
32 | } | |||||
33 | ||||||
34 | 1 | 6 | $self->{tabs} = int (($longest + 1)/8); | |||
35 | ||||||
36 | 1 1 1 | 2 5 4 | my $selectors = join ', ' , map {$self->format_selector($_) } @{$ruleset->{selectors}}; | |||
37 | 1 1 1 | 3 3 4 | my $declarations = join ";\n", map {$self->format_declaration($_)} @{$ruleset->{declarations}}; | |||
38 | ||||||
39 | 1 | 6 | return $selectors." {\n".$declarations.";\n}\n"; | |||
40 | } | |||||
41 | ||||||
42 | sub format_declaration { | |||||
43 | 1 | 3 | my ($self, $declaration) = @_; | |||
44 | ||||||
45 | 1 | 3 | my $val = $declaration->{simple_value}; | |||
46 | 1 | 5 | $val =~ s/^\s*(.*?)\s*$/$1/; | |||
47 | ||||||
48 | 1 | 3 | my $prop = $declaration->{property}; | |||
49 | 1 | 4 | $prop =~ s/^\s*(.*?)\s*$/$1/; | |||
50 | ||||||
51 | 1 | 2 | my $sp = "\t"; | |||
52 | 1 | 4 | my $this = int (((length $prop) + 1)/8); | |||
53 | 1 | 5 | $sp .= "\t" x ($self->{tabs} - $this); | |||
54 | ||||||
55 | 1 | 5 | return "\t$prop:$sp$val"; | |||
56 | } | |||||
57 | ||||||
58 | 1; | |||||
59 |