webmcp
view libraries/luatex/luatex.lua @ 411:02a53308f008
Updated Makefile.options to use pg_config binary and included /usr/include/lua5.2 (for Debian)
| author | jbe | 
|---|---|
| date | Thu Jan 07 17:44:32 2016 +0100 (2016-01-07) | 
| parents | 3d43a5cf17c1 | 
| children | 
 line source
     1 #!/usr/bin/env lua
     3 local _G             = _G
     4 local _VERSION       = _VERSION
     5 local assert         = assert
     6 local error          = error
     7 local getmetatable   = getmetatable
     8 local ipairs         = ipairs
     9 local next           = next
    10 local pairs          = pairs
    11 local print          = print
    12 local rawequal       = rawequal
    13 local rawget         = rawget
    14 local rawlen         = rawlen
    15 local rawset         = rawset
    16 local select         = select
    17 local setmetatable   = setmetatable
    18 local tonumber       = tonumber
    19 local tostring       = tostring
    20 local type           = type
    22 local io        = io
    23 local math      = math
    24 local os        = os
    25 local string    = string
    26 local table     = table
    28 local multirand = require("multirand")
    30 local _M = {}
    31 if _ENV then
    32   _ENV = _M
    33 else
    34   _G[...] = _M
    35   setfenv(1, _M)
    36 end
    38 -- NOTE:
    39 -- temp_dir MUST NOT contain any charactes interpreted by system shell
    40 -- and has to be set to a private directory (/tmp may be unsafe)
    41 temp_dir = false
    43 function escape(str)
    44   return (
    45     string.gsub(
    46       str,
    47       "[\001-\031\127\\#$&~_^%%{}]",
    48       function(char)
    49         local b = string.byte(char)
    50         if (b > 1 and b < 31) or b == 127 then
    51           return " "
    52         elseif
    53           char == "#" or char == "$" or char == "&" or char == "_" or
    54           char == "%" or char == "{" or char == "}"
    55         then
    56           return "\\" .. char
    57         else
    58           return "\\symbol{" .. b .. "}"
    59         end
    60       end
    61     )
    62   )
    63 end
    65 document_methods = {}
    67 document_mt = {
    68   __index = document_methods,
    69   __call = function(...) return document_methods.write(...) end
    70 }
    72 function new_document()
    73   return setmetatable({}, document_mt)
    74 end
    76 function document_methods:write(...)
    77   local i = 1
    78   while true do
    79     local v = select(i, ...)
    80     if v == nil then
    81       break
    82     end
    83     self[#self+1] = v
    84     i = i + 1
    85   end
    86 end
    88 function document_methods:get_latex()
    89   local str = table.concat(self)
    90   for i in ipairs(self) do
    91     self[i] = nil
    92   end
    93   self[1] = str
    94   return str
    95 end
    97 function document_methods:get_pdf()
    98   -- TODO: proper escaping of shell commands
    99   -- (not a security risk, as args are safe)
   100   if not temp_dir then
   101     error("luatex.temp_dir not set")
   102   end
   103   local basename = temp_dir .. "/tmp.luatex_" .. multirand.string(16)
   104   local latex_file = assert(io.open(basename .. ".tex", "w"))
   105   latex_file:write(self:get_latex())
   106   latex_file:close()
   107   local result1, result2, result3 = os.execute(
   108     'latex -output-format=pdf "-output-directory=' .. temp_dir .. '" ' ..
   109     basename .. '< /dev/null > /dev/null 2> /dev/null'
   110   )
   111   -- NOTE: use result1 and result3 for lua5.1 and lua5.2 compatibility
   112   if not (result1 == 0 or (result2 == "exit" and result3 == 0)) then
   113     error('LaTeX failed, see "' .. basename .. '.log" for details.')
   114   end
   115   local pdf_file = assert(io.open(basename .. ".pdf", "r"))
   116   local pdf_data = pdf_file:read("*a")
   117   pdf_file:close()
   118   os.execute('rm -f "' .. basename .. '.*"')
   119   return pdf_data
   120 end
   122 return _M
   124 --[[
   126 luatex = require("luatex")
   127 luatex.temp_dir = "."
   129 local tex = luatex.new_document()
   131 tex "\\documentclass[a4paper,12pt]{article}\n"
   132 tex "\\usepackage{german}\n"
   133 tex "\\usepackage{amsfonts}\n"
   134 tex "\\usepackage{amssymb}\n"
   135 tex "\\usepackage{ulem}\n"
   136 tex "\\pagestyle{headings}\n"
   137 tex "\\begin{document}\n"
   138 tex "\\title{Demo}\n"
   139 tex "\\author{John Doe}\n"
   140 tex "\\date{\\small 25. August 2008}\n"
   141 tex "\\maketitle\n"
   142 tex "\\end{document}\n"
   144 local pdf = tex:get_pdf()
   146 --]]
