summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/penlightplus
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2023-09-06 03:01:07 +0000
committerNorbert Preining <norbert@preining.info>2023-09-06 03:01:07 +0000
commit30e902c79d7838a9ec84133dec2415eb48968762 (patch)
tree98f80627e917fc91308cdd784a43e58ce7e187f4 /macros/luatex/generic/penlightplus
parent42959ab19eab5531850a6067d53d13f777ec160b (diff)
CTAN sync 202309060301
Diffstat (limited to 'macros/luatex/generic/penlightplus')
-rw-r--r--macros/luatex/generic/penlightplus/penlightplus.lua82
-rw-r--r--macros/luatex/generic/penlightplus/penlightplus.pdfbin62224 -> 63923 bytes
-rw-r--r--macros/luatex/generic/penlightplus/penlightplus.sty57
-rw-r--r--macros/luatex/generic/penlightplus/penlightplus.tex63
4 files changed, 107 insertions, 95 deletions
diff --git a/macros/luatex/generic/penlightplus/penlightplus.lua b/macros/luatex/generic/penlightplus/penlightplus.lua
index 96eb565fb6..1f74d45777 100644
--- a/macros/luatex/generic/penlightplus/penlightplus.lua
+++ b/macros/luatex/generic/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/macros/luatex/generic/penlightplus/penlightplus.pdf b/macros/luatex/generic/penlightplus/penlightplus.pdf
index 32135ebb6e..0f35dbf75a 100644
--- a/macros/luatex/generic/penlightplus/penlightplus.pdf
+++ b/macros/luatex/generic/penlightplus/penlightplus.pdf
Binary files differ
diff --git a/macros/luatex/generic/penlightplus/penlightplus.sty b/macros/luatex/generic/penlightplus/penlightplus.sty
index 3f95ae4d9e..01b0aafc16 100644
--- a/macros/luatex/generic/penlightplus/penlightplus.sty
+++ b/macros/luatex/generic/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*}
diff --git a/macros/luatex/generic/penlightplus/penlightplus.tex b/macros/luatex/generic/penlightplus/penlightplus.tex
index fec72b8dfd..fbc4ec32d5 100644
--- a/macros/luatex/generic/penlightplus/penlightplus.tex
+++ b/macros/luatex/generic/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}-\\