From 3e321b4b1b3ac20d7e239a079279f3d7c666b118 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Fri, 21 Jul 2017 23:06:28 +0000 Subject: luapackageloader.lua in runtime git-svn-id: svn://tug.org/texlive/trunk@44865 c570f23f-e606-0410-a88d-b1316a301751 --- .../luatex/luapackageloader/luapackageloader.lua | 71 ---------------------- .../luatex/luapackageloader/luapackageloader.lua | 71 ++++++++++++++++++++++ Master/tlpkg/libexec/ctan2tds | 1 + 3 files changed, 72 insertions(+), 71 deletions(-) delete mode 100644 Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua create mode 100644 Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.lua (limited to 'Master') diff --git a/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua b/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua deleted file mode 100644 index 7ebd5b63e31..00000000000 --- a/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua +++ /dev/null @@ -1,71 +0,0 @@ --- Author: Deepak Jois --- License: http://www.latex-project.org/lppl.txt --- Date: 2017/05/26 --- Version: 0.1 -local luapackageloader = {} - --- Cache the current kpse based searchers -local kpse_lua_searcher = package.searchers[2] -local kpse_clua_searcher = package.searchers[3] - --- Emulates the default package.searchers[2] searcher. -local function lua_searcher(name) - local file, err = package.searchpath(name,package.path) - if err then - return string.format("[lua searcher]: module not found: '%s'%s", name, err) - else - return loadfile(file) - end -end - --- Emulates the default package.searchers[3] searcher. -local function clua_searcher(name) - local file, err = package.searchpath(name, package.cpath) - if err then - return string.format("[lua C searcher]: module not found: '%s'%s", name,err) - else - local symbol = name:gsub("%.","_") - return package.loadlib(file, "luaopen_"..symbol) - end -end - -local function combine_searchers(searcher1, searcher2) - return function(name) - local loader1 = searcher1(name) - if type(loader1) == "string" then -- Not found using searcher1. Try searcher2. - local loader2 = searcher2(name) - if type(loader2) == "string" then -- Not found using searcher2. Return error. - return string.format("%s\n\t%s", loader1, loader2) - end - return loader2 - end - return loader1 - end -end - ---- Use package.path and package.cpath to find Lua modules, --- in case kpse searching fails. --- --- Package searching logic is overridden by default in LuaTeX to use kpse. --- Calling this function reverts the searchers to use package.path and --- package.cpath, if the kpse based searcher is not able to locate --- a module. --- --- Package Loading References: --- 1. http://www.lua.org/manual/5.2/manual.html#pdf-package.searchers --- 2. LuaTeX Manual, Section 3.2, Lua behavior -function luapackageloader.add_lua_searchers() - package.searchers[2] = combine_searchers(kpse_lua_searcher, lua_searcher) - package.searchers[3] = combine_searchers(kpse_clua_searcher, clua_searcher) -end - ---- Restore the kpse package searchers that are used by default in LuaTeX. --- --- Call this to restore the default LuaTeX behavior for searching packages, --- if you had earlier overridden it using `luapackageloader.add_lua_searchers()`. -function luapackageloader.restore_kpse_searchers() - package.searchers[2] = kpse_lua_searcher - package.searchers[3] = kpse_clua_searcher -end - -return luapackageloader diff --git a/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.lua b/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.lua new file mode 100644 index 00000000000..7ebd5b63e31 --- /dev/null +++ b/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.lua @@ -0,0 +1,71 @@ +-- Author: Deepak Jois +-- License: http://www.latex-project.org/lppl.txt +-- Date: 2017/05/26 +-- Version: 0.1 +local luapackageloader = {} + +-- Cache the current kpse based searchers +local kpse_lua_searcher = package.searchers[2] +local kpse_clua_searcher = package.searchers[3] + +-- Emulates the default package.searchers[2] searcher. +local function lua_searcher(name) + local file, err = package.searchpath(name,package.path) + if err then + return string.format("[lua searcher]: module not found: '%s'%s", name, err) + else + return loadfile(file) + end +end + +-- Emulates the default package.searchers[3] searcher. +local function clua_searcher(name) + local file, err = package.searchpath(name, package.cpath) + if err then + return string.format("[lua C searcher]: module not found: '%s'%s", name,err) + else + local symbol = name:gsub("%.","_") + return package.loadlib(file, "luaopen_"..symbol) + end +end + +local function combine_searchers(searcher1, searcher2) + return function(name) + local loader1 = searcher1(name) + if type(loader1) == "string" then -- Not found using searcher1. Try searcher2. + local loader2 = searcher2(name) + if type(loader2) == "string" then -- Not found using searcher2. Return error. + return string.format("%s\n\t%s", loader1, loader2) + end + return loader2 + end + return loader1 + end +end + +--- Use package.path and package.cpath to find Lua modules, +-- in case kpse searching fails. +-- +-- Package searching logic is overridden by default in LuaTeX to use kpse. +-- Calling this function reverts the searchers to use package.path and +-- package.cpath, if the kpse based searcher is not able to locate +-- a module. +-- +-- Package Loading References: +-- 1. http://www.lua.org/manual/5.2/manual.html#pdf-package.searchers +-- 2. LuaTeX Manual, Section 3.2, Lua behavior +function luapackageloader.add_lua_searchers() + package.searchers[2] = combine_searchers(kpse_lua_searcher, lua_searcher) + package.searchers[3] = combine_searchers(kpse_clua_searcher, clua_searcher) +end + +--- Restore the kpse package searchers that are used by default in LuaTeX. +-- +-- Call this to restore the default LuaTeX behavior for searching packages, +-- if you had earlier overridden it using `luapackageloader.add_lua_searchers()`. +function luapackageloader.restore_kpse_searchers() + package.searchers[2] = kpse_lua_searcher + package.searchers[3] = kpse_clua_searcher +end + +return luapackageloader diff --git a/Master/tlpkg/libexec/ctan2tds b/Master/tlpkg/libexec/ctan2tds index da1ccac4ecc..a4c10e7fc11 100755 --- a/Master/tlpkg/libexec/ctan2tds +++ b/Master/tlpkg/libexec/ctan2tds @@ -1692,6 +1692,7 @@ $standardtex = '(\.(.bx|4ht|cls|clo|cmap|code\.tex|def|fd|fontspec|ldf|sty)' 'luaintro', 'NULL', # doc pkg 'lualatex-math', '\.sty', # not phst-doc.cls 'luamesh', '\.sty', # not lltxdoc.cls + 'luapackageloader', '\.lua|' . $standardtex, 'luatexko', '\.lua|' . $standardtex, 'luatodonotes','\.lua|' . $standardtex, 'luaxml', '\.lua|' . $standardtex, -- cgit v1.2.3