| 1 |
%% lua-placeholders.sty |
| 2 |
%% Copyright 2024 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.sty |
| 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 |
\NeedsTeXFormat{LaTeX2e} |
| 22 |
\ProvidesPackage{lua-placeholders}[2026/05/19 2.0.2 Lua Placeholders Package] |
| 23 |
|
| 24 |
\RequirePackage{textcomp} |
| 25 |
\RequirePackage{xspace} |
| 26 |
|
| 27 |
\newcommand\curnamespace{\jobname} |
| 28 |
\newcommand\paramplaceholder[1]{\texttt{\textbf{[}#1\textbf{]}}} |
| 29 |
\newcommand\paramnotfound[1]{\paramplaceholder{{\textnormal{\textlangle unknown\textrangle}} #1}} |
| 30 |
\newcommand\paramlistconjunction{,~} |
| 31 |
|
| 32 |
% --- Engine/format hooks the Lua core defers to ---------------------------- |
| 33 |
% The Lua side never references package-specific commands directly: it emits |
| 34 |
% these wrappers and the .sty (or the plain-LuaTeX .tex) decides what they |
| 35 |
% mean. Each is \providecommand so a user can override before \usepackage. |
| 36 |
% |
| 37 |
% \paramnumberformat{N} -- format a numeric value |
| 38 |
% \paramdateformat{Y}{M}{D} -- format a YYYY-MM-DD date scalar |
| 39 |
% \paramfieldterm -- token appended after each \paramobject field |
| 40 |
% binding (so \name without {} doesn't gobble |
| 41 |
% the next space) |
| 42 |
\providecommand\paramnumberformat[1]{% |
| 43 |
\@ifundefined{numprint}{#1}{\numprint{#1}}% |
| 44 |
} |
| 45 |
\providecommand\paramdateformat[3]{% |
| 46 |
\@ifundefined{printdateTeX}{#1/#2/#3}{\printdateTeX{#1/#2/#3}}% |
| 47 |
} |
| 48 |
\providecommand\paramfieldterm{\xspace} |
| 49 |
|
| 50 |
% --- Boolean flag bridge ---------------------------------------------------- |
| 51 |
% Lua emits \paramnewbool{key} when a bool-typed parameter is declared and |
| 52 |
% \paramsetbool{key}{true|false} when its value is loaded. We \let the |
| 53 |
% if-token directly to \iftrue / \iffalse (no \newif, whose internal |
| 54 |
% \@if / \if@ machinery breaks for \csname-built names). \paramnewbool |
| 55 |
% routes through \@ifundefined so the literal \iffalse stays in a brace |
| 56 |
% argument, hidden from TeX's skip-scanner -- otherwise a second call for |
| 57 |
% the same key trips on it with "Incomplete \ifx". |
| 58 |
\providecommand\paramnewbool[1]{% |
| 59 |
\@ifundefined{if#1}{% |
| 60 |
\expandafter\let\csname if#1\endcsname=\iffalse% |
| 61 |
}{}% |
| 62 |
} |
| 63 |
% Dispatch \paramsetbool{key}{true|false} to a per-value helper. Putting |
| 64 |
% \iftrue/\iffalse inside an \if-conditional branch trips TeX's skip scanner |
| 65 |
% (which counts every \if* token as a nested conditional, even when it's an |
| 66 |
% argument to \let), so we keep them at the top level of the helper bodies. |
| 67 |
\providecommand\paramsetbool[2]{\csname paramsetbool#2\endcsname{#1}} |
| 68 |
\providecommand\paramsetbooltrue[1]{\expandafter\let\csname if#1\endcsname=\iftrue} |
| 69 |
\providecommand\paramsetboolfalse[1]{\expandafter\let\csname if#1\endcsname=\iffalse} |
| 70 |
|
| 71 |
\directlua{lua_placeholders = require('lua-placeholders')} |
| 72 |
|
| 73 |
\newcommand\setnamespace[1]{\renewcommand\curnamespace{#1}} |
| 74 |
\newcommand\strictparams{\directlua{lua_placeholders.set_strict()}} |
| 75 |
\newcommand%! suppress = NonMatchingIf |
| 76 |
\ifparam[4][\curnamespace]{% |
| 77 |
\directlua{local p = lua_placeholders.param_object('#2','#1') if p then p:set_bool('#2') end}% |
| 78 |
\csname if#2\endcsname #3\else #4\fi} |
| 79 |
\newcommand\param[2][\curnamespace]{\directlua{lua_placeholders.param('#2', '#1')}} |
| 80 |
\newcommand\PARAM[2][\curnamespace]{\directlua{local p = lua_placeholders.param_object('#2', '#1') if p then tex.print(p:to_upper()) end}} |
| 81 |
\def\rawparam#1#2{\directlua{local p = lua_placeholders.param_object('#2', '#1') if p then tex.print(p:raw_val() or p.default or p.placeholder) end}} |
| 82 |
\newcommand\numparam[2][\curnamespace]{\directlua{local n = lua_placeholders.param_object('#2', '#1') if n then n:print_num() end}} |
| 83 |
\newcommand\hasparam[4][\curnamespace]{% |
| 84 |
\def\paramhastrue{#3}% |
| 85 |
\def\paramhasfalse{#4}% |
| 86 |
\directlua{lua_placeholders.handle_param_is_set('#2', '#1')}} |
| 87 |
\newcommand\paramfield[3][\curnamespace]{\directlua{lua_placeholders.field('#2','#3', '#1')}} |
| 88 |
\newenvironment{paramobject}[2][\curnamespace]{\directlua{lua_placeholders.with_object('#2', '#1')}}{\directlua{lua_placeholders.exit_object()}} |
| 89 |
\newcommand\forlistitem[3][\curnamespace]{\directlua{lua_placeholders.for_item('#2', '#1', '#3')}} |
| 90 |
\newcommand\fortablerow[3][\curnamespace]{\directlua{lua_placeholders.with_rows('#2', '#1', '#3')}} |
| 91 |
\newcommand\loadrecipe[2][]{\directlua{lua_placeholders.recipe('#2', '#1')}} |
| 92 |
\newcommand\loadpayload[2][]{\directlua{lua_placeholders.payload('#2', '#1')}} |