Navalla Supporting the evolution of a new free world.

 

CSS Formatter

DESCRIPTION

Adds syntax highlighting to CSS stylesheets.

SYNOPSIS

Perl

require( 'css_formatter.pl' );
# load stylesheet code
# ...
print "<pre>", html_format( $css_code ), "</pre>\n";

Perl-CGI

<?perl
App::require_pl( 'css_formatter.pl' );
# load stylesheet code
# ...
print "<pre>", css_format( $css_code ), "</pre>\n";
?>

COPYRIGHT

The library is free software. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.

SOURCE CODE

# *******************
# * file: css_formatter.pl
# * license: GPL or Artistic License
# * copyright: Christian Mueller <chrmue[at]cpan.org>
# ******************************************************

#use strict;
our( $FmtHTML, $Formatter, $CSS_HTML_Tags, $CSS_Properties, $CSS_Values );

sub css_format {
    my( $code, $pre ) = @_;
    my(  $out, $len, $space, $lf, $tab, $i, $m, $l, $type );
    $pre = 1 if ! defined $pre;
    $out = '';
    $code =~ s/\r//gs;
    $len = length( $code );
    if( $pre ) {
        $space = ' ';
        $lf = "\n";
        $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    else {
        $space = '&nbsp;';
        $lf = "<br />\n";
        $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    for( $i = 0; $i < $len; ) {
        while( 1 ) {
            $m = substr( $code, $i, 1 );
            if( $m eq ' ' ) {
                $out .= $space;
            }
            elsif( $m eq "\n" ) {
                $out .= $lf;
            }
            elsif( $m eq "\t" ) {
                $out .= $tab;
            }
            else {
                last;
            }
            $i ++;
        }
        $l = css_get_token( $code, $i, $len, $type );
        $out .= '<span class="CSS_' . $type . '">'
            . substr( $code, $i, $l ) . '</span>';
        $i += $l;
        while( 1 ) {
            $m = substr( $code, $i, 1 );
            if( $m eq ' ' ) {
                $out .= $space;
            }
            elsif( $m eq "\n" ) {
                $out .= $lf;
            }
            elsif( $m eq "\t" ) {
                $out .= $tab;
            }
            else {
                last;
            }
            $i ++;
        }
    }
    return $out;
}

sub css_get_token {
    my( $str, $offset, $len, $type ) = @_;
    my( $i, $ch, $s1, $s2 );
    $str = substr( $str, $offset );
    $i = 0;
    $ch = substr( $str, 0, 1 );
    if( $ch eq '{' || $ch eq '}' ) {
        $_[3] = 'CBR';
        return 1;
    }
    elsif( $ch eq ';' ) {
        $_[3] = 'SEMI';
        return 1;
    }
    elsif( $ch eq '<' ) {
        if( $str =~ m/^\<\/\w+/ ) {
            $ch = uc( $& ) . ' ';
            if( $ch eq '</STYLE ' && $FmtHTML ) {
                $Formatter = 'HTML';
            }
            $_[3] = 'TAG';
            return $+[0];
        }
    }
    elsif( $ch eq '/' ) {
        if( substr( $str, 1, 1 ) eq '*' ) {
            $_[3] = 'CMT';
            $i = index( $str, "*/", 2 );
            return $i >= 0 ? $i + 2 : $len - $offset;
        }
        $_[3] = 'TEXT';
        return 1;
    }
    elsif( $ch eq '"' || $ch eq '\'' ) {
        $_[3] = 'STR';
        while( 1 ) {
            # not perfect but fast
            $i = index( $str, $ch, $i + 1 );
            return $len - $offset if $i < 0;
            if( substr( $str, $i - 1, 1 ) ne '\\' ||
                substr( $str, $i - 2, 1 ) eq '\\'
            ) {
                return $i + 1;
            }
        }
    }
    elsif( $ch eq '0' || $ch eq '1' || $ch eq '2' || $ch eq '3' ||
        $ch eq '4' || $ch eq '5' || $ch eq '6' || $ch eq '7' ||
        $ch eq '8' || $ch eq '9'
    ) {
        if( $ch eq '0' && substr( $str, 1, 1 ) eq 'x' ) {
            for( $i += 2; ; $i ++ ) {
                $ch = substr( $str, $i, 1 );
                if( ($ch lt '0' || $ch gt '9') &&
                    ($ch lt 'a' || $ch gt 'f') &&
                    ($ch lt 'A' || $ch gt 'F')
                ) {
                    last;
                }
            }
        }
        else {
            for( $i ++; ; $i ++ ) {
                $ch = substr( $str, $i, 1 );
                last if $ch lt '0' || $ch gt '9' || $ch ne '.';
            }
        }
        $_[3] = 'NUM';
        return $i;
    }
    elsif( $ch eq '#' ) {
        for( $i ++; ; $i ++ ) {
            $ch = substr( $str, $i, 1 );
            if( ($ch lt '0' || $ch gt '9') && ($ch lt 'a' || $ch gt 'f') &&
                ($ch lt 'A' || $ch gt 'F')
            ) {
                last;
            }
        }
        $_[3] = 'HEX';
        return $i;
    }
    elsif( $str =~ m/^[\w\-]+/ ) {
        $s1 = lc( ' ' . $& . ' ' );
        $s2 = uc( ' ' . $& . ' ' );
        if( index( $CSS_Properties, $s1 ) >= 0 ) {
            $_[3] = 'PROP';
        }
        elsif( index( $CSS_HTML_Tags, $s2 ) >= 0 ) {
            $_[3] = 'HTML_TAG';
        }
        elsif( index( $CSS_Values, $s1 ) >= 0 ) {
            $_[3] = 'VAL';
        }
        else {
            $_[3] = 'TEXT';
        }
        return $+[0];
    }
    $_[3] = 'TEXT';
    return 1;
}

$CSS_HTML_Tags = q/
A ABOVE ACTIVE ADDRESS APPLET AREA ARRAY
B BASE BASEFONT BGSOUND BIG BLOCKQUOTE BODY BR
CAPTION CITE CODE
DD DFN DIR DIV DL DT
EM EMBED
FIG FIRST-LETTER FIRST-LINE FORM FRAME FRAMESET
H H1 H2 H3 H4 H5 H6 HEAD HR HTML
I IMG INPUT ISINDEX
KBD
LI LINK
MAP MARQUEE MENU META
NEXTID NOBR NOFRAMES NOSCRIPT NOTE
OL OPTION
P PRE
RANGE ROOT
SAMP SCRIPT SELECT SOUND SPAN SQRT STRIKE STRONG STYLE SUP
TABLE TD TEXT TEXTAREA TH TITLE TR TT
U UL
VAR VISITED
WBR
/;

$CSS_Properties = q/
background background-attachment background-color background-image
background-position background-repeat
border border-bottom border-bottom-color border-bottom-style
border-bottom-width border-color border-left border-left-color
border-left-style border-left-top border-left-width
border-right border-right-color border-right-style border-right-width
border-spacing border-style border-top border-top-color border-top-style
border-top-width border-width
clear color
display
float font font-family font-size font-style font-variant font-weight
height
letter-spacing line-height list-style list-style-image list-style-position
list-style-type
margin margin-bottom margin-left margin-right margin-top
padding padding-bottom padding-left padding-right padding-top
text-align text-decoration text-indent text-transform
vertical-align
white-space width word-spacing
/;

$CSS_Values = q/
aqua auto
baseline black blink block blue bold bolder both bottom
capitalize center circle cursive cyan
dashed decimal disc dotted double
fantasy fixed fuchsia
gray green groove
inline inset inside italic
justify
large larger left lighter lime line-through list-item lower-alpha
lower-roman lowercase
magenta maroon medium middle monospace
navy no-repeat none normal nowrap
oblique olive orange outset outside overline
purple
red repeat repeat-x repeat-y rgb ridge right
sans-serif scroll serif silver small small-caps smaller solid square sub
super
teal text-bottom text-top thick thin top transparent
underline upper-alpha upper-roman uppercase url
white
x-large x-small xx-large xx-small
yellow
/;

$CSS_HTML_Tags =~ tr/\n/ /;
$CSS_Properties =~ tr/\n/ /;
$CSS_Values =~ tr/\n/ /;

1;

STYLESHEET FORMAT

The webpage uses the stylsheet settings below to highlight CSS code.

span.CSS_PROP, span.CSS_NUM {
    color: red;
}
span.CSS_HTML_TAG {
    color: blue;
}
span.CSS_VAL {
    color: orange;
}
span.CSS_CMT {
    color: teal;
}
span.CSS_STR {
    color: gray;
}
span.CSS_HEX {
    color: purple;
}
 
UNITE FOR CHILDREN - UNITE AGAINST AIDS
 
Generated with Perl 5.10.0 and Perl-CGI 1.0 over FastCGI within 16.59ms in memory safe mode.