Untitled

 avatar
unknown
lua
16 days ago
1.9 kB
7
Indexable
-- setup package path
-- My project strcuture:
--  ├── libs
--  ├── luadraw_setup.lua
--  ├── main.lua
--  └── tex
package.path = package.path..";./libs/?.lua"..";./libs/extensions/?.lua"
-- print(package.path)


-- NOTE: rewrite function 'require', make "require(XXX.lua)" to be valid
local original_require = require
_G.require = function(modname)
    if type(modname) == "string" and modname:match("%.lua$") then
        modname = modname:gsub("%.lua$", "")
    end
    return original_require(modname)
end


-- Add table.copy polyfill for pure Lua execution (usually provided by LaTeX lualibs)
function table.copy(t)
    if type(t) ~= "table" then return t end
    local res = {}
    for k, v in pairs(t) do
        if type(v) == "table" then
            res[k] = table.copy(v)
        else
            res[k] = v
        end
    end
    setmetatable(res, getmetatable(t))
    return res
end


-- Mock 'tex' table for pure Lua execution (usually provided by LuaTeX)
_G.tex = {
    print = function(...) print(...) end,
    sprint = function(...) 
        local args = {...}
        for i, v in ipairs(args) do
            if v ~= "" then
                print(v)
            end
        end
    end
}


-- LuaDraw NameSpace Implementation
luadraw = {}

-- cache dir setup
local lfs = require('lfs')
luadraw.cachedir = ".luadraw"
lfs.mkdir(luadraw.cachedir)

-- load Functions/Variables to table 'luadraw'
luadraw.graph = require("luadraw_graph2d")
luadraw.graph3d = require("luadraw_graph3d")

-- export luadraw result
function luadraw.graph:Show(graph_name)
  self:Sendtotex()
  if graph_name and graph_name ~= "" then
    self:Savetofile(luadraw.cachedir.."/"..graph_name..".tkz")
  end
end

function luadraw.graph:Save(graph_name)
  graph_name = graph_name or "test_output"
  if graph_name ~= "" then
    self:Savetofile(luadraw.cachedir.."/"..graph_name..".tkz")
  end
end
Editor is loading...
Leave a Comment