summaryrefslogtreecommitdiff
path: root/macros/luatex/generic
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2022-10-23 03:01:07 +0000
committerNorbert Preining <norbert@preining.info>2022-10-23 03:01:07 +0000
commit588d4f81788b3a24bd9bd480ec40bb55be4d1da1 (patch)
treea035dd5b60e2cf4afabea9fc6f8a34e5505ba6ac /macros/luatex/generic
parent3183c46c6fe93c96cf342a65ba2ac2b74a14dcbd (diff)
CTAN sync 202210230301
Diffstat (limited to 'macros/luatex/generic')
-rw-r--r--macros/luatex/generic/lutabulartools/lutabulartools.lua29
-rw-r--r--macros/luatex/generic/lutabulartools/lutabulartools.pdfbin105162 -> 110213 bytes
-rw-r--r--macros/luatex/generic/lutabulartools/lutabulartools.sty23
-rw-r--r--macros/luatex/generic/lutabulartools/lutabulartools.tex28
-rw-r--r--macros/luatex/generic/penlight/penlight.lua202
-rw-r--r--macros/luatex/generic/penlight/penlight.pdfbin42045 -> 59406 bytes
-rw-r--r--macros/luatex/generic/penlight/penlight.sty18
-rw-r--r--macros/luatex/generic/penlight/penlight.tex100
-rw-r--r--macros/luatex/generic/penlight/penlightextras.lua55
9 files changed, 332 insertions, 123 deletions
diff --git a/macros/luatex/generic/lutabulartools/lutabulartools.lua b/macros/luatex/generic/lutabulartools/lutabulartools.lua
index 361530bc97..24c8d5598e 100644
--- a/macros/luatex/generic/lutabulartools/lutabulartools.lua
+++ b/macros/luatex/generic/lutabulartools/lutabulartools.lua
@@ -1,5 +1,5 @@
--% Kale Ewasiuk (kalekje@gmail.com)
---% 2022-10-15
+--% 2022-10-22
--% Copyright (C) 2021-2022 Kale Ewasiuk
--%
--% Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,7 +25,8 @@
local pl = _G['penlight'] or _G['pl'] -- penlight for this namespace is pl
if (__PL_EXTRAS__ == nil) or (__PENLIGHT__ == nil) then
- tex.sprint('\\PackageError{yamlvars}{penlight package with extras (or extrasnoglobals) option must be loaded before this package}{}')
+ tex.sprint('\\PackageError{yamlvars}{penlight package with extras (or extrasglobals) option must be loaded before this package}{}')
+ tex.print('\\stop')
end
local ltt = {}
@@ -111,7 +112,7 @@ function ltt.MagicCell(s0,spec,mcspec,pre,content)
ltt.debugtalk(pl.List{v, h, r, c, mcspec}:join'; ','v, h, r, c, mcspec')
--help_wrt(_CurTabColAbv,'current column')
- if s0 == _xTrue or (pl.List(ltt.SI_cols):contains(ltt.col) -- special columns for SI
+ if s0 == pl.tex._xTrue or (pl.List(ltt.SI_cols):contains(ltt.col) -- special columns for SI
and c == '') then -- multicolumn cannot have {} around it
STR = STR .. '{' -- multirow and makcell must have {} around it S column is used
pl.tex.add_bkt_cnt()
@@ -266,8 +267,6 @@ function ltt.make1cmidrule(s, r, c, cmd) -- s=square r=round c=curly
end
c = ltt.get_midrule_col(t[1])..'-'..ltt.get_midrule_col(t[2])
cmd = cmd..'{'..c..'}'
- --help_wrt(cmd)
-
ltt.debugtalk(cmd,'make1cmidrule')
tex.print(cmd)
end
@@ -303,28 +302,34 @@ function ltt.mrX.set_midruleX(new_sett, def)
end
new_sett = luakeys.parse(new_sett)
ltt.mrX.settings = T.union(curr_sett, new_sett)
- ltt.debugtalk{ltt.mrX.settings, 'new settings'}
+ ltt.debugtalk(ltt.mrX.settings, 'new midruleX settings')
ltt.mrX.cntr = curr_sett.cntr
end
function ltt.mrX.midruleX(n)
n = n or '' -- todo placeholder for noalign ?
local s = ltt.mrX.settings
+ local rule = s.rule
if pl.hasval(s.reset) and ltt.mrX.add_label_and_check_page_change() then ltt.mrX.cntr = s.resetnum end
ltt.mrX.cntr = ltt.mrX.cntr + 1
if ltt.mrX.cntr == s.step then
- ltt.debugtalk{s.rule, 'apply midruleX'}
- tex.sprint('\\'..s.rule)
+ if not rule:startswith('\\') then rule = '\\'..rule end -- todo consider allowing \gmidrule syntax, possible issue with expansion
+ ltt.debugtalk(rule, 'apply midruleX')
+ tex.sprint(rule)
ltt.mrX.cntr = 0
end
end
function ltt.mrX.add_label_and_check_page_change()
- ltt.mrX.pgcntr = ltt.mrX.pgcntr + 1
+ ltt.mrX.pgcntr = ltt.mrX.pgcntr + 1
tex.print('\\noalign{\\label{ltt@tabular@row@'..ltt.mrX.pgcntr..'}}')
- local _, pgcurr = pl.tex.get_ref_info('ltt@tabular@row@'..ltt.mrX.pgcntr)
- local _, pgprev = pl.tex.get_ref_info('ltt@tabular@row@'..ltt.mrX.pgcntr-1)
- if pgcurr ~= pgprev then
+ local rcurr = pl.tex.get_ref_info('ltt@tabular@row@'..ltt.mrX.pgcntr)
+ local rprev = pl.tex.get_ref_info('ltt@tabular@row@'..ltt.mrX.pgcntr-1)
+ --local rcurrc, _, _ = pl.tex.get_ref_info_all_cref('ltt@tabular@row@'..ltt.mrX.pgcntr)
+ ltt.debugtalk('curr: '..rcurr[2]..' prev: '..rprev[2]..' row: '..ltt.mrX.pgcntr, 'check midruleX page change')
+ ltt.debugtalk(rcurr, 'miduleX current reference info for row: '..ltt.mrX.pgcntr)
+ --ltt.debugtalk(rcurrc, 'miduleX current cleveref cref info')
+ if rcurr[2] ~= rprev[2] then -- pg no is second element
return true
end
return false
diff --git a/macros/luatex/generic/lutabulartools/lutabulartools.pdf b/macros/luatex/generic/lutabulartools/lutabulartools.pdf
index 789cf01125..5d6e7f62d0 100644
--- a/macros/luatex/generic/lutabulartools/lutabulartools.pdf
+++ b/macros/luatex/generic/lutabulartools/lutabulartools.pdf
Binary files differ
diff --git a/macros/luatex/generic/lutabulartools/lutabulartools.sty b/macros/luatex/generic/lutabulartools/lutabulartools.sty
index 5a501a8346..f1dea60c5a 100644
--- a/macros/luatex/generic/lutabulartools/lutabulartools.sty
+++ b/macros/luatex/generic/lutabulartools/lutabulartools.sty
@@ -1,5 +1,5 @@
% Kale Ewasiuk (kalekje@gmail.com)
-% 2022-10-15
+% 2022-10-22
% Copyright (C) 2021-2022 Kale Ewasiuk
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,7 +23,7 @@
% OR OTHER DEALINGS IN THE SOFTWARE.
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{lutabulartools}[2022-10-15]
+\ProvidesPackage{lutabulartools}[2022-10-22]
\RequirePackage{booktabs}
\RequirePackage{multirow}
@@ -53,7 +53,10 @@
\def\midruleX{\aftergroup\aftergroup\aftergroup\midruleX@aux}
\def\midruleX@aux{\luadirect{lutabt.mrX.midruleX('')}} % todo argument for vadjust or noalign??
-\NewDocumentCommand{\setmidruleX}{s m}{\luadirect{lutabt.mrX.set_midruleX(\luastring{#2}, \luastring{#1})}}
+\NewDocumentCommand{\setmidruleX}{s O{N} m}{%
+ \MakeluastringCommands[N]{#2}%
+ \luadirect{lutabt.mrX.set_midruleX(\plluastringA{#3}, \luastring{#1})}%
+}
\NewDocumentCommand{\midruleXreset}{O{0}}{\luadirect{lutabt.mrX.cntr = tonumber(\luastring{#1})}}
\NewCommandCopy\resetmidruleX\midruleXreset
@@ -141,14 +144,14 @@
\let\oldtabular\tabular
-\RenewExpandableDocumentCommand{\tabular}{ O{c} m }{%
+\RenewExpandableDocumentCommand{\tabular}{ O{l} m }{%
\luadirect{lutabt.set_col_spec(\luastringN{#2})}%
\oldtabular[#1]{\ltt@ltrim#2\ltt@rtrim}%
}
\expandafter\let\expandafter\oldtabulars\csname tabular*\endcsname
\expandafter\let\expandafter\endoldtabulars\csname endtabular*\endcsname
-\RenewDocumentEnvironment{tabular*}{ m O{c} m }{%
+\RenewDocumentEnvironment{tabular*}{ m O{l} m }{%
\luadirect{lutabt.set_col_spec(\luastringN{#3})}%
\begin{oldtabulars}{#1}[#2]{\ltt@ltrim#3\ltt@rtrim}%
}{%
@@ -156,6 +159,12 @@
}
%%% NOTE tabularx uses tabular*, NO NEED TO CHANGE TABULARX!
+\let\oldlongtable\longtable
+\RenewExpandableDocumentCommand{\longtable}{ O{l} m }{%
+ \luadirect{lutabt.set_col_spec(\luastringN{#2})}%
+ \oldlongtable[#1]{\ltt@ltrim#2\ltt@rtrim}%
+}
+
%s0,spec,mcspec,pre,content
% Magic Cell
@@ -184,6 +193,10 @@
lutabt.SI_cols[\string##lutabt.SI_cols+1] = '#1'
}}
+\NewDocumentCommand{\forcecolspec}{ m }{\luadirect{
+ lutabt.col_spec = (\luastring{#1}):totable()
+}}
+
\ProcessOptions
diff --git a/macros/luatex/generic/lutabulartools/lutabulartools.tex b/macros/luatex/generic/lutabulartools/lutabulartools.tex
index ecc35df511..97802fce07 100644
--- a/macros/luatex/generic/lutabulartools/lutabulartools.tex
+++ b/macros/luatex/generic/lutabulartools/lutabulartools.tex
@@ -1,5 +1,5 @@
% Kale Ewasiuk (kalekje@gmail.com)
-% 2022-10-15
+% 2022-10-22
% Copyright (C) 2021-2022 Kale Ewasiuk
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -62,9 +62,12 @@
\newcommand{\tMC}{\cmd{\\MC}}
+\usepackage{caption}
+
+\usepackage{ltxtable}
\usepackage{longtable}
-\usepackage[pl,extrasnoglobals]{penlight}
+\usepackage[pl,extras]{penlight}
\RequirePackage{lutabulartools}
@@ -78,6 +81,7 @@
\usepackage{hyperref}
+
\begin{document}
% todo: can improve naming... set col spec to a table instead of a string
@@ -85,10 +89,9 @@
\maketitle
-lutabulartools is a package that contains a few useful macros to help with tables.
-Most functions require LuaLaTeX.
+lutabulartools is a package that contains a few useful Lua\LaTeX-based macros to help with tables.
This package redefines the {\ttfamily tabular} and {\ttfamily tabular*} environments.
-The following packages are loaded:
+The following packages are loaded if not already, so if you have specific settings for these packages, load this package after:
\texttt{
{booktabs},
{multirow},
@@ -186,7 +189,6 @@ To add a column that should be surrounded by brackets for \cmd{siunitx} purposes
S is included by default.
-\newpage
\subsection{Examples}
@@ -350,11 +352,14 @@ Concering \cmd{longtable}s: If \cmd{reset} is set to \cmd{true},
resets the row counter to \cmd{resetnum}, to ensure you have the same X number of rows after the header of each page of a \cmd{longtable}.\\
The starred version of first reverts the settings to default, then loads your inputted settings, which the non-starred
version only overwrites the settings specified. \cmd{\setmidruleX*{}} will restore to default.
+Use optional parameter \cmd{\setmidruleX*[o|n|f]{}}
+to control expansion of the key-val settings (\cmd{[n]} for not expanded is default).
Before you want counting to begin, you should
apply \cmd{\resetmidruleX} to avoid header rows being counted.
Use \cmd{\noalign{\resetmiduleX}} if you need place a rule on the same line the reset takes place, or in a cell before \cmd{\\}.
Note that \cmd{\resetmidruleX} resets the row counter to 0, and is in effect, a short-cut for \cmd{\setmidruleX{}} or \cmd{\setmidruleX{cntr=0}}
+
%
%\begin{tabular}{l@{\luadirect{tex.print('\\midrule')}}}
% Hello \\
@@ -395,7 +400,7 @@ Num & . & . & . \\
Here's an example with long table. Notice the gray rules reliably appear 3 rows after each header.
\begin{verbatim}
-\setmidruleX{reset=true,resetnum=0}
+\setmidruleX{reset=true,resetnum=0,rule="\gcmidrules{1r,2-}"}
\def\tblhead{\toprule No & Name & Place & other\\\midrule}
\def\tblcontinued{\MC[+l]{Continued...}\\}
\def\tblcontinues\MC[+r]{...Continues}\\}
@@ -421,13 +426,13 @@ Here's an example with long table. Notice the gray rules reliably appear 3 rows
14 & & & \\
15 & & & \\\newpage
16 & & & \\
-17 & & & \\\resetmidruleX
-18 & & & \\
+17 & & & \\
+18 & & & \\\resetmidruleX
19 & & & \\
\end{longtable}
\end{verbatim}
-\setmidruleX{reset=true,resetnum=0}
+\setmidruleX{reset=true,resetnum=0,rule="\gcmidrules{1r,2-}"}
\def\tblhead{\toprule No & Name & Place & other\\\midrule}
\def\tblcontinued{\MC[+l]{Continued...}\\}
\def\tblcontinues{\MC[+r]{...Continues}\\}
@@ -459,4 +464,7 @@ Here's an example with long table. Notice the gray rules reliably appear 3 rows
\end{longtable}
+\LTXtable{\linewidth}{ltxexample}
+
+
\end{document}
diff --git a/macros/luatex/generic/penlight/penlight.lua b/macros/luatex/generic/penlight/penlight.lua
index 4b2436be1b..6ffcaf49b7 100644
--- a/macros/luatex/generic/penlight/penlight.lua
+++ b/macros/luatex/generic/penlight/penlight.lua
@@ -133,10 +133,11 @@ function _M.parse(text_or_filename, is_file, use_basic)
end
if is_file then
- local text_or_filename, err = utils.readfile(text_or_filename)
- if not text_or_filename then
+ local text, err = utils.readfile(text_or_filename)
+ if not text then
return nil, err
end
+ text_or_filename = text
end
local doc, err = parser(text_or_filename)
@@ -1219,12 +1220,24 @@ end
--
-- Note: the path is prefixed, so it is searched first when requiring modules.
-- @string base optional base directory (absolute, or relative path).
+-- @bool nofollow always use the invocation's directory, even if the invoked file is a symlink
-- @treturn string the current script's path with a trailing slash
-function app.require_here (base)
- local p = path.dirname(app.script_name())
+function app.require_here (base, nofollow)
+ local p = app.script_name()
if not path.isabs(p) then
p = path.join(path.currentdir(),p)
end
+ if not nofollow then
+ local t = path.link_attrib(p)
+ if t and t.mode == 'link' then
+ t = t.target
+ if not path.isabs(t) then
+ t = path.join(path.dirname(p), t)
+ end
+ p = t
+ end
+ end
+ p = path.normpath(path.dirname(p))
if p:sub(-1,-1) ~= path.sep then
p = p..path.sep
end
@@ -2583,9 +2596,9 @@ end
-- This functions mimics the `warn` function added in Lua 5.4.
-- @function warn
-- @param ... any arguments
-if not warn then -- luacheck: ignore
+if not rawget(_G, "warn") then
local enabled = false
- local function warn(arg1, ...) -- luacheck: ignore
+ local function warn(arg1, ...)
if type(arg1) == "string" and arg1:sub(1, 1) == "@" then
-- control message
if arg1 == "@on" then
@@ -2957,7 +2970,7 @@ end
local config = {}
---- like io.lines(), but allows for lines to be continued with '\'.
+--- like `io.lines`, but allows for lines to be continued with '`\`'.
-- @param file a file-like object (anything where read() returns the next line) or a filename.
-- Defaults to stardard input.
-- @return an iterator over the lines, or nil
@@ -5993,7 +6006,8 @@ function lapp.process_options_string(str,args)
elseif check '$<{name} $' then -- is it <parameter_name>?
-- so <input file...> becomes input_file ...
optparm,rest = res.name:match '([^%.]+)(.*)'
- optparm = optparm:gsub('%A','_')
+ -- follow lua legal variable names
+ optparm = optparm:sub(1,1):gsub('%A','_') .. optparm:sub(2):gsub('%W', '_')
varargs = rest == '...'
append(parmlist,optparm)
end
@@ -6002,6 +6016,7 @@ function lapp.process_options_string(str,args)
line = res.rest
res = {}
local optional
+ local defval_str
-- do we have ([optional] [<type>] [default <val>])?
if match('$({def} $',line,res) or match('$({def}',line,res) then
local typespec = strip(res.def)
@@ -6044,6 +6059,7 @@ function lapp.process_options_string(str,args)
-- optional 'default value' clause. Type is inferred as
-- 'string' or 'number' if there's no explicit type
if default or match('default $r{rest}',typespec,res) then
+ defval_str = res.rest
defval,vtype = process_default(res.rest,vtype)
end
else -- must be a plain flag, no extra parameter required
@@ -6053,6 +6069,7 @@ function lapp.process_options_string(str,args)
local ps = {
type = vtype,
defval = defval,
+ defval_str = defval_str,
required = defval == nil and not optional,
comment = res.rest or optparm,
constraint = constraint,
@@ -6182,6 +6199,9 @@ function lapp.process_options_string(str,args)
if not ps.used then
if ps.required then lapp.error("missing required parameter: "..parm) end
set_result(ps,parm,ps.defval)
+ if builtin_types[ps.type] == "file" then
+ set_result(ps, parm .. "_name", ps.defval_str)
+ end
end
end
return results
@@ -8279,9 +8299,6 @@ end
-- @string P A file path
function path.isdir(P)
assert_string(1,P)
- if P:match("\\$") then
- P = P:sub(1,-2)
- end
return attrib(P,'mode') == 'directory'
end
@@ -9227,10 +9244,11 @@ function pretty.write (tbl,space,not_clever)
end
end
table.sort(ordered_keys, function (a, b)
- if type(a) == type(b) and type(a) == 'string' then
- return a < b
+ if type(a) == type(b) then
+ return tostring(a) < tostring(b)
+ else
+ return type(a) < type(b)
end
- return type(a) == 'boolean' or (type(b) ~= 'boolean' and type(a) == 'table')
end)
local function write_entry (key, val)
local tkey = type(key)
@@ -12327,7 +12345,7 @@ local function keys_op(i,v) return i end
--- return all the keys of a table in arbitrary order.
-- @within Extraction
--- @tab t A table
+-- @tab t A list-like table where the values are the keys of the input table
function tablex.keys(t)
assert_arg_iterable(1,t)
return makelist(tablex.pairmap(keys_op,t))
@@ -12337,7 +12355,7 @@ local function values_op(i,v) return v end
--- return all the values of the table in arbitrary order
-- @within Extraction
--- @tab t A table
+-- @tab t A list-like table where the values are the values of the input table
function tablex.values(t)
assert_arg_iterable(1,t)
return makelist(tablex.pairmap(values_op,t))
@@ -13425,6 +13443,8 @@ local concat = table.concat
local _unpack = table.unpack -- always injected by 'compat'
local find = string.find
local sub = string.sub
+local next = next
+local floor = math.floor
local is_windows = compat.is_windows
local err_mode = 'default'
@@ -13433,7 +13453,7 @@ local operators
local _function_factories = {}
-local utils = { _VERSION = "1.12.0" }
+local utils = { _VERSION = "1.13.1" }
for k, v in pairs(compat) do utils[k] = v end
--- Some standard patterns
@@ -13635,6 +13655,47 @@ end
+--- an iterator over all non-integer keys (inverse of `ipairs`).
+-- It will skip any key that is an integer number, so negative indices or an
+-- array with holes will not return those either (so it returns slightly less than
+-- 'the inverse of `ipairs`').
+--
+-- This uses `pairs` under the hood, so any value that is iterable using `pairs`
+-- will work with this function.
+-- @tparam table t the table to iterate over
+-- @treturn key
+-- @treturn value
+-- @usage
+-- local t = {
+-- "hello",
+-- "world",
+-- hello = "hallo",
+-- world = "Welt",
+-- }
+--
+-- for k, v in utils.kpairs(t) do
+-- print("German: ", v)
+-- end
+--
+-- -- output;
+-- -- German: hallo
+-- -- German: Welt
+function utils.kpairs(t)
+ local index
+ return function()
+ local value
+ while true do
+ index, value = next(t, index)
+ if type(index) ~= "number" or floor(index) ~= index then
+ break
+ end
+ end
+ return index, value
+ end
+end
+
+
+
--- Error handling
-- @section Error-handling
@@ -13661,17 +13722,20 @@ function utils.assert_arg (n,val,tp,verify,msg,lev)
return val
end
---- creates an Enum table.
+--- creates an Enum or constants lookup table for improved error handling.
-- This helps prevent magic strings in code by throwing errors for accessing
--- non-existing values.
---
--- Calling on the object does the same, but returns a soft error; `nil + err`.
---
--- The values are equal to the keys. The enum object is
--- read-only.
--- @param ... strings that make up the enumeration.
--- @return Enum object
--- @usage -- accessing at runtime
+-- non-existing values, and/or converting strings/identifiers to other values.
+--
+-- Calling on the object does the same, but returns a soft error; `nil + err`, if
+-- the call is succesful (the key exists), it will return the value.
+--
+-- When calling with varargs or an array the values will be equal to the keys.
+-- The enum object is read-only.
+-- @tparam table|vararg ... the input for the Enum. If varargs or an array then the
+-- values in the Enum will be equal to the names (must be strings), if a hash-table
+-- then values remain (any type), and the keys must be strings.
+-- @return Enum object (read-only table/object)
+-- @usage -- Enum access at runtime
-- local obj = {}
-- obj.MOVEMENT = utils.enum("FORWARD", "REVERSE", "LEFT", "RIGHT")
--
@@ -13683,21 +13747,81 @@ end
-- -- "'REVERES' is not a valid value (expected one of: 'FORWARD', 'REVERSE', 'LEFT', 'RIGHT')"
--
-- end
--- @usage -- validating user-input
--- local parameter = "...some user provided option..."
--- local ok, err = obj.MOVEMENT(parameter) -- calling on the object
--- if not ok then
--- print("bad 'parameter', " .. err)
+-- @usage -- standardized error codes
+-- local obj = {
+-- ERR = utils.enum {
+-- NOT_FOUND = "the item was not found",
+-- OUT_OF_BOUNDS = "the index is outside the allowed range"
+-- },
+--
+-- some_method = function(self)
+-- return self.ERR.OUT_OF_BOUNDS
+-- end,
+-- }
+--
+-- local result, err = obj:some_method()
+-- if not result then
+-- if err == obj.ERR.NOT_FOUND then
+-- -- check on error code, not magic strings
+--
+-- else
+-- -- return the error description, contained in the constant
+-- return nil, "error: "..err -- "error: the index is outside the allowed range"
+-- end
+-- end
+-- @usage -- validating/converting user-input
+-- local color = "purple"
+-- local ansi_colors = utils.enum {
+-- black = 30,
+-- red = 31,
+-- green = 32,
+-- }
+-- local color_code, err = ansi_colors(color) -- calling on the object, returns the value from the enum
+-- if not color_code then
+-- print("bad 'color', " .. err)
+-- -- "bad 'color', 'purple' is not a valid value (expected one of: 'black', 'red', 'green')"
-- os.exit(1)
-- end
function utils.enum(...)
- local lst = utils.pack(...)
- utils.assert_arg(1, lst[1], "string") -- at least 1 string
-
+ local first = select(1, ...)
local enum = {}
- for i, value in ipairs(lst) do
- utils.assert_arg(i, value, "string")
- enum[value] = value
+ local lst
+
+ if type(first) ~= "table" then
+ -- vararg with strings
+ lst = utils.pack(...)
+ for i, value in utils.npairs(lst) do
+ utils.assert_arg(i, value, "string")
+ enum[value] = value
+ end
+
+ else
+ -- table/array with values
+ utils.assert_arg(1, first, "table")
+ lst = {}
+ -- first add array part
+ for i, value in ipairs(first) do
+ if type(value) ~= "string" then
+ error(("expected 'string' but got '%s' at index %d"):format(type(value), i), 2)
+ end
+ lst[i] = value
+ enum[value] = value
+ end
+ -- add key-ed part
+ for key, value in utils.kpairs(first) do
+ if type(key) ~= "string" then
+ error(("expected key to be 'string' but got '%s'"):format(type(key)), 2)
+ end
+ if enum[key] then
+ error(("duplicate entry in array and hash part: '%s'"):format(key), 2)
+ end
+ enum[key] = value
+ lst[#lst+1] = key
+ end
+ end
+
+ if not lst[1] then
+ error("expected at least 1 entry", 2)
end
local valid = "(expected one of: '" .. concat(lst, "', '") .. "')"
@@ -13711,7 +13835,7 @@ function utils.enum(...)
__call = function(self, key)
if type(key) == "string" then
local v = rawget(self, key)
- if v then
+ if v ~= nil then
return v
end
end
diff --git a/macros/luatex/generic/penlight/penlight.pdf b/macros/luatex/generic/penlight/penlight.pdf
index b0b89352b2..586bfa8dbf 100644
--- a/macros/luatex/generic/penlight/penlight.pdf
+++ b/macros/luatex/generic/penlight/penlight.pdf
Binary files differ
diff --git a/macros/luatex/generic/penlight/penlight.sty b/macros/luatex/generic/penlight/penlight.sty
index d796f1e1c8..5af886786e 100644
--- a/macros/luatex/generic/penlight/penlight.sty
+++ b/macros/luatex/generic/penlight/penlight.sty
@@ -1,5 +1,5 @@
% Kale Ewasiuk (kalekje@gmail.com)
-% 2022-10-15
+% 2022-10-22
% Copyright (C) 2021-2022 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{penlight}[2022-10-15]
+\ProvidesPackage{penlight}[2022-10-22]
\RequirePackage{luacode}
@@ -43,8 +43,8 @@
\DeclareOption{format}{\luadirect{_G[__PENLIGHT__].stringx.format_operator()}}
\DeclareOption{func}{\luadirect{_G[__PENLIGHT__].utils.import(__PENLIGHT__..'.func')}}
\DeclareOption{extras}{\luadirect{require'penlightextras'}}
-\DeclareOption{extrasnoglobals}{\luadirect{
- __PL_NO_GLOBALS__ = true
+\DeclareOption{extrasglobals}{\luadirect{
+ __PL_GLOBALS__ = true
require'penlightextras'
}}
@@ -58,23 +58,23 @@ end
-\newcommand{\writePDFmetadata}{\luadirect{writePDFmetadata()}}
+\newcommand{\writePDFmetadata}{\luadirect{_G[__PENLIGHT__].tex.writePDFmetadata()}}
\NewDocumentCommand{\writePDFmetadatakv}{ s m }{
\IfBooleanTF{#1}{% if *, overwrite everything
\luadirect{
__PDFmetadata__ = luakeys.parse(\luastring{#2})
- writePDFmetadata()
+ _G[__PENLIGHT__].tex.writePDFmetadata()
}}{
\luadirect{
__PDFmetadata__ = __PDFmetadata__ or {}
- table.update(__PDFmetadata__, luakeys.parse(\luastring{#2}))
- writePDFmetadata()
+ _G[__PENLIGHT__].tablex.update(__PDFmetadata__, luakeys.parse(\luastring{#2}))
+ _G[__PENLIGHT__].tex.writePDFmetadata()
}}
}
-\def\luastringT#1{\luastring{\unexpanded\expandafter\expandafter\expandafter{#1}}}
+\def\luastringT#1{\luastring{\unexpanded\expandafter\expandafter\expandafter{#1}}} % expand luastring twice
\let\luastringF\luastring % fully expanded luastring
% allow control over expansion of arguments to a latex function
diff --git a/macros/luatex/generic/penlight/penlight.tex b/macros/luatex/generic/penlight/penlight.tex
index 869fda37ed..f3ee02f2ec 100644
--- a/macros/luatex/generic/penlight/penlight.tex
+++ b/macros/luatex/generic/penlight/penlight.tex
@@ -1,5 +1,5 @@
% Kale Ewasiuk (kalekje@gmail.com)
-% 2022-10-15
+% 2022-10-22
% Copyright (C) 2021-2022 Kale Ewasiuk
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -29,6 +29,7 @@
\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}
@@ -57,11 +58,14 @@
\subtitle{Lua libraries for use in LuaLaTeX}
\begin{document}
+
+%{\Huge todo add split to text example with ltxexample\\reference the exact version of penlight used in this library. consider no globals by default\\ COVERT existing examples to extras globals, document hyperref}
+
\maketitle
- The official documentation for the Lua library can be found here:\\
+ Documentation for the Lua library this includes can be found here:\\
\mbox{\url{https://lunarmodules.github.io/Penlight}}
- \\
+ \\ This package uses version \cmd{1.13.1}
\subsection*{Required Package Option}
The first option sent to this package MUST be one of: \\
@@ -80,7 +84,7 @@ penlight = require'penlight'
-- below is optional
__SKIP_TEX__ = true --only required if you want to use
--penlightextras without a LaTeX run
--- __PL_NO_GLOBALS__ = true -- optional, skip global definitions
+__PL_GLOBALS__ = true -- optional, include global definitions
__SKIP_LUAKEYS__ = true -- luakeys is laoded in penlightextras be default, you may skip it with this
require'penlightextras'
\end{verbatim}
@@ -101,8 +105,8 @@ require'penlightextras'
\texttt{func} & allows placehold expressions eg. \texttt{\_1+1} to be used \\
& this will be ran in pre-amble: \texttt{penlight.utils.import('pl.func')}\\
& \url{https://lunarmodules.github.io/Penlight/libraries/pl.func}\\
- \texttt{\llap{extras}noglobals} & does the above three (\cmd{func,stringx,format}); adds some additional functions to \cmd{penlight} module; and adds the \cmd{pl.tex} sub-module.\\
- \texttt{extras} & does the above \texttt{extrasnoglobals} but makes many of the functions global variables.
+ \texttt{extras} & does the above three (\cmd{func,stringx,format}); adds some additional functions to \cmd{penlight} module; and adds the \cmd{pl.tex} sub-module.\\
+ \texttt{extrasglobals} & does the above \texttt{extras} but makes many of the functions global variables as well.
\end{tabular}
@@ -114,7 +118,12 @@ If \cmd{extras} is used, the following Lua globals will be defined:\\
\subsubsection*{Misc stuff}
\llcmd{__SKIP_TEX__} If using package with \cmd{texlua}, set this global before loading \cmd{penlight}\\
-\llcmd{__PL_NO_}\cmd{GLOBALS__} If using package with \cmd{texlua} and you don't want to set some globals (described in next sections), set this global before to \cmd{true} loading \cmd{penlight}\\
+The gloals flags below are taken care of in the package options:\\
+\llcmd{__PL_}\cmd{GLOBALS__} If using package with \cmd{texlua} and you don't want to set some globals (described in next sections), set this global before to \cmd{true} loading \cmd{penlight}\\
+\cmd{__SKIP_LUAKEYS__}\\
+\cmd{__PL_NO_HYPERREF__}\\
+\cmd{__PL_EXTRAS__} false, 1 or 2\\
+
\llcmd{hasval(x)} Python-like boolean testing\\
\llcmd{COMP'xyz'()} Python-like comprehensions:\\\url{https://lunarmodules.github.io/Penlight/libraries/pl.comprehension.html}\\
\llcmd{math.mod(n,d)}, \cmd{math.mod2(n)} math modulous\\
@@ -124,11 +133,11 @@ If \cmd{extras} is used, the following Lua globals will be defined:\\
\llcmd{pl.}\cmd{Char(n)} return letter corresponding to 1=A, 2=B, etc.\\
\llcmd{kpairs(t), }\cmd{npairs(t)} iterate over keys only, or include nil value from table ipairs\\
-{\Large todo document all string helper functions}
+
+%{\Large todo document all string helper functions}
\llcmd{pl.utils.}\cmd{filterfiles}\cmd{(dir,filt,rec)} Get files from dir and apply glob-like filters. Set rec to \cmd{true} to include sub directories\\
-\pagebreak
\subsubsection*{\cmd{pl.tex.} module is added}
\llcmd{add_bkt}\cmd{_cnt(n), }\cmd{close_bkt_cnt(n), reset_bkt_cnt} functions to keep track of adding curly brackets as strings. \cmd{add} will return \cmd{n} (default 1) \{'s and increment a counter. \cmd{close} will return \cmd{n} \}'s (default will close all brackets) and decrement.\\
@@ -153,9 +162,45 @@ If \cmd{extras} is used, the following Lua globals will be defined:\\
\llcmd{deccmd}\cmd{(cs, dft, overwrite)} declare a command. If \cmd{dft} (default) is \cmd{nil}, \cmd{cs} is set
to a package warning saying \cmd{'cs' was declared and used in document, but never set}. If \cmd{overwrite}
is true, it will overwrite an existing command (using \cmd{defcmd}), otherwise, it will throw error like \cmd{newcmd}.\\
+\llcmd{get_ref_info(l)}accesses the \cmd{\r@label} and returns a table\\
+
+
+
+
+\subsection*{Macro helpers}
+\cmd{\MakeluastringCommands[def]{spec}} will let \cmd{\plluastring(A|B|C..)} be \cmd{\luastring(N|O|T|F)}
+based on the letters that \cmd{spec} is set to (or \cmd{def} if nothing is provided)
+This is useful if you want to write a command with flexibility on argument expansion.
+The user can specify \cmd{n}, \cmd{o}, \cmd{t}, and \cmd{f} (case insensitve) if they want
+no, once, twice, or full expansion.
+
+% BELOW IS FOR TROUBLESHOOTING ABOVE
+%\def\NOTexp{\ONEexp}
+%\def\ONEexp{\TWOexp}
+%\def\TWOexp{\TREexp}
+%\def\TREexp{Fully expanded}
+%
+%\NewDocumentCommand{\luastringExpTest}{m m}{
+% \MakeluastringCommands{#1}
+% \luadirect{texio.write_nl('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV')}
+% \luadirect{texio.write_nl(\plluastringA{#2}..' | Not')}
+% \luadirect{texio.write_nl(\plluastringB{#2}..' | Once')}
+% \luadirect{texio.write_nl(\plluastringC{#2}..' | Twice')}
+% \luadirect{texio.write_nl(\plluastringD{#2}..' | Full')}
+% \luadirect{texio.write_nl('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV')}
+%}
+%
+%\luastringExpTest{ n o t f }{\NOTexp}
+
+
+
+
+\subsection*{Split stuff}
+Splitting text (or a cmd) into oxford comma format via:
+\cmd{\splitToComma[expansion level]{text}{text to split on}}:
+
+\begin{LTXexample}
-\subsection{Split stuff}
-splitToComma:
-\splitToComma{ j doe }{\and}-\\
-\splitToComma{ j doe \and s else }{\and}-\\
-\splitToComma{ j doe \and s else \and a per }{\and}-\\
@@ -163,44 +208,23 @@ splitToComma:
\def\authors{j doe \and s else \and a per \and f guy}
\splitToComma[o]{\authors}{\and}
+\end{LTXexample}
+The expansion level is up to two characters, \cmd{n|o|t|f}, to control teh expasion of each argument.
+\begin{LTXexample}
spliToItems:
\begin{itemize}
\splitToItems{kale\and john}{\and}
\splitToItems{kale -john -someone else}{-}
\end{itemize}
+\end{LTXexample}
-\subsection{Macro helpers}
-\cmd{\MakeluastringCommands[def]{spec}} will let \cmd{\plluastring(A|B|C..)} be \cmd{\luastring(N|O|T|F)}
-based on the letters that \cmd{spec} is set to (or \cmd{def} if nothing is provided)
-This is useful if you want to write a command with flexibility on argument expansion.
-The user can specify \cmd{n}, \cmd{o}, \cmd{t}, and \cmd{f} (case insensitve) if they want
-no, once, twice, or full expansion.
-
-
-\def\NOTexp{\ONEexp}
-\def\ONEexp{\TWOexp}
-\def\TWOexp{\TREexp}
-\def\TREexp{Fully expanded}
-
-\NewDocumentCommand{\luastringExpTest}{m m}{
- \MakeluastringCommands{#1}
- \luadirect{texio.write_nl('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV')}
- \luadirect{texio.write_nl(\plluastringA{#2}..' | Not')}
- \luadirect{texio.write_nl(\plluastringB{#2}..' | Once')}
- \luadirect{texio.write_nl(\plluastringC{#2}..' | Twice')}
- \luadirect{texio.write_nl(\plluastringD{#2}..' | Full')}
- \luadirect{texio.write_nl('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV')}
-}
-
-\luastringExpTest{ n o t f }{\NOTexp}
\subsubsection*{global extras}
-If \cmd{extras} is used and NOT \cmd{extrasnoglobals}, then some globals are set.\\
-All \cmd{pl.tex} modules are added.\\
+If \cmd{extrasglobals} is used and NOT \cmd{extras}, many globals are set.\\
+All \cmd{pl.tex} modules are made global.\\
\cmd{hasval}, \cmd{COMP}, \cmd{kpairs}, \cmd{npairs} are globals.\\
-\cmd{pl.tablex} functions are added to the \cmd{table} table.\\
\section*{}
Disclaimer: I am not the author of the Lua Penlight library.
diff --git a/macros/luatex/generic/penlight/penlightextras.lua b/macros/luatex/generic/penlight/penlightextras.lua
index 9ce0903f1c..a6a5c1ff0a 100644
--- a/macros/luatex/generic/penlight/penlightextras.lua
+++ b/macros/luatex/generic/penlight/penlightextras.lua
@@ -1,5 +1,5 @@
--% Kale Ewasiuk (kalekje@gmail.com)
---% 2022-10-15
+--% 2022-10-22
--% Copyright (C) 2021-2022 Kale Ewasiuk
--%
--% Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -22,16 +22,17 @@
--% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
--% OR OTHER DEALINGS IN THE SOFTWARE.
-__SKIP_TEX__ = __SKIP_TEX__ or false --if declared true before here, it will use regular print functions
+__PL_SKIP_TEX__ = __PL_SKIP_TEX__ or false --if declared true before here, it will use regular print functions
-- (for troubleshooting with texlua instead of actual use in lua latex)
-__SKIP_LUAKEYS__ = __SKIP_LUAKEYS__ or false
-__PL_NO_GLOBALS__ = __PL_NO_GLOBALS__ or false
+__PL_SKIP_LUAKEYS__ = __PL_SKIP_LUAKEYS__ or false
+__PL_GLOBALS__ = __PL_GLOBALS__ or false
__PL_EXTRAS__ = 1
+__PL_NO_HYPERREF__ = __PL_NO_HYPERREF__ or false
-- requires penlight
local pl = _G['penlight'] or _G['pl'] -- penlight for this namespace is pl
-if not __SKIP_LUAKEYS__ then
+if not __PL_SKIP_LUAKEYS__ then
luakeys = require'luakeys'
end
@@ -74,7 +75,7 @@ pl.tex._xNoValue = '-NoValue-'
--Generic LuaLaTeX utilities for print commands or environments
-if not __SKIP_TEX__ then
+if not __PL_SKIP_TEX__ then
local function check_special_chars(s) -- todo extend to toher special chars?
if type(s) == 'string' then
if string.find(s, '[\n\r\t\0]') then
@@ -142,6 +143,7 @@ function pl.tex.help_wrt(s1, s2) -- helpful printing, makes it easy to debug, s1
end
wrt2('\n^^^^^\n')
end
+pl.help_wrt = pl.tex.help_wrt
function pl.tex.prt_array2d(t)
for _, r in ipairs(t) do
@@ -292,15 +294,37 @@ end
+
+
function pl.tex.get_ref_info(l)
+ local n = 5
+ if __PL_NO_HYPERREF__ then
+ local n = 2
+ end
local r = token.get_macro('r@'..l)
+ local t = {}
if r == nil then
- return 0, 0
+ t = pl.tablex.new(n, 0) -- make all 0s
+ r = '-not found-'
+ else
+ t = {r:match(("(%b{})"):rep(n))}
+ t = pl.tablex.map(string.trimfl, t)
end
- local sec, page = r:match("{([^}]*)}{([^}]*)}")
- return sec, page
+ t[#t+1] = r -- add the og return of label
+ pl.help_wrt(t, 'ref info')
+ return t
end
+-- todo add regex pattern for cref info
+--function pl.tex.get_ref_info_all_cref(l)
+-- local r = token.get_macro('r@'..l..'@cref')
+-- if r == nil then
+-- return r, 0, 0
+-- end
+-- local sec, page = r:match("{([^}]*)}{([^}]*)}")
+-- return r, sec, page
+--end
+
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
@@ -420,6 +444,10 @@ function str_mt.__index.delspace(str)
return str:gsub('%s','')
end
+function str_mt.__index.trimfl(str)
+ return str:sub(2,-2)
+end
+
-- -- -- -- function stuff
@@ -750,7 +778,7 @@ end
-if not __PL_NO_GLOBALS__ then
+if _PL_GLOBALS__ then
__PL_EXTRAS__ = 2
-- iterators
kpairs = pl.utils.kpairs
@@ -767,6 +795,13 @@ if not __PL_NO_GLOBALS__ then
end
end
table.join = table.concat -- alias
+ -- todo should tablex have all table functions
+
+ pl.tablex.concat = table.concat
+ pl.tablex.insert = table.insert
+ pl.tablex.maxn = table.maxn
+ pl.tablex.remove = table.remove
+ pl.tablex.sort = table.sort
hasval = pl.hasval
COMP = pl.COMP