Lua To Html |
|
[!] VersionNotice: The source code requires a small change in _VERSION handling to work under Lua 5.1. Some additional changes might be needed for proper 5.1 syntax highlighting.
There is a Lua 4.0 version [3],
and a Lua 5.0 version [4], which is the same file with all the % signs for upvalues deleted.
Here is a sample web listing [5]
Run the Lua 4.0 version like so:
lua -f lua2html.lua x.lua > x.html
and the Lua 5.0 version like this:
lua lua52html.lua x.lua > x.html
lua2html.pl can be found [here].
$ lua
Lua 5.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> = 1 + math.sqrt(2)
2.4142135623731
> print("math.sqrt(2)")
math.sqrt(2)
>
{{{!LuaInteractive
$ lua
Lua 5.1 Copyright (C) 1994-2006 Lua.org, PUC-Rio
> = 1 + math.sqrt(2)
2.4142135623731
>}}}
use strict;
use Syntax::Highlight::Engine::Kate::Lua;
my $sh = Syntax::Highlight::Engine::Kate::Lua->new(
substitutions => {
"<" => "<",
">" => ">",
"&" => "&",
" " => " ",
"\t" => " ",
"\n" => "<BR>\n",
},
format_table => {
Alert => ['<span class="alert">', '</span>'],
BaseN => ['<span class="basen">', '</span>'],
BString => ['<span class="bstring">', '</span>'],
Char => ['<cpan class="char">', '</span>'],
Comment => ['<span class="comment">', '</span>'],
DataType => ['<span class="datatype">', '</span>'],
DecVal => ['<span class="decval">', '</span>'],
Error => ['<span class="error">', '</span>'],
Float => ['<span class="float">', '</span>'],
Function => ['<span class="function">', '</span>'],
IString => ['<span class="istring">', '</span>'],
Keyword => ['<span class="keyword">', '</span>'],
Normal => ['', ''],
Operator => ['<span class="operator">', '</span>'],
Others => ['<span class="others">', '</span>'],
RegionMarker => ['<span class="regionmarker">', '</span>'],
Reserved => ['<span class="reserved">', '</span>'],
String => ['<span class="string">', '</span>'],
Variable => ['<span class="variable">', '</span>'],
Warning => ['<span class="warning">', '</span>'],
},
);
my $html = $sh->highlightText(qq(
local function test(y, z, ...)
for x in 1,y do
print(x)
end -- loop
local w = y * 2 + math.random()
print(2, y, 'test"2', "test\"'2'", [[math.random]], #z, 3, ...)
));
my $css = qq(
<style type="text/css">
span.comment { color: #00a000; }
span.string { color: #0000c0; }
span.keyword { color: #a00000; font-weight: bold; }
span.reserved { color: #a0a000; font-weight: bold; }
</style>
);
$html = qq(
<html>
<head>
$css
</head>
<body>
$html
</body>
);
print $html;