Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Melbourne PMer Scott Penrose asked on the Melbourne-PM mailing list about merging and simplifying CSS. If you view the source of the page you're currently looking at you'll see the default Perl Monk CSS at the top, then if you've set your own, yours is underneath it.

Scott wanted a way to merge multiple sheets to provide a single simplified CSS. Thinking about this I've cobbled together the following solution. Its not elegant and it doesn't run fast. But it was fast to write and it does the job. Well I assume it does. Scott, if you're reading this let me know :)

Basically this script will take input from one or more CSS files:
File 1
P { align: left; font-family: sans; font-size: 12pt; }
File 2
TD { text-align: left; font-family: sans; font-size: 12pt; } P { text-align: left; width: 80%; }
It will then return a simplified 'cascading' (inherited) style sheet:
P, TD { text-align: left; font-family: sans; font-size: 12pt; } P { width: 80%; }
#!/usr/bin/perl use CSS; use Strict; #--------------------------------------------------------------------- +----------- # USAGE: # simplify(list_of_files); # NOTE: # list_of_files should be in _increasing_ importance. Styles from th +e last # file will overwrite any duplicate styles of all previous files. # RETURNS: # Simplified formatted CSS #--------------------------------------------------------------------- +----------- # NOTE CAREFULLY: # CSS.pm is very strict in its parsing. Make sure your style sheets +use a # semi-colon after every parameter, even at the end of a block: # body { # font-family: 'times'; # font-size: '12pt'; <--- this semicolon MUST exist # } even though its at the end of the bloc +k # This ain't perl buster! #--------------------------------------------------------------------- +----------- # Author's jottings: This is probably not the most 'golf' way to do th +is, but # its a working solution. #--------------------------------------------------------------------- +----------- print simplify('pmdefault.css','pmuser.css'); #--------------------------------------------------------------------- +----------- # The routine #--------------------------------------------------------------------- +----------- sub simplify { my ($output, %usedby, %css); my @files = @_; die("You have to tell me at least one css file to simplify.") unle +ss @files; my $stylesheet = CSS->new(-source=>$files[0]); shift @files; foreach my $file (@files) { $stylesheet->add_file($file); } my @styles = $stylesheet->styles; foreach my $style (@styles) { my %props = $style->properties; foreach my $property (keys %props) { $props{$property} = "'$props{$property}'" if $props{$prope +rty}=~/\D/; push (@{$usedby{sprintf("%19s : %s",$property,$props{$prop +erty})}}, $style->selector); } } foreach my $style (sort keys %usedby) { my $stylename = join(', ',sort @{$usedby{$style}}); push(@{$css{$stylename}},$style); } foreach my $style (sort keys %css) { $output .= "$style {\n"; $output .= join(";\n",@{$css{$style}}); $output .= ";\n}\n\n"; } return $output; }

In reply to Simplifying CSS by BigLug

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found