texlive[60990] Master/texmf-dist: yamlvars (7nov21)

commits+karl at tug.org commits+karl at tug.org
Sun Nov 7 22:27:54 CET 2021


Revision: 60990
          http://tug.org/svn/texlive?view=revision&revision=60990
Author:   karl
Date:     2021-11-07 22:27:54 +0100 (Sun, 07 Nov 2021)
Log Message:
-----------
yamlvars (7nov21)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/lualatex/yamlvars/README.md
    trunk/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf
    trunk/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex
    trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua
    trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty

Modified: trunk/Master/texmf-dist/doc/lualatex/yamlvars/README.md
===================================================================
--- trunk/Master/texmf-dist/doc/lualatex/yamlvars/README.md	2021-11-07 21:27:40 UTC (rev 60989)
+++ trunk/Master/texmf-dist/doc/lualatex/yamlvars/README.md	2021-11-07 21:27:54 UTC (rev 60990)
@@ -1,6 +1,7 @@
 # YAMLvars -- parse a YAML document and create LaTeX definitons
 
 This LuaLaTeX package provides a YAML parser and some functions to declare and define LaTeX definitions using YAML files. 
+It uses the [`tinyyaml`](https://github.com/api7/lua-tinyyaml) parser.
 
 
 

Modified: trunk/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex
===================================================================
--- trunk/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex	2021-11-07 21:27:40 UTC (rev 60989)
+++ trunk/Master/texmf-dist/doc/lualatex/yamlvars/YAMLvars.tex	2021-11-07 21:27:54 UTC (rev 60990)
@@ -1,6 +1,5 @@
 % Kale Ewasiuk (kalekje at gmail.com)
-% 2021-09-24
-%
+% 2021-11-07
 % Copyright (C) 2021 Kale Ewasiuk
 %
 % Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -24,17 +23,43 @@
 % OR OTHER DEALINGS IN THE SOFTWARE.
 
 
-\documentclass{article}
+\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{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 at gmail.com})}
+\usepackage[yyyymmdd]{datetime}\renewcommand{\dateseparator}{--}
+\date{\today}
+
+
+
 \usepackage{YAMLvars}
-\setlength{\parindent}{0ex}
-\setlength{\parskip}{0.75em}
+\title{YAMLvars}
+\subtitle{a YAML variable parser for LuaLaTeX}
 
 \begin{document}
+\maketitle
 
-{\noindent\huge\bfseries YAMLvars \LARGE -- a YAML variable parser for LuaLaTeX}\\
-    2021-09-24, Kale Ewasiuk, \url{kalekje at gmail.com}
-
 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,
@@ -163,6 +188,14 @@
 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.

Modified: trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua
===================================================================
--- trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua	2021-11-07 21:27:40 UTC (rev 60989)
+++ trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua	2021-11-07 21:27:54 UTC (rev 60990)
@@ -1,6 +1,5 @@
 --% Kale Ewasiuk (kalekje at gmail.com)
---% 2021-09-24
---%
+--% 2021-11-07
 --% Copyright (C) 2021 Kale Ewasiuk
 --%
 --% Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -62,6 +61,9 @@
 
 YAMLvars.allowUndeclared = false
 
+YAMLvars.valTemp = ''
+YAMLvars.varTemp = ''
+
 YAMLvars.tabmidrule = 'hline'
 
 YAMLvars.yaml = require('tinyyaml')
@@ -114,10 +116,10 @@
     val = pl.List(val):sort(complastname)
     return val
 end
-
+-- todo need distinction beyyween table and penlight list ???
 function YAMLvars.xfm.list2nl(var, val)
     val = pl.array2d.map_slice1(_1..'\\\\', val, 1,-2)
-    return table.concat(val, '')
+    return val:join('')
     --return pl.tablex.reduce(_1.._2, val, '')
 end
 
@@ -194,12 +196,34 @@
         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.prc.toggle(t, v) -- requires penlight extras
+    if hasval(v) then
+        tex.print('\\global\\toggletrue{'..t..'}')
+    else
+        tex.print('\\global\\togglefalse{'..t..'}')
+    end
+end
+
 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
 
 
 
-local function prvcmd(cs, val) -- provide command via lua
+function YAMLvars.prvcmd(cs, val) -- provide command via lua
    if token.is_defined(cs) then
         tex.print('\\PackageError{YAMLvars}{Variable '..cs..' already defined, could not declare}{}')
     else
@@ -207,14 +231,15 @@
     end
 end
 
-local function deccmd(cs, def)
+function YAMLvars.deccmd(cs, def)
     if def == nil then
