Not exactly a direct solution, but I wrote this to turn Lua (from the same source program as yours, coincidentally :) into YAML.
Adjust the initial shebang line as appropriate to point to your local Lua interpreter.
#!/opt/local/bin/lua -- -- l2y -- Extract data from a Lua file as YAML -- -- $Id$ -- -- -- {{{ Map of data type to dump function -- Dumpers = { ["table"] = function ( t, indent ) indent = indent + 2; for k, v in pairs( t ) do local keyname = cleanup_str( k ); io.write( prefix(indent) .. keyname .. ": " ); if type( v ) == 'table' then io.write( "\n" ); end do_dump( v, indent ); end end, ["string"] = function ( s, indent ) local outstr = cleanup_str( s ) io.write( prefix( indent ), outstr, "\n" ); end, ["number"] = function ( n, indent ) io.write( prefix( indent ), tostring( n ), "\n" ); end, ["boolean"] = function ( b, indent ) local val; if b then val = "true"; else val = "false"; end io.write( prefix( indent ), val, "\n" ); end, ["unknown"] = function ( u, indent ) io.write( "## Don't know how to dump type:", type( u ), "\n" ); io.write( prefix( indent ), "~\n" ); end, } -- -- }}} -- -- {{{ cleanup_str -- if s contains any "funny" chars spit out a quote +d -- version instead function cleanup_str( s ) local outstr = s; if string.find( outstr, "[^%w]" ) then outstr = string.gsub( string.format( "%q", s ), "\\\n", "\\n" ); outstr = string.gsub( outstr, "\r", "" ); end return outstr end -- -- }}} -- function prefix( indent ) return string.rep( " ", indent ); end function do_dump( val, indent ) local d = Dumpers[ type( val ) ]; if d then d( val, indent ); else Dumpers[ "unknown" ]( val, indent ); end end if arg["n"] < 2 then io.write("usage: ", arg[0], " filename VarName [VarName...]\n" ); os.exit( 1 ); end local file = arg[1] assert(loadfile(file))() local fenv = getfenv( 1 ); io.write( "--- #YAML:1.0\n" ); for i = 2, arg["n"] do local cur = arg[ i ]; local val = fenv[ cur ]; if val then io.write( "## ", tostring(cur), " val: ", tostring(val), "\n" ); io.write( tostring(cur), ":\n" ); do_dump( val, 0 ); else io.write( "## ", tostring(cur), " nil???\n" ) io.write( tostring(cur), ": ~\n" ); end end os.exit( 0 );
You can open a pipe from that and then have YAML do the work for you. Zug zug.
In reply to Re: Help with making a string into a hash
by Fletch
in thread Help with making a string into a hash
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |