texlive[44500] Master: luapackageloader (27may17)

commits+karl at tug.org commits+karl at tug.org
Tue Jun 6 23:51:05 CEST 2017


Revision: 44500
          http://tug.org/svn/texlive?view=revision&revision=44500
Author:   karl
Date:     2017-06-06 23:51:05 +0200 (Tue, 06 Jun 2017)
Log Message:
-----------
luapackageloader (27may17)

Modified Paths:
--------------
    trunk/Master/tlpkg/bin/tlpkg-ctan-check
    trunk/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc

Added Paths:
-----------
    trunk/Master/texmf-dist/doc/luatex/luapackageloader/
    trunk/Master/texmf-dist/doc/luatex/luapackageloader/README.md
    trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua
    trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.pdf
    trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.tex
    trunk/Master/texmf-dist/tex/luatex/luapackageloader/
    trunk/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.sty
    trunk/Master/tlpkg/tlpsrc/luapackageloader.tlpsrc

Added: trunk/Master/texmf-dist/doc/luatex/luapackageloader/README.md
===================================================================
--- trunk/Master/texmf-dist/doc/luatex/luapackageloader/README.md	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/luatex/luapackageloader/README.md	2017-06-06 21:51:05 UTC (rev 44500)
@@ -0,0 +1,22 @@
+# luapackageloader
+CTAN package to allow LuaTeX to load packages from the default `package.path` and `package.cpath` locations. This could be useful
+to load external Lua modules, including modules installed via [LuaRocks](https://luarocks.org).
+
+## Usage
+See the [PDF documentation](luapackageloader.pdf) for details.
+
+Here is a quick TeX example:
+
+```tex
+\input luapackageloader
+
+% Now we proceed to load a LuaRocks package
+\directlua{
+  local spt = require("serpent")
+}
+```
+
+## License
+Distributable under the LaTeX Project Public License, version 1.3c or higher (your choice). The latest version of this license is at: http://www.latex-project.org/lppl.txt
+
+This work is "maintained" (as per LPPL maintenance status) by Deepak Jois.


Property changes on: trunk/Master/texmf-dist/doc/luatex/luapackageloader/README.md
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua
===================================================================
--- trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua	2017-06-06 21:51:05 UTC (rev 44500)
@@ -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


Property changes on: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.lua
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.pdf
===================================================================
(Binary files differ)

Index: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.pdf
===================================================================
--- trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.pdf	2017-06-06 21:50:19 UTC (rev 44499)
+++ trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.pdf	2017-06-06 21:51:05 UTC (rev 44500)

Property changes on: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.pdf
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/pdf
\ No newline at end of property
Added: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.tex
===================================================================
--- trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.tex	                        (rev 0)
+++ trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.tex	2017-06-06 21:51:05 UTC (rev 44500)
@@ -0,0 +1,116 @@
+\documentclass[a4paper]{article}
+
+%% Language and font encodings
+\usepackage[english]{babel}
+\usepackage[utf8x]{inputenc}
+\usepackage[T1]{fontenc}
+
+%% Sets page size and margins
+\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
+
+%% Useful packages
+\usepackage[colorlinks=true,
+            linkcolor = blue,
+            urlcolor  = blue,
+            citecolor = blue,
+            anchorcolor = blue]{hyperref}
+\usepackage{xcolor}
+\usepackage{minted}
+\usepackage{csquotes}
+
+\definecolor{LG}{HTML}{F9F9F9}
+
+%% Informations
+\title{\textsf{luapackageloader} package}
+\author{Deepak Jois \\ \href{deepak.jois at gmail.com}{deepak.jois at gmail.com}}
+
+\begin{document}
+\maketitle
+
+\section*{Introduction}
+LuaTeX modifies the package loading behavior for Lua package, as mentioned in
+Section 3.2 of the LuaTeX manual:
+
+\begin{displayquote}
+LuaTEX is able to use the kpathsea library to find require()d modules. For this
+purpose, \texttt{package.searchers[2]} is replaced by a different loader function,
+that decides at runtime whether to use kpathsea or the built-in core Lua
+function. It uses kpathsea when that is already initialized at that point in
+  time, otherwise it reverts to using the normal \texttt{package.path} loader.
+\end{displayquote}
+
+What this means is that once kpathsea is initialized, the normal package
+loading behavior is no longer available.
+
+This package allows Lua package loading behavior inside LuaTeX to use \textit{both}
+kpathsea and the default loading mechanism. If the module is not available in kpathsea,
+LuaTeX will try to load it from \texttt{package.path}. This functionality is very useful,
+for instance when you need to use \href{https://luarocks.org/}{LuaRocks} modules inside LuaTeX.
+
+\section{Installation}
+
+Install this package like any other \TeX~or \LaTeX~package.
+
+\section{Dependencies}
+
+This package depends on \href{https://www.ctan.org/pkg/ifluatex}{ifluatex} CTAN package.
+
+\section{Usage}
+
+\subsection{Plain \TeX}
+In Plain \TeX, you can import the module as follows:
+
+\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
+\input luapackageloader.sty
+\end{minted}
+
+\subsection{\LaTeX}
+
+In \LaTeX, add the \texttt{luapackageloader} packages to your document:
+
+\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
+\usepackage{luapackageloader}
+\end{minted}
+
+\subsection{Loading Packages}
+
+Once you have imported \texttt{luapackageloader}, it adds a table called
+\texttt{luapackageloader} under the global namepace, and automatically
+overrides the default LuaTeX package searchers. It should be possible to load a
+Lua package (including \href{https://luarocks.org/}{LuaRocks} modules, as long
+as \texttt{package.path} and \texttt{package.cpath} is setup properly in your
+environment) as follows:
+
+\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
+\directlua{
+  local serpent = require("serpent") % luarocks install serpent
+}
+\end{minted}
+
+\subsection{Reverting to default loading behavior}
+
+The package loading can be reverted back to the default LuaTeX behavior using the \texttt{restore\_kpse\_searchers()}
+\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
+\directlua{
+  luapackageloader.resetore_kpse_searchers()
+}
+\end{minted}
+
+\subsection{Manually setting up to load Lua packages}
+
+This package exposes the function \texttt{add\_lua\_searchers()}, to manually setup the package searchers again, if it was reverted for some reason, after loading the package.
+
+\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
+\input luapackageloader.sty
+\directlua{
+  % Let’s revert the package searchers to their default loading behavior.
+  luapackageloader.restore_kpse_searchers()
+
+ % Some code here...
+
+  % Now let’s setup the package searchers to use Lua searchers again.
+  luapackageloader.add_lua_searchers()
+}
+\end{minted}
+
+\end{document}


Property changes on: trunk/Master/texmf-dist/doc/luatex/luapackageloader/luapackageloader.tex
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: trunk/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.sty
===================================================================
--- trunk/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.sty	                        (rev 0)
+++ trunk/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.sty	2017-06-06 21:51:05 UTC (rev 44500)
@@ -0,0 +1,35 @@
+% Author: Deepak Jois
+% License: http://www.latex-project.org/lppl.txt
+% Date: 2017/05/26
+% Version: 0.1
+\def\luapackageloaderpkgdate{2017/05/26}
+\def\luapackageloaderversion{0.1}
+
+\expandafter\ifx\csname ProvidesPackage\endcsname\relax
+  \input ifluatex.sty\relax
+\else
+  \NeedsTeXFormat{LaTeX2e}
+  \ProvidesPackage{}[\luapackageloaderpkgdate\space v\luapackageloaderversion\space Lua package searchers for LuaTeX]
+  \RequirePackage{ifluatex}
+\fi
+
+\def\luapackageloader at dothings{%
+  \directlua{
+    luapackageloader = require("luapackageloader")
+    luapackageloader.add_lua_searchers()
+  }%
+}
+
+\expandafter\ifx\csname ProvidesPackage\endcsname\relax
+  \ifluatex
+   \luapackageloader at dothings
+  \else
+    \message{Warning: luapackageloader only works with LuaTeX (plain and LaTeX format)}
+  \fi
+\else
+  \ifluatex
+    \luapackageloader at dothings
+  \else
+    \PackageWarning{luapackageloader}{You are using this package without LuaTeX. This is not supported.}
+  \fi
+\fi


Property changes on: trunk/Master/texmf-dist/tex/luatex/luapackageloader/luapackageloader.sty
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Modified: trunk/Master/tlpkg/bin/tlpkg-ctan-check
===================================================================
--- trunk/Master/tlpkg/bin/tlpkg-ctan-check	2017-06-06 21:50:19 UTC (rev 44499)
+++ trunk/Master/tlpkg/bin/tlpkg-ctan-check	2017-06-06 21:51:05 UTC (rev 44500)
@@ -389,7 +389,7 @@
     lua2dox luabibentry luabidi luacode luahyphenrules
     luaindex luainputenc luaintro lualatex-doc lualatex-doc-de
     lualatex-math lualibs
-    luamesh luamplib luaotfload
+    luamesh luamplib luaotfload luapackageloader
     luasseq luatex85 luatexbase luatexja luatexko luatextra
     luatodonotes luaxml
     lwarp lxfonts ly1

Modified: trunk/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc
===================================================================
--- trunk/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc	2017-06-06 21:50:19 UTC (rev 44499)
+++ trunk/Master/tlpkg/tlpsrc/collection-luatex.tlpsrc	2017-06-06 21:51:05 UTC (rev 44500)
@@ -28,6 +28,7 @@
 depend lualibs
 depend luamplib
 depend luaotfload
+depend luapackageloader
 depend luasseq
 depend luatex85
 depend luatexbase

Added: trunk/Master/tlpkg/tlpsrc/luapackageloader.tlpsrc
===================================================================


More information about the tex-live-commits mailing list