diff options
author | Karl Berry <karl@freefriends.org> | 2021-11-07 21:27:54 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2021-11-07 21:27:54 +0000 |
commit | f74dcee34dd73e7bc5e394c7dcb4d81b185d0a30 (patch) | |
tree | 99c329535e6c476d1cb82bd1cf855435861de1f2 /Master/texmf-dist/tex/lualatex/yamlvars | |
parent | 4515fa2482931c7a82579ed40e52d15cfcedcd16 (diff) |
yamlvars (7nov21)
git-svn-id: svn://tug.org/texlive/trunk@60990 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/yamlvars')
-rw-r--r-- | Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua | 135 | ||||
-rw-r--r-- | Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty | 9 |
2 files changed, 105 insertions, 39 deletions
diff --git a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua b/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua index 11f75edda35..e7baaf26f7e 100644 --- a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua +++ b/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.lua @@ -1,6 +1,5 @@ --% Kale Ewasiuk (kalekje@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.xfmDefault = {} YAMLvars.allowUndeclared = false +YAMLvars.valTemp = '' +YAMLvars.varTemp = '' + YAMLvars.tabmidrule = 'hline' YAMLvars.yaml = require('tinyyaml') @@ -114,10 +116,10 @@ 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) 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 @@ 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.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 @@ local function prvcmd(cs, val) -- provide command via lua 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 @@ local function getYAMLfile(y) end + function YAMLvars.declareYAMLvarsStr(y) local t = YAMLvars.yaml.parse(y) for var, specs in pairs(t) do @@ -241,9 +267,12 @@ function YAMLvars.declareYAMLvarsStr(y) 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 @@ 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 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 @@ function YAMLvars.getYAMLcli() end end end - help_wrt(t) + --help_wrt(t) return t end diff --git a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty b/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty index fdc39fa3b05..f7154389d7f 100644 --- a/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty +++ b/Master/texmf-dist/tex/lualatex/yamlvars/YAMLvars.sty @@ -1,6 +1,5 @@ % Kale Ewasiuk (kalekje@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}} |