From 7ad11d496d8b859868ec23586cb099b6b48eb9b4 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Fri, 25 Feb 2022 22:14:36 +0000 Subject: yamlvars (25feb22) git-svn-id: svn://tug.org/texlive/trunk@62176 c570f23f-e606-0410-a88d-b1316a301751 --- .../texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf | Bin 69229 -> 0 bytes .../texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex | 368 --------------- .../texmf-dist/doc/lualatex/yamlvars/yamlvars.pdf | Bin 0 -> 69483 bytes .../texmf-dist/doc/lualatex/yamlvars/yamlvars.tex | 368 +++++++++++++++ .../texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua | 492 -------------------- .../texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty | 74 --- .../texmf-dist/tex/lualatex/yamlvars/yamlvars.lua | 498 +++++++++++++++++++++ .../texmf-dist/tex/lualatex/yamlvars/yamlvars.sty | 75 ++++ 8 files changed, 941 insertions(+), 934 deletions(-) delete mode 100644 Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf delete mode 100644 Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex create mode 100644 Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.pdf create mode 100644 Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.tex delete mode 100644 Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua delete mode 100644 Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty create mode 100644 Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.lua create mode 100644 Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.sty diff --git a/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf b/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf deleted file mode 100644 index 974873680d8..00000000000 Binary files a/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf and /dev/null differ diff --git a/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex b/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex deleted file mode 100644 index 057aac5d39d..00000000000 --- a/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex +++ /dev/null @@ -1,368 +0,0 @@ -% Kale Ewasiuk (kalekje@gmail.com) -% 2022-01-18 -% Copyright (C) 2021-2022 Kale Ewasiuk -% -% Permission is hereby granted, free of charge, to any person obtaining a copy -% of this software and associated documentation files (the "Software"), to deal -% in the Software without restriction, including without limitation the rights -% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -% copies of the Software, and to permit persons to whom the Software is -% furnished to do so, subject to the following conditions: -% -% The above copyright notice and this permission notice shall be included in -% all copies or substantial portions of the Software. -% -% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR -% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE -% OR OTHER DEALINGS IN THE SOFTWARE. - - -\documentclass[11pt,parskip=half]{scrartcl} -\setlength{\parindent}{0ex} -\newcommand{\llcmd}[1]{\leavevmode\llap{\texttt{\detokenize{#1}\ }}} -\newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} -\newcommand{\qcmd}[1]{``\cmd{#1}''} -\usepackage{url} -\usepackage{xcolor} -\usepackage{showexpl} -\lstset{explpreset={justification=\raggedright,pos=r,wide=true}} -\setlength\ResultBoxRule{0mm} -\lstset{ - language=[LaTeX]TeX, - basicstyle=\ttfamily\small, - commentstyle=\ttfamily\small\color{gray}, - frame=none, - numbers=left, - numberstyle=\ttfamily\small\color{gray}, - prebreak=\raisebox{0ex}[0ex][0ex]{\color{gray}\ensuremath{\hookleftarrow}}, - extendedchars=true, - breaklines=true, - tabsize=4, -} -\addtokomafont{title}{\raggedright} -\addtokomafont{author}{\raggedright} -\addtokomafont{date}{\raggedright} -\author{Kale Ewasiuk (\url{kalekje@gmail.com})} -\usepackage[yyyymmdd]{datetime}\renewcommand{\dateseparator}{--} -\date{\today} - - - -\usepackage[overwritedefs]{YAMLvars} -\title{YAMLvars} -\subtitle{a YAML variable parser for LuaLaTeX} - -\begin{document} - - -\maketitle - - -%%%%% - -%{NOTE::: \Huge todo use LTXexample and improve examples/testing\\ -%todo need way better error tracing for this pkg} - - - -%%%%%%%%%%% - - -YAMLvars is a LuaLaTeX-based package to help make definitions or produce LaTeX code using a YAML file. -This package might be useful for you if you want to batch create docummnts -by pushing various sets YAML data to a fixed LaTeX template, -or just find it easier to read document metadata from a YAML file compared to the -standard title, author, etc. commands. - -\section{Package Options} -\leavevmode\llap{\texttt{useyv}\ \ \ }By default, when you specify a YAML variable, it will be defined using \texttt{gdef} -(only if it wasn't defined previously). -If you use this setting, unless otherwise specified, YAML variables will be accessible under -the \texttt{\textbackslash yv\{\}} command. -This also allows numbers and symbols in the variable names. -Note that internally, the variables are stored in the command sequence -\texttt{yv--}. - -\leavevmode\llap{\texttt{parseCLI}\ \ \ }If this option is enabled, any arguments passed to your lualatex compile -command that end in ``.yaml'' will be used, separated by a space. -If two yaml files are passed, the first one will be the declaration file, -and the second will be the parsing file. -They will be used at the beginning of the document. -If one yaml file is passed, it will be treated as a parsing file, so you should -declare the variables somewhere in the preamble. -This option is offered to help with automation scripts. -An example is showin in Section \ref{example}. - -\hspace*{3ex}\llcmd{allowundeclared}% -It might be helpful to define something in your YAML parsing doc without declaring it. -If you want this flexibility, use this setting. Note that eisting definitions will not be overwritten and an error -will br thrown if the name exists. - -\llcmd{overwritedefs}Danger! This will allow you to \cmd{gdef} commands with YAML. Caution should be taken to not set definitions like \cmd{begin}, \cmd{section}, etc. - -\section{Dependencies} -%This package contains the \texttt{tinyyaml} Lua package. -\llcmd{Note:}This package requires that the \cmd{markdown} (\url{https://ctan.org/pkg/markdown}) be installed. This package does not use the package in its entirety, but rather depends on the YAML interpreter it comes with: \cmd{markdown-tinyyaml.lua}. This dependency is chosen to avoid redundancy in your TeX installation and align development of the \cmd{tinyyaml} Lua package. -If you want to use the YAML interpreter for other purposes, you can bring it into Lua by either: -\begin{verbatim} -\directlua{yaml = YAMLvars.yaml} or -\directlua{yaml = require'markdown-tinyyaml'} -\end{verbatim} -The distribution: \url{https://github.com/api7/lua-tinyyaml}\\ -The YAML specification: \url{https://yaml.org/spec/}\\ - -Many of the ``transform'' and ``processing'' functions built-in to this package rely on other packages, -like -\texttt{hyperref}, for example, but it is not loaded, and this package will only load -\cmd{penlight}, -\cmd{luacode}, -\cmd{xspace}, and -\cmd{etoolbox}. - -\section{Declaring variables} -A declaration file can either be parsed with the command \texttt{declareYAMLvarsFile} command, -or, if you want to do it \LaTeX, you can put the YAML code in the \texttt{declareYAMLvars} environment. -It is a declaring YAML docuemnt is (like all YAML) key-value ditionary: -The top level key is the name of the variable to be defined/used. -If the value of the top level is a string: it's interpreted as a single transform function to be applied. -Otherwise, it must -be a table that contains at least one of the following keys:\\ -\texttt{xfm} (transform, may be a string or list of strings),\\ -\texttt{prc} (processing, must be a single string), or \\ -\texttt{dft} (default value, if being defined. Must be a string). - -If you want to change the way a variable is initialized, you can change the function -\cmd{YAMLvars.dec.PRC = function (var) ... end } where \cmd{PRC} is how the variable will be processed (\cmd{gdef}, \cmd{yvdef}, \cmd{length}, or something of your choosing). - -The default value for variables is the Lua \texttt{nil}. -YAMLvars will first check if the definition exists, if so, an error will be thrown -so that we avoid overwriting. -If the token is available, it is set to a package error, so that if the variable no defined later on, an error will -tell the user they forgot to set it. -This will be overwritten when you parse the variables and assign a value to it. - -You can change the default \texttt{xfm}, \texttt{prc}, or \texttt{dft} by changing -the value (in Lua): \texttt{YAMLvars.xfmDefault = ''} etc. - -Here is an example of a declaration document. -\begin{verbatim} -\begin{declareYAMLvars} -Location: addxspace # sets xfm=addxspace -People: [arrsortlastnameAZ, list2nl] # BAD! don't do. -People: - xfm: [arrsortlastnameAZ, list2nl] # Correct way -Company: - dft: Amazon # Change default only -Revisions: - dft: '1 & \today & initial version \\' - xfm: [sortZA, list2tab] -Rhead: - prc: setRightHead -\end{declareYAMLvars} -\end{verbatim} - - -To change how a variable is declared (initialize), you can modify or add functions in -\texttt{YAMLvars.dec} table, where the index is the same as the \texttt{prc} name. -This function accepts two variables, the var name, and the default value set by dft. -For lengths and toggles (from etoolbox), these functions are used to initialize lengths -with newlength and newtoggle. - - -\section{Parsing variables} -A YAML file to be parsed will contain the variables as the top level keys, similar to declaring. -The value can be anything you want; as long as you have applied appropriate transform and declaring -functions to it so that it can be useful. For example, a value specified as a YAML list will first be -interpreted as a Lua table (with numeric indexes/keys). You could declare a series of transforms functions -to sort this table, map functions, and convert it to a series of \LaTeX \texttt{\textbackslash item}s. - -Here is an example of a parsing document. - - -\begin{verbatim} -\begin{declareYAMLvars} -Location: Planet Earth -People: # a YAML list - - Some One # turns into Lua table - - No Body -# company assumed Amazon if not set here -Rhead: \today -\end{declareYAMLvars} -\end{verbatim} - - - \section{xfm -- Transform Functions} -These functions accept two arguments: \texttt{(var, val)} where \texttt{var} is the variable (or key) and val is the value. -The transforms are specified as a list and are iteratively applied to the val. -Usually, the final \texttt{xfm} function should produce a string so it can be defined. - -Hint: if for some reason, your \texttt{xfm} and \texttt{prc} depends on other variables, -you can access them within the function -with \texttt{YAMLvars.varsvals} - -\subsection{Defining your own transform functions} -After the package is loaded, you may add your function (somewhere in Lua) -by adding it to the \texttt{YAMLvars.xfm} table. -For example, if you wanted to wrap a variable's value with ``xxx'', here's how you could do that. -\begin{verbatim} -function myfunction(var, val) - return 'xxx'..val..'xxx' -end -YAMLvars.xfm['addmyfunction'] = myfunction -\end{verbatim} - -If you want to run some Lua code and write in your YAML file (weird idea, but maybe useful for one-off functions), -you can do so by specifying a transform function with an \texttt{=} in it to make a lambda function. -For example, a \texttt{xfm} equal to -``\texttt{= '---'..x..'---'}'' -would surround your YAML variable's value with em-dashes. -You can access the variable name with this lambda function with \texttt{v}. -If you want to just execute code (instead of settings \texttt{x = }, use \texttt{/}). - - \section{prc -- Processing Functions} -Like the transform functions, the processing function must accept \texttt{(var, val)}. -Only one processing function is applied to the final (var, val) after the transforms are done. - -This package includes \texttt{gdef} to set a definition, \texttt{yvdef} to define a variable under the \texttt{yv} command. -\texttt{title, author, date} to set \texttt{\textbackslash @title, \textbackslash @author, \textbackslash @date}, -respectively - -\pagebreak - -\section{Some Examples} - -\begin{LTXexample} -%! language = yaml -\begin{declareYAMLvars} -address: - xfm: - - list2nl - - = x..'!!!' -name: null - -title: - xfm: - - lb2nl -# - / YAMLvars.prvcmd(titletext, YAMLvars.varsvals['atitle']:gsub('\n', ' ')..'\\xspace{}') -\end{declareYAMLvars} - -%! language = yaml -\begin{parseYAMLvars} -title: |- - A Multiline - Monumental Title! - -name: Joe Smith -address: - - 1234 Fake St. - - City -\end{parseYAMLvars} - -\title - -%\titletext! - -\name - -\address - -\end{LTXexample} - -\pagebreak - \section{Automation Example}\label{example} - - Suppose you had a number of bills of sales in yaml format and wanted to produce some nice pdfs. - The following code shows how this could be done. - - \pagebreak - \vspace*{-5em} - \subsection{The main tex template} - \begin{verbatim} - %% main.tex -\documentclass{article} -\usepackage[paperheight=4in,paperwidth=3in,margin=0.25in]{geometry} -\usepackage[pl,func,extras]{penlight} -\usepackage[useyv,parseCLI]{YAMLvars} % using command line option to make files -\usepackage{hyperref} -\usepackage{xspace} -\usepackage{luacode} - -\setlength{\parindent}{0ex} -\setlength{\parskip}{0.75em} - -\begin{luacode*} -- adding a custom function, put hfill between k-v pairs - function YAMLvars.xfm.kv2hfill(var, val) - local t = {} - for k, v in pairs(val) do - t[#t+1] = k..'\\hfill '..tostring(v) - end - return t - end -\end{luacode*} - - -%! language = yaml -\begin{declareYAMLvars} -Customer: addxspace -Date: addxspace -Items: - xfm: [kv2hfill, arr2itemize] -\end{declareYAMLvars} - -\begin{document} - Bill of sale for: \hfill \yv{Customer}\\ - Purchased: \hfill \yv{Date}\\ - \begin{itemize} - \item[] ITEM \hfill PRICE - \yv{Items} % the yaml variable - \begin{luacode*} - totalcost = pl.tablex.reduce('+', - pl.tablex.values(YAMLvars.varsvals['Items']), 0) - tex.print('\\item[] TOTAL:\\hfill'..tostring(totalcost)) - \end{luacode*} - \end{itemize} -\end{document} - \end{verbatim} - - \subsection{The lua automation script} - \begin{verbatim} ---automate.lua -for f in io.popen('dir .'):lines() do -- get all files and info in cwd - local i, j = f:find('%S*%.yaml') -- find fnames - if i ~= nil then - f = f:sub(i,j) -- extract .yaml file name (no space in fname allowed) - os.execute('lualatex -output-format=pdf main.tex '.. f) - -- compile w/ yaml file as arg - local fnew = f:gsub('yaml', 'pdf') -- file name for output pdf - os.remove(fnew) -- delete if it exists already - os.rename('main.pdf', fnew) -- change main.pdf to same as yaml file name - end -end - \end{verbatim} - -\subsection{The yaml data files} - \begin{verbatim} -# sale1.yaml -Customer: Someone Cold -Date: January 2, 2021 -Items: - Toque: 12 - Mitts: 5.6 - Boots: 80 - -# sale2.yaml -Customer: Someone Warm -Date: July 1, 2021 -Items: - Beer (24 pk): 24 - Sunscreen: 5 - Hat: 12 - \end{verbatim} -\end{document} - diff --git a/Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.pdf b/Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.pdf new file mode 100644 index 00000000000..bdb6b811da3 Binary files /dev/null and b/Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.pdf differ diff --git a/Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.tex b/Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.tex new file mode 100644 index 00000000000..a61df030241 --- /dev/null +++ b/Master/texmf-dist/doc/lualatex/yamlvars/yamlvars.tex @@ -0,0 +1,368 @@ +% Kale Ewasiuk (kalekje@gmail.com) +% 2022-02-25 +% Copyright (C) 2021-2022 Kale Ewasiuk +% +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR +% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +% OR OTHER DEALINGS IN THE SOFTWARE. + + +\documentclass[11pt,parskip=half]{scrartcl} +\setlength{\parindent}{0ex} +\newcommand{\llcmd}[1]{\leavevmode\llap{\texttt{\detokenize{#1}\ }}} +\newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} +\newcommand{\qcmd}[1]{``\cmd{#1}''} +\usepackage{url} +\usepackage{xcolor} +\usepackage{showexpl} +\lstset{explpreset={justification=\raggedright,pos=r,wide=true}} +\setlength\ResultBoxRule{0mm} +\lstset{ + language=[LaTeX]TeX, + basicstyle=\ttfamily\small, + commentstyle=\ttfamily\small\color{gray}, + frame=none, + numbers=left, + numberstyle=\ttfamily\small\color{gray}, + prebreak=\raisebox{0ex}[0ex][0ex]{\color{gray}\ensuremath{\hookleftarrow}}, + extendedchars=true, + breaklines=true, + tabsize=4, +} +\addtokomafont{title}{\raggedright} +\addtokomafont{author}{\raggedright} +\addtokomafont{date}{\raggedright} +\author{Kale Ewasiuk (\url{kalekje@gmail.com})} +\usepackage[yyyymmdd]{datetime}\renewcommand{\dateseparator}{--} +\date{\today} + + + +\usepackage[overwritedefs]{YAMLvars} +\title{YAMLvars} +\subtitle{a YAML variable parser for LuaLaTeX} + +\begin{document} + + +\maketitle + + +%%%%% + +%{NOTE::: \Huge todo use LTXexample and improve examples/testing\\ +%todo need way better error tracing for this pkg} + + + +%%%%%%%%%%% + + +YAMLvars is a LuaLaTeX-based package to help make definitions or produce LaTeX code using a YAML file. +This package might be useful for you if you want to batch create docummnts +by pushing various sets YAML data to a fixed LaTeX template, +or just find it easier to read document metadata from a YAML file compared to the +standard title, author, etc. commands. + +\section{Package Options} +\leavevmode\llap{\texttt{useyv}\ \ \ }By default, when you specify a YAML variable, it will be defined using \texttt{gdef} +(only if it wasn't defined previously). +If you use this setting, unless otherwise specified, YAML variables will be accessible under +the \texttt{\textbackslash yv\{\}} command. +This also allows numbers and symbols in the variable names. +Note that internally, the variables are stored in the command sequence +\texttt{yv--}. + +\leavevmode\llap{\texttt{parseCLI}\ \ \ }If this option is enabled, any arguments passed to your lualatex compile +command that end in ``.yaml'' will be used, separated by a space. +If two yaml files are passed, the first one will be the declaration file, +and the second will be the parsing file. +They will be used at the beginning of the document. +If one yaml file is passed, it will be treated as a parsing file, so you should +declare the variables somewhere in the preamble. +This option is offered to help with automation scripts. +An example is showin in Section \ref{example}. + +\hspace*{3ex}\llcmd{allowundeclared}% +It might be helpful to define something in your YAML parsing doc without declaring it. +If you want this flexibility, use this setting. Note that existing definitions will not be overwritten and an error +will br thrown if the name exists. Alternatively, you can use the commands \cmd{\AllowUndeclaredYV} or \cmd{\ForbidUndeclaredYV} to toggle this behavior. + +\llcmd{overwritedefs}Danger! This will allow you to \cmd{gdef} commands with YAML. Caution should be taken to not set definitions like \cmd{begin}, \cmd{section}, etc. + +\section{Dependencies} +%This package contains the \texttt{tinyyaml} Lua package. +\llcmd{Note:}This package requires that the \cmd{markdown} (\url{https://ctan.org/pkg/markdown}) be installed. This package does not use the package in its entirety, but rather depends on the YAML interpreter it comes with: \cmd{markdown-tinyyaml.lua}. This dependency is chosen to avoid redundancy in your TeX installation and align development of the \cmd{tinyyaml} Lua package. +If you want to use the YAML interpreter for other purposes, you can bring it into Lua by either: +\begin{verbatim} +\directlua{yaml = YAMLvars.yaml} or +\directlua{yaml = require'markdown-tinyyaml'} +\end{verbatim} +The distribution: \url{https://github.com/api7/lua-tinyyaml}\\ +The YAML specification: \url{https://yaml.org/spec/}\\ + +Many of the ``transform'' and ``processing'' functions built-in to this package rely on other packages, +like +\texttt{hyperref}, for example, but it is not loaded, and this package will only load +\cmd{penlight}, +\cmd{luacode}, +\cmd{xspace}, and +\cmd{etoolbox}. + +\section{Declaring variables} +A declaration file can either be parsed with the command \texttt{declareYAMLvarsFile} command, +or, if you want to do it \LaTeX, you can put the YAML code in the \texttt{declareYAMLvars} environment. +It is a declaring YAML docuemnt is (like all YAML) key-value ditionary: +The top level key is the name of the variable to be defined/used. +If the value of the top level is a string: it's interpreted as a single transform function to be applied. +Otherwise, it must +be a table that contains at least one of the following keys:\\ +\texttt{xfm} (transform, may be a string or list of strings),\\ +\texttt{prc} (processing, must be a single string), or \\ +\texttt{dft} (default value, if being defined. Must be a string). + +If you want to change the way a variable is initialized, you can change the function +\cmd{YAMLvars.dec.PRC = function (var) ... end } where \cmd{PRC} is how the variable will be processed (\cmd{gdef}, \cmd{yvdef}, \cmd{length}, or something of your choosing). + +The default value for variables is the Lua \texttt{nil}. +YAMLvars will first check if the definition exists, if so, an error will be thrown +so that we avoid overwriting. +If the token is available, it is set to a package error, so that if the variable no defined later on, an error will +tell the user they forgot to set it. +This will be overwritten when you parse the variables and assign a value to it. + +You can change the default \texttt{xfm}, \texttt{prc}, or \texttt{dft} by changing +the value (in Lua): \texttt{YAMLvars.xfmDefault = ''} etc. + +Here is an example of a declaration document. +\begin{verbatim} +\begin{declareYAMLvars} +Location: addxspace # sets xfm=addxspace +People: [arrsortlastnameAZ, list2nl] # BAD! don't do. +People: + xfm: [arrsortlastnameAZ, list2nl] # Correct way +Company: + dft: Amazon # Change default only +Revisions: + dft: '1 & \today & initial version \\' + xfm: [sortZA, list2tab] +Rhead: + prc: setRightHead +\end{declareYAMLvars} +\end{verbatim} + + +To change how a variable is declared (initialize), you can modify or add functions in +\texttt{YAMLvars.dec} table, where the index is the same as the \texttt{prc} name. +This function accepts two variables, the var name, and the default value set by dft. +For lengths and toggles (from etoolbox), these functions are used to initialize lengths +with newlength and newtoggle. + + +\section{Parsing variables} +A YAML file to be parsed will contain the variables as the top level keys, similar to declaring. +The value can be anything you want; as long as you have applied appropriate transform and declaring +functions to it so that it can be useful. For example, a value specified as a YAML list will first be +interpreted as a Lua table (with numeric indexes/keys). You could declare a series of transforms functions +to sort this table, map functions, and convert it to a series of \LaTeX \texttt{\textbackslash item}s. + +Here is an example of a parsing document. + + +\begin{verbatim} +\begin{parseYAMLvars} +Location: Planet Earth +People: # a YAML list + - Some One # turns into Lua table + - No Body +# company assumed Amazon if not set here +Rhead: \today +\end{parseYAMLvars} +\end{verbatim} + + + \section{xfm -- Transform Functions} +These functions accept two arguments: \texttt{(var, val)} where \texttt{var} is the variable (or key) and val is the value. +The transforms are specified as a list and are iteratively applied to the val. +Usually, the final \texttt{xfm} function should produce a string so it can be defined. + +Hint: if for some reason, your \texttt{xfm} and \texttt{prc} depends on other variables, +you can access them within the function +with \texttt{YAMLvars.varsvals} + +\subsection{Defining your own transform functions} +After the package is loaded, you may add your function (somewhere in Lua) +by adding it to the \texttt{YAMLvars.xfm} table. +For example, if you wanted to wrap a variable's value with ``xxx'', here's how you could do that. +\begin{verbatim} +function myfunction(var, val) + return 'xxx'..val..'xxx' +end +YAMLvars.xfm['addmyfunction'] = myfunction +\end{verbatim} + +If you want to run some Lua code and write in your YAML file (weird idea, but maybe useful for one-off functions), +you can do so by specifying a transform function with an \texttt{=} in it to make a lambda function. +For example, a \texttt{xfm} equal to +``\texttt{= '---'..x..'---'}'' +would surround your YAML variable's value with em-dashes. +You can access the variable name with this lambda function with \texttt{v}. +If you want to just execute code (instead of settings \texttt{x = }, use \texttt{/}). + + \section{prc -- Processing Functions} +Like the transform functions, the processing function must accept \texttt{(var, val)}. +Only one processing function is applied to the final (var, val) after the transforms are done. + +This package includes \texttt{gdef} to set a definition, \texttt{yvdef} to define a variable under the \texttt{yv} command. +\texttt{title, author, date} to set \texttt{\textbackslash @title, \textbackslash @author, \textbackslash @date}, +respectively + +\pagebreak + +\section{Some Examples} + +\begin{LTXexample} +%! language = yaml +\begin{declareYAMLvars} +address: + xfm: + - list2nl + - = x..'!!!' +name: null + +title: + xfm: + - lb2nl +# - / YAMLvars.prvcmd(titletext, YAMLvars.varsvals['atitle']:gsub('\n', ' ')..'\\xspace{}') +\end{declareYAMLvars} + +%! language = yaml +\begin{parseYAMLvars} +title: |- + A Multiline + Monumental Title! + +name: Joe Smith +address: + - 1234 Fake St. + - City +\end{parseYAMLvars} + +\title + +%\titletext! + +\name + +\address + +\end{LTXexample} + +\pagebreak + \section{Automation Example}\label{example} + + Suppose you had a number of bills of sales in yaml format and wanted to produce some nice pdfs. + The following code shows how this could be done. + + \pagebreak + \vspace*{-5em} + \subsection{The main tex template} + \begin{verbatim} + %% main.tex +\documentclass{article} +\usepackage[paperheight=4in,paperwidth=3in,margin=0.25in]{geometry} +\usepackage[pl,func,extras]{penlight} +\usepackage[useyv,parseCLI]{YAMLvars} % using command line option to make files +\usepackage{hyperref} +\usepackage{xspace} +\usepackage{luacode} + +\setlength{\parindent}{0ex} +\setlength{\parskip}{0.75em} + +\begin{luacode*} -- adding a custom function, put hfill between k-v pairs + function YAMLvars.xfm.kv2hfill(var, val) + local t = {} + for k, v in pairs(val) do + t[#t+1] = k..'\\hfill '..tostring(v) + end + return t + end +\end{luacode*} + + +%! language = yaml +\begin{declareYAMLvars} +Customer: addxspace +Date: addxspace +Items: + xfm: [kv2hfill, arr2itemize] +\end{declareYAMLvars} + +\begin{document} + Bill of sale for: \hfill \yv{Customer}\\ + Purchased: \hfill \yv{Date}\\ + \begin{itemize} + \item[] ITEM \hfill PRICE + \yv{Items} % the yaml variable + \begin{luacode*} + totalcost = pl.tablex.reduce('+', + pl.tablex.values(YAMLvars.varsvals['Items']), 0) + tex.print('\\item[] TOTAL:\\hfill'..tostring(totalcost)) + \end{luacode*} + \end{itemize} +\end{document} + \end{verbatim} + + \subsection{The lua automation script} + \begin{verbatim} +--automate.lua +for f in io.popen('dir .'):lines() do -- get all files and info in cwd + local i, j = f:find('%S*%.yaml') -- find fnames + if i ~= nil then + f = f:sub(i,j) -- extract .yaml file name (no space in fname allowed) + os.execute('lualatex -output-format=pdf main.tex '.. f) + -- compile w/ yaml file as arg + local fnew = f:gsub('yaml', 'pdf') -- file name for output pdf + os.remove(fnew) -- delete if it exists already + os.rename('main.pdf', fnew) -- change main.pdf to same as yaml file name + end +end + \end{verbatim} + +\subsection{The yaml data files} + \begin{verbatim} +# sale1.yaml +Customer: Someone Cold +Date: January 2, 2021 +Items: + Toque: 12 + Mitts: 5.6 + Boots: 80 + +# sale2.yaml +Customer: Someone Warm +Date: July 1, 2021 +Items: + Beer (24 pk): 24 + Sunscreen: 5 + Hat: 12 + \end{verbatim} +\end{document} + diff --git a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua b/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua deleted file mode 100644 index 93234e5d596..00000000000 --- a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua +++ /dev/null @@ -1,492 +0,0 @@ ---% Kale Ewasiuk (kalekje@gmail.com) ---% 2022-01-18 ---% Copyright (C) 2021-2022 Kale Ewasiuk ---% ---% Permission is hereby granted, free of charge, to any person obtaining a copy ---% of this software and associated documentation files (the "Software"), to deal ---% in the Software without restriction, including without limitation the rights ---% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ---% copies of the Software, and to permit persons to whom the Software is ---% furnished to do so, subject to the following conditions: ---% ---% The above copyright notice and this permission notice shall be included in ---% all copies or substantial portions of the Software. ---% ---% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ---% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED ---% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A ---% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT ---% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ---% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ---% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ---% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE ---% OR OTHER DEALINGS IN THE SOFTWARE. - - --- tinyyaml license ---MIT License --- ---Copyright (c) 2017 peposso --- ---Permission is hereby granted, free of charge, to any person obtaining a copy ---of this software and associated documentation files (the "Software"), to deal ---in the Software without restriction, including without limitation the rights ---to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ---copies of the Software, and to permit persons to whom the Software is ---furnished to do so, subject to the following conditions: --- ---The above copyright notice and this permission notice shall be included in all ---copies or substantial portions of the Software. --- ---THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ---IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ---FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ---AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ---LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ---OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ---SOFTWARE. - - - -YAMLvars = {} -- self table -YAMLvars.xfm = {} -YAMLvars.prc = {} -YAMLvars.dec = {} -- table of declare function - -YAMLvars.varsvals = {} -YAMLvars.varspecs = {} - -YAMLvars.prcDefault = 'gdef' -YAMLvars.dftDefault = nil -YAMLvars.xfmDefault = {} - -YAMLvars.allowUndeclared = false -YAMLvars.overwritedefs = false - -YAMLvars.valTemp = '' -YAMLvars.varTemp = '' - -YAMLvars.tabmidrule = 'hline' - -YAMLvars.debug = false - -YAMLvars.yaml = require'markdown-tinyyaml' -- note: YAMLvars.sty will have checked existence of this already -local pl = _G['penlight'] or _G['pl'] -- penlight for this namespace is pl - - -function YAMLvars.debugtalk(s, ss) - if YAMLvars.debug then - help_wrt(s, ss) - end -end - - --- xfm functions (transforms) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -function YAMLvars.xfm.addxspace(var, val) - return val .. '\\xspace{}' -end - -function YAMLvars.xfm.tab2arr(var, val) - return pl.array2d.from_table(val) -end - -function YAMLvars.xfm.arrsort2ZA(var, val) - return pl.array2d.sortOP(val, pl.operator.strgt) -end - -function YAMLvars.xfm.addrule2arr(var, val) - return pl.array2d.map_slice2(_1..'\\\\\\'.. YAMLvars.tabmidrule..' ', val, 1,-1,-2,-1) -- todo make gmidrule -end - -function YAMLvars.xfm.arr2tabular(var, val) - return pl.array2d.toTeX(val)..'\\\\' -end - - -function YAMLvars.xfm.list2items(var, val) -- todo should be list2item - return pl.List(val):map('\\item '.._1):join(' ') -end -YAMLvars.xfm.arr2itemize = YAMLvars.xfm.list2items - -function YAMLvars.xfm.arrsortAZ(var, val) - return pl.List(val):sort(pl.operator.strlt) -end - -function YAMLvars.xfm.arrsortZA(var, val) - return pl.List(val):sort(pl.operator.strgt) -end - -local function complastname(a, b) - a = a:split(' ') - b = b:split(' ') - a = a[#a] - b = b[#b] - return a < b -end - -function YAMLvars.xfm.arrsortlastnameAZ(var, val) - val = pl.List(val):sort(complastname) - return val -end --- todo need distinction beyyween table and penlight list ??? -function YAMLvars.xfm.list2nl(var, val) - return pl.tablex.join(val,'\\\\ ') -end - - --val = pl.array2d.map_slice1(_1..'\\\\', val, 1,-2) - --return val:join('') - --return pl.tablex.reduce(_1.._2, val, '') - - -function YAMLvars.xfm.lb2nl(var, val) --linebreak in text 2 new line - val, _ = val:gsub('\n','\\\\ ') - return val -end - -function YAMLvars.xfm.lowercase(var, val) - return val:lower() -end - - -function YAMLvars.xfm.markdown(var, val) - --return '\\begin{markdown} '..val..'\n \\end{markdown}' - help_wrt(val, md) - return [[begin markdown ..val.. - - par end markdown]] -end - - - --- dec laration functions, -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -function YAMLvars.dec.gdef(var, dft) - YAMLvars.deccmd(var, dft) -end - -function YAMLvars.dec.yvdef(var, dft) - YAMLvars.deccmd('yv--'..var, dft) -end - -function YAMLvars.dec.toggle(var, dft) - tex.print('\\global\\newtoggle{'..var..'}') - YAMLvars.prc.toggle(var, dft) -end - -function YAMLvars.dec.length(var, dft) - tex.print('\\global\\newlength{\\'..var..'}') - YAMLvars.prc.length(var, dft) -end - - - --- prc functions (processing) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -function YAMLvars.prc.gdef(var, val) - token.set_macro(var, val, 'global') - YAMLvars.debugtalk(var..' = '..val, 'prc gdef') -end - -function YAMLvars.prc.yvdef(var, val) - token.set_macro('yv--'..var, val, 'global') - YAMLvars.debugtalk('yv--'..var..' = '..val, 'prc yvdef') -end - -function YAMLvars.prc.toggle(t, v) -- requires penlight extras - local s = '' - if hasval(v) then - s = '\\global\\toggletrue{'..t..'}' - else - s = '\\global\\togglefalse{'..t..'}' - end - tex.print(s) - YAMLvars.debugtalk(s, 'prc toggle') -end - -function YAMLvars.prc.length(t, v) - v = v or '0pt' - local s = '\\global\\setlength{\\global\\'..t..'}{'..v..'}' - tex.print(s) - YAMLvars.debugtalk(s, 'prc length') -end - -function YAMLvars.prc.PDFtitle(var, val) - tex.print('\\hypersetup{pdftitle={'..val..'}}') - --tex.print('\\setPDFtitle{'..val..'}') -end - -function YAMLvars.prc.PDFauthor(var, val) - tex.print('\\hypersetup{pdfauthor={'..val..'}}') - --tex.print('\\setPDFauthor{'..val..'}') -end - - -function YAMLvars.prc.setheader(val, rl) - local _, count = string.gsub(val, '\\\\', '') - if count == 0 then - val = '{\\ }\\\\'..val - end - val = '\\setstretch{0.8}'..val - tex.print('\\'..rl..'ohead{'..val..'}') -end - -function YAMLvars.prc.rhead(var, val) - YAMLvars.prc.setheader(val, 'r') -end - -function YAMLvars.prc.lhead(var, val) - YAMLvars.prc.setheader(val, 'l') -end - -function YAMLvars.prc.title(var, val) - token.set_macro('@title', val, 'global') -end - -function YAMLvars.prc.author(var, val) - token.set_macro('@author', val, 'global') -end - -function YAMLvars.prc.date(var, val) - token.set_macro('@date', val, 'global') -end - --- do this with author, title, company -function YAMLvars.prc.setdocvar(var, val) - tex.print('\\'..var..'{'..val..'}') -end - -function YAMLvars.prc.setdocvarOpts(var, val) - local s = '\\'..var..'{'..tostring(val[1])..'}' - for k, v in pairs(val) do - if k > 1 then - s = s..'['..tostring(v)..']' - end - end - tex.print(s) -end - - - --- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - - - -function YAMLvars.prvcmd(cs, val) -- provide command via lua - if token.is_defined(cs) and (not YAMLvars.overwritedefs) then - tex.print('\\PackageError{YAMLvars}{Variable '..cs..' already defined, could not declare}{}') - else - token.set_macro(cs, val, 'global') - end -end - -function YAMLvars.deccmd(cs, def) - if def == nil then - YAMLvars.prvcmd(cs, '\\PackageError{YAMLvars}{Variable "'..cs..'" was declared and used but, not set}{}') - else - YAMLvars.prvcmd(cs, def) - end -end - - -local function getYAMLfile(y) - local f = io.open(y,"r") - if f ~= nil then - y = f:read('*a') - io.close(f) - return y - else - tex.print('\\PackageError{YAMLvars}{YAML file "'..y..'" not found}{}') - return 'YAMLvars: FileNotFound' - end -end - - - -function YAMLvars.declareYAMLvarsStr(y) - local t = YAMLvars.yaml.parse(y) - for var, specs in pairs(t) do - YAMLvars.varspecs[var] = {xfm=YAMLvars.xfmDefault,prc=YAMLvars.prcDefault,dft=YAMLvars.dftDefault} - if type(specs) == 'string' then - specs = {xfm={specs}} - end - if specs['xfm'] == nil then specs['xfm'] = {} end - for s, p in pairs(specs) do - if s == 'xfm' and type(p) ~= 'table' then p = {p} end - YAMLvars.varspecs[var][s] = p -- set property of var - end - if YAMLvars.dec[YAMLvars.varspecs[var].prc] ~= nil then - YAMLvars.dec[YAMLvars.varspecs[var].prc](var, YAMLvars.varspecs[var].dft) - --else -- actually don't a dec function for all - -- -- -- -- tex.print('\\PackageError{YAMLvars}{Declaration function for '..YAMLvarspecs[var].prc..'not found}{}') - end - end - YAMLvars.debugtalk(YAMLvars.varspecs, 'declared YAML vars, varspecs') -end - - - -function YAMLvars.declareYAMLvarsFile(y) - YAMLvars.declareYAMLvarsStr(getYAMLfile(y)) -end - - -local function check_def(var, val) - if YAMLvars.allowUndeclared then - if YAMLvars.prcDefault == 'yvdef' then - YAMLvars.prc.yvdef(var, val) - else - YAMLvars.prvcmd(var, val) - end - else - tex.print('\\PackageError{YAMLvars}{Variable "'..var..'" set but not declared}{}') - end -end - -local function sub_lua_var(s, v1, v2) - return s:gsub('([%A?%-?])('..v1..')([%W?%-?])', '%1'..v2..'%3') -- replace x variables -end - -local _YV_invalid_expression = '\1 invalid expression' -local _YV_no_return = '\2 no return val' -local function eval_expr(func, var, val) - local s, c = func:gsub('^[=/]', {['/'] = '\2', ['='] = 'YAMLvars.valTemp = '}, 1) -- / is run code, = sets val = code - if c == 0 then - return _YV_invalid_expression - else - --help_wrt(s, var) - --help_wrt(val, var) - YAMLvars.valTemp = val - YAMLvars.varTemp = var - --help_wrt(s, var) - s, c = s:gsub('\2', '') -- strip \2 that might have appeared if / was applied - s = sub_lua_var(' '..s, 'x', 'YAMLvars.valTemp') - s = sub_lua_var(s, 'v', 'YAMLvars.varTemp') - --help_wrt(s, var) - local f, err = pcall(loadstring(s)) - if not f then - tex.print('\\PackageError{YAMLvars}{xfm with "= or /" error on var "'..var..'"}{}') -- - end - if c > 0 then - return _YV_no_return - end - return YAMLvars.valTemp - end -end - -local function transform_and_prc(var, val) - for _, func in pairs(YAMLvars.varspecs[var]['xfm']) do --apply cleaning functions - local f = YAMLvars.xfm[func] - if f == nil then - local val2 = eval_expr(func, var, val) - if val2 == _YV_invalid_expression then - tex.print('\\PackageError{YAMLvars}{xfm function "'..func..'" not defined or invalid expression passed on var "'..var..'"}{}') - elseif val == _YV_no_return then - else - val = val2 - end - else - val = f(var, val) - end - end - f = YAMLvars.prc[YAMLvars.varspecs[var]['prc']] - if f == nil then - tex.print('\\PackageError{YAMLvars}{prc function "'..YAMLvars.varspecs[var]['prc']..'" on var "'..var..'" not defined}{}') - end - f(var, val) -- prc the value of the variable -end - -function YAMLvars.parseYAMLvarsStr(y) - YAMLvars.varsvals = YAMLvars.yaml.parse(y) - for var, val in pairs(YAMLvars.varsvals) do - if YAMLvars.varspecs[var] == nil then - check_def(var, val) -- if not declared - -- todo consider free form parse declaring - -- variable name: {xfm:, dec:, prc:, val: } - -- definitely doable here - else - transform_and_prc(var, val) - end - end -end - -function YAMLvars.parseYAMLvarsFile(y) - YAMLvars.parseYAMLvarsStr(getYAMLfile(y)) -end - - - -function YAMLvars.print_varspecs() - local pretty = require('pl.pretty') - texio.write_nl('VVVVVV Var specifications:') - texio.write_nl(pretty.write(YAMLvars.varspecs)) -end - - - --- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - --- https://tex.stackexchange.com/questions/38150/in-lualatex-how-do-i-pass-the-content-of-an-environment-to-lua-verbatim -recordedbuf = "" -function readbuf(buf) - i,j = string.find(buf, '\\end{%w+}') - if i==nil then -- if not ending an environment - recordedbuf = recordedbuf .. buf .. "\n" - return "" - else - return nil - end -end - -function startrecording() - recordedbuf = "" - luatexbase.add_to_callback('process_input_buffer', readbuf, 'readbuf') -end - -function stoprecording() - luatexbase.remove_from_callback('process_input_buffer', 'readbuf') - recordedbuf = recordedbuf:gsub("\\end{%w+}\n","") -end - - - -function YAMLvars.doYAMLfiles(t) - if #t == 2 then - YAMLvars.declareYAMLvarsFile(t[1]) - YAMLvars.parseYAMLvarsFile(t[2]) - elseif #t == 1 then - YAMLvars.parseYAMLvarsFile(t[1]) - else - tex.print('\\PackageWarning{YAMLvars}{No .yaml files found in CLI args"}{}') - end -end - -function YAMLvars.getYAMLcli() - local t = {} - if arg then - for i,v in pairs(arg) do - if v:find('.*%.yaml$') then - t[#t+1] = v - end - end - end - --help_wrt(t) - return t - end - - -return YAMLvars - - - - --clean = clean or true - --if clean then -- clean first part of yaml string - -- y = clean_tex_spaces(y) - --end ---local function clean_tex_spaces(s) --- help_wrt(s) --- if s:sub(1,2) == '%s' then --- s, _ = s:gsub('%s+','',1) --- end --- s, _ = s:gsub('\\par ','\n\n') --- return s ---end \ No newline at end of file diff --git a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty b/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty deleted file mode 100644 index 73c40ba6931..00000000000 --- a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty +++ /dev/null @@ -1,74 +0,0 @@ -% Kale Ewasiuk (kalekje@gmail.com) -% 2022-01-18 -% Copyright (C) 2021-2022 Kale Ewasiuk -% -% Permission is hereby granted, free of charge, to any person obtaining a copy -% of this software and associated documentation files (the "Software"), to deal -% in the Software without restriction, including without limitation the rights -% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -% copies of the Software, and to permit persons to whom the Software is -% furnished to do so, subject to the following conditions: -% -% The above copyright notice and this permission notice shall be included in -% all copies or substantial portions of the Software. -% -% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR -% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE -% OR OTHER DEALINGS IN THE SOFTWARE. - - - -\NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{YAMLvars}[2022-01-18] - -\IfFileExists{markdown-tinyyaml.lua}{}{\PackageError{YAMLvars}{This package requires installation of the 'markdown' package, please install it and try again}{}} - -\RequirePackage{luacode} -\RequirePackage{xspace} -\RequirePackage{etoolbox} -\RequirePackage[pl,extras]{penlight} - -\luadirect{YAMLvars = require('YAMLvars')} - -\DeclareOption{useyv}{ - \gdef\yv#1{\csname yv--#1\endcsname} - \luadirect{YAMLvars.prcDefault = 'yvdef'} -} -\DeclareOption{parseCLI}{ - \AtBeginDocument{\luadirect{YAMLvars.doYAMLfiles(YAMLvars.getYAMLcli())}} -} -\DeclareOption{allowundeclared}{ - \luadirect{YAMLvars.allowUndeclared = true} -} -\DeclareOption{overwritedefs}{ - \luadirect{YAMLvars.overwritedefs = true} -} -\DeclareOption{debug}{ - \luadirect{YAMLvars.debug = true} -} - -\DeclareOption*{\PackageWarning{YAMLvars}{Unknown option: '\CurrentOption'}{}} -\ProcessOptions\relax - - -\newcommand{\declareYAMLvarsFile}[1]{\luadirect{YAMLvars.declareYAMLvarsFile(\luastring{#1})}} -\newcommand{\parseYAMLvarsFile}[1]{\luadirect{YAMLvars.parseYAMLvarsFile(\luastring{#1})}} - - -\newenvironment{declareYAMLvars}{\luadirect{startrecording()}}{\luadirect{stoprecording()} -\luadirect{YAMLvars.declareYAMLvarsStr(recordedbuf)} -} - -\newenvironment{parseYAMLvars}{\luadirect{startrecording()}}{\luadirect{stoprecording()} -\luadirect{YAMLvars.parseYAMLvarsStr(recordedbuf)} -} - -\newcommand{\resetYAMLvarsspec}{\luadirect{YAMLvars.varspec = {}}} - -\newcommand{\AllowUndeclaredYV}{\luadirect{YAMLvars.allowUndeclared = true}} diff --git a/Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.lua b/Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.lua new file mode 100644 index 00000000000..1f95a50f250 --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.lua @@ -0,0 +1,498 @@ +--% Kale Ewasiuk (kalekje@gmail.com) +--% 2022-02-25 +--% Copyright (C) 2021-2022 Kale Ewasiuk +--% +--% Permission is hereby granted, free of charge, to any person obtaining a copy +--% of this software and associated documentation files (the "Software"), to deal +--% in the Software without restriction, including without limitation the rights +--% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +--% copies of the Software, and to permit persons to whom the Software is +--% furnished to do so, subject to the following conditions: +--% +--% The above copyright notice and this permission notice shall be included in +--% all copies or substantial portions of the Software. +--% +--% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +--% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +--% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +--% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +--% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR +--% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +--% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +--% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +--% OR OTHER DEALINGS IN THE SOFTWARE. + + +-- tinyyaml license +--MIT License +-- +--Copyright (c) 2017 peposso +-- +--Permission is hereby granted, free of charge, to any person obtaining a copy +--of this software and associated documentation files (the "Software"), to deal +--in the Software without restriction, including without limitation the rights +--to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +--copies of the Software, and to permit persons to whom the Software is +--furnished to do so, subject to the following conditions: +-- +--The above copyright notice and this permission notice shall be included in all +--copies or substantial portions of the Software. +-- +--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +--IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +--FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +--AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +--LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +--OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +--SOFTWARE. + + + +YAMLvars = {} -- self table +YAMLvars.xfm = {} +YAMLvars.prc = {} +YAMLvars.dec = {} -- table of declare function + +YAMLvars.varsvals = {} +YAMLvars.varspecs = {} + +YAMLvars.xfmDefault = {} +YAMLvars.prcDefault = 'gdef' +YAMLvars.dftDefault = nil + +YAMLvars.allowUndeclared = false +YAMLvars.overwritedefs = false + +YAMLvars.valTemp = '' +YAMLvars.varTemp = '' + +YAMLvars.tabmidrule = 'hline' + +YAMLvars.debug = false + +YAMLvars.yaml = require'markdown-tinyyaml' -- note: YAMLvars.sty will have checked existence of this already +local pl = _G['penlight'] or _G['pl'] -- penlight for this namespace is pl + + +function YAMLvars.debugtalk(s, ss) + if YAMLvars.debug then + help_wrt(s, ss) + end +end + + +-- xfm functions (transforms) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +function YAMLvars.xfm.addxspace(var, val) + return val .. '\\xspace' +end + +function YAMLvars.xfm.tab2arr(var, val) + return pl.array2d.from_table(val) +end + +function YAMLvars.xfm.arrsort2ZA(var, val) + return pl.array2d.sortOP(val, pl.operator.strgt) +end + +function YAMLvars.xfm.addrule2arr(var, val) + return pl.array2d.map_slice2(_1..'\\\\\\'.. YAMLvars.tabmidrule..' ', val, 1,-1,-2,-1) -- todo make gmidrule +end + +function YAMLvars.xfm.arr2tabular(var, val) + return pl.array2d.toTeX(val)..'\\\\' +end + + +function YAMLvars.xfm.list2items(var, val) -- todo should be list2item + return pl.List(val):map('\\item '.._1):join(' ') +end +YAMLvars.xfm.arr2itemize = YAMLvars.xfm.list2items + +function YAMLvars.xfm.arrsortAZ(var, val) + return pl.List(val):sort(pl.operator.strlt) +end + +function YAMLvars.xfm.arrsortZA(var, val) + return pl.List(val):sort(pl.operator.strgt) +end + +local function complastname(a, b) + a = a:split(' ') + b = b:split(' ') + a = a[#a] + b = b[#b] + return a < b +end + +function YAMLvars.xfm.arrsortlastnameAZ(var, val) + val = pl.List(val):sort(complastname) + return val +end +-- todo need distinction beyyween table and penlight list ??? +function YAMLvars.xfm.list2nl(var, val) + return pl.tablex.join(val,'\\\\ ') +end + + --val = pl.array2d.map_slice1(_1..'\\\\', val, 1,-2) + --return val:join('') + --return pl.tablex.reduce(_1.._2, val, '') + + +function YAMLvars.xfm.lb2nl(var, val) --linebreak in text 2 new line + val, _ = val:gsub('\n','\\\\ ') + return val +end + +function YAMLvars.xfm.lowercase(var, val) + return val:lower() +end + + +function YAMLvars.xfm.markdown(var, val) + --return '\\begin{markdown} '..val..'\n \\end{markdown}' + help_wrt(val, md) + return [[begin markdown ..val.. + + par end markdown]] +end + + + +-- dec laration functions, -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + +function YAMLvars.dec.gdef(var, dft) + YAMLvars.deccmd(var, dft) +end + +function YAMLvars.dec.yvdef(var, dft) + YAMLvars.deccmd('yv--'..var, dft) +end + +function YAMLvars.dec.toggle(var, dft) + tex.print('\\global\\newtoggle{'..var..'}') + YAMLvars.prc.toggle(var, dft) +end + +function YAMLvars.dec.length(var, dft) + tex.print('\\global\\newlength{\\'..var..'}') + YAMLvars.prc.length(var, dft) +end + + + +-- prc functions (processing) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + +function YAMLvars.prc.gdef(var, val) + token.set_macro(var, val, 'global') + YAMLvars.debugtalk(var..' = '..val, 'prc gdef') +end + +function YAMLvars.prc.yvdef(var, val) + token.set_macro('yv--'..var, val, 'global') + YAMLvars.debugtalk('yv--'..var..' = '..val, 'prc yvdef') +end + +function YAMLvars.prc.toggle(t, v) -- requires penlight extras + local s = '' + if hasval(v) then + s = '\\global\\toggletrue{'..t..'}' + else + s = '\\global\\togglefalse{'..t..'}' + end + tex.print(s) + YAMLvars.debugtalk(s, 'prc toggle') +end + +function YAMLvars.prc.length(t, v) + v = v or '0pt' + local s = '\\global\\setlength{\\global\\'..t..'}{'..v..'}' + tex.print(s) + YAMLvars.debugtalk(s, 'prc length') +end + +function YAMLvars.prc.PDFtitle(var, val) + tex.print('\\hypersetup{pdftitle={'..val..'}}') + --tex.print('\\setPDFtitle{'..val..'}') +end + +function YAMLvars.prc.PDFauthor(var, val) + tex.print('\\hypersetup{pdfauthor={'..val..'}}') + --tex.print('\\setPDFauthor{'..val..'}') +end + + +function YAMLvars.prc.setheader(val, rl) + local _, count = string.gsub(val, '\\\\', '') + if count == 0 then + val = '{\\ }\\\\'..val + end + val = '\\setstretch{0.8}'..val + tex.print('\\'..rl..'ohead{'..val..'}') +end + +function YAMLvars.prc.rhead(var, val) + YAMLvars.prc.setheader(val, 'r') +end + +function YAMLvars.prc.lhead(var, val) + YAMLvars.prc.setheader(val, 'l') +end + +function YAMLvars.prc.title(var, val) + token.set_macro('@title', val, 'global') +end + +function YAMLvars.prc.author(var, val) + token.set_macro('@author', val, 'global') +end + +function YAMLvars.prc.date(var, val) + token.set_macro('@date', val, 'global') +end + +-- do this with author, title, company +function YAMLvars.prc.setdocvar(var, val) + tex.print('\\'..var..'{'..val..'}') +end + +function YAMLvars.prc.setdocvarOpts(var, val) + local s = '\\'..var..'{'..tostring(val[1])..'}' + for k, v in pairs(val) do + if k > 1 then + s = s..'['..tostring(v)..']' + end + end + tex.print(s) +end + + + +-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + + + +function YAMLvars.prvcmd(cs, val) -- provide command via lua + if token.is_defined(cs) and (not YAMLvars.overwritedefs) then + tex.print('\\PackageError{YAMLvars}{Variable '..cs..' already defined, could not declare}{}') + else + token.set_macro(cs, val, 'global') + end +end + +function YAMLvars.deccmd(cs, def) + if def == nil then + YAMLvars.prvcmd(cs, '\\PackageError{YAMLvars}{Variable "'..cs..'" was declared and used but, not set}{}') + else + YAMLvars.prvcmd(cs, def) + end +end + + +local function getYAMLfile(y) + local f = io.open(y,"r") + if f ~= nil then + y = f:read('*a') + io.close(f) + return y + else + tex.print('\\PackageError{YAMLvars}{YAML file "'..y..'" not found}{}') + return 'YAMLvars: FileNotFound' + end +end + + + +function YAMLvars.declareYAMLvarsStr(y) + local t = YAMLvars.yaml.parse(y) + for var, specs in pairs(t) do + YAMLvars.varspecs[var] = {xfm=YAMLvars.xfmDefault,prc=YAMLvars.prcDefault,dft=YAMLvars.dftDefault} + if type(specs) == 'string' then + specs = {xfm={specs}} + end + if specs['xfm'] == nil then specs['xfm'] = {} end + for s, p in pairs(specs) do + if s == 'xfm' and type(p) ~= 'table' then p = {p} end + YAMLvars.varspecs[var][s] = p -- set property of var + end + if YAMLvars.dec[YAMLvars.varspecs[var].prc] ~= nil then + YAMLvars.dec[YAMLvars.varspecs[var].prc](var, YAMLvars.varspecs[var].dft) + --else -- actually don't a dec function for all + -- -- -- -- tex.print('\\PackageError{YAMLvars}{Declaration function for '..YAMLvarspecs[var].prc..'not found}{}') + end + end + YAMLvars.debugtalk(YAMLvars.varspecs, 'declared YAML vars, varspecs') +end + + + +function YAMLvars.declareYAMLvarsFile(y) + YAMLvars.declareYAMLvarsStr(getYAMLfile(y)) +end + + +local function check_def(var, val) + if YAMLvars.allowUndeclared then + if YAMLvars.prcDefault == 'yvdef' then + YAMLvars.prc.yvdef(var, val) + else + YAMLvars.prvcmd(var, val) + end + else + tex.print('\\PackageError{YAMLvars}{Variable "'..var..'" set but not declared}{}') + end +end + +local function sub_lua_var(s, v1, v2) + return s:gsub('([%A?%-?])('..v1..')([%W?%-?])', '%1'..v2..'%3') -- replace x variables +end + +local _YV_invalid_expression = '\1 invalid expression' +local _YV_no_return = '\2 no return val' + +local function expr_err(var, val) + tex.print('\\PackageError{YAMLvars}{xfm with "= or /" error on var "'..var..'"}{}') -- todo make program stop +end + +local function eval_expr(func, var, val) + local s, c = func:gsub('^[=/]', {['/'] = '\2', ['='] = 'YAMLvars.valTemp = '}, 1) -- / is run code, = sets val = code + if c == 0 then + return _YV_invalid_expression + else + --help_wrt(s, var) + --help_wrt(val, var) + YAMLvars.valTemp = val + YAMLvars.varTemp = var + --help_wrt(s, var) + s, c = s:gsub('\2', '') -- strip \2 that might have appeared if / was applied + s = sub_lua_var(' '..s, 'x', 'YAMLvars.valTemp') + s = sub_lua_var(s, 'v', 'YAMLvars.varTemp') + --help_wrt(s, var) + local f, err = pcall(loadstring(s)) + if not f then + tex.print('\\PackageError{YAMLvars}{xfm with "= or /" error on var "'..var..'"}{}') -- + end + if c > 0 then + expr_err(var) + return _YV_no_return + end + return YAMLvars.valTemp + end +end + +local function transform_and_prc(var, val) + for _, func in pairs(YAMLvars.varspecs[var]['xfm']) do --apply cleaning functions + local f = YAMLvars.xfm[func] + if f == nil then + local val2 = eval_expr(func, var, val) + if val2 == _YV_invalid_expression then + tex.print('\\PackageError{YAMLvars}{xfm function "'..func..'" not defined or invalid expression passed on var "'..var..'"}{}') + elseif val == _YV_no_return then + else + val = val2 + end + else + val = f(var, val) + end + end + f = YAMLvars.prc[YAMLvars.varspecs[var]['prc']] + if f == nil then + tex.print('\\PackageError{YAMLvars}{prc function "'..YAMLvars.varspecs[var]['prc']..'" on var "'..var..'" not defined}{}') + end + f(var, val) -- prc the value of the variable +end + +function YAMLvars.parseYAMLvarsStr(y) + YAMLvars.varsvals = YAMLvars.yaml.parse(y) + for var, val in pairs(YAMLvars.varsvals) do + if YAMLvars.varspecs[var] == nil then + check_def(var, val) -- if not declared + -- todo consider free form parse declaring + -- variable name: {xfm:, dec:, prc:, val: } + -- definitely doable here + else + transform_and_prc(var, val) + end + end +end + +function YAMLvars.parseYAMLvarsFile(y) + YAMLvars.parseYAMLvarsStr(getYAMLfile(y)) +end + + + +function YAMLvars.print_varspecs() + local pretty = require('pl.pretty') + texio.write_nl('VVVVVV Var specifications:') + texio.write_nl(pretty.write(YAMLvars.varspecs)) +end + + + +-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + +-- https://tex.stackexchange.com/questions/38150/in-lualatex-how-do-i-pass-the-content-of-an-environment-to-lua-verbatim +recordedbuf = "" +function readbuf(buf) + i,j = string.find(buf, '\\end{%w+}') + if i==nil then -- if not ending an environment + recordedbuf = recordedbuf .. buf .. "\n" + return "" + else + return nil + end +end + +function startrecording() + recordedbuf = "" + luatexbase.add_to_callback('process_input_buffer', readbuf, 'readbuf') +end + +function stoprecording() + luatexbase.remove_from_callback('process_input_buffer', 'readbuf') + recordedbuf = recordedbuf:gsub("\\end{%w+}\n","") +end + + + +function YAMLvars.doYAMLfiles(t) + if #t == 2 then + YAMLvars.declareYAMLvarsFile(t[1]) + YAMLvars.parseYAMLvarsFile(t[2]) + elseif #t == 1 then + YAMLvars.parseYAMLvarsFile(t[1]) + else + tex.print('\\PackageWarning{YAMLvars}{No .yaml files found in CLI args"}{}') + end +end + +function YAMLvars.getYAMLcli() + local t = {} + if arg then + for i,v in pairs(arg) do + if v:find('.*%.yaml$') then + t[#t+1] = v + end + end + end + --help_wrt(t) + return t + end + + +return YAMLvars + + + + --clean = clean or true + --if clean then -- clean first part of yaml string + -- y = clean_tex_spaces(y) + --end +--local function clean_tex_spaces(s) +-- help_wrt(s) +-- if s:sub(1,2) == '%s' then +-- s, _ = s:gsub('%s+','',1) +-- end +-- s, _ = s:gsub('\\par ','\n\n') +-- return s +--end \ No newline at end of file diff --git a/Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.sty b/Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.sty new file mode 100644 index 00000000000..8e295988fbf --- /dev/null +++ b/Master/texmf-dist/tex/lualatex/yamlvars/yamlvars.sty @@ -0,0 +1,75 @@ +% Kale Ewasiuk (kalekje@gmail.com) +% 2022-02-25 +% Copyright (C) 2021-2022 Kale Ewasiuk +% +% Permission is hereby granted, free of charge, to any person obtaining a copy +% of this software and associated documentation files (the "Software"), to deal +% in the Software without restriction, including without limitation the rights +% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +% copies of the Software, and to permit persons to whom the Software is +% furnished to do so, subject to the following conditions: +% +% The above copyright notice and this permission notice shall be included in +% all copies or substantial portions of the Software. +% +% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR +% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +% OR OTHER DEALINGS IN THE SOFTWARE. + + + +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{YAMLvars}[2022-02-25] + +\IfFileExists{markdown-tinyyaml.lua}{}{\PackageError{YAMLvars}{This package requires installation of the 'markdown' package, please install it and try again}{}} + +\RequirePackage{luacode} +\RequirePackage{xspace} +\RequirePackage{etoolbox} +\RequirePackage[pl,extras]{penlight} + +\luadirect{YAMLvars = require('YAMLvars')} + +\DeclareOption{useyv}{ + \gdef\yv#1{\csname yv--#1\endcsname} + \luadirect{YAMLvars.prcDefault = 'yvdef'} +} +\DeclareOption{parseCLI}{ + \AtBeginDocument{\luadirect{YAMLvars.doYAMLfiles(YAMLvars.getYAMLcli())}} +} +\DeclareOption{allowundeclared}{ + \luadirect{YAMLvars.allowUndeclared = true} +} +\DeclareOption{overwritedefs}{ + \luadirect{YAMLvars.overwritedefs = true} +} +\DeclareOption{debug}{ + \luadirect{YAMLvars.debug = true} +} + +\DeclareOption*{\PackageWarning{YAMLvars}{Unknown option: '\CurrentOption'}{}} +\ProcessOptions\relax + + +\newcommand{\declareYAMLvarsFile}[1]{\luadirect{YAMLvars.declareYAMLvarsFile(\luastring{#1})}} +\newcommand{\parseYAMLvarsFile}[1]{\luadirect{YAMLvars.parseYAMLvarsFile(\luastring{#1})}} + + +\newenvironment{declareYAMLvars}{\luadirect{startrecording()}}{\luadirect{stoprecording()} +\luadirect{YAMLvars.declareYAMLvarsStr(recordedbuf)} +} + +\newenvironment{parseYAMLvars}{\luadirect{startrecording()}}{\luadirect{stoprecording()} +\luadirect{YAMLvars.parseYAMLvarsStr(recordedbuf)} +} + +\newcommand{\resetYAMLvarsspec}{\luadirect{YAMLvars.varspec = {}}} + +\newcommand{\AllowUndeclaredYV}{\luadirect{YAMLvars.allowUndeclared = true}} +\newcommand{\ForbidUndeclaredYV}{\luadirect{YAMLvars.allowUndeclared = false}} -- cgit v1.2.3