| 1 |
%% lua-placeholders.tex |
| 2 |
%% Copyright 2026 E. Nijenhuis |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the |
| 5 |
% conditions of the LaTeX Project Public License, either version 1.3c |
| 6 |
% of this license or (at your option) any later version. |
| 7 |
% The latest version of this license is in |
| 8 |
% http://www.latex-project.org/lppl.txt |
| 9 |
% and version 1.3c or later is part of all distributions of LaTeX |
| 10 |
% version 2005/12/01 or later. |
| 11 |
% |
| 12 |
% This work has the LPPL maintenance status ‘maintained’. |
| 13 |
% |
| 14 |
% The Current Maintainer of this work is E. Nijenhuis. |
| 15 |
% |
| 16 |
% This work consists of the files lua-placeholders.tex |
| 17 |
% lua-placeholders-manual.pdf lua-placeholders.lua |
| 18 |
% lua-placeholders-common.lua lua-placeholders-namespace.lua |
| 19 |
% lua-placeholders-parser.lua and lua-placeholders-types.lua |
| 20 |
%% |
| 21 |
%% Defines the engine bridge macros the Lua core defers to, plus user-facing |
| 22 |
%% commands that mirror the LaTeX side without requiring \newcommand, |
| 23 |
%% \newenvironment, or any LaTeX2e package. Load with: |
| 24 |
%% |
| 25 |
%% \input lua-placeholders |
| 26 |
%% |
| 27 |
%% from a plain LuaTeX document. All macros take mandatory arguments; |
| 28 |
%% switch the active namespace with \setnamespace{name} before \param / |
| 29 |
%% \forlistitem / etc. |
| 30 |
|
| 31 |
% --- Defaults and bridges the Lua core renders into ------------------------ |
| 32 |
\def\curnamespace{\jobname} |
| 33 |
\def\paramplaceholder#1{[#1]} |
| 34 |
\def\paramnotfound#1{[<unknown> #1]} |
| 35 |
\def\paramlistconjunction{, } |
| 36 |
|
| 37 |
% --- Engine-specific hotfix wrappers (mirror tex/lua-placeholders.sty) |
| 38 |
% The .sty wires these to numprint / isodate / xspace; here we keep them |
| 39 |
% minimal -- a power user can override before \input. |
| 40 |
\def\paramnumberformat#1{#1} |
| 41 |
\def\paramdateformat#1#2#3{#1/#2/#3} |
| 42 |
\def\paramfieldterm{} |
| 43 |
|
| 44 |
% --- Boolean flag bridge: \let the if-token directly to \iftrue/\iffalse. |
| 45 |
% \newif's internal \@if/\if@ machinery requires literal CS names and breaks |
| 46 |
% when called with \expandafter\newif\csname if#1\endcsname, especially in |
| 47 |
% plain LuaTeX where @ is "other" at user time. The simpler \let approach |
| 48 |
% works identically in plain TeX and LaTeX, supports keys with spaces, and |
| 49 |
% has no package dependency. |
| 50 |
\def\paramnewbool#1{% |
| 51 |
\expandafter\ifx\csname if#1\endcsname\relax |
| 52 |
\expandafter\let\csname if#1\endcsname=\iffalse |
| 53 |
\fi} |
| 54 |
\def\paramsetbool#1#2{\csname paramsetbool#2\endcsname{#1}} |
| 55 |
\def\paramsetbooltrue#1{\expandafter\let\csname if#1\endcsname=\iftrue} |
| 56 |
\def\paramsetboolfalse#1{\expandafter\let\csname if#1\endcsname=\iffalse} |
| 57 |
|
| 58 |
% --- User-callable macros --------------------------------------------------- |
| 59 |
% No optional arguments (plain TeX has no \newcommand). Set the default |
| 60 |
% namespace via \setnamespace; per-call namespace can be passed by |
| 61 |
% temporarily re-\def-ing \curnamespace inside a group. |
| 62 |
\def\setnamespace#1{\def\curnamespace{#1}} |
| 63 |
\def\strictparams{\directlua{lua_placeholders.set_strict()}} |
| 64 |
|
| 65 |
\def\loadrecipe#1{\directlua{lua_placeholders.recipe('#1', '\curnamespace')}} |
| 66 |
\def\loadpayload#1{\directlua{lua_placeholders.payload('#1', '\curnamespace')}} |
| 67 |
|
| 68 |
\def\param#1{\directlua{lua_placeholders.param('#1', '\curnamespace')}} |
| 69 |
\def\PARAM#1{\directlua{local p = lua_placeholders.param_object('#1', '\curnamespace') if p then tex.print(p:to_upper()) end}} |
| 70 |
\def\rawparam#1{\directlua{local p = lua_placeholders.param_object('#1', '\curnamespace') if p then tex.print(p:raw_val() or p.default or p.placeholder) end}} |
| 71 |
|
| 72 |
\def\hasparam#1#2#3{% |
| 73 |
\def\paramhastrue{#2}% |
| 74 |
\def\paramhasfalse{#3}% |
| 75 |
\directlua{lua_placeholders.handle_param_is_set('#1', '\curnamespace')}} |
| 76 |
|
| 77 |
\def\ifparam#1#2#3{% |
| 78 |
\directlua{local p = lua_placeholders.param_object('#1','\curnamespace') if p then p:set_bool('#1') end}% |
| 79 |
\csname if#1\endcsname #2\else #3\fi} |
| 80 |
|
| 81 |
\def\paramfield#1#2{\directlua{lua_placeholders.field('#1','#2','\curnamespace')}} |
| 82 |
|
| 83 |
% paramobject: plain TeX has no \newenvironment, so the user wraps content |
| 84 |
% with \paramobject{key}...\endparamobject (matching \begin / \end shape). |
| 85 |
\def\paramobject#1{\directlua{lua_placeholders.with_object('#1','\curnamespace')}} |
| 86 |
\def\endparamobject{\directlua{lua_placeholders.exit_object()}} |
| 87 |
|
| 88 |
\def\forlistitem#1#2{\directlua{lua_placeholders.for_item('#1','\curnamespace','#2')}} |
| 89 |
\def\fortablerow#1#2{\directlua{lua_placeholders.with_rows('#1','\curnamespace','#2')}} |
| 90 |
|
| 91 |
% --- Load the Lua core ------------------------------------------------------ |
| 92 |
\directlua{lua_placeholders = require('lua-placeholders')} |
| 93 |
\endinput |