diff options
author | Karl Berry <karl@freefriends.org> | 2023-09-05 20:06:27 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2023-09-05 20:06:27 +0000 |
commit | 46cd06e16d8d655721ece4f672750b0a62ca4703 (patch) | |
tree | 9b18d53584a30ad8263430034043c5e768d56c08 /Master | |
parent | 9d86556bb016e4ff1cf1e9f1479ccfbf895de518 (diff) |
penlightplus (5sep23)
git-svn-id: svn://tug.org/texlive/trunk@68185 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master')
-rw-r--r-- | Master/texmf-dist/doc/luatex/penlightplus/penlightplus.pdf | bin | 62224 -> 63923 bytes | |||
-rw-r--r-- | Master/texmf-dist/doc/luatex/penlightplus/penlightplus.tex | 63 | ||||
-rw-r--r-- | Master/texmf-dist/tex/luatex/penlightplus/penlightplus.lua | 82 | ||||
-rw-r--r-- | Master/texmf-dist/tex/luatex/penlightplus/penlightplus.sty | 57 |
4 files changed, 107 insertions, 95 deletions
diff --git a/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.pdf b/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.pdf Binary files differindex 32135ebb6e6..0f35dbf75ad 100644 --- a/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.pdf +++ b/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.pdf diff --git a/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.tex b/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.tex index fec72b8dfd6..fbc4ec32d59 100644 --- a/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.tex +++ b/Master/texmf-dist/doc/luatex/penlightplus/penlightplus.tex @@ -1,5 +1,5 @@ % Kale Ewasiuk (kalekje@gmail.com) -% 2023-08-27 +% 2023-09-04 % Copyright (C) 2021-2023 Kale Ewasiuk % % Permission is hereby granted, free of charge, to any person obtaining a copy @@ -58,6 +58,8 @@ \title{penlightplus} \subtitle{Additions to the Penlight Lua Libraries} + + \begin{document} % @@ -72,15 +74,17 @@ If you want to use penlightplus.lua with the \texttt{texlua} interpreter (no document is made, but useful for testing your Lua code), you can access it by setting \cmd{__SKIP_TEX__ = true} before loading. For example: \begin{verbatim} -package.path = package.path .. ';'..'path/to/texmf/tex/latex/penlightplus/?.lua' -package.path = package.path .. ';'..'path/to/texmf/tex/latex/penlight/?.lua' +package.path = package.path .. ';'..'path/to/texmf/tex/lualatex/penlightplus/?.lua' +package.path = package.path .. ';'..'path/to/texmf/tex/lualatex/penlight/?.lua' penlight = require'penlight' __SKIP_TEX__ = true --only required if you want to use --penlightplus without a LaTeX run __PL_GLOBALS__ = true -- optional, include global definitions + require'penlightplus' -\end{verbatim} + + \end{verbatim} @@ -182,7 +186,7 @@ no, once, twice, or full expansion. For example, we can control the expansion of \cmd{\ifluax{<Lua expr>}{<do if true>}[<do if false>]} and\\ \cmd{\ifluax{<Lua expr>}{<do if true>}[<do if false>]} for truthy (uses \cmd{penlight.hasval}) -\begin{LTXexample} +\begin{LTXexample}[width=0.3\linewidth] \ifluax{3^3 == 27}{3*3*3 is 27}[WRONG]\\ \ifluax{abc123 == nil}{Var is nil}[WRONG]\\ \ifluax{not true}{tRuE}[fAlSe]\\ @@ -197,38 +201,55 @@ no, once, twice, or full expansion. For example, we can control the expansion of \cmd{\newtbl{t}} declares a new table with name \cmd{t}\\ \cmd{\chgtbl{t}} changes the 'recent' table\\ -\cmd{\tblfrkv{t}{key-val string}[luakeys opts]} new table from key-vals, used \cmd{luakeys} \\ +\cmd{\tblfrkv{t}{key-val string}[luakeys opts]} new table from key-vals using \cmd{luakeys} \\ + \cmd{\tblfrcsv} a shorthand \cmd{\tblfrkv{t}{csv}[naked_as_value=true,opts]}, a good way to convert a comma-separated list to an array\\ \cmd{\settbl{i}{v}} sets a value of the table/index \cmd{i} to \cmd{v}\\ \cmd{\gettbl{i}} gets the value and \cmd{tex.sprint()}'s it\\ \cmd{\deftbl{i}{d}} pushes the value to a \cmd{cs} named \cmd{d}\\ \cmd{\gdeftbl{i}{d}} pushes the value to a global\\ -\cmd{\iftbl{i}{t}[f]} runs code \cmd{t} if the item is true else \cmd{f}\\ -\cmd{\iftblv{i}{t}[f]} runs code \cmd{t} if the item is truthy else \cmd{f}\\ +\cmd{\iftbl{i}{tr}[fa]} runs code \cmd{ta} if the item is true else \cmd{fr}\\ +\cmd{\iftblv{i}{tr}[fa]} runs code \cmd{ta} if the item is truthy else \cmd{fr}\\ +\cmd{\kvtblundefcheck} will throw an error if you use define a table from key-values +and use a key that was not specified in the luakeys parse options via \cmd{opts.defaults} or \cmd{opts.defs}. There are 3 ways to use the index (placeholder \cmd{{i}} above). \cmd{t.key} where \cmd{t} is the table name and \cmd{key} is a string key, -\cmd{t/int} where \cmd{int} is a numeric indexer (ie uses \cmd{t[int]}), -or simply \cmd{ind} without the table name, where the assumed table is the last one that was created or changed to, (pure-numbers are not treated as strings in this case). - -\begin{LTXexample} -\tblfrkv{my}{a,b,c,first=john,last=smith}[defaults={x=0,1=one,n=false,y=yes}, naked_as_value=true] - -\settbl{my/4}{d}\gettbl{my/4}\\ -\settbl{5}{e}\gettbl{5}\\ +\cmd{t/int} where \cmd{int} is an integer index (ie. uses \cmd{t[int]}, note that negative indexes are allowered where -1 is the last element), +or simply use \cmd{ind} without the table name, where the assumed table is the last one that was created or changed to, (passing a number will used as an integer index). +\enlargethispage{2em} +\begin{LTXexample}[width=0.3\linewidth] +\tblfrkv{my}{a,b,c,first=john,last=smith}% + [defaults={x=0,1=one,n=false,y=yes}] +\gettbl{my.a}\\ \gettbl{my.x}\\ -\iftbl{n}{t}[f]\\ -\iftblv{n}{t}[f]\\ -\iftbl{my.y}{t}[f]\\ -\iftblv{y}{t}[f]\\ +\iftbl{n}{tr}[fa]\\ +\iftblv{n}{TR}[FA]\\ +\iftbl{my.y}{Tr}[Fa]\\ +\iftblv{y}{tR}[fA]\\ +%% \kvtblundefcheck % would throw error +\deftbl{my.first}{mydef} \mydef\\ +{\deftbl{last}{mydef} \mydef} \mydef\\ +{\gdeftbl{last}{mydef}} \mydef\\ + +\tblfrcsv{me}{a,b,"c,see",d,e} +\gettbl{me/1},\gettbl{2}\\ +\gettbl{3}\\ +\settbl{me/4}{D}\gettbl{me/4}\\ +\settbl{5}{E}\gettbl{5}\\ +\gettbl{-2},\gettbl{me/-1}\\ +%% \gettbl{k} % would throw error \end{LTXexample} + + + \subsection*{Splitting strings} Splitting text (or a cmd) into oxford comma format via: \cmd{\splitToComma[expansion level]{text}{text to split on}}: -\begin{LTXexample} +\begin{LTXexample}[width=0.3\linewidth] -\splitToComma{ j doe }{\and}-\\ -\splitToComma{ j doe \and s else }{\and}-\\ -\splitToComma{ j doe \and s else \and a per }{\and}-\\ diff --git a/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.lua b/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.lua index 96eb565fb65..1f74d45777a 100644 --- a/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.lua +++ b/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.lua @@ -1,5 +1,5 @@ --% Kale Ewasiuk (kalekje@gmail.com) ---% 2023-08-27 +--% 2023-09-04 --% Copyright (C) 2021-2023 Kale Ewasiuk --% --% Permission is hereby granted, free of charge, to any person obtaining a copy @@ -776,22 +776,36 @@ end penlight.tbls = {} -function penlight.get_tbl_item(s, p) -- get item with string, p means print value - p = p or false - local itm = 'tbl: INVALID INDEX WITH: "'..s..'"' - if s:find('%.') then - local tt = s:split('.') - itm = penlight.tbls[tt[1]][tt[2]] - elseif s:find('/') then - local tt = s:split('/') - itm = penlight.tbls[tt[1]][tonumber(tonumber(tt[2]))] - else - if s:isdigit() then - itm = penlight.tbls._recent[tonumber(s)] +penlight.rec_tbl = '' +penlight.rec_tbl_opts = {} + +function penlight.get_tbl_index(s) + local tbl = '' + local key = '' + if s:find('%.') then + local tt = s:split('.') + tbl = tt[1] + key = tt[2] + elseif s:find('/') then + local tt = s:split('/') + tbl = tt[1] + key = tonumber(tonumber(tt[2])) + if key < 0 then key = #penlight.tbls[tbl]+1+key end else - itm = penlight.tbls._recent[s] + tbl = penlight.rec_tbl + key = tonumber(s) or s + if type(key) == 'number' and key < 0 then key = #penlight.tbls[tbl]+1+key end end - end + if penlight.tbls[tbl] == nil or penlight.tbls[tbl][key] == nil then + penlight.tex.pkgerror('penlightplus', 'Invalid index of tbl using: "'..s..'"') + end + return tbl, key +end + +function penlight.get_tbl_item(s, p) -- get item with string, p means print value + p = p or false + local tbl, key = penlight.get_tbl_index(s) + local itm = penlight.tbls[tbl][key] if p then tex.sprint(tostring(itm)) end @@ -800,20 +814,30 @@ end function penlight.set_tbl_item(s, v) - if s:find('%.') then - local tt = s:split('.') - penlight.tbls[tt[1]][tt[2]] = v - elseif s:find('/') then - local tt = s:split('/') - penlight.tbls[tt[1]][tonumber(tonumber(tt[2]))] = v - else - if s:isdigit() then - penlight.tbls._recent[tonumber(s)] = v - else - penlight.tbls._recent[s] = v - end - end - end + tbl, key = penlight.get_tbl_index(s) + penlight.tbls[tbl][key] = v +end + +function penlight.check_recent_tbl_undefault() + local undefaults = {} + if penlight.rec_tbl_opts ~= nil then + local defaults = penlight.tablex.union( + penlight.rec_tbl_opts.defs or {}, + penlight.rec_tbl_opts.defaults or {} + ) + for k, v in pairs(penlight.tbls[penlight.rec_tbl]) do + if defaults[k] == nil then + undefaults[#undefaults+1] = k + end + end + if penlight.hasval(undefaults) then + penlight.tex.pkgerror('penlightplus', + 'Invalid keys passed to tbl keyval: ' .. (', '):join(undefaults) .. + ' ; choices are: ' .. (', '):join(penlight.tablex.keys(defaults)) + ) + end + end +end if penlight.hasval(__PL_GLOBALS__) then diff --git a/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.sty b/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.sty index 3f95ae4d9ef..01b0aafc167 100644 --- a/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.sty +++ b/Master/texmf-dist/tex/luatex/penlightplus/penlightplus.sty @@ -1,5 +1,5 @@ % Kale Ewasiuk (kalekje@gmail.com) -% 2023-08-27 +% 2023-09-04 % Copyright (C) 2021-2023 Kale Ewasiuk % % Permission is hereby granted, free of charge, to any person obtaining a copy @@ -22,7 +22,7 @@ % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE % OR OTHER DEALINGS IN THE SOFTWARE. -\ProvidesPackage{penlightplus}[2023-08-27] +\ProvidesPackage{penlightplus}[2023-09-04] \RequirePackage{luacode} \RequirePackage{luakeys} @@ -102,25 +102,27 @@ \NewDocumentCommand{\ifluaxv}{m m O{}}{\ifluax{penlight.hasval(#1)}{#2}[#3]} -% testing ifluax -% {-->\ifluax{1 == 2}{\LARGE TRUE}[\tiny FALSE]<--test\\ -% -->\ifluax{abc == nil}{\LARGE TRUE}[\tiny FALSE]<--test} - %%% \NewDocumentCommand{\newtbl}{m}{\luadirect{ penlight.tbls[\luastring{#1}] = {} - penlight.tbls._recent = penlight.tbls[\luastring{#1}] + penlight.rec_tbl = \luastring{#1} }} \NewDocumentCommand{\tblfrkv}{m m O{}}{ \luadirect{ - penlight.tbls[\luastring{#1}] = penlight.luakeys.parse(\luastring{#2}, penlight.luakeys.parse(\luastring{#3})) - penlight.tbls._recent = penlight.tbls[\luastring{#1}] + penlight.rec_tbl_opts = penlight.luakeys.parse(\luastring{#3}) + penlight.tbls[\luastring{#1}] = penlight.luakeys.parse(\luastring{#2}, penlight.rec_tbl_opts) + penlight.rec_tbl = \luastring{#1} }} +\NewDocumentCommand{\tblfrcsv}{m m O{}}{\tblfrkv{#1}{#2}[naked_as_value=true,#3]} + + +\NewDocumentCommand{\kvtblundefcheck}{}{\luadirect{penlight.check_recent_tbl_undefault()}} % todo + \NewDocumentCommand{\chgtbl}{ m }{ \luadirect{ - penlight.tbls._recent = penlight.tbls[\luastring{#1}] + penlight.rec_tbl = \luastring{#1} }} \NewDocumentCommand{\gettbl}{m}{\luadirect{ @@ -143,38 +145,3 @@ \NewDocumentCommand{\iftblv}{m m O{}}{\ifluaxv{penlight.get_tbl_item(\luastring{#1})}{#2}[#3]} - - -%\NewDocumentCommand{\tblupd}{ m m}{\luadirect{ -% penlight.tablex.update(penlight.tbls[\luastring{#1}], penlight.tbls[\luastring{#2}]) -%}} -% -%\NewDocumentCommand{\tblupdkv}{ m m }{\luadirect{ -% penlight.tablex.update(penlight.tbls[\luastring{#1}], penlight.luakeys.parse(\luastring{#2})) -%}} - -% -%% todo need a get table function. If blank is passed, the last table referenced is used. -%\NewDocumentCommand{\tblfrkvII}{ m m m }{\luadirect{ -% _G[\luastring{#1}] = penlight.tablex.update(penlight.luakeys.parse(\luastring{#2}), luakeys.parse(\luastring{#3})) -%}} - -%\NewDocumentCommand{\tbldo}{m m O{} }{\luadirect{ -% % tex.sprint(tostring(_G[\luastring{#1}][#2])) -% % todo allow one to use pl tablex function here, -% % {tbl}{update}[arg1][arg2] -% penlight.tablex[\luastring[#1]](_G[\luastring{}], _G[\luastring{}]) -%}} - - - - -%%%%%%%%5 - -%\begin{luacode*} -% function set_dim(name, len) -% tex.sprint('\\global\\let\\'..name..'\\relax') -- reset length -% tex.sprint('\\global\\newlength{\\'..name..'}') -- make new len -% tex.setglue(name, tex.sp(len)) -% end -%\end{luacode*} |