#!/usr/bin/env perl use 5.014; use warnings; { my %entity_for = qw{& & < < > >}; sub chars_to_ents { $_[0] =~ s/([&<>])/$entity_for{$1}/gr } } my @plain_captures = qw{white_space remainder}; my @highlight_captures = qw{operator variable function constant statement datatype comment string integer float pragma}; my $re = qr{ (?> (? \s+ ) | (? (?> \/\* (?: . (?! \*\/ ) )*+ (?: . (?= \*\/ ) )?+ \*\/ | \/\/ [^\n]* $ ) ) | (? (?> [#]include \s+ " \w+ " \s* $ | [#]define \s+ \w+ \s+ \w+ \s* $ ) ) | (? " (?: [^"\\]++ | \\. )*+ " ) | (? \b \d+ \. \d+ f? \b ) | (? \b \d+ \b ) | (? \b [A-Z0-9_]+ \b ) | (? \b (?> action | const | effect | event | float | int | itemproperty | location | object | string | struct \s+ \w+ | talent | vector | void ) \b ) | (? \b (?> break | continue | do | for | if | else | return | switch | case | default | while ) \b ) | (? \b [A-Za-z_] \w* (?= \s*\( ) ) | (? \b [A-Za-z_] \w* \b ) | (? (?> \>\>\>\= | \>\>\> | \>\>\= | \<\<\= | \>\> | \<\< | \+\+ | \-\- | \&\= | \|\= | \^\= | \*\= | \/\= | \%\= | \+\= | \-\= | \=\= | \!\= | \<\= | \>\= | \&\& | \|\| | \< | \> | \! | \& | \| | \^ | \~ | \* | \/ | \% | \+ | \- | \= | \? | \: | \; | \. | \{ | \} | \( | \) | \, | \@ ) ) | (? .*? ) ) }msx; my $init_code = do { local $/; <> }; say '
';

MATCH:
while ($init_code =~ /$re/g) {
    for my $plain_capture (@plain_captures) {
        if (exists $+{$plain_capture}) {
            print $+{$plain_capture};
            next MATCH;
        }
    }

    for my $highlight_capture (@highlight_captures) {
        if (exists $+{$highlight_capture}) {
            print '',
                    chars_to_ents($+{$highlight_capture}), '';
            next MATCH;
        }
    }
}

say '
'; exit; #### /* hello.c */ #include "stdio" main() { printf("hello, world\n"); } ####
/* hello.c */

#include "stdio"

main()
{
    printf("hello, world\n");
}
##
## pre { font-size: 1.000em; white-space: pre-wrap; } pre.syntax-highlight { color: #666666; background-color: #000000; font-weight: normal; margin: 1.000em; border-color: #888888 #444444 #222222 #666666; border-width: 1px; border-style: solid; border-radius: 0.500em 0.500em; padding: 1.000em; } /* comment olive #808000 pragma orange #ff9900 string lime #00ff00 float mauve #9966ff integer copper #cc9966 datatype yellow #ffff00 statement mid blue #6699ff operator white #ffffff constant sea green #33cc99 variable cyan #00ffff function light red #ff6666 */ pre.syntax-highlight > span.comment { color: #808000; background-color: inherit; } pre.syntax-highlight > span.pragma { color: #ff9900; background-color: inherit; } pre.syntax-highlight > span.string { color: #00ff00; background-color: inherit; } pre.syntax-highlight > span.float { color: #9966ff; background-color: inherit; } pre.syntax-highlight > span.integer { color: #cc9966; background-color: inherit; } pre.syntax-highlight > span.constant { color: #33cc99; background-color: inherit; } pre.syntax-highlight > span.datatype { color: #ffff00; background-color: inherit; } pre.syntax-highlight > span.statement { color: #6699ff; background-color: inherit; } pre.syntax-highlight > span.function { color: #ff6666; background-color: inherit; } pre.syntax-highlight > span.variable { color: #00ffff; background-color: inherit; } pre.syntax-highlight > span.operator { color: #ffffff; background-color: inherit; }