#!/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 definition.
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 definition
%classes{ $0.Str } = ~ $1 # - save to hash
} else { $out_fh.say("$line") }; # Otherwise just pass line 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 file
# No class definitions or declarations
} else {
$out_fh.say("$line"); # pass line unaltered
}
}
close $out_fh;
};