| 1 | package Syntax::Highlight::CSS; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '0.0103'; | |||||||||
| 7 | |||||||||||
| 8 | # # # | ||||||||||
| 9 | # # # | ||||||||||
| 10 | # # # | ||||||||||
| 11 | # | ||||||||||
| 12 | # | ||||||||||
| 13 | # | ||||||||||
| 14 | # WARNING!!!! The code presented below is NOT suitable for persons | ||||||||||
| 15 | # with heart deseases and mental disorders, | ||||||||||
| 16 | # | ||||||||||
| 17 | # LOOK AT IT AT YOUR OWN RISK!!!! | ||||||||||
| 18 | # | ||||||||||
| 19 | # | ||||||||||
| 20 | # | ||||||||||
| 21 | # # # | ||||||||||
| 22 | # # # | ||||||||||
| 23 | # # # | ||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 26 | 2 | my $class = shift; | |||||||||
| 27 | 2 | my %args = @_; | |||||||||
| 28 | $args{ +lc } = delete $args{$_} | ||||||||||
| 29 | 2 | for keys %args; | |||||||||
| 30 | |||||||||||
| 31 | 2 | my $self = bless {}, $class; | |||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | $self->_pre( defined $args{pre} ? $args{pre} : 1 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 2 | ▶ | $self->_line_numbers( defined $args{nnn} ? $args{nnn} : 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 2 | return $self; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 2 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 40 | 2 | my ( $self, $input ) = @_; | |||||||||
| 41 | |||||||||||
| 42 | 2 | ▶ | $input = '' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | unless defined $input; | ||||||||||
| 44 | |||||||||||
| 45 | 2 | my $text = $self->_parse_css( $input ); | |||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | if ( $self->_line_numbers ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 1 | $text = $self->_add_line_numbers( $text ); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 2 | ▶ | if ( $self->_pre ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 2 | $text = '<pre class="css-code">' . $text . '</pre>'; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | return $text; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 1 | ▶ | sub _add_line_numbers { | ||||||||
|
Subroutine
_add_line_numbers: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 1 | my ( $self, $text ) = @_; | |||||||||
| 60 | 1 | my $line = 0; | |||||||||
| 61 | 1 | $text =~ s|^| | |||||||||
| 62 | 7 | '<span class="ch-l">' | |||||||||
| 63 | . sprintf('%3d', $line++) | ||||||||||
| 64 | . '</span> ' | ||||||||||
| 65 | |gem; | ||||||||||
| 66 | |||||||||||
| 67 | 1 | return $text; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | sub _parse_css { | ||||||||
|
Subroutine
_parse_css: called CC=3 SCAR=11.0
| |||||||||||
| 71 | 2 | my $self = shift; | |||||||||
| 72 | |||||||||||
| 73 | 2 | $_ = shift; # input CSS | |||||||||
| 74 | |||||||||||
| 75 | # yes YES, <SYNTAX<>blah blah is really retarded.. but oh well | ||||||||||
| 76 | 2 | my @quoted; | |||||||||
| 77 | 2 | s/(" .+? (?<!\\)" )/ | |||||||||
| 78 | 0 | push @quoted, $1; | |||||||||
| 79 | 0 | '<SYNTAX<>HIGHLIGHT<>PARSER<>QUOTED<>' . $#quoted . '>' | |||||||||
| 80 | /gsex; | ||||||||||
| 81 | |||||||||||
| 82 | 2 | my @comments; | |||||||||
| 83 | 2 | s{(/\* .+? \*/)}{ | |||||||||
| 84 | 2 | push @comments, $1; | |||||||||
| 85 | 2 | '<SYNTAX<>HIGHLIGHT<>PARSER<>COMMENT<>' . $#comments . '>' | |||||||||
| 86 | }gsex; | ||||||||||
| 87 | |||||||||||
| 88 | 2 | my @rulesets; | |||||||||
| 89 | 2 | s/(?<={)([^{}]+)(?=})/ | |||||||||
| 90 | 4 | push @rulesets, $1; | |||||||||
| 91 | 4 | '<SYNTAX<>HIGHLIGHT<>PARSER<>RULESET<>' . $#rulesets . '>'; | |||||||||
| 92 | /gex; | ||||||||||
| 93 | |||||||||||
| 94 | # parse selectors | ||||||||||
| 95 | 2 | s# (?:\A | (?<=[{};]) ) | |||||||||
| 96 | (\s*) | ||||||||||
| 97 | ( [^;{}]+? ) | ||||||||||
| 98 | (\s*) | ||||||||||
| 99 | (?=[{;])# | ||||||||||
| 100 | 10 | $1 | |||||||||
| 101 | . $self->_parse_sel($2) | ||||||||||
| 102 | . $3 | ||||||||||
| 103 | #gex; | ||||||||||
| 104 | |||||||||||
| 105 | # properties/values; | ||||||||||
| 106 | 2 | for my $ruleset ( @rulesets ) { | |||||||||
| 107 | 4 | $ruleset =~ s#(?: \A | (?<=[;]) ) | |||||||||
| 108 | (\s*) ([a-zA-Z-]+) (\s*) : (\s*) (.+?) (\s*) | ||||||||||
| 109 | (?=[;}]|\z)#$1<span class="ch-p">$2</span>$3:$4<span class="ch-v">$5</span>$6#gsx; | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 2 | s#<SYNTAX<>HIGHLIGHT<>PARSER<>RULESET<>(\d+)>#$rulesets[$1]#g; | |||||||||
| 113 | |||||||||||
| 114 | 2 | s#<span\sclass="ch-sel"> | |||||||||
| 115 | (<SYNTAX<>HIGHLIGHT<>PARSER<>COMMENT<>\d+>) | ||||||||||
| 116 | (\s*)#$1$2<span class="ch-sel">#xg; | ||||||||||
| 117 | |||||||||||
| 118 | 2 | s{(<span class="ch-sel">)((?:(?!</span>).)+)}{ | |||||||||
| 119 | 4 | my ($one, $two) = ($1, $2); | |||||||||
| 120 | 4 | ▶ | $two =~ /(.+?)(\s*<SYNTAX<>HIGHLIGHT<>PARSER<>COMMENT<>\d>\s*)(.+)/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | ? qq|$one$1</span>$2<span class="ch-sel">$3| | ||||||||||
| 122 | : "$one$two"; | ||||||||||
| 123 | }ges; | ||||||||||
| 124 | |||||||||||
| 125 | 2 | s#<SYNTAX<>HIGHLIGHT<>PARSER<>COMMENT<>(\d+)>#<span class="ch-com">$comments[$1]</span>#g; | |||||||||
| 126 | |||||||||||
| 127 | 2 | s#<SYNTAX<>HIGHLIGHT<>PARSER<>QUOTED<>(\d+)>#$quoted[$1]#g; | |||||||||
| 128 | |||||||||||
| 129 | 2 | return $_; | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 10 | ▶ | sub _parse_sel { | ||||||||
|
Subroutine
_parse_sel: called CC=2 SCAR=6.9
| |||||||||||
| 133 | 10 | my $self = shift; | |||||||||
| 134 | 10 | $_ = shift; # selector | |||||||||
| 135 | |||||||||||
| 136 | 10 | ▶ | unless ( s# | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | ( \@(?:media|charset|import) ) | ||||||||||
| 138 | (\s+) | ||||||||||
| 139 | ([^;{]+?) | ||||||||||
| 140 | (\s*) | ||||||||||
| 141 | \z#<span class="ch-at">$1$2$3</span>$4#gix | ||||||||||
| 142 | ) { | ||||||||||
| 143 | 4 | s{(:{1,2} \s*)(\S+)}{<span class="ch-ps">$1$2</span>}gx; | |||||||||
| 144 | 4 | s#(\s*)(.+)(\s*)#$1<span class="ch-sel">$2</span>$3#sg; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 10 | return $_; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 4 | ▶ | sub _pre { | ||||||||
|
Subroutine
_pre: called CC=2 SCAR=6.9
| |||||||||||
| 151 | 4 | my $self = shift; | |||||||||
| 152 | 4 | ▶ | if ( @_ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 2 | $self->{PRE} = shift; | |||||||||
| 154 | } | ||||||||||
| 155 | 4 | return $self->{PRE}; | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 4 | ▶ | sub _line_numbers { | ||||||||
|
Subroutine
_line_numbers: called CC=2 SCAR=6.9
| |||||||||||
| 159 | 4 | my $self = shift; | |||||||||
| 160 | 4 | ▶ | if ( @_ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 2 | $self->{LINE_NUMBERS} = shift; | |||||||||
| 162 | } | ||||||||||
| 163 | 4 | return $self->{LINE_NUMBERS}; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 2 | 1; | |||||||||
| 167 | __END__ | ||||||||||