| 1 | package CSS::Struct::Output::Indent::ANSIColor; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 3 | ▶ | use base qw(CSS::Struct::Output::Indent); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 3 | ▶ | use Indent; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 3 | ▶ | use Readonly; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 3 | ▶ | use Term::ANSIColor; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | # Constants. | ||||||||||||||||||
| 12 | 3 | Readonly::Scalar my $EMPTY_STR => q{}; | |||||||||||||||||
| 13 | 3 | Readonly::Scalar my $SPACE => q{ }; | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 3 | our $CSS_COMMENT = 'r064g085b107'; | |||||||||||||||||
| 16 | 3 | our $CSS_SELECTOR_ELEMENT = 'red'; | |||||||||||||||||
| 17 | 3 | our $CSS_SELECTOR_CLASS = 'r255g156b000'; | |||||||||||||||||
| 18 | 3 | our $CSS_SELECTOR_ID = 'r255g156b000'; | |||||||||||||||||
| 19 | 3 | our $CSS_AT_RULE_KEYWORD = 'r255g156b000'; | |||||||||||||||||
| 20 | 3 | our $CSS_AT_RULE_VALUE = 'yellow'; | |||||||||||||||||
| 21 | 3 | our $CSS_DEFINITION_KEY = 'green'; | |||||||||||||||||
| 22 | 3 | our $CSS_DEFINITION_KEY_PREFIX = 'blue'; | |||||||||||||||||
| 23 | 3 | our $CSS_DEFINITION_VALUE_NUMBER = 'red'; | |||||||||||||||||
| 24 | 3 | our $CSS_DEFINITION_VALUE_STRING = 'blue'; | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 3 | our $VERSION = 0.02; | |||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | # At-rules. | ||||||||||||||||||
| 29 | sub _put_at_rules { | ||||||||||||||||||
| 30 | 0 | ▶ | my ($self, $at_rule, $value) = @_; | ||||||||||||||||
|
Subroutine
_put_at_rules: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 31 | 0 | push @{$self->{'flush_code'}}, color($CSS_AT_RULE_KEYWORD).$at_rule.color('reset'). | |||||||||||||||||
| 32 | ' "'.color($CSS_AT_RULE_VALUE).$value.color('reset').'";'; | ||||||||||||||||||
| 33 | 0 | $self->{'processed'} = 1; | |||||||||||||||||
| 34 | 0 | return; | |||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | # Comment. | ||||||||||||||||||
| 38 | sub _put_comment { | ||||||||||||||||||
| 39 | 0 | ▶ | my ($self, @comments) = @_; | ||||||||||||||||
|
Subroutine
_put_comment: not called CC=5 SCAR=34.0
| |||||||||||||||||||
| 40 | 0 | ▶ | if (! $self->{'skip_comments'}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | 0 | push @comments, $SPACE.$self->{'comment_delimeters'}->[1]; | |||||||||||||||||
| 42 | 0 | unshift @comments, $self->{'comment_delimeters'}->[0].$SPACE; | |||||||||||||||||
| 43 | 0 | ▶ | if ($self->{'processed'}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 0 | push @{$self->{'flush_code'}}, $EMPTY_STR; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | 0 | my $comment = color($CSS_COMMENT). | |||||||||||||||||
| 47 | (join $EMPTY_STR, @comments). | ||||||||||||||||||
| 48 | color('reset'); | ||||||||||||||||||
| 49 | 0 | ▶ | if (@{$self->{'tmp_code'}}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 50 | 0 | my $sep = $EMPTY_STR; | |||||||||||||||||
| 51 | 0 | ▶ | if ($self->{'comment_after_selector'} == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 0 | $sep = $self->{'output_sep'}; | |||||||||||||||||
| 53 | 0 | pop @{$self->{'tmp_code'}}; | |||||||||||||||||
| 54 | } | ||||||||||||||||||
| 55 | 0 | push @{$self->{'tmp_code'}}, ($sep) x 2, $comment, | |||||||||||||||||
| 56 | 0 | $self->{'output_sep'}; | |||||||||||||||||
| 57 | 0 | $self->{'comment_after_selector'} += 4; | |||||||||||||||||
| 58 | } else { | ||||||||||||||||||
| 59 | 0 | push @{$self->{'flush_code'}}, | |||||||||||||||||
| 60 | 0 | $self->{'indent'}->get.$comment; | |||||||||||||||||
| 61 | } | ||||||||||||||||||
| 62 | 0 | $self->{'processed'} = 0; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | # Definition. | ||||||||||||||||||
| 68 | sub _put_definition { | ||||||||||||||||||
| 69 | 0 | ▶ | my ($self, $key, $value) = @_; | ||||||||||||||||
|
Subroutine
_put_definition: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 0 | $self->_check_opened_selector; | |||||||||||||||||
| 72 | 0 | $self->_flush_tmp; | |||||||||||||||||
| 73 | 0 | push @{$self->{'flush_code'}}, | |||||||||||||||||
| 74 | 0 | $self->{'indent'}->get. | |||||||||||||||||
| 75 | $self->_colorize_definition_key($key). | ||||||||||||||||||
| 76 | ':'. | ||||||||||||||||||
| 77 | $SPACE. | ||||||||||||||||||
| 78 | $self->_colorize_definition_value($value). | ||||||||||||||||||
| 79 | ';'; | ||||||||||||||||||
| 80 | 0 | $self->{'processed'} = 1; | |||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||
| 83 | } | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | # Selectors. | ||||||||||||||||||
| 86 | sub _put_selector { | ||||||||||||||||||
| 87 | 0 | ▶ | my ($self, $selector) = @_; | ||||||||||||||||
|
Subroutine
_put_selector: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 0 | $selector = $self->_split_selector($selector); | |||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 0 | push @{$self->{'tmp_code'}}, $selector, ',', ' '; | |||||||||||||||||
| 92 | 0 | $self->{'comment_after_selector'} = 0; | |||||||||||||||||
| 93 | 0 | $self->{'open_selector'} = 1; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | sub _split_selector { | ||||||||||||||||||
| 99 | 0 | ▶ | my ($self, $selector) = @_; | ||||||||||||||||
|
Subroutine
_split_selector: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 0 | my @selector; | |||||||||||||||||
| 102 | 0 | ▶ | if ($selector =~ m/\s+/ms) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 0 | @selector = split m/\s+/ms, $selector; | |||||||||||||||||
| 104 | } else { | ||||||||||||||||||
| 105 | 0 | @selector = $selector; | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 0 | my $ret_selector; | |||||||||||||||||
| 109 | 0 | foreach my $one_selector (@selector) { | |||||||||||||||||
| 110 | 0 | ▶ | if (defined $ret_selector) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 111 | 0 | $ret_selector .= ' '; | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | 0 | $ret_selector .= $self->_detect_selector($one_selector); | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | return $ret_selector; | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | sub _detect_selector { | ||||||||||||||||||
| 120 | 0 | ▶ | my ($self, $selector) = @_; | ||||||||||||||||
|
Subroutine
_detect_selector: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 0 | ▶ | if ($selector =~ m/^\./ms) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 123 | 0 | $selector = $self->_colorize_selector($selector, 'class'); | |||||||||||||||||
| 124 | } elsif ($selector =~ m/^(.*)(#.*)$/ms) { | ||||||||||||||||||
| 125 | 0 | $selector = $self->_colorize_selector($1, 'element'). | |||||||||||||||||
| 126 | $self->_colorize_selector($2, 'id'); | ||||||||||||||||||
| 127 | } elsif ($selector =~ m/^(.*)(\..*)$/ms) { | ||||||||||||||||||
| 128 | 0 | $selector = $self->_colorize_selector($1, 'element'). | |||||||||||||||||
| 129 | $self->_colorize_selector($2, 'class'); | ||||||||||||||||||
| 130 | } else { | ||||||||||||||||||
| 131 | 0 | $selector = $self->_colorize_selector($selector, 'element'); | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 0 | return $selector; | |||||||||||||||||
| 135 | } | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | sub _colorize_selector { | ||||||||||||||||||
| 138 | 0 | ▶ | my ($self, $selector, $type) = @_; | ||||||||||||||||
|
Subroutine
_colorize_selector: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 0 | ▶ | if ($type eq 'element') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 141 | 0 | $selector = color($CSS_SELECTOR_ELEMENT).$selector.color('reset'); | |||||||||||||||||
| 142 | } elsif ($type eq 'class') { | ||||||||||||||||||
| 143 | 0 | $selector = color($CSS_SELECTOR_CLASS).$selector.color('reset'); | |||||||||||||||||
| 144 | } elsif ($type eq 'id') { | ||||||||||||||||||
| 145 | 0 | $selector = color($CSS_SELECTOR_ID).$selector.color('reset'); | |||||||||||||||||
| 146 | } | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | 0 | return $selector; | |||||||||||||||||
| 149 | } | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | sub _colorize_definition_key { | ||||||||||||||||||
| 152 | 0 | ▶ | my ($self, $key) = @_; | ||||||||||||||||
|
Subroutine
_colorize_definition_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | 0 | ▶ | if ($key =~ m/^--/ms) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 155 | 0 | $key = color($CSS_DEFINITION_KEY_PREFIX).$key.color('reset'); | |||||||||||||||||
| 156 | } else { | ||||||||||||||||||
| 157 | 0 | $key = color($CSS_DEFINITION_KEY).$key.color('reset'); | |||||||||||||||||
| 158 | } | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | 0 | return $key; | |||||||||||||||||
| 161 | } | ||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | sub _colorize_definition_value { | ||||||||||||||||||
| 164 | 0 | ▶ | my ($self, $value) = @_; | ||||||||||||||||
|
Subroutine
_colorize_definition_value: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 165 | |||||||||||||||||||
| 166 | 0 | ▶ | if ($value =~ m/^\d+$/ms) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 167 | 0 | $value = color($CSS_DEFINITION_VALUE_NUMBER).$value.color('reset'); | |||||||||||||||||
| 168 | } else { | ||||||||||||||||||
| 169 | 0 | $value = color($CSS_DEFINITION_VALUE_STRING).$value.color('reset'); | |||||||||||||||||
| 170 | } | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | 0 | return $value; | |||||||||||||||||
| 173 | } | ||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | 3 | 1; | |||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||