webmcp
view libraries/luatex/luatex.lua @ 2:72860d232f32
Version 1.0.2
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
author | jbe/bsw |
---|---|
date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) |
parents | 9fdfb27f8e67 |
children | 3d43a5cf17c1 |
line source
1 #!/usr/bin/env lua
3 local _G = _G
4 local _VERSION = _VERSION
5 local assert = assert
6 local collectgarbage = collectgarbage
7 local dofile = dofile
8 local error = error
9 local getfenv = getfenv
10 local getmetatable = getmetatable
11 local ipairs = ipairs
12 local load = load
13 local loadfile = loadfile
14 local loadstring = loadstring
15 local module = module
16 local next = next
17 local pairs = pairs
18 local pcall = pcall
19 local print = print
20 local rawequal = rawequal
21 local rawget = rawget
22 local rawset = rawset
23 local require = require
24 local select = select
25 local setfenv = setfenv
26 local setmetatable = setmetatable
27 local tonumber = tonumber
28 local tostring = tostring
29 local type = type
30 local unpack = unpack
31 local xpcall = xpcall
33 local coroutine = coroutine
34 local debug = debug
35 local io = io
36 local math = math
37 local os = os
38 local package = package
39 local string = string
40 local table = table
42 require("multirand")
43 local multirand = multirand
45 module(...)
47 temp_dir = false -- has to be set to a private directory (/tmp can be unsafe)
49 function escape(str)
50 return (
51 string.gsub(
52 str,
53 "[\001-\031\127\\#$&~_^%%{}]",
54 function(char)
55 local b = string.byte(char)
56 if (b > 1 and b < 31) or b == 127 then
57 return " "
58 elseif
59 char == "#" or char == "$" or char == "&" or char == "_" or
60 char == "%" or char == "{" or char == "}"
61 then
62 return "\\" .. char
63 else
64 return "\\symbol{" .. b .. "}"
65 end
66 end
67 )
68 )
69 end
71 document_methods = {}
73 document_mt = {
74 __index = document_methods,
75 __call = function(...) return document_methods.write(...) end
76 }
78 function new_document()
79 return setmetatable({}, document_mt)
80 end
82 function document_methods:write(...)
83 local i = 1
84 while true do
85 local v = select(i, ...)
86 if v == nil then
87 break
88 end
89 self[#self+1] = v
90 i = i + 1
91 end
92 end
94 function document_methods:get_latex()
95 local str = table.concat(self)
96 for i in ipairs(self) do
97 self[i] = nil
98 end
99 self[1] = str
100 return str
101 end
103 function document_methods:get_pdf()
104 -- TODO: proper escaping of shell commands (should not be a real risk)
105 if not temp_dir then
106 error("luatex.temp_dir not set")
107 end
108 local basename = temp_dir .. "/tmp.luatex_" .. multirand.string(16)
109 local latex_file = assert(io.open(basename .. ".tex", "w"))
110 latex_file:write(self:get_latex())
111 latex_file:close()
112 local result = os.execute(
113 'latex -output-format=pdf "-output-directory=' .. temp_dir .. '" ' ..
114 basename .. '< /dev/null > /dev/null 2> /dev/null'
115 )
116 if result ~= 0 then
117 error('LaTeX failed, see "' .. basename .. '.log" for details.')
118 end
119 local pdf_file = assert(io.open(basename .. ".pdf", "r"))
120 local pdf_data = pdf_file:read("*a")
121 pdf_file:close()
122 os.execute('rm -f "' .. basename .. '.*"')
123 return pdf_data
124 end
126 --[[
128 require("luatex")
129 luatex.temp_dir = "."
131 local tex = luatex.new_document()
133 tex "\\documentclass[a4paper,12pt]{article}\n"
134 tex "\\usepackage{german}\n"
135 tex "\\usepackage{amsfonts}\n"
136 tex "\\usepackage{amssymb}\n"
137 tex "\\usepackage{ulem}\n"
138 tex "\\pagestyle{headings}\n"
139 tex "\\begin{document}\n"
140 tex "\\title{Demo}\n"
141 tex "\\author{John Doe}\n"
142 tex "\\date{\\small 25. August 2008}\n"
143 tex "\\maketitle\n"
144 tex "\\end{document}\n"
146 local pdf = tex:get_pdf()
148 --]]