%% lua-placeholders.sty
%% Copyright 2024 E. Nijenhuis
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status ‘maintained’.
%
% The Current Maintainer of this work is E. Nijenhuis.
%
% This work consists of the files lua-placeholders.sty
% lua-placeholders-manual.pdf lua-placeholders.lua
% lua-placeholders-common.lua lua-placeholders-namespace.lua
% lua-placeholders-parser.lua and lua-placeholders-types.lua

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lua-placeholders}[2024/04/02 1.0.3 Lua Placeholders Package]

\RequirePackage{ifthen}
\RequirePackage{luapackageloader}
\RequirePackage{textcomp}
\RequirePackage{xspace}

\newcommand\curnamespace{\jobname}
\newcommand\paramplaceholder[1]{\texttt{\textbf{[}#1\textbf{]}}}
\newcommand\paramnotfound[1]{\paramplaceholder{{\textnormal{\textlangle unknown\textrangle}} #1}}
\newcommand\paramlistconjunction{,~}

\directlua{lua_placeholders = require('lua-placeholders')}

\newcommand\setnamespace[1]{\renewcommand\curnamespace{#1}}
\newcommand\strictparams{\directlua{lua_placeholders.set_strict()}}
\newcommand%! suppress = NonMatchingIf
\ifparam[4][\curnamespace]{%
    \directlua{local p = lua_placeholders.param_object('#2','#1') if p then p:set_bool('#2') end}%
    \ifthenelse{\boolean{#2}}{#3}{#4}}
\newcommand\param[2][\curnamespace]{\directlua{lua_placeholders.param('#2', '#1')}}
\newcommand\PARAM[2][\curnamespace]{\directlua{local p = lua_placeholders.param_object('#2', '#1') if p then tex.print(p:to_upper()) end}}
\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}}
\newcommand\numparam[2][\curnamespace]{\directlua{local n = lua_placeholders.param_object('#2', '#1') if n then n:print_num() end}}
\newcommand\hasparam[4][\curnamespace]{%
    \def\has@param@true{#3}%
    \def\has@param@false{#4}%
    \directlua{lua_placeholders.handle_param_is_set('#2', '#1')}}
\newcommand\paramfield[3][\curnamespace]{\directlua{lua_placeholders.field('#2','#3', '#1')}}
\newenvironment{paramobject}[2][\curnamespace]{\directlua{lua_placeholders.with_object('#2', '#1')}}{}
\newcommand\forlistitem[3][\curnamespace]{\directlua{lua_placeholders.for_item('#2', '#1', '#3')}}
\newcommand\fortablerow[3][\curnamespace]{\directlua{lua_placeholders.with_rows('#2', '#1', '#3')}}
\newcommand\loadrecipe[2][]{\directlua{lua_placeholders.recipe('#2', '#1')}}
\newcommand\loadpayload[2][]{\directlua{lua_placeholders.payload('#2', '#1')}}
