wrinkles has asked for the wisdom of the Perl Monks concerning the following question:
I wrote a (perl6) script that collects class definitions from the HTML head in a hash, and inlines the style by substitution into the body. Here's the substitution:
UPDATE: match is actually coming from the "if" and "while" statements, not the repeated regex as I thought. Works without capture braces in repeated block. Yikes, never mind!
repeat { # Replace class with inline styles (repeatedly) $new ~~ s/ class \= \" \w+ \" / style="%classes{ $0.Str }" /; } while $new ~~ / class \= \" (\w+) \" /;
Yes it works, I'm just not convinced there isn't a slicker way. In particular, is there a simple way to substitute a capture at the corresponding match location, using a global substitution ( or something else entirely)?
TIA! The full script is below:
#!/usr/bin/env perl6 use v6; # Takes vim html export as input. # Outputs html file with css class styles inlined. # Regexp depend on precise whitespace output of Vim export. # Also depends on html format where each span has a single class defin +ition. sub MAIN($in, $out) { my %classes; my $out_fh = open $out, :w; for $in.IO.lines -> $line { # Between style tags of the html head if ( $line ~~ /^ \< style / ) ^fff^ ( $line ~~ /^ \< \/ style +/ ) { # Capture class definitions $line ~~ /^ \. ( \w+ ) \s+ \{ \s+ ( .* ) \s+ \} /; if ( $0 and $1) { # If found class definiti +on %classes{ $0.Str } = ~ $1 # - save to hash } else { $out_fh.say("$line") }; # Otherwise just pass lin +e to file # Class is declared in body } elsif $line ~~ / class \= \" (\w+) \" / { my $new = $line; repeat { # Replace class with inline styles (repeatedly) $new ~~ s/ class \= \" (\w+) \" / style="%classes{ $0 +.Str }" /; } while $new ~~ / class \= \" (\w+) \" /; $out_fh.say("$new") if $new; # And write changes to fi +le # No class definitions or declarations } else { $out_fh.say("$line"); # pass line unaltered } } close $out_fh; };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: multiple backreferences in a substitution
by FreeBeerReekingMonk (Deacon) on Jun 30, 2015 at 20:42 UTC |