summaryrefslogtreecommitdiff
path: root/luatexbase-modutils.dtx
diff options
context:
space:
mode:
Diffstat (limited to 'luatexbase-modutils.dtx')
-rw-r--r--luatexbase-modutils.dtx241
1 files changed, 86 insertions, 155 deletions
diff --git a/luatexbase-modutils.dtx b/luatexbase-modutils.dtx
index bd84dc7..4a08c4c 100644
--- a/luatexbase-modutils.dtx
+++ b/luatexbase-modutils.dtx
@@ -66,7 +66,7 @@ See source file '\inFileName' for details.
\generate{%
\usedir{tex/luatex/luatexbase}%
- \file{luatexbase.modutils.lua}{\from{luatexbase-modutils.dtx}{luamodule}}%
+ \file{modutils.lua}{\from{luatexbase-modutils.dtx}{luamodule}}%
\usedir{doc/luatex/luatexbase}%
\file{test-modutils.lua}{\from{luatexbase-modutils.dtx}{testdummy}}%
}
@@ -77,7 +77,7 @@ See source file '\inFileName' for details.
\Msg{* To finish the installation you have to move the following}
\Msg{* files into a directory searched by TeX:}
\Msg{*}
-\Msg{* luatexbase-modutils.sty luatexbase.modutils.lua}
+\Msg{* luatexbase-modutils.sty modutils.lua}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
@@ -126,6 +126,8 @@ See source file '\inFileName' for details.
% \begin{abstract}
% \end{abstract}
%
+% \tableofcontents
+%
% \section{Documentation}
%
% Lua has some embedded module management, with the functions \texttt{module}
@@ -135,8 +137,8 @@ See source file '\inFileName' for details.
% \texttt{\string\luatexUseModule} and \texttt{\string\luatexRequireModule}
% that act like them, but for lua files. The functions \texttt{module} and
% \texttt{require} should not be used, in profit of the lua functions
-% \texttt{luatextra.provides\_module} and \texttt{luatextra.use\_module} or
-% \texttt{luatextra.require\_module}.
+% \texttt{luatexbase.provides\_module} and \texttt{luatexbase.use\_module} or
+% \texttt{luatexbase.require\_module}.
%
% \section{Implementation}
%
@@ -202,7 +204,7 @@ See source file '\inFileName' for details.
\let\x\ProvidesPackage
\fi
\expandafter\endgroup
-\x{luatexbase-modutils}[2010/03/26 v0.1 Module utilities for LuaTeX (mpg)]
+\x{luatexbase-modutils}[2010/03/26 v0.1 Module utilities for LuaTeX]
% \end{macrocode}
%
% Make sure \luatex is used.
@@ -229,7 +231,8 @@ See source file '\inFileName' for details.
\fi
% \end{macrocode}
%
-% Load the package loader.
+% Load \pk{luatexbase-loader} (hence \pk{luatexbase-compat}) and require
+% supporting Lua module.
%
% \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
@@ -238,37 +241,22 @@ See source file '\inFileName' for details.
\else
\RequirePackage{luatexbase-loader}
\fi
+\luatexbase@directlua{require('luatexbase.modutils')}
% \end{macrocode}
%
-% \subsubsection{Main code}
-%
-% The \texttt{\string\luatexModuleError} macro is called by the lua function
-% \texttt{luatextra.module\_error}. It is necessary because we can't call
-% directly \texttt{\string\errmessage} in lua.
-%
-% \begin{macrocode}
-
-\def\luatexModuleError#1#2{%
- \errorcontextlines=0\relax
- \immediate\write16{}%
- \errmessage{Module #1 error: #2^^J^^J%
-See the module #1 documentation for explanation.^^J ...^^J}%
-}
-
-% \end{macrocode}
+% \subsubsection{User macros}
%
% Then we define
% \texttt{\string\luatexUseModule} that simply calls
-% \texttt{luatextra.use\_module}.
+% \texttt{luatexbase.use\_module}.
% If the package is loaded with Plain, we define
% \texttt{\string\luaRequireModule} with two mandatory arguments.
%
% \begin{macrocode}
-\def\luatexUseModule#1{\directlua{luatextra.use_module([[#1]])}}
-
+\def\luatexUseModule#1{\luatexbase@directlua{luatexbase.use_module([[#1]])}}
\expandafter\ifx\csname ProvidesPackage\endcsname\relax
- \def\luatexRequireModule#1#2{\directlua{%
- luatextra.require_module([[#1]], [[#2]])}}
+ \def\luatexRequireModule#1#2{\luatexbase@directlua{%
+ luatexbase.require_module([[#1]], [[#2]])}}
\else
% \end{macrocode}
%
@@ -278,12 +266,12 @@ See the module #1 documentation for explanation.^^J ...^^J}%
% date).
%
% \begin{macrocode}
- \newcommand\luatexRequireModule[2][0]{\directlua{luatextra.require_module([[#2]], [[#1]])}}
+ \newcommand\luatexRequireModule[2][0]{%
+ \luatexbase@directlua{luatexbase.require_module([[#2]], [[#1]])}}
\fi
% \end{macrocode}
%
% \begin{macrocode}
-\directlua{dofile(assert(kpse.find_file('luatexbase.modutils.lua', 'tex')))}
% \end{macrocode}
%
% \begin{macrocode}
@@ -295,37 +283,13 @@ See the module #1 documentation for explanation.^^J ...^^J}%
%
% \begin{macrocode}
%<*luamodule>
+module("luatexbase", package.seeall)
% \end{macrocode}
%
% Initialisation borrowed from luatextra
%
% \begin{macrocode}
-module("luatextra", package.seeall)
-
-local format = string.format
-
-luatextra.modules = luatextra.modules or {}
-% \end{macrocode}
-%
-% Here we define the warning and error functions specific to
-% \texttt{luatextra}.
-%
-% \begin{macrocode}
-
-luatextra.internal_warning_spaces = " "
-
-function luatextra.internal_warning(msg)
- if not msg then return end
- texio.write_nl(format("\nLuaTeXtra Warning: %s\n\n", msg))
-end
-
-luatextra.internal_error_spaces = " "
-
-function luatextra.internal_error(msg)
- if not msg then return end
- tex.sprint(format("\\immediate\\write16{}\\errmessage{LuaTeXtra error: %s^^J^^J}", msg))
-end
-
+local modules = modules or {}
% \end{macrocode}
%
% \subsubsection{Error, warning and info function for modules}
@@ -337,153 +301,115 @@ end
% to be used by lua module writers.
%
% \begin{macrocode}
-
-function luatextra.module_error(package, msg, helpmsg)
- if not package or not msg then
- return
- end
- if helpmsg then
- tex.sprint(format("\\errhelp{%s}", helpmsg))
- end
- tex.sprint(format("\\luatexModuleError{%s}{%s}", package, msg))
+local function module_error_int(mod, ...)
+ error('Module '..mod..' error: '..string.format(...), 3)
end
-
-function luatextra.module_warning(modulename, msg)
- if not modulename or not msg then
- return
- end
- texio.write_nl(format("\nModule %s Warning: %s\n\n", modulename, msg))
+function module_error(mod, ...)
+ module_error_int(mod, ...)
end
-
-function luatextra.module_log(modulename, msg)
- if not modulename or not msg then
- return
- end
- texio.write_nl('log', format("%s: %s", modulename, msg))
+function module_warning(mod, ...)
+ texio.write_nl("Module "..mod.." warning: "..string.format(...))
end
-
-function luatextra.module_term(modulename, msg)
- if not modulename or not msg then
- return
- end
- texio.write_nl('term', format("%s: %s", modulename, msg))
+function module_info(mod, ...)
+ texio.write_nl(mod..": "..string.format(...))
end
-
-function luatextra.module_info(modulename, msg)
- if not modulename or not msg then
- return
- end
- texio.write_nl(format("%s: %s\n", modulename, msg))
+function module_log(mod, ...)
+ texio.write_nl('log', mod..": "..string.format(...))
+end
+function module_term(mod, ...)
+ texio.write_nl('term', mod..": "..string.format(...))
end
-
% \end{macrocode}
%
-% \subsubsection{module loading and providing functions}
-%
-% A small patch, for the \texttt{module} function to work in this file. I
-% can't understand why it doens't otherwise.
+% For our own convenience, local functions for warning and errors in this
+% module.
%
% \begin{macrocode}
-
-luatextra.module = module
-
+local function err(...) module_error_int('luatexbase.modutils', ...) end
+local function warn(...) module_warning('luatexbase.modutils', ...) end
% \end{macrocode}
%
-% \begin{macro}{luatextra.use module}
+% \subsubsection{module loading and providing functions}
%
% This macro is the one used to simply load a lua module file. It does not
% reload it if it's already loaded, and prints the filename in the terminal
% and the log. A lua module must call the macro
-% \texttt{luatextra.provides\_module}.
+% \texttt{provides\_module}.
%
% \begin{macrocode}
-function luatextra.use_module(name)
- if not name or luatextra.modules[name] then
+function use_module(name)
+ if not name or modules[name] then
return
end
require(name)
- if not luatextra.modules[name] then
- luatextra.internal_warning(format("You have requested module `%s',\n%s but the file %s does not provide it.", name, luatextra.internal_warning_spaces))
- end
- if not package.loaded[name] then
- luatextra.module(name, package.seeall)
+ if not modules[name] then
+ warn("You have requested module `%s', "
+ .."but no file seems to provide it.", name)
end
end
% \end{macrocode}
%
-% \end{macro}
-%
% Some internal functions to convert a date into a number, and to determine
% if a string is a date. It is useful for
-% \texttt{luatextra.require\_package} to understand if a user asks a
+% \texttt{require\_package} to understand if a user asks a
% version with a date or a version number.
%
% \begin{macrocode}
-
-function luatextra.datetonumber(date)
+local function datetonumber(date)
numbers = string.gsub(date, "(%d+)/(%d+)/(%d+)", "%1%2%3")
return tonumber(numbers)
end
-
-function luatextra.isdate(date)
+local function isdate(date)
for _, _ in string.gmatch(date, "%d+/%d+/%d+") do
return true
end
return false
end
-
local date, number = 1, 2
-
-function luatextra.versiontonumber(version)
- if luatextra.isdate(version) then
- return {type = date, version = luatextra.datetonumber(version), orig = version}
+local function versiontonumber(version)
+ if isdate(version) then
+ return {type = date, version = datetonumber(version), orig = version}
else
return {type = number, version = tonumber(version), orig = version}
end
end
-
-luatextra.requiredversions = {}
-
+local requiredversions = {}
% \end{macrocode}
%
-% \begin{macro}{luatextra.require module}
-%
-% This function is like the \texttt{luatextra.use\_module} function, but
+% This function is like the \texttt{use\_module} function, but
% can accept a second argument that checks for the version of the module.
% The version can be a number or a date (format yyyy/mm/dd).
%
% \begin{macrocode}
-
-function luatextra.require_module(name, version)
+function require_module(name, version)
if not name then
return
end
if not version then
- return luatextra.use_module(name)
+ return use_module(name)
end
- luaversion = luatextra.versiontonumber(version)
- if luatextra.modules[name] then
+ luaversion = versiontonumber(version)
+ if modules[name] then
if luaversion.type == date then
- if luatextra.datetonumber(luatextra.modules[name].date) < luaversion.version then
- luatextra.internal_error(format("found module `%s' loaded in version %s, but version %s was required", name, luatextra.modules[name].date, version))
+ if datetonumber(modules[name].date) < luaversion.version then
+ err("found module `%s' loaded in version %s, "
+ .."but version %s was required",
+ name, modules[name].date, version)
end
else
- if luatextra.modules[name].version < luaversion.version then
- luatextra.internal_error(format("found module `%s' loaded in version %.02f, but version %s was required", name, luatextra.modules[name].version, version))
+ if modules[name].version < luaversion.version then
+ err("found module `%s' loaded in version %.02f, "
+ .."but version %s was required",
+ name, modules[name].version, version)
end
end
else
- luatextra.requiredversions[name] = luaversion
- luatextra.use_module(name)
+ requiredversions[name] = luaversion
+ use_module(name)
end
end
-
% \end{macrocode}
%
-% \end{macro}
-%
-% \begin{macro}{luatextra.provides module}
-%
% This macro is the one that must be called in the module files. It takes a
% table as argument. You can put any information you want in this table,
% but the mandatory ones are \texttt{name} (a string), \texttt{version} (a
@@ -495,32 +421,37 @@ end
% does for informations about packages.
%
% \begin{macrocode}
-
-function luatextra.provides_module(mod)
+function provides_module(mod)
if not mod then
- luatextra.internal_error('cannot provide nil module')
+ err('cannot provide nil module')
return
end
- if not mod.version or not mod.name or not mod.date or not mod.description then
- luatextra.internal_error('invalid module registered, fields name, version, date and description are mandatory')
+ if not mod.version or not mod.name or not mod.date
+ or not mod.description then
+ err("invalid module registered: "
+ .."fields name, version, date and description are mandatory")
return
end
- requiredversion = luatextra.requiredversions[mod.name]
+ requiredversion = requiredversions[mod.name]
if requiredversion then
- if requiredversion.type == date and requiredversion.version > luatextra.datetonumber(mod.date) then
- luatextra.internal_error(format("loading module %s in version %s, but version %s was required", mod.name, mod.date, requiredversion.orig))
- elseif requiredversion.type == number and requiredversion.version > mod.version then
- luatextra.internal_error(format("loading module %s in version %.02f, but version %s was required", mod.name, mod.version, requiredversion.orig))
+ if requiredversion.type == date
+ and requiredversion.version > datetonumber(mod.date) then
+ err("loading module %s in version %s, "
+ .."but version %s was required",
+ mod.name, mod.date, requiredversion.orig)
+ elseif requiredversion.type == number
+ and requiredversion.version > mod.version then
+ err("loading module %s in version %.02f, "
+ .."but version %s was required",
+ mod.name, mod.version, requiredversion.orig)
end
end
- luatextra.modules[mod.name] = module
- texio.write_nl('log', format("Lua module: %s %s v%.02f %s\n", mod.name, mod.date, mod.version, mod.description))
+ modules[mod.name] = module
+ texio.write_nl('log', string.format("Lua module: %s %s v%.02f %s\n",
+ mod.name, mod.date, mod.version, mod.description))
end
-
% \end{macrocode}
%
-% \end{macro}
-%
% \begin{macrocode}
%</luamodule>
% \end{macrocode}
@@ -531,7 +462,7 @@ end
%
% \begin{macrocode}
%<*testdummy>
-luatextra.provides_module {
+luatexbase.provides_module {
name = 'test-modutils',
date = '2000/01/01',
version = 1,