summaryrefslogtreecommitdiff
path: root/lualibs.lua
blob: 7255147cff0dce001c7783f6bbd58e354d4e73fe (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
--  This is file `lualibs.lua',
module('lualibs', package.seeall)

local lualibs_module = {
  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",
}

local prefer_merged = true -- | false --- TODO should be set in some global config

local lpeg, kpse = lpeg, kpse

local dofile       = dofile
local lpegmatch    = lpeg.match
local stringformat = string.format

local find_file, 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 luatexbase and luatexbase.find_file then
  find_file = luatexbase.find_file
else
  kpse.set_program_name"luatex"
  find_file = kpse.find_file
end

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

local merged_suffix = "-merged.lua"

local p_suffix      = lpeg.P".lua" * lpeg.P(-1)
local p_nosuffix    = (1 - p_suffix)^0
local p_hassuffix   = (p_nosuffix) * p_suffix
local p_stripsuffix = lpeg.C(p_nosuffix) * p_suffix

local loadmerged = function (basename)
  basename = lualibs_module.name .. "-" .. basename
  if not lpegmatch(p_hassuffix, basename) then -- force .lua suffix
    basename = basename .. ".lua"
  end
  local res
  if prefer_merged then
    local mergedname = lpegmatch(p_stripsuffix, basename) .. merged_suffix
    res = loadmodule(mergedname, "merged package")
  else
    info"Ignoring merged packages."
  end
  if not res then -- package not present, load individual libs
    info(stringformat("Falling back to “%s”.", basename))
    res = loadmodule(basename, "metapackage")
  end
  if res == false then
    error(stringformat("Could not load metapackage “%s”.", basename))
  end
end

--[[doc--
The separation of the “basic” from the “extended” sets coincides with
the split into luat-bas.mkiv and luat-lib.mkiv.
--doc]]--
loadmerged"basic.lua"
--inspect(table.keys(table))
--inspect(table.keys(string))
loadmerged"extended.lua"

--- merge generates wrong syntax for this one, so we load it separately
--- http://www.ntg.nl/pipermail/ntg-context/2013/072336.html
--loadmodule"util-dim"

io.write"\n"
-- 
-- vim:tw=71:sw=2:ts=2:expandtab
--  End of File `lualibs-basic.lua'.