#! /usr/bin/lua local file = assert( io.open( arg[ 1 ] ) ) local contents = file:read( "*all" ) file:close() local bold = false local fg = 7 local bg = 0 local function setFG( colour ) return function() fg = colour end end local function setBG( colour ) return function() bg = colour end end local Colours = { [ false ] = { [ 0 ] = "333333", [ 1 ] = "CA4433", [ 2 ] = "178A3A", [ 3 ] = "DC7C2A", [ 4 ] = "415E87", [ 5 ] = "5E468C", [ 6 ] = "35789B", [ 7 ] = "B6C2C4", }, [ true ] = { [ 0 ] = "666666", [ 1 ] = "FF2954", [ 2 ] = "5DD030", [ 3 ] = "FAFC4F", [ 4 ] = "3581E1", [ 5 ] = "875FFF", [ 6 ] = "29FBFF", [ 7 ] = "CEDBDE", }, } local function hex( colour, bold ) return Colours[ bold ][ colour ] end local Escapes = { [ "0" ] = function() bold = false fg = 7 bg = 0 end, [ "1" ] = function() bold = true end, [ "30" ] = setFG( 0 ), [ "31" ] = setFG( 1 ), [ "32" ] = setFG( 2 ), [ "33" ] = setFG( 3 ), [ "34" ] = setFG( 4 ), [ "35" ] = setFG( 5 ), [ "36" ] = setFG( 6 ), [ "37" ] = setFG( 7 ), } local function handleEscape( escape ) for code in escape:gmatch( "(%d+)" ) do if Escapes[ code ] then Escapes[ code ]() end end return ( [[]] ):format( hex( fg, bold ), bold and "bold" or "normal" ) end contents = contents:gsub( "&", "&" ):gsub( "<", "<" ):gsub( ">", ">" ) contents = ( contents .. "\27[m" ):gsub( "(.-)\27%[([%d;]*)m", function( text, escape ) return text .. handleEscape( escape ) end ) print( "" ) print( contents )