summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg42.2a@gmail.com>2013-04-29 15:04:45 -0700
committerPhilipp Gesang <phg42.2a@gmail.com>2013-04-29 15:04:45 -0700
commit202f064c65283e482f149868437e2881a73ebe3d (patch)
treeb31b5a94146a0a300b3fd09cb50d5736a002f086
parent5ff06a36a0e82f3350bc955fac3825d7a1969289 (diff)
parent1e04ecc2c4918448e8ff30a2e6363025267cac79 (diff)
downloadlualibs-202f064c65283e482f149868437e2881a73ebe3d.tar.gz
Merge pull request #2 from phi-gamma/master
merge experimental branch
-rw-r--r--.gitignore2
-rw-r--r--Makefile13
-rw-r--r--lualibs-basic.lua54
-rw-r--r--lualibs-compat.lua29
-rw-r--r--lualibs-extended.lua159
-rw-r--r--lualibs-file.lua18
-rw-r--r--lualibs-lpeg.lua2
-rw-r--r--lualibs-lua.lua245
-rw-r--r--lualibs-package.lua338
-rw-r--r--lualibs-trac-inf.lua193
-rw-r--r--lualibs-util-deb.lua128
-rw-r--r--lualibs-util-env.lua258
-rw-r--r--lualibs-util-mrg.lua67
-rw-r--r--lualibs-util-prs.lua562
-rw-r--r--lualibs-util-sta.lua342
-rw-r--r--lualibs-util-tpl.lua174
-rw-r--r--lualibs.dtx2
-rw-r--r--lualibs.lua118
18 files changed, 2372 insertions, 332 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1fcf771
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+testing/*
+*-merged.lua
diff --git a/Makefile b/Makefile
index 6d7673b..4e79621 100644
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,9 @@ MODULES = $(wildcard lualibs-*.lua)
# Files grouped by generation mode
UNPACKED= lualibs.lua
COMPILED = $(DOC_DTX)
-GENERATED = $(UNPACKED) $(DOC_DTX)
+GENERATED = $(UNPACKED) $(DOC_DTX) $(MERGED)
SOURCE = $(DTX) $(MODULES) README Makefile NEWS
+MERGED = lualibs-basic-merged.lua lualibs-extended-merged.lua
# Files grouped by installation location
RUNFILES = $(UNPACKED) $(MODULES)
@@ -18,7 +19,7 @@ SRCFILES = $(DTX) $(SRC_TEX) Makefile
# The following definitions should be equivalent
# ALL_FILES = $(RUNFILES) $(DOCFILES) $(SRCFILES)
-ALL_FILES = $(GENERATED) $(SOURCE)
+ALL_FILES = $(GENERATED) $(SOURCE) $(MERGED)
# Installation locations
FORMAT = luatex
@@ -35,13 +36,15 @@ DO_TEX = tex --interaction=batchmode $< >/dev/null
DO_PDFLATEX = pdflatex --interaction=batchmode $< >/dev/null
DO_PDFLUALATEX = pdflualatex --interaction=batchmode $< >/dev/null
DO_MAKEINDEX = makeindex -s gind.ist $(subst .dtx,,$<) >/dev/null 2>&1
+DO_PACKAGE = mtxrun --script package --merge $< >/dev/null
-all: $(GENERATED) $(DOC_TEX)
+all: $(GENERATED) $(DOC_TEX) $(MERGED)
doc: $(COMPILED)
unpack: $(UNPACKED)
ctan: $(CTAN_ZIP)
tds: $(TDS_ZIP)
world: all ctan
+
.PHONY: all doc unpack ctan tds world
%.pdf: %.dtx
@@ -50,6 +53,9 @@ world: all ctan
$(DO_PDFLATEX)
$(DO_PDFLATEX)
+%-merged.lua: %.lua
+ $(DO_PACKAGE)
+
$(UNPACKED): lualibs.dtx
$(DO_TEX)
@@ -91,3 +97,4 @@ clean:
mrproper: clean
@$(RM) -- $(GENERATED) $(ZIPS)
+merge: $(MERGED)
diff --git a/lualibs-basic.lua b/lualibs-basic.lua
new file mode 100644
index 0000000..8bdaf70
--- /dev/null
+++ b/lualibs-basic.lua
@@ -0,0 +1,54 @@
+-- This is file `lualibs-basic.lua',
+module('lualibs-basic', package.seeall)
+
+local lualibs_basic_module = {
+ name = "lualibs-basic",
+ version = 1.01,
+ date = "2013/04/10",
+ description = "Basic Lua extensions, meta package.",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "See ConTeXt's mreadme.pdf for the license",
+}
+
+local lualibs = _G.config.lualibs
+local error, warn, info = lualibs.error, lualibs.warn, lualibs.info
+
+local loadmodule = lualibs.loadmodule
+local stringformat = string.format
+
+local loaded = false
+if lualibs.prefer_merged then
+ info"Loading merged package for collection “basic”."
+ loaded = loadmodule('lualibs-basic-merged.lua')
+else
+ info"Ignoring merged packages."
+ info"Falling back to individual libraries from collection “basic”."
+end
+
+if loaded == false then
+ loadmodule("lualibs-lua.lua")
+ loadmodule("lualibs-package.lua")
+ loadmodule("lualibs-lpeg.lua")
+ loadmodule("lualibs-function.lua")
+ loadmodule("lualibs-string.lua")
+ loadmodule("lualibs-table.lua")
+ loadmodule("lualibs-boolean.lua")
+ loadmodule("lualibs-number.lua")
+ loadmodule("lualibs-math.lua")
+ loadmodule("lualibs-io.lua")
+ loadmodule("lualibs-os.lua")
+ loadmodule("lualibs-file.lua")
+ loadmodule("lualibs-md5.lua")
+ loadmodule("lualibs-dir.lua")
+ loadmodule("lualibs-unicode.lua")
+ loadmodule("lualibs-url.lua")
+ loadmodule("lualibs-set.lua")
+end
+
+-- these don’t look much basic to me:
+--l-pdfview.lua
+--l-xml.lua
+
+-- vim:tw=71:sw=2:ts=2:expandtab
+-- End of File `lualibs.lua'.
diff --git a/lualibs-compat.lua b/lualibs-compat.lua
new file mode 100644
index 0000000..cb9d8f0
--- /dev/null
+++ b/lualibs-compat.lua
@@ -0,0 +1,29 @@
+#!/usr/bin/env texlua
+
+local stringgsub = string.gsub
+local stringlower = string.lower
+local next = next
+local Ct, splitat = lpeg.Ct, lpeg.splitat
+
+--[[doc
+Needed by legacy luat-dum.lua.
+--doc]]--
+table.reverse_hash = function (h)
+ local r = { }
+ for k,v in next, h do
+ r[v] = stringlower(stringgsub(k," ",""))
+ end
+ return r
+end
+
+--[[doc
+Needed by legacy font-otn.lua.
+--doc]]--
+lpeg.splitters = { [" "] = Ct(splitat" ") }
+
+--[[doc
+Needed by legacy font-nms.lua.
+--doc]]--
+
+file.split_path = file.splitpath
+file.collapse_path = file.collapsepath
diff --git a/lualibs-extended.lua b/lualibs-extended.lua
new file mode 100644
index 0000000..b0aea31
--- /dev/null
+++ b/lualibs-extended.lua
@@ -0,0 +1,159 @@
+-- This is file `lualibs-extended.lua',
+module('lualibs-extended', package.seeall)
+
+local lualibs_extended_module = {
+ name = "lualibs-extended",
+ version = 1.01,
+ date = "2013/04/10",
+ description = "Basic Lua extensions, meta package.",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "See ConTeXt's mreadme.pdf for the license",
+}
+
+local lualibs = _G.config.lualibs
+local error, warn, info = lualibs.error, lualibs.warn, lualibs.info
+
+local stringformat = string.format
+local loadmodule = lualibs.loadmodule
+local texiowrite = texio.write
+local texiowrite_nl = texio.write_nl
+
+--[[doc--
+Here we define some functions that fake the elaborate logging/tracking
+mechanism Context provides.
+--doc]]--
+local error, logger, mklog
+if luatexbase and luatexbase.provides_module then
+ --- TODO test how those work out when running tex
+ local __error,_,_,__logger =
+ luatexbase.provides_module(lualibs_extended_module)
+ error = __error
+ logger = __logger
+ mklog = function ( ) return logger end
+else
+ local stringformat = string.format
+ mklog = function (t)
+ local prefix = stringformat("[%s] ", t)
+ return function (...)
+ texiowrite_nl(prefix)
+ texiowrite (stringformat(...))
+ end
+ end
+ error = mklog"ERROR"
+ logger = mklog"INFO"
+end
+
+--[[doc--
+We temporarily put our own global table in place and restore whatever
+we overloaded afterwards.
+
+\CONTEXT\ modules each have a custom logging mechanism that can be
+enabled for debugging.
+In order to fake the presence of this facility we need to define at
+least the function \verb|logs.reporter|.
+For now it’s sufficient to make it a reference to \verb|mklog| as
+defined above.
+--doc]]--
+
+local dummy_function = function ( ) end
+local newline = function ( ) texiowrite_nl"" end
+
+local fake_logs = function (name)
+ return {
+ name = name,
+ enable = dummy_function,
+ disable = dummy_function,
+ reporter = mklog,
+ newline = newline
+ }
+end
+
+local fake_trackers = function (name)
+ return {
+ name = name,
+ enable = dummy_function,
+ disable = dummy_function,
+ register = mklog,
+ newline = newline,
+ }
+end
+
+--[[doc--
+Among the libraries loaded is \verb|util-env.lua|, which adds
+\CONTEXT’s own, superior command line argument handler.
+Packages that rely on their own handling of arguments might not be
+aware of this, or the library might have been loaded by another package
+altogether.
+For these cases we provide a copy of the original \verb|arg| list and
+restore it after we are done loading.
+--doc]]--
+
+local backup_store = { }
+
+local fake_context = function ( )
+ if _G.logs then backup_store.logs = _G.logs end
+ if _G.trackers then backup_store.trackers = _G.trackers end
+ _G.logs = fake_logs"logs"
+ _G.trackers = fake_trackers"trackers"
+
+ backup_store.argv = table.fastcopy(_G.arg)
+end
+
+
+--[[doc--
+Restore a backed up logger if appropriate.
+--doc]]--
+local unfake_context = function ( )
+ if backup_store then
+ local bl, bt = backup_store.logs, backup_store.trackers
+ local argv = backup_store.argv
+ if bl then _G.logs = bl end
+ if bt then _G.trackers = bt end
+ if argv then _G.arg = argv end
+ end
+end
+
+fake_context()
+
+local loaded = false
+if lualibs.prefer_merged then
+ info"Loading merged package for collection “extended”."
+ loaded = loadmodule('lualibs-extended-merged.lua')
+else
+ info"Ignoring merged packages."
+ info"Falling back to individual libraries from collection “extended”."
+end
+
+if loaded == false then
+ loadmodule("lualibs-util-str.lua")--- string formatters (fast)
+ loadmodule("lualibs-util-tab.lua")--- extended table operations
+ loadmodule("lualibs-util-sto.lua")--- storage (hash allocation)
+ ----------("lualibs-util-pck.lua")---!packers; necessary?
+ ----------("lualibs-util-seq.lua")---!sequencers (function chaining)
+ ----------("lualibs-util-mrg.lua")---!only relevant in mtx-package
+ loadmodule("lualibs-util-prs.lua")--- miscellaneous parsers; cool. cool cool cool
+ ----------("lualibs-util-fmt.lua")---!column formatter (rarely used)
+ loadmodule("lualibs-util-dim.lua")--- conversions between dimensions
+ ----------("lualibs-util-jsn.lua")--- JSON parser
+
+ ----------("lualibs-trac-set.lua")---!generalization of trackers
+ ----------("lualibs-trac-log.lua")---!logging
+ loadmodule("lualibs-trac-inf.lua")--- timing/statistics
+ loadmodule("lualibs-util-lua.lua")--- operations on lua bytecode
+ loadmodule("lualibs-util-deb.lua")--- extra debugging
+ loadmodule("lualibs-util-tpl.lua")--- templating
+ loadmodule("lualibs-util-sta.lua")--- stacker (for writing pdf)
+ -------------------------------------!data-* -- Context specific
+ ----------("lualibs-util-lib.lua")---!swiglib; there is a luatex-swiglib
+ loadmodule("lualibs-util-env.lua")--- environment arguments
+ ----------("lualibs-mult-ini.lua")---
+ ----------("lualibs-core-con.lua")---
+end
+
+loadmodule"lualibs-util-jsn.lua"--- cannot be merged because of return statement
+
+unfake_context() --- TODO check if this works at runtime
+
+-- vim:tw=71:sw=2:ts=2:expandtab
+-- End of File `lualibs-extended.lua'.
diff --git a/lualibs-file.lua b/lualibs-file.lua
index af86f93..29416ca 100644
--- a/lualibs-file.lua
+++ b/lualibs-file.lua
@@ -62,7 +62,7 @@ elseif not lfs.isfile then
end
local insert, concat = table.insert, table.concat
-local match = string.match
+local match, find = string.match, string.find
local lpegmatch = lpeg.match
local getcurrentdir, attributes = lfs.currentdir, lfs.attributes
local checkedsplit = string.checkedsplit
@@ -410,11 +410,11 @@ local untouched = periods + (1-period)^1 * P(-1)
local splitstarter = (Cs(drivespec * (bwslash/"/" + fwslash)^0) + Cc(false)) * Ct(lpeg.splitat(S("/\\")^1))
local absolute = fwslash
-function file.collapsepath(str,anchor)
+function file.collapsepath(str,anchor) -- anchor: false|nil, true, "."
if not str then
return
end
- if anchor and not lpegmatch(anchors,str) then
+ if anchor == true and not lpegmatch(anchors,str) then
str = getcurrentdir() .. "/" .. str
end
if str == "" or str =="." then
@@ -455,12 +455,17 @@ function file.collapsepath(str,anchor)
elseif lpegmatch(absolute,str) then
return "/" .. concat(newelements,'/')
else
- return concat(newelements, '/')
+ newelements = concat(newelements, '/')
+ if anchor == "." and find(str,"^%./") then
+ return "./" .. newelements
+ else
+ return newelements
+ end
end
end
--- local function test(str)
--- print(string.format("%-20s %-15s %-15s",str,file.collapsepath(str),file.collapsepath(str,true)))
+-- local function test(str,...)
+-- print(string.format("%-20s %-15s %-30s %-20s",str,file.collapsepath(str),file.collapsepath(str,true),file.collapsepath(str,".")))
-- end
-- test("a/b.c/d") test("b.c/d") test("b.c/..")
-- test("/") test("c:/..") test("sys://..")
@@ -468,6 +473,7 @@ end
-- test("a") test("./a") test("/a") test("a/../..")
-- test("a/./b/..") test("a/aa/../b/bb") test("a/.././././b/..") test("a/./././b/..")
-- test("a/b/c/../..") test("./a/b/c/../..") test("a/b/c/../..")
+-- test("./a")
local validchars = R("az","09","AZ","--","..")
local pattern_a = lpeg.replacer(1-validchars)
diff --git a/lualibs-lpeg.lua b/lualibs-lpeg.lua
index 681ef09..323c73b 100644
--- a/lualibs-lpeg.lua
+++ b/lualibs-lpeg.lua
@@ -300,7 +300,7 @@ function string.splitlines(str)
return lpegmatch(linesplitter,str)
end
---~ lpeg.splitters = cache -- no longer public
+-- lpeg.splitters = cache -- no longer public
local cache = { }
diff --git a/lualibs-lua.lua b/lualibs-lua.lua
index 538c65d..fc05afa 100644
--- a/lualibs-lua.lua
+++ b/lualibs-lua.lua
@@ -6,7 +6,7 @@ if not modules then modules = { } end modules ['l-lua'] = {
license = "see context related readme files"
}
--- compatibility hacks ... try to avoid usage
+-- compatibility hacksand helpers
local major, minor = string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$")
@@ -148,246 +148,3 @@ function optionalrequire(...)
return result
end
end
-
--- Code moved from data-lua and changed into a plug-in.
-
--- We overload the regular loader. We do so because we operate mostly in
--- tds and use our own loader code. Alternatively we could use a more
--- extensive definition of package.path and package.cpath but even then
--- we're not done. Also, we now have better tracing.
---
--- -- local mylib = require("libtest")
--- -- local mysql = require("luasql.mysql")
-
-local type = type
-local gsub, format = string.gsub, string.format
-
-local package = package
-local searchers = package.searchers or package.loaders
-
-local libpaths = nil
-local clibpaths = nil
-local libhash = { }
-local clibhash = { }
-local libextras = { }
-local clibextras = { }
-
--- dummies
-
-local filejoin = file and file.join or function(path,name) return path .. "/" .. name end
-local isreadable = file and file.is_readable or function(name) local f = io.open(name) if f then f:close() return true end end
-local addsuffix = file and file.addsuffix or function(name,suffix) return name .. "." .. suffix end
-
---
-
-local function cleanpath(path) -- hm, don't we have a helper for this?
- return path
-end
-
-local helpers = package.helpers or {
- libpaths = function() return { } end,
- clibpaths = function() return { } end,
- cleanpath = cleanpath,
- trace = false,
- report = function(...) print(format(...)) end,
-}
-package.helpers = helpers
-
-local function getlibpaths()
- return libpaths or helpers.libpaths(libhash)
-end
-
-local function getclibpaths()
- return clibpaths or helpers.clibpaths(clibhash)
-end
-
-package.libpaths = getlibpaths
-package.clibpaths = getclibpaths
-
-local function addpath(what,paths,extras,hash,...)
- local pathlist = { ... }
- local cleanpath = helpers.cleanpath
- local trace = helpers.trace
- local report = helpers.report
- --
- local function add(path)
- local path = cleanpath(path)
- if not hash[path] then
- if trace then
- report("extra %s path: %s",what,path)
- end
- paths [#paths +1] = path
- extras[#extras+1] = path
- end
- end
- --
- for p=1,#pathlist do
- local path = pathlist[p]
- if type(path) == "table" then
- for i=1,#path do
- add(path[i])
- end
- else
- add(path)
- end
- end
- return paths, extras
-end
-
-function package.extralibpath(...)
- libpaths, libextras = addpath("lua", getlibpaths(), libextras, libhash,...)
-end
-
-function package.extraclibpath(...)
- clibpaths, clibextras = addpath("lib",getclibpaths(),clibextras,clibhash,...)
-end
-
--- function package.extralibpath(...)
--- libpaths = getlibpaths()
--- local pathlist = { ... }
--- local cleanpath = helpers.cleanpath
--- local trace = helpers.trace
--- local report = helpers.report
--- --
--- local function add(path)
--- local path = cleanpath(path)
--- if not libhash[path] then
--- if trace then
--- report("extra lua path: %s",path)
--- end
--- libextras[#libextras+1] = path
--- libpaths [#libpaths +1] = path
--- end
--- end
--- --
--- for p=1,#pathlist do
--- local path = pathlist[p]
--- if type(path) == "table" then
--- for i=1,#path do
--- add(path[i])
--- end
--- else
--- add(path)
--- end
--- end
--- end
-
--- function package.extraclibpath(...)
--- clibpaths = getclibpaths()
--- local pathlist = { ... }
--- local cleanpath = helpers.cleanpath
--- local trace = helpers.trace
--- local report = helpers.report
--- --
--- local function add(path)
--- local path = cleanpath(path)
--- if not clibhash[path] then
--- if trace then
--- report("extra lib path: %s",path)
--- end
--- clibextras[#clibextras+1] = path
--- clibpaths [#clibpaths +1] = path
--- end
--- end
--- --
--- for p=1,#pathlist do
--- local path = pathlist[p]
--- if type(path) == "table" then
--- for i=1,#path do
--- add(path[i])
--- end
--- else
--- add(path)
--- end
--- end
--- end
-
-if not searchers[-2] then
- -- use package-path and package-cpath
- searchers[-2] = searchers[2]
-end
-
-searchers[2] = function(name)
- return helpers.loaded(name)
-end
-
-searchers[3] = nil -- get rid of the built in one
-
-local function loadedaslib(resolved,rawname)
- -- local init = "luaopen_" .. string.match(rawname,".-([^%.]+)$")
- local init = "luaopen_"..gsub(rawname,"%.","_")
- if helpers.trace then
- helpers.report("calling loadlib with '%s' with init '%s'",resolved,init)
- end
- return package.loadlib(resolved,init)
-end
-
-local function loadedbylua(name)
- if helpers.trace then
- helpers.report("locating '%s' using normal loader",name)
- end
- return true, searchers[-2](name) -- the original
-end
-
-local function loadedbypath(name,rawname,paths,islib,what)
- local trace = helpers.trace
- local report = helpers.report
- if trace then
- report("locating '%s' as '%s' on '%s' paths",rawname,name,what)
- end
- for p=1,#paths do
- local path = paths[p]
- local resolved = filejoin(path,name)
- if trace then -- mode detail
- report("checking for '%s' using '%s' path '%s'",name,what,path)
- end
- if isreadable(resolved) then
- if trace then
- report("lib '%s' located on '%s'",name,resolved)
- end
- if islib then
- return true, loadedaslib(resolved,rawname)
- else
- return true, loadfile(resolved)
- end
- end
- end
-end
-
-local function notloaded(name)
- if helpers.trace then
- helpers.report("? unable to locate library '%s'",name)
- end
-end
-
-helpers.loadedaslib = loadedaslib
-helpers.loadedbylua = loadedbylua
-helpers.loadedbypath = loadedbypath
-helpers.notloaded = notloaded
-
--- alternatively we could set the package.searchers
-
-function helpers.loaded(name)
- local thename = gsub(name,"%.","/")
- local luaname = addsuffix(thename,"lua")
- local libname = addsuffix(thename,os.libsuffix or "so") -- brrr
- local libpaths = getlibpaths()
- local clibpaths = getclibpaths()
- local done, result = loadedbypath(luaname,name,libpaths,false,"lua")
- if done then
- return result
- end
- local done, result = loadedbypath(luaname,name,clibpaths,false,"lua")
- if done then
- return result
- end
- local done, result = loadedbypath(libname,name,clibpaths,true,"lib")
- if done then
- return result
- end
- local done, result = loadedbylua(name)
- if done then
- return result
- end
- return notloaded(name)
-end
diff --git a/lualibs-package.lua b/lualibs-package.lua
new file mode 100644
index 0000000..7b82fa5
--- /dev/null
+++ b/lualibs-package.lua
@@ -0,0 +1,338 @@
+if not modules then modules = { } end modules ['l-package'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+-- Code moved from data-lua and changed into a plug-in.
+
+-- We overload the regular loader. We do so because we operate mostly in
+-- tds and use our own loader code. Alternatively we could use a more
+-- extensive definition of package.path and package.cpath but even then
+-- we're not done. Also, we now have better tracing.
+--
+-- -- local mylib = require("libtest")
+-- -- local mysql = require("luasql.mysql")
+
+local type = type
+local gsub, format = string.gsub, string.format
+
+local P, S, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.Cs, lpeg.match
+
+local package = package
+local searchers = package.searchers or package.loaders
+
+-- dummies
+
+local filejoin = file and file.join or function(path,name) return path .. "/" .. name end
+local isreadable = file and file.is_readable or function(name) local f = io.open(name) if f then f:close() return true end end
+local addsuffix = file and file.addsuffix or function(name,suffix) return name .. "." .. suffix end
+
+--
+
+-- local separator, concatinator, placeholder, pathofexecutable, ignorebefore = string.match(package.config,"(.-)\n(.-)\n(.-)\n(.-)\n(.-)\n")
+--
+-- local config = {
+-- separator = separator, -- \ or /
+-- concatinator = concatinator, -- ;
+-- placeholder = placeholder, -- ? becomes name
+-- pathofexecutable = pathofexecutable, -- ! becomes executables dir (on windows)
+-- ignorebefore = ignorebefore, -- - remove all before this when making lua_open
+-- }
+
+--
+
+local function cleanpath(path) -- hm, don't we have a helper for this?
+ return path
+end
+
+local pattern = Cs((((1-S("\\/"))^0 * (S("\\/")^1/"/"))^0 * (P(".")^1/"/"+P(1))^1) * -1)
+
+local function lualibfile(name)
+ return lpegmatch(pattern,name) or name
+end
+
+local helpers = package.helpers or {
+ cleanpath = cleanpath,
+ lualibfile = lualibfile,
+ trace = false,
+ report = function(...) print(format(...)) end,
+ builtin = {
+ ["preload table"] = searchers[1], -- special case, built-in libs
+ ["path specification"] = searchers[2],
+ ["cpath specification"] = searchers[3],
+ ["all in one fallback"] = searchers[4], -- special case, combined libs
+ },
+ methods = {
+ },
+ sequence = {
+ "already loaded",
+ "preload table",
+ "lua extra list",
+ "lib extra list",
+ "path specification",
+ "cpath specification",
+ "all in one fallback",
+ "not loaded",
+ }
+}
+
+package.helpers = helpers
+
+local methods = helpers.methods
+local builtin = helpers.builtin
+
+-- extra tds/ctx paths
+
+local extraluapaths = { }
+local extralibpaths = { }
+local luapaths = nil -- delayed
+local libpaths = nil -- delayed
+
+local function getextraluapaths()
+ return extraluapaths
+end
+
+local function getextralibpaths()
+ return extralibpaths
+end
+
+local function getluapaths()
+ luapaths = luapaths or file.splitpath(package.path, ";")
+ return luapaths
+end
+
+local function getlibpaths()
+ libpaths = libpaths or file.splitpath(package.cpath, ";")
+ return libpaths
+end
+
+package.luapaths = getluapaths
+package.libpaths = getlibpaths
+package.extraluapaths = getextraluapaths
+package.extralibpaths = getextralibpaths
+
+local hashes = {
+ lua = { },
+ lib = { },
+}
+
+local function registerpath(tag,what,target,...)
+ local pathlist = { ... }
+ local cleanpath = helpers.cleanpath
+ local trace = helpers.trace
+ local report = helpers.report
+ local hash = hashes[what]
+ --
+ local function add(path)
+ local path = cleanpath(path)
+ if not hash[path] then
+ target[#target+1] = path
+ hash[path] = true
+ if trace then
+ report("registered %s path %s: %s",tag,#target,path)
+ end
+ else
+ if trace then
+ report("duplicate %s path: %s",tag,path)
+ end
+ end
+ end
+ --
+ for p=1,#pathlist do
+ local path = pathlist[p]
+ if type(path) == "table" then
+ for i=1,#path do
+ add(path[i])
+ end
+ else
+ add(path)
+ end
+ end
+ return paths
+end
+
+helpers.registerpath = registerpath
+
+function package.extraluapath(...)
+ registerpath("extra lua","lua",extraluapaths,...)
+end
+
+function package.extralibpath(...)
+ registerpath("extra lib","lib",extralibpaths,...)
+end
+
+-- lib loader (used elsewhere)
+
+local function loadedaslib(resolved,rawname) -- todo: strip all before first -
+ -- local init = "luaopen_" .. string.match(rawname,".-([^%.]+)$")
+ local init = "luaopen_"..gsub(rawname,"%.","_")
+ if helpers.trace then
+ helpers.report("calling loadlib with '%s' with init '%s'",resolved,init)
+ end
+ return package.loadlib(resolved,init)
+end
+
+helpers.loadedaslib = loadedaslib
+
+-- wrapped and new loaders
+
+local function loadedbypath(name,rawname,paths,islib,what)
+ local trace = helpers.trace
+ local report = helpers.report
+ if trace then
+ report("locating '%s' as '%s' on '%s' paths",rawname,name,what)
+ end
+ for p=1,#paths do
+ local path = paths[p]
+ local resolved = filejoin(path,name)
+ if trace then -- mode detail
+ report("checking '%s' using '%s' path '%s'",name,what,path)
+ end
+ if isreadable(resolved) then
+ if trace then
+ report("'%s' located on '%s'",name,resolved)
+ end
+ local result = nil
+ if islib then
+ result = loadedaslib(resolved,rawname)
+ else
+ result = loadfile(resolved)
+ end
+ if result then
+ result()
+ end
+ return true, result
+ end
+ end
+end
+
+helpers.loadedbypath = loadedbypath
+
+-- alternatively we could set the package.searchers
+
+methods["already loaded"] = function(name)
+ local result = package.loaded[name]
+ if result then
+ return true, result
+ end
+end
+
+methods["preload table"] = function(name)
+ local result = builtin["preload table"](name)
+ if type(result) == "function" then
+ return true, result
+ end
+end
+
+methods["lua extra list"] = function(name)
+ local thename = lualibfile(name)
+ local luaname = addsuffix(thename,"lua")
+ local luapaths = getextraluapaths()
+ local done, result = loadedbypath(luaname,name,luapaths,false,"lua")
+ if done then
+ return true, result
+ end
+end
+
+methods["lib extra list"] = function(name)
+ local thename = lualibfile(name)
+ local libname = addsuffix(thename,os.libsuffix)
+ local libpaths = getextralibpaths()
+ local done, result = loadedbypath(libname,name,libpaths,true,"lib")
+ if done then
+ return true, result
+ end
+end
+
+local shown = false
+
+methods["path specification"] = function(name)
+ if not shown and helpers.trace then
+ local luapaths = getluapaths() -- triggers list building
+ if #luapaths > 0 then
+ helpers.report("using %s built in lua paths",#luapaths)
+ else
+ helpers.report("no built in lua paths defined")
+ end
+ shown = true
+ end
+ local result = builtin["path specification"](name)
+ if type(result) == "function" then
+ return true, result()
+ end
+end
+
+local shown = false
+
+methods["cpath specification"] = function(name)
+ if not shown and helpers.trace then
+ local libpaths = getlibpaths() -- triggers list building
+ if #libpaths > 0 then
+ helpers.report("using %s built in lib paths",#libpaths)
+ else
+ helpers.report("no built in lib paths defined")
+ end
+ shown = true
+ end
+ local result = builtin["cpath specification"](name)
+ if type(result) == "function" then
+ return true, result()
+ end
+end
+
+methods["all in one fallback"] = function(name)
+ local result = builtin["all in one fallback"](name)
+ if type(result) == "function" then
+ return true, result()
+ end
+end
+
+methods["not loaded"] = function(name)
+ if helpers.trace then
+ helpers.report("unable to locate '%s'",name)
+ end
+end
+
+function helpers.loaded(name)
+ local sequence = helpers.sequence
+ for i=1,#sequence do
+ local step = sequence[i]
+ if helpers.trace then
+ helpers.report("locating '%s' using method '%s'",name,step)
+ end
+ local done, result = methods[step](name)
+ if done then
+ if helpers.trace then
+ helpers.report("'%s' located via method '%s' returns '%s'",name,step,type(result))
+ end
+ if result then
+ package.loaded[name] = result
+ end
+ return result
+ end
+ end
+ return nil -- we must return a value
+end
+
+function helpers.unload(name)
+ if helpers.trace then
+ if package.loaded[name] then
+ helpers.report("unloading '%s', %s",name,"done")
+ else
+ helpers.report("unloading '%s', %s",name,"not loaded")
+ end
+ end
+ package.loaded[name] = nil -- does that work? is readable only, maybe we need our own hash
+end
+
+searchers[1] = nil
+searchers[2] = nil
+searchers[3] = nil
+searchers[4] = nil
+
+helpers.savedrequire = helpers.savedrequire or require
+
+require = helpers.loaded
diff --git a/lualibs-trac-inf.lua b/lualibs-trac-inf.lua
new file mode 100644
index 0000000..eefc15a
--- /dev/null
+++ b/lualibs-trac-inf.lua
@@ -0,0 +1,193 @@
+if not modules then modules = { } end modules ['trac-inf'] = {
+ version = 1.001,
+ comment = "companion to trac-inf.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+-- As we want to protect the global tables, we no longer store the timing
+-- in the tables themselves but in a hidden timers table so that we don't
+-- get warnings about assignments. This is more efficient than using rawset
+-- and rawget.
+
+local type, tonumber = type, tonumber
+local format, lower = string.format, string.lower
+local concat = table.concat
+local clock = os.gettimeofday or os.clock -- should go in environment
+
+statistics = statistics or { }
+local statistics = statistics
+
+statistics.enable = true
+statistics.threshold = 0.01
+
+local statusinfo, n, registered, timers = { }, 0, { }, { }
+
+table.setmetatableindex(timers,function(t,k)
+ local v = { timing = 0, loadtime = 0 }
+ t[k] = v
+ return v
+end)
+
+local function hastiming(instance)
+ return instance and timers[instance]
+end
+
+local function resettiming(instance)
+ timers[instance or "notimer"] = { timing = 0, loadtime = 0 }
+end
+
+local function starttiming(instance)
+ local timer = timers[instance or "notimer"]
+ local it = timer.timing or 0
+ if it == 0 then
+ timer.starttime = clock()
+ if not timer.loadtime then
+ timer.loadtime = 0
+ end
+ end
+ timer.timing = it + 1
+end
+
+local function stoptiming(instance)
+ local timer = timers[instance or "notimer"]
+ local it = timer.timing
+ if it > 1 then
+ timer.timing = it - 1
+ else
+ local starttime = timer.starttime
+ if starttime then
+ local stoptime = clock()
+ local loadtime = stoptime - starttime
+ timer.stoptime = stoptime
+ timer.loadtime = timer.loadtime + loadtime
+ timer.timing = 0
+ return loadtime
+ end
+ end
+ return 0
+end
+
+local function elapsed(instance)
+ if type(instance) == "number" then
+ return instance or 0
+ else
+ local timer = timers[instance or "notimer"]
+ return timer and timer.loadtime or 0
+ end
+end
+
+local function elapsedtime(instance)
+ return format("%0.3f",elapsed(instance))
+end
+
+local function elapsedindeed(instance)
+ return elapsed(instance) > statistics.threshold
+end
+
+local function elapsedseconds(instance,rest) -- returns nil if 0 seconds
+ if elapsedindeed(instance) then
+ return format("%0.3f seconds %s", elapsed(instance),rest or "")
+ end
+end
+
+statistics.hastiming = hastiming
+statistics.resettiming = resettiming
+statistics.starttiming = starttiming
+statistics.stoptiming = stoptiming
+statistics.elapsed = elapsed
+statistics.elapsedtime = elapsedtime
+statistics.elapsedindeed = elapsedindeed
+statistics.elapsedseconds = elapsedseconds
+
+-- general function .. we might split this module
+
+function statistics.register(tag,fnc)
+ if statistics.enable and type(fnc) == "function" then
+ local rt = registered[tag] or (#statusinfo + 1)
+ statusinfo[rt] = { tag, fnc }
+ registered[tag] = rt
+ if #tag > n then n = #tag end
+ end
+end
+
+local report = logs.reporter("mkiv lua stats")
+
+function statistics.show()
+ if statistics.enable then
+ -- this code will move
+ local register = statistics.register
+ register("luatex banner", function()
+ return lower(status.banner)
+ end)
+ register("control sequences", function()
+ return format("%s of %s + %s", status.cs_count, status.hash_size,status.hash_extra)
+ end)
+ register("callbacks", function()
+ local total, indirect = status.callbacks or 0, status.indirect_callbacks or 0
+ return format("%s direct, %s indirect, %s total", total-indirect, indirect, total)
+ end)
+ if jit then
+ local status = { jit.status() }
+ if status[1] then
+ register("luajit status", function()
+ return concat(status," ",2)
+ end)
+ end
+ end
+ -- so far
+ -- collectgarbage("collect")
+ register("current memory usage",statistics.memused)
+ register("runtime",statistics.runtime)
+ logs.newline() -- initial newline
+ for i=1,#statusinfo do
+ local s = statusinfo[i]
+ local r = s[2]()
+ if r then
+ report("%s: %s",s[1],r)
+ end
+ end
+ -- logs.newline() -- final newline
+ statistics.enable = false
+ end
+end
+
+function statistics.memused() -- no math.round yet -)
+ local round = math.round or math.floor
+ return format("%s MB (ctx: %s MB)",round(collectgarbage("count")/1000), round(status.luastate_bytes/1000000))
+end
+
+starttiming(statistics)
+
+function statistics.formatruntime(runtime) -- indirect so it can be overloaded and
+ return format("%s seconds", runtime) -- indeed that happens in cure-uti.lua
+end
+
+function statistics.runtime()
+ stoptiming(statistics)
+ return statistics.formatruntime(elapsedtime(statistics))
+end
+
+local report = logs.reporter("system")
+
+function statistics.timed(action)
+ starttiming("run")
+ action()
+ stoptiming("run")
+ report("total runtime: %s",elapsedtime("run"))
+end
+
+-- where, not really the best spot for this:
+
+commands = commands or { }
+
+function commands.resettimer(name)
+ resettiming(name or "whatever")
+ starttiming(name or "whatever")
+end
+
+function commands.elapsedtime(name)
+ stoptiming(name or "whatever")
+ context(elapsedtime(name or "whatever"))
+end
diff --git a/lualibs-util-deb.lua b/lualibs-util-deb.lua
new file mode 100644
index 0000000..785373f
--- /dev/null
+++ b/lualibs-util-deb.lua
@@ -0,0 +1,128 @@
+if not modules then modules = { } end modules ['util-deb'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+-- the <anonymous> tag is kind of generic and used for functions that are not
+-- bound to a variable, like node.new, node.copy etc (contrary to for instance
+-- node.has_attribute which is bound to a has_attribute local variable in mkiv)
+
+local debug = require "debug"
+
+local getinfo = debug.getinfo
+local type, next, tostring = type, next, tostring
+local format, find = string.format, string.find
+local is_boolean = string.is_boolean
+
+utilities = utilities or { }
+local debugger = utilities.debugger or { }
+utilities.debugger = debugger
+
+local counters = { }
+local names = { }
+
+local report = logs.reporter("debugger")
+
+-- one
+
+local function hook()
+ local f = getinfo(2) -- "nS"
+ if f then
+ local n = "unknown"
+ if f.what == "C" then
+ n = f.name or '<anonymous>'
+ if not names[n] then
+ names[n] = format("%42s",n)
+ end
+ else
+ -- source short_src linedefined what name namewhat nups func
+ n = f.name or f.namewhat or f.what
+ if not n or n == "" then
+ n = "?"
+ end
+ if not names[n] then
+ names[n] = format("%42s : % 5i : %s",n,f.linedefined or 0,f.short_src or "unknown source")
+ end
+ end
+ counters[n] = (counters[n] or 0) + 1
+ end
+end
+
+function debugger.showstats(printer,threshold) -- hm, something has changed, rubish now
+ printer = printer or report
+ threshold = threshold or 0
+ local total, grandtotal, functions = 0, 0, 0
+ local dataset = { }
+ for name, count in next, counters do
+ dataset[#dataset+1] = { name, count }
+ end
+ table.sort(dataset,function(a,b) return a[2] == b[2] and b[1] > a[1] or a[2] > b[2] end)
+ for i=1,#dataset do
+ local d = dataset[i]
+ local name = d[1]
+ local count = d[2]
+ if count > threshold and not find(name,"for generator") then -- move up
+ printer(format("%8i %s\n", count, names[name]))
+ total = total + count
+ end
+ grandtotal = grandtotal + count
+ functions = functions + 1
+ end
+ printer("\n")
+ printer(format("functions : % 10i\n", functions))
+ printer(format("total : % 10i\n", total))
+ printer(format("grand total: % 10i\n", grandtotal))
+ printer(format("threshold : % 10i\n", threshold))
+end
+
+function debugger.savestats(filename,threshold)
+ local f = io.open(filename,'w')
+ if f then
+ debugger.showstats(function(str) f:write(str) end,threshold)
+ f:close()
+ end
+end
+
+function debugger.enable()
+ debug.sethook(hook,"c")
+end
+
+function debugger.disable()
+ debug.sethook()
+--~ counters[debug.getinfo(2,"f").func] = nil
+end
+
+--~ debugger.enable()
+
+--~ print(math.sin(1*.5))
+--~ print(math.sin(1*.5))
+--~ print(math.sin(1*.5))
+--~ print(math.sin(1*.5))
+--~ print(math.sin(1*.5))
+
+--~ debugger.disable()
+
+--~ print("")
+--~ debugger.showstats()
+--~ print("")
+--~ debugger.showstats(print,3)
+
+-- from the lua book:
+
+function traceback()
+ local level = 1
+ while true do
+ local info = debug.getinfo(level, "Sl")
+ if not info then
+ break
+ elseif info.what == "C" then
+ print(format("%3i : C function",level))
+ else
+ print(format("%3i : [%s]:%d",level,info.short_src,info.currentline))
+ end
+ level = level + 1
+ end
+end
diff --git a/lualibs-util-env.lua b/lualibs-util-env.lua
new file mode 100644
index 0000000..283b91c
--- /dev/null
+++ b/lualibs-util-env.lua
@@ -0,0 +1,258 @@
+if not modules then modules = { } end modules ['util-env'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local allocate, mark = utilities.storage.allocate, utilities.storage.mark
+
+local format, sub, match, gsub, find = string.format, string.sub, string.match, string.gsub, string.find
+local unquoted, quoted = string.unquoted, string.quoted
+local concat, insert, remove = table.concat, table.insert, table.remove
+
+environment = environment or { }
+local environment = environment
+
+-- precautions
+
+os.setlocale(nil,nil) -- useless feature and even dangerous in luatex
+
+function os.setlocale()
+ -- no way you can mess with it
+end
+
+-- dirty tricks (we will replace the texlua call by luatex --luaonly)
+
+local validengines = allocate {
+ ["luatex"] = true,
+ ["luajittex"] = true,
+ -- ["luatex.exe"] = true,
+ -- ["luajittex.exe"] = true,
+}
+
+local basicengines = allocate {
+ ["luatex"] = "luatex",
+ ["texlua"] = "luatex",
+ ["texluac"] = "luatex",
+ ["luajittex"] = "luajittex",
+ ["texluajit"] = "luajittex",
+ -- ["texlua.exe"] = "luatex",
+ -- ["texluajit.exe"] = "luajittex",
+}
+
+local luaengines=allocate {
+ ["lua"] = true,
+ ["luajit"] = true,
+}
+
+environment.validengines = validengines
+environment.basicengines = basicengines
+
+-- [-1] = binary
+-- [ 0] = self
+-- [ 1] = argument 1 ...
+
+-- instead we could set ranges
+
+if not arg then
+ -- used as library
+elseif luaengines[file.removesuffix(arg[-1])] then
+-- arg[-1] = arg[0]
+-- arg[ 0] = arg[1]
+-- for k=2,#arg do
+-- arg[k-1] = arg[k]
+-- end
+-- remove(arg) -- last
+elseif validengines[file.removesuffix(arg[0])] then
+ if arg[1] == "--luaonly" then
+ arg[-1] = arg[0]
+ arg[ 0] = arg[2]
+ for k=3,#arg do
+ arg[k-2] = arg[k]
+ end
+ remove(arg) -- last
+ remove(arg) -- pre-last
+ else
+ -- tex run
+ end
+
+ -- This is an ugly hack but it permits symlinking a script (say 'context') to 'mtxrun' as in:
+ --
+ -- ln -s /opt/minimals/tex/texmf-linux-64/bin/mtxrun context
+ --
+ -- The special mapping hack is needed because 'luatools' boils down to 'mtxrun --script base'
+ -- but it's unlikely that there will be more of this
+
+ local originalzero = file.basename(arg[0])
+ local specialmapping = { luatools == "base" }
+
+ if originalzero ~= "mtxrun" and originalzero ~= "mtxrun.lua" then
+ arg[0] = specialmapping[originalzero] or originalzero
+ insert(arg,0,"--script")
+ insert(arg,0,"mtxrun")
+ end
+
+end
+
+-- environment
+
+environment.arguments = allocate()
+environment.files = allocate()
+environment.sortedflags = nil
+
+-- context specific arguments (in order not to confuse the engine)
+
+function environment.initializearguments(arg)
+ local arguments, files = { }, { }
+ environment.arguments, environment.files, environment.sortedflags = arguments, files, nil
+ for index=1,#arg do
+ local argument = arg[index]
+ if index > 0 then
+ local flag, value = match(argument,"^%-+(.-)=(.-)$")
+ if flag then
+ flag = gsub(flag,"^c:","")
+ arguments[flag] = unquoted(value or "")
+ else
+ flag = match(argument,"^%-+(.+)")
+ if flag then
+ flag = gsub(flag,"^c:","")
+ arguments[flag] = true
+ else
+ files[#files+1] = argument
+ end
+ end
+ end
+ end
+ environment.ownname = file.reslash(environment.ownname or arg[0] or 'unknown.lua')
+end
+
+function environment.setargument(name,value)
+ environment.arguments[name] = value
+end
+
+-- todo: defaults, better checks e.g on type (boolean versus string)
+--
+-- tricky: too many hits when we support partials unless we add
+-- a registration of arguments so from now on we have 'partial'
+
+function environment.getargument(name,partial)
+ local arguments, sortedflags = environment.arguments, environment.sortedflags
+ if arguments[name] then
+ return arguments[name]
+ elseif partial then
+ if not sortedflags then
+ sortedflags = allocate(table.sortedkeys(arguments))
+ for k=1,#sortedflags do
+ sortedflags[k] = "^" .. sortedflags[k]
+ end
+ environment.sortedflags = sortedflags
+ end
+ -- example of potential clash: ^mode ^modefile
+ for k=1,#sortedflags do
+ local v = sortedflags[k]
+ if find(name,v) then
+ return arguments[sub(v,2,#v)]
+ end
+ end
+ end
+ return nil
+end
+
+environment.argument = environment.getargument
+
+function environment.splitarguments(separator) -- rather special, cut-off before separator
+ local done, before, after = false, { }, { }
+ local originalarguments = environment.originalarguments
+ for k=1,#originalarguments do
+ local v = originalarguments[k]
+ if not done and v == separator then
+ done = true
+ elseif done then
+ after[#after+1] = v
+ else
+ before[#before+1] = v
+ end
+ end
+ return before, after
+end
+
+function environment.reconstructcommandline(arg,noquote)
+ arg = arg or environment.originalarguments
+ if noquote and #arg == 1 then
+ -- we could just do: return unquoted(resolvers.resolve(arg[i]))
+ local a = arg[1]
+ a = resolvers.resolve(a)
+ a = unquoted(a)
+ return a
+ elseif #arg > 0 then
+ local result = { }
+ for i=1,#arg do
+ -- we could just do: result[#result+1] = format("%q",unquoted(resolvers.resolve(arg[i])))
+ local a = arg[i]
+ a = resolvers.resolve(a)
+ a = unquoted(a)
+ a = gsub(a,'"','\\"') -- tricky
+ if find(a," ") then
+ result[#result+1] = quoted(a)
+ else
+ result[#result+1] = a
+ end
+ end
+ return concat(result," ")
+ else
+ return ""
+ end
+end
+
+-- -- to be tested:
+--
+-- function environment.reconstructcommandline(arg,noquote)
+-- arg = arg or environment.originalarguments
+-- if noquote and #arg == 1 then
+-- return unquoted(resolvers.resolve(arg[1]))
+-- elseif #arg > 0 then
+-- local result = { }
+-- for i=1,#arg do
+-- result[#result+1] = format("%q",unquoted(resolvers.resolve(arg[i]))) -- always quote
+-- end
+-- return concat(result," ")
+-- else
+-- return ""
+-- end
+-- end
+
+if arg then
+
+ -- new, reconstruct quoted snippets (maybe better just remove the " then and add them later)
+ local newarg, instring = { }, false
+
+ for index=1,#arg do
+ local argument = arg[index]
+ if find(argument,"^\"") then
+ newarg[#newarg+1] = gsub(argument,"^\"","")
+ if not find(argument,"\"$") then
+ instring = true
+ end
+ elseif find(argument,"\"$") then
+ newarg[#newarg] = newarg[#newarg] .. " " .. gsub(argument,"\"$","")
+ instring = false
+ elseif instring then
+ newarg[#newarg] = newarg[#newarg] .. " " .. argument
+ else
+ newarg[#newarg+1] = argument
+ end
+ end
+ for i=1,-5,-1 do
+ newarg[i] = arg[i]
+ end
+
+ environment.initializearguments(newarg)
+
+ environment.originalarguments = mark(newarg)
+ environment.rawarguments = mark(arg)
+
+ arg = { } -- prevent duplicate handling
+
+end
diff --git a/lualibs-util-mrg.lua b/lualibs-util-mrg.lua
index 78b23dc..690188e 100644
--- a/lualibs-util-mrg.lua
+++ b/lualibs-util-mrg.lua
@@ -77,36 +77,43 @@ end
-- -- saves some 20K .. ldx comments
-- data = gsub(data,"%-%-%[%[ldx%-%-.-%-%-ldx%]%]%-%-","")
-local space = patterns.space
-local eol = patterns.newline
-local equals = P("=")^0
-local open = P("[") * Cg(equals,"init") * P("[") * P("\n")^-1
-local close = P("]") * C(equals) * P("]")
-local closeeq = Cmt(close * Cb("init"), function(s,i,a,b) return a == b end)
-local longstring = open * (1 - closeeq)^0 * close
-
-local quoted = patterns.quoted
-local emptyline = space^0 * eol
-local operator1 = P("<=") + P(">=") + P("~=") + P("..") + S("/^<>=*+%%")
-local operator2 = S("*+/")
-local operator3 = S("-")
-local separator = S(",;")
-
-local ignore = (P("]") * space^1 * P("=") * space^1 * P("]")) / "]=[" +
- (P("=") * space^1 * P("{")) / "={" +
- (P("(") * space^1) / "(" +
- (P("{") * (space+eol)^1 * P("}")) / "{}"
-local strings = quoted -- / function (s) print("<<"..s..">>") return s end
-local longcmt = (emptyline^0 * P("--") * longstring * emptyline^0) / ""
-local longstr = longstring
-local comment = emptyline^0 * P("--") * P("-")^0 * (1-eol)^0 * emptyline^1 / "\n"
-local pack = ((eol+space)^0 / "") * operator1 * ((eol+space)^0 / "") +
- ((eol+space)^0 / "") * operator2 * ((space)^0 / "") +
- ((eol+space)^1 / "") * operator3 * ((space)^1 / "") +
- ((space)^0 / "") * separator * ((space)^0 / "")
-local lines = emptyline^2 / "\n"
-local spaces = (space * space) / " "
------ spaces = ((space+eol)^1 ) / " "
+local space = patterns.space
+local eol = patterns.newline
+local equals = P("=")^0
+local open = P("[") * Cg(equals,"init") * P("[") * P("\n")^-1
+local close = P("]") * C(equals) * P("]")
+local closeeq = Cmt(close * Cb("init"), function(s,i,a,b) return a == b end)
+local longstring = open * (1 - closeeq)^0 * close
+
+local quoted = patterns.quoted
+local digit = patterns.digit
+local emptyline = space^0 * eol
+local operator1 = P("<=") + P(">=") + P("~=") + P("..") + S("/^<>=*+%%")
+local operator2 = S("*+/")
+local operator3 = S("-")
+local operator4 = P("..")
+local separator = S(",;")
+
+local ignore = (P("]") * space^1 * P("=") * space^1 * P("]")) / "]=[" +
+ (P("=") * space^1 * P("{")) / "={" +
+ (P("(") * space^1) / "(" +
+ (P("{") * (space+eol)^1 * P("}")) / "{}"
+local strings = quoted -- / function (s) print("<<"..s..">>") return s end
+local longcmt = (emptyline^0 * P("--") * longstring * emptyline^0) / ""
+local longstr = longstring
+local comment = emptyline^0 * P("--") * P("-")^0 * (1-eol)^0 * emptyline^1 / "\n"
+local optionalspaces = space^0 / ""
+local mandatespaces = space^1 / ""
+local optionalspacing = (eol+space)^0 / ""
+local mandatespacing = (eol+space)^1 / ""
+local pack = digit * space^1 * operator4 * optionalspacing +
+ optionalspacing * operator1 * optionalspacing +
+ optionalspacing * operator2 * optionalspaces +
+ mandatespacing * operator3 * mandatespaces +
+ optionalspaces * separator * optionalspaces
+local lines = emptyline^2 / "\n"
+local spaces = (space * space) / " "
+----- spaces = ((space+eol)^1 ) / " "
local compact = Cs ( (
ignore +
diff --git a/lualibs-util-prs.lua b/lualibs-util-prs.lua
new file mode 100644
index 0000000..31e7ffa
--- /dev/null
+++ b/lualibs-util-prs.lua
@@ -0,0 +1,562 @@
+if not modules then modules = { } end modules ['util-prs'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local lpeg, table, string = lpeg, table, string
+local P, R, V, S, C, Ct, Cs, Carg, Cc, Cg, Cf, Cp = lpeg.P, lpeg.R, lpeg.V, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cs, lpeg.Carg, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.Cp
+local lpegmatch, lpegpatterns = lpeg.match, lpeg.patterns
+local concat, format, gmatch, find = table.concat, string.format, string.gmatch, string.find
+local tostring, type, next, rawset = tostring, type, next, rawset
+
+utilities = utilities or {}
+local parsers = utilities.parsers or { }
+utilities.parsers = parsers
+local patterns = parsers.patterns or { }
+parsers.patterns = patterns
+
+local setmetatableindex = table.setmetatableindex
+local sortedhash = table.sortedhash
+
+-- we share some patterns
+
+local digit = R("09")
+local space = P(' ')
+local equal = P("=")
+local comma = P(",")
+local lbrace = P("{")
+local rbrace = P("}")
+local lparent = P("(")
+local rparent = P(")")
+local period = S(".")
+local punctuation = S(".,:;")
+local spacer = lpegpatterns.spacer
+local whitespace = lpegpatterns.whitespace
+local newline = lpegpatterns.newline
+local anything = lpegpatterns.anything
+local endofstring = lpegpatterns.endofstring
+
+local nobrace = 1 - ( lbrace + rbrace )
+local noparent = 1 - ( lparent + rparent)
+
+-- we could use a Cf Cg construct
+
+local escape, left, right = P("\\"), P('{'), P('}')
+
+lpegpatterns.balanced = P {
+ [1] = ((escape * (left+right)) + (1 - (left+right)) + V(2))^0,
+ [2] = left * V(1) * right
+}
+
+local nestedbraces = P { lbrace * (nobrace + V(1))^0 * rbrace }
+local nestedparents = P { lparent * (noparent + V(1))^0 * rparent }
+local spaces = space^0
+local argument = Cs((lbrace/"") * ((nobrace + nestedbraces)^0) * (rbrace/""))
+local content = (1-endofstring)^0
+
+lpegpatterns.nestedbraces = nestedbraces -- no capture
+lpegpatterns.nestedparents = nestedparents -- no capture
+lpegpatterns.nested = nestedbraces -- no capture
+lpegpatterns.argument = argument -- argument after e.g. =
+lpegpatterns.content = content -- rest after e.g =
+
+local value = P(lbrace * C((nobrace + nestedbraces)^0) * rbrace) + C((nestedbraces + (1-comma))^0)
+
+local key = C((1-equal-comma)^1)
+local pattern_a = (space+comma)^0 * (key * equal * value + key * C(""))
+local pattern_c = (space+comma)^0 * (key * equal * value)
+
+local key = C((1-space-equal-comma)^1)
+local pattern_b = spaces * comma^0 * spaces * (key * ((spaces * equal * spaces * value) + C("")))
+
+-- "a=1, b=2, c=3, d={a{b,c}d}, e=12345, f=xx{a{b,c}d}xx, g={}" : outer {} removes, leading spaces ignored
+
+-- todo: rewrite to fold etc
+--
+-- parse = lpeg.Cf(lpeg.Carg(1) * lpeg.Cg(key * equal * value) * separator^0,rawset)^0 -- lpeg.match(parse,"...",1,hash)
+
+local hash = { }
+
+local function set(key,value)
+ hash[key] = value
+end
+
+local pattern_a_s = (pattern_a/set)^1
+local pattern_b_s = (pattern_b/set)^1
+local pattern_c_s = (pattern_c/set)^1
+
+patterns.settings_to_hash_a = pattern_a_s
+patterns.settings_to_hash_b = pattern_b_s
+patterns.settings_to_hash_c = pattern_c_s
+
+function parsers.make_settings_to_hash_pattern(set,how)
+ if how == "strict" then
+ return (pattern_c/set)^1
+ elseif how == "tolerant" then
+ return (pattern_b/set)^1
+ else
+ return (pattern_a/set)^1
+ end
+end
+
+function parsers.settings_to_hash(str,existing)
+ if str and str ~= "" then
+ hash = existing or { }
+ lpegmatch(pattern_a_s,str)
+ return hash
+ else
+ return { }
+ end
+end
+
+function parsers.settings_to_hash_tolerant(str,existing)
+ if str and str ~= "" then
+ hash = existing or { }
+ lpegmatch(pattern_b_s,str)
+ return hash
+ else
+ return { }
+ end
+end
+
+function parsers.settings_to_hash_strict(str,existing)
+ if str and str ~= "" then
+ hash = existing or { }
+ lpegmatch(pattern_c_s,str)
+ return next(hash) and hash
+ else
+ return nil
+ end
+end
+
+local separator = comma * space^0
+local value = P(lbrace * C((nobrace + nestedbraces)^0) * rbrace)
+ + C((nestedbraces + (1-comma))^0)
+local pattern = spaces * Ct(value*(separator*value)^0)
+
+-- "aap, {noot}, mies" : outer {} removes, leading spaces ignored
+
+patterns.settings_to_array = pattern
+
+-- we could use a weak table as cache
+
+function parsers.settings_to_array(str,strict)
+ if not str or str == "" then
+ return { }
+ elseif strict then
+ if find(str,"{") then
+ return lpegmatch(pattern,str)
+ else
+ return { str }
+ end
+ else
+ return lpegmatch(pattern,str)
+ end
+end
+
+local function set(t,v)
+ t[#t+1] = v
+end
+
+local value = P(Carg(1)*value) / set
+local pattern = value*(separator*value)^0 * Carg(1)
+
+function parsers.add_settings_to_array(t,str)
+ return lpegmatch(pattern,str,nil,t)
+end
+
+function parsers.hash_to_string(h,separator,yes,no,strict,omit)
+ if h then
+ local t, tn, s = { }, 0, table.sortedkeys(h)
+ omit = omit and table.tohash(omit)
+ for i=1,#s do
+ local key = s[i]
+ if not omit or not omit[key] then
+ local value = h[key]
+ if type(value) == "boolean" then
+ if yes and no then
+ if value then
+ tn = tn + 1
+ t[tn] = key .. '=' .. yes
+ elseif not strict then
+ tn = tn + 1
+ t[tn] = key .. '=' .. no
+ end
+ elseif value or not strict then
+ tn = tn + 1
+ t[tn] = key .. '=' .. tostring(value)
+ end
+ else
+ tn = tn + 1
+ t[tn] = key .. '=' .. value
+ end
+ end
+ end
+ return concat(t,separator or ",")
+ else
+ return ""
+ end
+end
+
+function parsers.array_to_string(a,separator)
+ if a then
+ return concat(a,separator or ",")
+ else
+ return ""
+ end
+end
+
+function parsers.settings_to_set(str,t) -- tohash? -- todo: lpeg -- duplicate anyway
+ t = t or { }
+-- for s in gmatch(str,"%s*([^, ]+)") do -- space added
+ for s in gmatch(str,"[^, ]+") do -- space added
+ t[s] = true
+ end
+ return t
+end
+
+function parsers.simple_hash_to_string(h, separator)
+ local t, tn = { }, 0
+ for k, v in sortedhash(h) do
+ if v then
+ tn = tn + 1
+ t[tn] = k
+ end
+ end
+ return concat(t,separator or ",")
+end
+
+-- for chem (currently one level)
+
+local value = P(lbrace * C((nobrace + nestedbraces)^0) * rbrace)
+ + C(digit^1 * lparent * (noparent + nestedparents)^1 * rparent)
+ + C((nestedbraces + (1-comma))^1)
+local pattern_a = spaces * Ct(value*(separator*value)^0)
+
+local function repeater(n,str)
+ if not n then
+ return str
+ else
+ local s = lpegmatch(pattern_a,str)
+ if n == 1 then
+ return unpack(s)
+ else
+ local t, tn = { }, 0
+ for i=1,n do
+ for j=1,#s do
+ tn = tn + 1
+ t[tn] = s[j]
+ end
+ end
+ return unpack(t)
+ end
+ end
+end
+
+local value = P(lbrace * C((nobrace + nestedbraces)^0) * rbrace)
+ + (C(digit^1)/tonumber * lparent * Cs((noparent + nestedparents)^1) * rparent) / repeater
+ + C((nestedbraces + (1-comma))^1)
+local pattern_b = spaces * Ct(value*(separator*value)^0)
+
+function parsers.settings_to_array_with_repeat(str,expand) -- beware: "" => { }
+ if expand then
+ return lpegmatch(pattern_b,str) or { }
+ else
+ return lpegmatch(pattern_a,str) or { }
+ end
+end
+
+--
+
+local value = lbrace * C((nobrace + nestedbraces)^0) * rbrace
+local pattern = Ct((space + value)^0)
+
+function parsers.arguments_to_table(str)
+ return lpegmatch(pattern,str)
+end
+
+-- temporary here (unoptimized)
+
+function parsers.getparameters(self,class,parentclass,settings)
+ local sc = self[class]
+ if not sc then
+ sc = { }
+ self[class] = sc
+ if parentclass then
+ local sp = self[parentclass]
+ if not sp then
+ sp = { }
+ self[parentclass] = sp
+ end
+ setmetatableindex(sc,sp)
+ end
+ end
+ parsers.settings_to_hash(settings,sc)
+end
+
+function parsers.listitem(str)
+ return gmatch(str,"[^, ]+")
+end
+
+--
+
+local pattern = Cs { "start",
+ start = V("one") + V("two") + V("three"),
+ rest = (Cc(",") * V("thousand"))^0 * (P(".") + endofstring) * anything^0,
+ thousand = digit * digit * digit,
+ one = digit * V("rest"),
+ two = digit * digit * V("rest"),
+ three = V("thousand") * V("rest"),
+}
+
+lpegpatterns.splitthousands = pattern -- maybe better in the parsers namespace ?
+
+function parsers.splitthousands(str)
+ return lpegmatch(pattern,str) or str
+end
+
+-- print(parsers.splitthousands("11111111111.11"))
+
+local optionalwhitespace = whitespace^0
+
+lpegpatterns.words = Ct((Cs((1-punctuation-whitespace)^1) + anything)^1)
+lpegpatterns.sentences = Ct((optionalwhitespace * Cs((1-period)^0 * period))^1)
+lpegpatterns.paragraphs = Ct((optionalwhitespace * Cs((whitespace^1*endofstring/"" + 1 - (spacer^0*newline*newline))^1))^1)
+
+-- local str = " Word1 word2. \n Word3 word4. \n\n Word5 word6.\n "
+-- inspect(lpegmatch(lpegpatterns.paragraphs,str))
+-- inspect(lpegmatch(lpegpatterns.sentences,str))
+-- inspect(lpegmatch(lpegpatterns.words,str))
+
+-- handy for k="v" [, ] k="v"
+
+local dquote = P('"')
+local equal = P('=')
+local escape = P('\\')
+local separator = S(' ,')
+
+local key = C((1-equal)^1)
+local value = dquote * C((1-dquote-escape*dquote)^0) * dquote
+
+local pattern = Cf(Ct("") * Cg(key * equal * value) * separator^0,rawset)^0 * P(-1)
+
+patterns.keq_to_hash_c = pattern
+
+function parsers.keq_to_hash(str)
+ if str and str ~= "" then
+ return lpegmatch(pattern,str)
+ else
+ return { }
+ end
+end
+
+-- inspect(lpeg.match(pattern,[[key="value"]]))
+
+local defaultspecification = { separator = ",", quote = '"' }
+
+-- this version accepts multiple separators and quotes as used in the
+-- database module
+
+function parsers.csvsplitter(specification)
+ specification = specification and table.setmetatableindex(specification,defaultspecification) or defaultspecification
+ local separator = specification.separator
+ local quotechar = specification.quote
+ local separator = S(separator ~= "" and separator or ",")
+ local whatever = C((1 - separator - newline)^0)
+ if quotechar and quotechar ~= "" then
+ local quotedata = nil
+ for chr in gmatch(quotechar,".") do
+ local quotechar = P(chr)
+ local quoteword = quotechar * C((1 - quotechar)^0) * quotechar
+ if quotedata then
+ quotedata = quotedata + quoteword
+ else
+ quotedata = quoteword
+ end
+ end
+ whatever = quotedata + whatever
+ end
+ local parser = Ct((Ct(whatever * (separator * whatever)^0) * S("\n\r"))^0 )
+ return function(data)
+ return lpegmatch(parser,data)
+ end
+end
+
+-- and this is a slightly patched version of a version posted by Philipp Gesang
+
+-- local mycsvsplitter = utilities.parsers.rfc4180splitter()
+--
+-- local crap = [[
+-- first,second,third,fourth
+-- "1","2","3","4"
+-- "a","b","c","d"
+-- "foo","bar""baz","boogie","xyzzy"
+-- ]]
+--
+-- local list, names = mycsvsplitter(crap,true) inspect(list) inspect(names)
+-- local list, names = mycsvsplitter(crap) inspect(list) inspect(names)
+
+function parsers.rfc4180splitter(specification)
+ specification = specification and table.setmetatableindex(specification,defaultspecification) or defaultspecification
+ local separator = specification.separator --> rfc: COMMA
+ local quotechar = P(specification.quote) --> DQUOTE
+ local dquotechar = quotechar * quotechar --> 2DQUOTE
+ / specification.quote
+ local separator = S(separator ~= "" and separator or ",")
+ local escaped = quotechar
+ * Cs((dquotechar + (1 - quotechar))^0)
+ * quotechar
+ local non_escaped = C((1 - quotechar - newline - separator)^1)
+ local field = escaped + non_escaped
+ local record = Ct((field * separator^-1)^1)
+ local headerline = record * Cp()
+ local wholeblob = Ct((newline^-1 * record)^0)
+ return function(data,getheader)
+ if getheader then
+ local header, position = lpegmatch(headerline,data)
+ local data = lpegmatch(wholeblob,data,position)
+ return data, header
+ else
+ return lpegmatch(wholeblob,data)
+ end
+ end
+end
+
+-- utilities.parsers.stepper("1,7-",9,function(i) print(">>>",i) end)
+-- utilities.parsers.stepper("1-3,7,8,9")
+-- utilities.parsers.stepper("1-3,6,7",function(i) print(">>>",i) end)
+-- utilities.parsers.stepper(" 1 : 3, ,7 ")
+-- utilities.parsers.stepper("1:4,9:13,24:*",30)
+
+local function ranger(first,last,n,action)
+ if not first then
+ -- forget about it
+ elseif last == true then
+ for i=first,n or first do
+ action(i)
+ end
+ elseif last then
+ for i=first,last do
+ action(i)
+ end
+ else
+ action(first)
+ end
+end
+
+local cardinal = lpegpatterns.cardinal / tonumber
+local spacers = lpegpatterns.spacer^0
+local endofstring = lpegpatterns.endofstring
+
+local stepper = spacers * ( C(cardinal) * ( spacers * S(":-") * spacers * ( C(cardinal) + Cc(true) ) + Cc(false) )
+ * Carg(1) * Carg(2) / ranger * S(", ")^0 )^1
+
+local stepper = spacers * ( C(cardinal) * ( spacers * S(":-") * spacers * ( C(cardinal) + (P("*") + endofstring) * Cc(true) ) + Cc(false) )
+ * Carg(1) * Carg(2) / ranger * S(", ")^0 )^1 * endofstring -- we're sort of strict (could do without endofstring)
+
+function parsers.stepper(str,n,action)
+ if type(n) == "function" then
+ lpegmatch(stepper,str,1,false,n or print)
+ else
+ lpegmatch(stepper,str,1,n,action or print)
+ end
+end
+
+--
+
+local pattern_math = Cs((P("%")/"\\percent " + P("^") * Cc("{") * lpegpatterns.integer * Cc("}") + P(1))^0)
+local pattern_text = Cs((P("%")/"\\percent " + (P("^")/"\\high") * Cc("{") * lpegpatterns.integer * Cc("}") + P(1))^0)
+
+patterns.unittotex = pattern
+
+function parsers.unittotex(str,textmode)
+ return lpegmatch(textmode and pattern_text or pattern_math,str)
+end
+
+local pattern = Cs((P("^") / "<sup>" * lpegpatterns.integer * Cc("</sup>") + P(1))^0)
+
+function parsers.unittoxml(str)
+ return lpegmatch(pattern,str)
+end
+
+-- print(utilities.parsers.unittotex("10^-32 %"),utilities.parsers.unittoxml("10^32 %"))
+
+local cache = { }
+local spaces = lpeg.patterns.space^0
+local dummy = function() end
+
+table.setmetatableindex(cache,function(t,k)
+ local separator = P(k)
+ local value = (1-separator)^0
+ local pattern = spaces * C(value) * separator^0 * Cp()
+ t[k] = pattern
+ return pattern
+end)
+
+local commalistiterator = cache[","]
+
+function utilities.parsers.iterator(str,separator)
+ local n = #str
+ if n == 0 then
+ return dummy
+ else
+ local pattern = separator and cache[separator] or commalistiterator
+ local p = 1
+ return function()
+ if p <= n then
+ local s, e = lpegmatch(pattern,str,p)
+ if e then
+ p = e
+ return s
+ end
+ end
+ end
+ end
+end
+
+-- for s in utilities.parsers.iterator("a b c,b,c") do
+-- print(s)
+-- end
+
+local function initialize(t,name)
+ local source = t[name]
+ if source then
+ local result = { }
+ for k, v in next, t[name] do
+ result[k] = v
+ end
+ return result
+ else
+ return { }
+ end
+end
+
+local function fetch(t,name)
+ return t[name] or { }
+end
+
+function process(result,more)
+ for k, v in next, more do
+ result[k] = v
+ end
+ return result
+end
+
+local name = C((1-S(", "))^1)
+local parser = (Carg(1) * name / initialize) * (S(", ")^1 * (Carg(1) * name / fetch))^0
+local merge = Cf(parser,process)
+
+function utilities.parsers.mergehashes(hash,list)
+ return lpegmatch(merge,list,1,hash)
+end
+
+-- local t = {
+-- aa = { alpha = 1, beta = 2, gamma = 3, },
+-- bb = { alpha = 4, beta = 5, delta = 6, },
+-- cc = { epsilon = 3 },
+-- }
+--
+-- inspect(utilities.parsers.mergehashes(t,"aa, bb, cc"))
diff --git a/lualibs-util-sta.lua b/lualibs-util-sta.lua
new file mode 100644
index 0000000..1a61ec4
--- /dev/null
+++ b/lualibs-util-sta.lua
@@ -0,0 +1,342 @@
+if not modules then modules = { } end modules ['util-sta'] = {
+ version = 1.001,
+ comment = "companion to util-ini.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local insert, remove, fastcopy, concat = table.insert, table.remove, table.fastcopy, table.concat
+local format = string.format
+local select, tostring = select, tostring
+
+local trace_stacker = false trackers.register("stacker.resolve", function(v) trace_stacker = v end)
+
+local stacker = stacker or { }
+
+utilities.stacker = stacker
+
+local function start(s,t,first,last)
+ if s.mode == "switch" then
+ local n = tostring(t[last])
+ if trace_stacker then
+ s.report("start: %s",n)
+ end
+ return n
+ else
+ local r = { }
+ for i=first,last do
+ r[#r+1] = tostring(t[i])
+ end
+ local n = concat(r," ")
+ if trace_stacker then
+ s.report("start: %s",n)
+ end
+ return n
+ end
+end
+
+local function stop(s,t,first,last)
+ if s.mode == "switch" then
+ local n = tostring(false)
+ if trace_stacker then
+ s.report("stop: %s",n)
+ end
+ return n
+ else
+ local r = { }
+ for i=last,first,-1 do
+ r[#r+1] = tostring(false)
+ end
+ local n = concat(r," ")
+ if trace_stacker then
+ s.report("stop: %s",n)
+ end
+ return n
+ end
+end
+
+local function change(s,t1,first1,last1,t2,first2,last2)
+ if s.mode == "switch" then
+ local n = tostring(t2[last2])
+ if trace_stacker then
+ s.report("change: %s",n)
+ end
+ return n
+ else
+ local r = { }
+ for i=last1,first1,-1 do
+ r[#r+1] = tostring(false)
+ end
+ local n = concat(r," ")
+ for i=first2,last2 do
+ r[#r+1] = tostring(t2[i])
+ end
+ if trace_stacker then
+ s.report("change: %s",n)
+ end
+ return n
+ end
+end
+
+function stacker.new(name)
+
+ local s
+
+ local stack = { }
+ local list = { }
+ local ids = { }
+ local hash = { }
+
+ local hashing = true
+
+ local function push(...)
+ for i=1,select("#",...) do
+ insert(stack,(select(i,...))) -- watch the ()
+ end
+ if hashing then
+ local c = concat(stack,"|")
+ local n = hash[c]
+ if not n then
+ n = #list+1
+ hash[c] = n
+ list[n] = fastcopy(stack)
+ end
+ insert(ids,n)
+ return n
+ else
+ local n = #list+1
+ list[n] = fastcopy(stack)
+ insert(ids,n)
+ return n
+ end
+ end
+
+ local function pop()
+ remove(stack)
+ remove(ids)
+ return ids[#ids] or s.unset or -1
+ end
+
+ local function clean()
+ if #stack == 0 then
+ if trace_stacker then
+ s.report("%s list entries, %s stack entries",#list,#stack)
+ end
+ end
+ end
+
+ local tops = { }
+ local top, switch
+
+ local function resolve_begin(mode)
+ if mode then
+ switch = mode == "switch"
+ else
+ switch = s.mode == "switch"
+ end
+ top = { switch = switch }
+ insert(tops,top)
+ end
+
+ local function resolve_step(ti) -- keep track of changes outside function !
+ -- todo: optimize for n=1 etc
+ local result = nil
+ local noftop = #top
+ if ti > 0 then
+ local current = list[ti]
+ if current then
+ local noflist = #current
+ local nofsame = 0
+ if noflist > noftop then
+ for i=1,noflist do
+ if current[i] == top[i] then
+ nofsame = i
+ else
+ break
+ end
+ end
+ else
+ for i=1,noflist do
+ if current[i] == top[i] then
+ nofsame = i
+ else
+ break
+ end
+ end
+ end
+ local plus = nofsame + 1
+ if plus <= noftop then
+ if plus <= noflist then
+ if switch then
+ result = s.change(s,top,plus,noftop,current,nofsame,noflist)
+ else
+ result = s.change(s,top,plus,noftop,current,plus,noflist)
+ end
+ else
+ if switch then
+ result = s.change(s,top,plus,noftop,current,nofsame,noflist)
+ else
+ result = s.stop(s,top,plus,noftop)
+ end
+ end
+ elseif plus <= noflist then
+ if switch then
+ result = s.start(s,current,nofsame,noflist)
+ else
+ result = s.start(s,current,plus,noflist)
+ end
+ end
+ top = current
+ else
+ if 1 <= noftop then
+ result = s.stop(s,top,1,noftop)
+ end
+ top = { }
+ end
+ return result
+ else
+ if 1 <= noftop then
+ result = s.stop(s,top,1,noftop)
+ end
+ top = { }
+ return result
+ end
+ end
+
+ local function resolve_end()
+ -- resolve_step(s.unset)
+ local noftop = #top
+ if noftop > 0 then
+ local result = s.stop(s,top,1,#top)
+ remove(tops)
+ top = tops[#tops]
+ switch = top and top.switch
+ return result
+ end
+ end
+
+ local function resolve(t)
+ resolve_begin()
+ for i=1,#t do
+ resolve_step(t[i])
+ end
+ resolve_end()
+ end
+
+ local report = logs.reporter("stacker",name or nil)
+
+ s = {
+ name = name or "unknown",
+ unset = -1,
+ report = report,
+ start = start,
+ stop = stop,
+ change = change,
+ push = push,
+ pop = pop,
+ clean = clean,
+ resolve = resolve,
+ resolve_begin = resolve_begin,
+ resolve_step = resolve_step,
+ resolve_end = resolve_end,
+ }
+
+ return s -- we can overload functions
+
+end
+
+-- local s = utilities.stacker.new("demo")
+--
+-- local unset = s.unset
+-- local push = s.push
+-- local pop = s.pop
+--
+-- local t = {
+-- unset,
+-- unset,
+-- push("a"), -- a
+-- push("b","c"), -- a b c
+-- pop(), -- a b
+-- push("d"), -- a b d
+-- pop(), -- a b
+-- unset,
+-- pop(), -- a
+-- pop(), -- b
+-- unset,
+-- unset,
+-- }
+--
+-- s.resolve(t)
+
+-- demostacker = utilities.stacker.new("demos")
+--
+-- local whatever = {
+-- one = "1 0 0 RG 1 0 0 rg",
+-- two = "1 1 0 RG 1 1 0 rg",
+-- [false] = "0 G 0 g",
+-- }
+--
+-- local concat = table.concat
+--
+-- local pdfliteral = nodes.pool.pdfliteral
+--
+-- function demostacker.start(s,t,first,last)
+-- local n = whatever[t[last]]
+-- -- s.report("start: %s",n)
+-- return pdfliteral(n)
+-- end
+--
+-- function demostacker.stop(s,t,first,last)
+-- local n = whatever[false]
+-- -- s.report("stop: %s",n)
+-- return pdfliteral(n)
+-- end
+--
+-- function demostacker.change(s,t1,first1,last1,t2,first2,last2)
+-- local n = whatever[t2[last2]]
+-- -- s.report("change: %s",n)
+-- return pdfliteral(n)
+-- end
+--
+-- demostacker.mode = "switch"
+--
+-- local whatever = {
+-- one = "/OC /test1 BDC",
+-- two = "/OC /test2 BDC",
+-- [false] = "EMC",
+-- }
+--
+-- demostacker = utilities.stacker.new("demos")
+--
+-- function demostacker.start(s,t,first,last)
+-- local r = { }
+-- for i=first,last do
+-- r[#r+1] = whatever[t[i]]
+-- end
+-- -- s.report("start: %s",concat(r," "))
+-- return pdfliteral(concat(r," "))
+-- end
+--
+-- function demostacker.stop(s,t,first,last)
+-- local r = { }
+-- for i=last,first,-1 do
+-- r[#r+1] = whatever[false]
+-- end
+-- -- s.report("stop: %s",concat(r," "))
+-- return pdfliteral(concat(r," "))
+-- end
+--
+-- function demostacker.change(s,t1,first1,last1,t2,first2,last2)
+-- local r = { }
+-- for i=last1,first1,-1 do
+-- r[#r+1] = whatever[false]
+-- end
+-- for i=first2,last2 do
+-- r[#r+1] = whatever[t2[i]]
+-- end
+-- -- s.report("change: %s",concat(r," "))
+-- return pdfliteral(concat(r," "))
+-- end
+--
+-- demostacker.mode = "stack"
diff --git a/lualibs-util-tpl.lua b/lualibs-util-tpl.lua
new file mode 100644
index 0000000..7a6abef
--- /dev/null
+++ b/lualibs-util-tpl.lua
@@ -0,0 +1,174 @@
+if not modules then modules = { } end modules ['util-tpl'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+-- This is experimental code. Coming from dos and windows, I've always used %whatever%
+-- as template variables so let's stick to it. After all, it's easy to parse and stands
+-- out well. A double %% is turned into a regular %.
+
+utilities.templates = utilities.templates or { }
+local templates = utilities.templates
+
+local trace_template = false trackers.register("templates.trace",function(v) trace_template = v end)
+local report_template = logs.reporter("template")
+
+local tostring = tostring
+local format, sub = string.format, string.sub
+local P, C, Cs, Carg, lpegmatch = lpeg.P, lpeg.C, lpeg.Cs, lpeg.Carg, lpeg.match
+
+-- todo: make installable template.new
+
+local replacer
+
+local function replacekey(k,t,how,recursive)
+ local v = t[k]
+ if not v then
+ if trace_template then
+ report_template("unknown key %a",k)
+ end
+ return ""
+ else
+ v = tostring(v)
+ if trace_template then
+ report_template("setting key %a to value %a",k,v)
+ end
+ if recursive then
+ return lpegmatch(replacer,v,1,t,how,recursive)
+ else
+ return v
+ end
+ end
+end
+
+local sqlescape = lpeg.replacer {
+ { "'", "''" },
+ { "\\", "\\\\" },
+ { "\r\n", "\\n" },
+ { "\r", "\\n" },
+ -- { "\t", "\\t" },
+}
+
+local sqlquotedescape = lpeg.Cs(lpeg.Cc("'") * sqlescape * lpeg.Cc("'"))
+
+-- escapeset : \0\1\2\3\4\5\6\7\8\9\10\11\12\13\14\15\16\17\18\19\20\21\22\23\24\25\26\27\28\29\30\31\"\\\127
+-- test string: [[1\0\31test23"\\]] .. string.char(19) .. "23"
+--
+-- slow:
+--
+-- local luaescape = lpeg.replacer {
+-- { '"', [[\"]] },
+-- { '\\', [[\\]] },
+-- { R("\0\9") * #R("09"), function(s) return "\\00" .. byte(s) end },
+-- { R("\10\31") * #R("09"), function(s) return "\\0" .. byte(s) end },
+-- { R("\0\31") , function(s) return "\\" .. byte(s) end },
+-- }
+--
+-- slightly faster:
+--
+-- local luaescape = Cs ((
+-- P('"' ) / [[\"]] +
+-- P('\\') / [[\\]] +
+-- Cc("\\00") * (R("\0\9") / byte) * #R("09") +
+-- Cc("\\0") * (R("\10\31") / byte) * #R("09") +
+-- Cc("\\") * (R("\0\31") / byte) +
+-- P(1)
+-- )^0)
+
+local escapers = {
+ lua = function(s)
+ return sub(format("%q",s),2,-2)
+ end,
+ sql = function(s)
+ return lpegmatch(sqlescape,s)
+ end,
+}
+
+local quotedescapers = {
+ lua = function(s)
+ return format("%q",s)
+ end,
+ sql = function(s)
+ return lpegmatch(sqlquotedescape,s)
+ end,
+}
+
+lpeg.patterns.sqlescape = sqlescape
+lpeg.patterns.sqlescape = sqlquotedescape
+
+local luaescaper = escapers.lua
+local quotedluaescaper = quotedescapers.lua
+
+local function replacekeyunquoted(s,t,how,recurse) -- ".. \" "
+ local escaper = how and escapers[how] or luaescaper
+ return escaper(replacekey(s,t,how,recurse))
+end
+
+local function replacekeyquoted(s,t,how,recurse) -- ".. \" "
+ local escaper = how and quotedescapers[how] or quotedluaescaper
+ return escaper(replacekey(s,t,how,recurse))
+end
+
+local single = P("%") -- test %test% test : resolves test
+local double = P("%%") -- test 10%% test : %% becomes %
+local lquoted = P("%[") -- test '%[test]%' test : resolves to test with escaped "'s
+local rquoted = P("]%") --
+local lquotedq = P("%(") -- test %(test)% test : resolves to 'test' with escaped "'s
+local rquotedq = P(")%") --
+
+local escape = double / '%%'
+local nosingle = single / ''
+local nodouble = double / ''
+local nolquoted = lquoted / ''
+local norquoted = rquoted / ''
+local nolquotedq = lquotedq / ''
+local norquotedq = rquotedq / ''
+
+local key = nosingle * ((C((1-nosingle )^1) * Carg(1) * Carg(2) * Carg(3)) / replacekey ) * nosingle
+local quoted = nolquotedq * ((C((1-norquotedq)^1) * Carg(1) * Carg(2) * Carg(3)) / replacekeyquoted ) * norquotedq
+local unquoted = nolquoted * ((C((1-norquoted )^1) * Carg(1) * Carg(2) * Carg(3)) / replacekeyunquoted) * norquoted
+local any = P(1)
+
+ replacer = Cs((unquoted + quoted + escape + key + any)^0)
+
+local function replace(str,mapping,how,recurse)
+ if mapping and str then
+ return lpegmatch(replacer,str,1,mapping,how or "lua",recurse or false) or str
+ else
+ return str
+ end
+end
+
+-- print(replace("test '%[x]%' test",{ x = [[a 'x'  a]] }))
+-- print(replace("test '%[x]%' test",{ x = true }))
+-- print(replace("test '%[x]%' test",{ x = [[a 'x'  a]], y = "oeps" },'sql'))
+-- print(replace("test '%[x]%' test",{ x = [[a '%y%'  a]], y = "oeps" },'sql',true))
+-- print(replace([[test %[x]% test]],{ x = [[a "x"  a]]}))
+-- print(replace([[test %(x)% test]],{ x = [[a "x"  a]]}))
+
+templates.replace = replace
+
+function templates.load(filename,mapping,how,recurse)
+ local data = io.loaddata(filename) or ""
+ if mapping and next(mapping) then
+ return replace(data,mapping,how,recurse)
+ else
+ return data
+ end
+end
+
+function templates.resolve(t,mapping,how,recurse)
+ if not mapping then
+ mapping = t
+ end
+ for k, v in next, t do
+ t[k] = replace(v,mapping,how,recurse)
+ end
+ return t
+end
+
+-- inspect(utilities.templates.replace("test %one% test", { one = "%two%", two = "two" }))
+-- inspect(utilities.templates.resolve({ one = "%two%", two = "two", three = "%three%" }))
diff --git a/lualibs.dtx b/lualibs.dtx
index e9a20a6..71f7b53 100644
--- a/lualibs.dtx
+++ b/lualibs.dtx
@@ -259,7 +259,7 @@ require("lualibs-util-sto")
require("lualibs-util-dim")
require("lualibs-util-jsn")
--require("lualibs-util-mrg")-- not required
-require("lualibs-util-lua")
+--require("lualibs-util-lua")
% \end{macrocode}
%
% \iffalse
diff --git a/lualibs.lua b/lualibs.lua
index cf9b039..f126fb7 100644
--- a/lualibs.lua
+++ b/lualibs.lua
@@ -1,54 +1,78 @@
---
-- This is file `lualibs.lua',
--- generated with the docstrip utility.
---
--- The original source files were:
---
--- lualibs.dtx (with options: `lua')
--- This is a generated file.
---
--- Copyright (C) 2009 by PRAGMA ADE / ConTeXt Development Team
---
--- See ConTeXt's mreadme.pdf for the license.
---
--- This work consists of the main source file lualibs.dtx
--- and the derived file lualibs.lua.
---
module('lualibs', package.seeall)
local lualibs_module = {
- name = "lualibs",
- version = 0.97,
- date = "2012/10/19",
- description = "Lua additional functions.",
- author = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux",
- copyright = "PRAGMA ADE / ConTeXt Development Team",
- license = "See ConTeXt's mreadme.pdf for the license",
+ name = "lualibs",
+ version = 1.01,
+ date = "2013/04/10",
+ description = "Lua additional functions.",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "See ConTeXt's mreadme.pdf for the license",
}
-if luatexbase and luatexbase.provides_module then
- luatexbase.provides_module(lualibs_module)
+_G.config = _G.config or { }
+_G.config.lualibs = _G.config.lualibs or { }
+local lualibs = _G.config.lualibs
+
+if lualibs.prefer_merged == nil then lualibs.prefer_merged = true end
+if lualibs.load_extended == nil then lualibs.load_extended = true end
+lualibs.verbose = lualibs.verbose == true or false
+
+local lpeg, kpse = lpeg, kpse
+
+local dofile = dofile
+local lpegmatch = lpeg.match
+local stringformat = string.format
+
+local find_file, error, warn, info
+do
+ local _error, _warn, _info
+ if luatexbase and luatexbase.provides_module then
+ _error, _warn, _info = luatexbase.provides_module(lualibs_module)
+ else
+ _error, _warn, _info = texio.write_nl, texio.write_nl, texio.write_nl -- stub
+ end
+
+ -- if lualibs.verbose then
+ if lualibs.verbose then
+ error, warn, info = _error, _warn, _info
+ else
+ local dummylogger = function ( ) end
+ error, warn, info = _error, dummylogger, dummylogger
+ end
+ lualibs.error, lualibs.warn, lualibs.info = error, warn, info
+end
+
+if luatexbase and luatexbase.find_file then
+ find_file = luatexbase.find_file
+else
+ kpse.set_program_name"luatex"
+ find_file = kpse.find_file
end
-require("lualibs-string")
-require("lualibs-lpeg")
-require("lualibs-boolean")
-require("lualibs-number")
-require("lualibs-math")
-require("lualibs-table")
-require("lualibs-io")
-require("lualibs-os")
-require("lualibs-file")
-require("lualibs-md5")
-require("lualibs-dir")
-require("lualibs-unicode")
-require("lualibs-url")
-require("lualibs-set")
-require("lualibs-util-lua")
-require("lualibs-util-sto")
-require("lualibs-util-mrg")
-require("lualibs-util-dim")
-require("lualibs-util-str")
-require("lualibs-util-tab")
-require("lualibs-util-jsn")
---
--- End of File `lualibs.lua'.
+
+loadmodule = _G.loadmodule or function (name, t)
+ if not t then t = "library" end
+ local filepath = kpse.find_file(name, "lua")
+ if not filepath or filepath == "" then
+ warn(stringformat("Could not locate %s “%s”.", t, name))
+ return false
+ end
+ dofile(filepath)
+ return true
+end
+lualibs.loadmodule = loadmodule
+
+--[[doc--
+The separation of the “basic” from the “extended” sets coincides with
+the split into luat-bas.mkiv and luat-lib.mkiv.
+--doc]]--
+loadmodule"lualibs-basic.lua"
+loadmodule"lualibs-compat.lua" --- restore stuff gone since v1.*
+
+if load_extended == true then
+ loadmodule"lualibs-extended.lua"
+end
+
+-- vim:tw=71:sw=2:ts=2:expandtab
+-- End of File `lualibs-basic.lua'.