webmcp

diff libraries/luatex/luatex.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children 3d43a5cf17c1
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libraries/luatex/luatex.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +#!/usr/bin/env lua
     1.5 +
     1.6 +local _G             = _G
     1.7 +local _VERSION       = _VERSION
     1.8 +local assert         = assert
     1.9 +local collectgarbage = collectgarbage
    1.10 +local dofile         = dofile
    1.11 +local error          = error
    1.12 +local getfenv        = getfenv
    1.13 +local getmetatable   = getmetatable
    1.14 +local ipairs         = ipairs
    1.15 +local load           = load
    1.16 +local loadfile       = loadfile
    1.17 +local loadstring     = loadstring
    1.18 +local module         = module
    1.19 +local next           = next
    1.20 +local pairs          = pairs
    1.21 +local pcall          = pcall
    1.22 +local print          = print
    1.23 +local rawequal       = rawequal
    1.24 +local rawget         = rawget
    1.25 +local rawset         = rawset
    1.26 +local require        = require
    1.27 +local select         = select
    1.28 +local setfenv        = setfenv
    1.29 +local setmetatable   = setmetatable
    1.30 +local tonumber       = tonumber
    1.31 +local tostring       = tostring
    1.32 +local type           = type
    1.33 +local unpack         = unpack
    1.34 +local xpcall         = xpcall
    1.35 +
    1.36 +local coroutine = coroutine
    1.37 +local debug     = debug
    1.38 +local io        = io
    1.39 +local math      = math
    1.40 +local os        = os
    1.41 +local package   = package
    1.42 +local string    = string
    1.43 +local table     = table
    1.44 +
    1.45 +require("multirand")
    1.46 +local multirand = multirand
    1.47 +
    1.48 +module(...)
    1.49 +
    1.50 +temp_dir = false  -- has to be set to a private directory (/tmp can be unsafe)
    1.51 +
    1.52 +function escape(str)
    1.53 +  return (
    1.54 +    string.gsub(
    1.55 +      str,
    1.56 +      "[\001-\031\127\\#$&~_^%%{}]",
    1.57 +      function(char)
    1.58 +        local b = string.byte(char)
    1.59 +        if (b > 1 and b < 31) or b == 127 then
    1.60 +          return " "
    1.61 +        elseif
    1.62 +          char == "#" or char == "$" or char == "&" or char == "_" or
    1.63 +          char == "%" or char == "{" or char == "}"
    1.64 +        then
    1.65 +          return "\\" .. char
    1.66 +        else
    1.67 +          return "\\symbol{" .. b .. "}"
    1.68 +        end
    1.69 +      end
    1.70 +    )
    1.71 +  )
    1.72 +end
    1.73 +
    1.74 +document_methods = {}
    1.75 +
    1.76 +document_mt = {
    1.77 +  __index = document_methods,
    1.78 +  __call = function(...) return document_methods.write(...) end
    1.79 +}
    1.80 +
    1.81 +function new_document()
    1.82 +  return setmetatable({}, document_mt)
    1.83 +end
    1.84 +
    1.85 +function document_methods:write(...)
    1.86 +  local i = 1
    1.87 +  while true do
    1.88 +    local v = select(i, ...)
    1.89 +    if v == nil then
    1.90 +      break
    1.91 +    end
    1.92 +    self[#self+1] = v
    1.93 +    i = i + 1
    1.94 +  end
    1.95 +end
    1.96 +
    1.97 +function document_methods:get_latex()
    1.98 +  local str = table.concat(self)
    1.99 +  for i in ipairs(self) do
   1.100 +    self[i] = nil
   1.101 +  end
   1.102 +  self[1] = str
   1.103 +  return str
   1.104 +end
   1.105 +
   1.106 +function document_methods:get_pdf()
   1.107 +  -- TODO: proper escaping of shell commands (should not be a real risk)
   1.108 +  if not temp_dir then
   1.109 +    error("luatex.temp_dir not set")
   1.110 +  end
   1.111 +  local basename = temp_dir .. "/tmp.luatex_" .. multirand.string(16)
   1.112 +  local latex_file = assert(io.open(basename .. ".tex", "w"))
   1.113 +  latex_file:write(self:get_latex())
   1.114 +  latex_file:close()
   1.115 +  local result = os.execute(
   1.116 +    'latex -output-format=pdf "-output-directory=' .. temp_dir .. '" ' ..
   1.117 +    basename .. '< /dev/null > /dev/null 2> /dev/null'
   1.118 +  )
   1.119 +  if result ~= 0 then
   1.120 +    error('LaTeX failed, see "' .. basename .. '.log" for details.')
   1.121 +  end
   1.122 +  local pdf_file = assert(io.open(basename .. ".pdf", "r"))
   1.123 +  local pdf_data = pdf_file:read("*a")
   1.124 +  pdf_file:close()
   1.125 +  os.execute('rm -f "' .. basename .. '.*"')
   1.126 +  return pdf_data
   1.127 +end
   1.128 +
   1.129 +--[[
   1.130 +
   1.131 +require("luatex")
   1.132 +luatex.temp_dir = "."
   1.133 +
   1.134 +local tex = luatex.new_document()
   1.135 +
   1.136 +tex "\\documentclass[a4paper,12pt]{article}\n"
   1.137 +tex "\\usepackage{german}\n"
   1.138 +tex "\\usepackage{amsfonts}\n"
   1.139 +tex "\\usepackage{amssymb}\n"
   1.140 +tex "\\usepackage{ulem}\n"
   1.141 +tex "\\pagestyle{headings}\n"
   1.142 +tex "\\begin{document}\n"
   1.143 +tex "\\title{Demo}\n"
   1.144 +tex "\\author{John Doe}\n"
   1.145 +tex "\\date{\\small 25. August 2008}\n"
   1.146 +tex "\\maketitle\n"
   1.147 +tex "\\end{document}\n"
   1.148 +
   1.149 +local pdf = tex:get_pdf()
   1.150 +
   1.151 +--]]

Impressum / About Us