Gentle Monks,

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; };

In reply to multiple backreferences in a substitution by wrinkles

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.