webmcp
diff libraries/luatex/luatex.lua @ 64:3d43a5cf17c1
Compatibility with Lua 5.2
| author | jbe |
|---|---|
| date | Sun Apr 15 16:04:33 2012 +0200 (2012-04-15) |
| parents | 9fdfb27f8e67 |
| children |
line diff
1.1 --- a/libraries/luatex/luatex.lua Tue Apr 03 00:56:02 2012 +0200 1.2 +++ b/libraries/luatex/luatex.lua Sun Apr 15 16:04:33 2012 +0200 1.3 @@ -3,48 +3,42 @@ 1.4 local _G = _G 1.5 local _VERSION = _VERSION 1.6 local assert = assert 1.7 -local collectgarbage = collectgarbage 1.8 -local dofile = dofile 1.9 local error = error 1.10 -local getfenv = getfenv 1.11 local getmetatable = getmetatable 1.12 local ipairs = ipairs 1.13 -local load = load 1.14 -local loadfile = loadfile 1.15 -local loadstring = loadstring 1.16 -local module = module 1.17 local next = next 1.18 local pairs = pairs 1.19 -local pcall = pcall 1.20 local print = print 1.21 local rawequal = rawequal 1.22 local rawget = rawget 1.23 +local rawlen = rawlen 1.24 local rawset = rawset 1.25 -local require = require 1.26 local select = select 1.27 -local setfenv = setfenv 1.28 local setmetatable = setmetatable 1.29 local tonumber = tonumber 1.30 local tostring = tostring 1.31 local type = type 1.32 -local unpack = unpack 1.33 -local xpcall = xpcall 1.34 1.35 -local coroutine = coroutine 1.36 -local debug = debug 1.37 local io = io 1.38 local math = math 1.39 local os = os 1.40 -local package = package 1.41 local string = string 1.42 local table = table 1.43 1.44 -require("multirand") 1.45 -local multirand = multirand 1.46 +local multirand = require("multirand") 1.47 1.48 -module(...) 1.49 +local _M = {} 1.50 +if _ENV then 1.51 + _ENV = _M 1.52 +else 1.53 + _G[...] = _M 1.54 + setfenv(1, _M) 1.55 +end 1.56 1.57 -temp_dir = false -- has to be set to a private directory (/tmp can be unsafe) 1.58 +-- NOTE: 1.59 +-- temp_dir MUST NOT contain any charactes interpreted by system shell 1.60 +-- and has to be set to a private directory (/tmp may be unsafe) 1.61 +temp_dir = false 1.62 1.63 function escape(str) 1.64 return ( 1.65 @@ -101,7 +95,8 @@ 1.66 end 1.67 1.68 function document_methods:get_pdf() 1.69 - -- TODO: proper escaping of shell commands (should not be a real risk) 1.70 + -- TODO: proper escaping of shell commands 1.71 + -- (not a security risk, as args are safe) 1.72 if not temp_dir then 1.73 error("luatex.temp_dir not set") 1.74 end 1.75 @@ -109,11 +104,12 @@ 1.76 local latex_file = assert(io.open(basename .. ".tex", "w")) 1.77 latex_file:write(self:get_latex()) 1.78 latex_file:close() 1.79 - local result = os.execute( 1.80 + local result1, result2, result3 = os.execute( 1.81 'latex -output-format=pdf "-output-directory=' .. temp_dir .. '" ' .. 1.82 basename .. '< /dev/null > /dev/null 2> /dev/null' 1.83 ) 1.84 - if result ~= 0 then 1.85 + -- NOTE: use result1 and result3 for lua5.1 and lua5.2 compatibility 1.86 + if not (result1 == 0 or (result2 == "exit" and result3 == 0)) then 1.87 error('LaTeX failed, see "' .. basename .. '.log" for details.') 1.88 end 1.89 local pdf_file = assert(io.open(basename .. ".pdf", "r")) 1.90 @@ -123,9 +119,11 @@ 1.91 return pdf_data 1.92 end 1.93 1.94 +return _M 1.95 + 1.96 --[[ 1.97 1.98 -require("luatex") 1.99 +luatex = require("luatex") 1.100 luatex.temp_dir = "." 1.101 1.102 local tex = luatex.new_document()