G'day All,

I use a scripting language, called NWScript, for some CRPG development that I do from time to time.

I wrote the following Perl script to syntax-highlight NWScript code for HTML rendering:

#!/usr/bin/env perl use 5.014; use warnings; { my %entity_for = qw{& &amp; < &lt; > &gt;}; 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 statem +ent datatype comment string integer float prag +ma}; my $re = qr{ (?> (?<white_space> \s+ ) | (?<comment> (?> \/\* (?: . (?! \*\/ ) )*+ (?: . (?= \*\/ ) )?+ \*\/ | \/\/ [^\n]* $ ) ) | (?<pragma> (?> [#]include \s+ " \w+ " \s* $ | [#]define \s+ \w+ \s+ \w+ \s* $ ) ) | (?<string> " (?: [^"\\]++ | \\. )*+ " ) | (?<float> \b \d+ \. \d+ f? \b ) | (?<integer> \b \d+ \b ) | (?<constant> \b [A-Z0-9_]+ \b ) | (?<datatype> \b (?> action | const | effect | event | float | int | itemproperty | location | object | string | struct \s+ \w+ | talent | vector | void ) \b ) | (?<statement> \b (?> break | continue | do | for | if | else | return | switch | case | default | while ) \b ) | (?<function> \b [A-Za-z_] \w* (?= \s*\( ) ) | (?<variable> \b [A-Za-z_] \w* \b ) | (?<operator> (?> \>\>\>\= | \>\>\> | \>\>\= | \<\<\= | \>\> | \<\< | \+ +\+ | \-\- | \&\= | \|\= | \^\= | \*\= | \/\= | \%\= | \+\= | \-\ += | \=\= | \!\= | \<\= | \>\= | \&\& | \|\| | \< | \> | \! | \& | \| | \^ | \~ | \* | \/ | \% | \+ + | \- | \= | \? | \: | \; | \. | \{ | \} | \( | \) | \, | \@ ) ) | (?<remainder> .*? ) ) }msx; my $init_code = do { local $/; <> }; say '<pre class="syntax-highlight">'; 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 '<span class="', $highlight_capture, '">', chars_to_ents($+{$highlight_capture}), '</span>'; next MATCH; } } } say '</pre>'; exit;

NWScript uses a C-like syntax. I'm aware that a few monks use NWScript; however, I'd guess most don't and have probably never heard of it. So, purely to provide an example that's looks a little more familiar to most, here's a slightly fudged (just the #include pragma) hello.c:

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

And here's the output after running that through my script:

<pre class="syntax-highlight"> <span class="comment">/* hello.c */</span> <span class="pragma">#include "stdio" </span> <span class="function">main</span><span class="operator">(</span><span + class="operator">)</span> <span class="operator">{</span> <span class="function">printf</span><span class="operator">(</span +><span class="string">"hello, world\n"</span><span class="operator">) +</span><span class="operator">;</span> <span class="operator">}</span> </pre>

For anyone wishing to use this script, here's the CSS I use (in the Spoiler):

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

-- Ken

Replies are listed 'Best First'.
Re: Syntax-highlight Non-Perl Code for HTML
by withering (Monk) on Jun 29, 2015 at 07:35 UTC

    NWScript? If it is the script used by Neverwinter Nights, I had used it ... a little.

    Quite nice work indeed. Thanks for sharing :D

      Yes, it's the scripting language used in Neverwinter Nights.

      Although substantially different in many respects (gameplay, GUI, etc.), I believe its successor, Neverwinter Nights 2, also used NWScript or, at least, something very similar with the same syntax. If so, this would probably work with those scripts as well.

      Thanks for the positive comments.

      -- Ken