#| Interaction with pygments unit module Pygments; my token fence { '```' } my token info-string { \w+ } # TODO: Be more precise about this so we can handle backticks in code blocks my token code { <-[`]>+ } my token code-block { <&fence> \h* \v <&fence> } sub pygment(Str:D $code, Str:D $lang --> Str:D) { my $pygments = run , $lang, :out, :in; $pygments.in.spurt: $code, :close; $pygments.out.slurp: :close; } sub highlight-code(Str:D $input --> Str:D) is export { my $text = $input; # TODO: Figure out a way to exclude idris code so we can process both in the # same file while $text ~~ &code-block { my $match = $/; # Extract the match and have pygments colorize the code my $code = $match.Str; my $lang = $match.Str; my $out = pygment $code, $lang; ## Mangle the html to meet our needs # Delete the existing div and construct a inside the
       $out ~~ s:g/'<' \/? 'div' <-[>]>* '>'//;
       $out ~~ s:g/'
'/
/;
       $out ~~ s:g/'
'/<\/code><\/pre>/; # Rename the classes to match our needs $out ~~ s:g/'span class="' (\w+) '"'/span class="hl-{$/[0].lc}"/; $text = $match.replace-with: $out; } $text }