-        prvcmd(cs, '\\PackageError{YAMLvars}{Variable "'..cs..'" was declared and used but, not set}{}')
+        YAMLvars.prvcmd(cs, '\\PackageError{YAMLvars}{Variable "'..cs..'" was declared and used but, not set}{}')
     else
-        prvcmd(cs, def)
+        YAMLvars.prvcmd(cs, def)
     end
 end
 
+
 local function getYAMLfile(y)
     local f = io.open(y,"r")
     if f ~= nil then
@@ -228,6 +253,7 @@
 end
 
 
+
 function YAMLvars.declareYAMLvarsStr(y)
     local t = YAMLvars.yaml.parse(y)
     for var, specs in pairs(t) do
@@ -241,9 +267,12 @@
             YAMLvars.varspecs[var][s] = p -- set property of var
         end
         if YAMLvars.varspecs[var]['prc'] == 'gdef' then
-            deccmd(var, YAMLvars.varspecs[var]['dft'])
+            YAMLvars.deccmd(var, YAMLvars.varspecs[var]['dft'])
         elseif YAMLvars.varspecs[var]['prc'] == 'yvdef' then
-            deccmd('yv--'..var, YAMLvars.varspecs[var]['dft'])
+            YAMLvars.deccmd('yv--'..var, YAMLvars.varspecs[var]['dft'])
+        elseif YAMLvars.varspecs[var]['prc'] == 'toggle' then
+            tex.print('\\global\\newtoggle{'..var..'}')
+            YAMLvars.prc.toggle(var, YAMLvars.varspecs[var]['dft'])
         end
     end
 end
@@ -252,36 +281,70 @@
     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 function eval_expr(func, var, val)
+    local s, c = func:gsub('^[=/]', {['/'] = '', ['='] = 'YAMLvars.valTemp = '}, 1) -- / is run code, = sets val = code
+    if c == 0 then
+        return nil
+    else
+        --help_wrt(s, var)
+        --help_wrt(val, var)
+        YAMLvars.valTemp = val
+        YAMLvars.varTemp = var
+        --help_wrt(s, var)
+        s = sub_lua_var(' '..s, 'x', 'YAMLvars.valTemp')
+        s = sub_lua_var(s, 'v', 'YAMLvars.varTemp')
+        --help_wrt(s, var)
+        loadstring(s)()
+        --help_wrt(val, var)
+        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 == nil then
+                tex.print('\\PackageWarning{YAMLvars}{xfm function "'..func..'" not defined, skipping}{}')
+            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']..'" 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
-            if YAMLvars.allowUndeclared then
-                if YAMLvars.prcDefault == 'yvdef' then
-                    YAMLvars.prc.yvdef(var, val)
-                else
-                    prvcmd(var, val)
-                end
-             else
-                tex.print('\\PackageError{YAMLvars}{Variable "'..var..'" set but not declared}{}')
-            end
+            check_def(var, val)
         else
-            --if string.sub(val, 1, 2) == '~' then -- todo
-            --    val = t[string.sub(val, 2, #val)]  -- use other val
-            --end
-            for _, func in pairs(YAMLvars.varspecs[var]['xfm']) do --apply cleaning functions
-                local f = YAMLvars.xfm[func]
-                if f == nil then
-                    tex.print('\\PackageWarning{YAMLvars}{xfm function "'..func..'" not defined, skipping}{}')
-                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']..'" not defined}{}')
-            end
-            f(var, val) -- prc the value of the variable
+            transform_and_prc(var, val)
         end
     end
 end
@@ -346,7 +409,7 @@
           end
       end
     end
-    help_wrt(t)
+    --help_wrt(t)
     return t
  end
 

Modified: trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty
===================================================================
--- trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty	2021-11-07 21:27:40 UTC (rev 60989)
+++ trunk/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty	2021-11-07 21:27:54 UTC (rev 60990)
@@ -1,6 +1,5 @@
 % Kale Ewasiuk (kalekje at gmail.com)
-% 2021-09-24
-%
+% 2021-11-07
 % Copyright (C) 2021 Kale Ewasiuk
 %
 % Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -56,4 +55,8 @@
 
 \newenvironment{parseYAMLvars}{\luadirect{startrecording()}}{\luadirect{stoprecording()}
 \luadirect{YAMLvars.parseYAMLvarsStr(recordedbuf)}
-}
\ No newline at end of file
+}
+
+\newcommand{\resetYAMLvarsspec}{\luadirect{YAMLvars.varspec = {}}}
+
+\newcommand{\AllowUndeclaredYV}{\luadirect{YAMLvars.allowUndeclared = true}}



More information about the tex-live-commits mailing list.