Lua To Html

lua-users home

This script converts Lua source files 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

This particular Wiki does Lua syntax highlighting with some tricky Perl code by Mike Pall. It supports the full Lua 5.1 syntax. You can customize it by modifying the embedded CSS. The standalone can be found [here].

Mike and John, is this the latest version of this source code used by the wiki? (Maybe the latest master copy of this function can be maintained on the wiki?) One extension (as recently mentioned in GuestBook and DavidManura) we miht want to see is properly syntax highlighting interpreter input/output such as

$ lua
Lua 5.1  Copyright (C) 1994-2006, PUC-Rio
> = 1 + math.sqrt(2)
> print("math.sqrt(2)")

That should not be difficult to add, and I'd offer to do the patch myself if desired.

The wiki might provide a different environment for this--e.g.
$ lua
Lua 5.1  Copyright (C) 1994-2006, PUC-Rio
> = 1 + math.sqrt(2)

RiciLake and I have also brought up the desire for C syntax highlighting (GuestBook) for the C code that's often posted on this wiki. Perhaps, to avoid reinventing the wheel, we can just use the existing Syntax-Highlight-Engine-Kate with the C module [1] similar to as shown below for Lua. There's some alternates too, e.g. GNU enscript.


The following Perl code demonstrates using the Syntax-Highlight-Engine-Kate [2] Perl module to syntax highlight Lua. It has a bit more baggage than Mike's Perl code above, and as of version 0.2 it trips up on some Lua syntax (i.e. patches needed):

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

$html = qq(

print $html;

RecentChanges · preferences
edit · history
Last edited January 2, 2007 7:03 pm GMT (diff)