summaryrefslogtreecommitdiff
path: root/scripts/context/lua/mtxlibs.lua
blob: 9c8cd28caa4f4194aa8b1706c8a27c2067608ba5 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
if not modules then modules = { } end modules ['mtxlibs'] = {
    version   = 1.001,
    comment   = "a reasonable subset of mtxrun preloaded libraries",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- This file can be used to load a the (relevant) helper libraries that are also used
-- in ConTeXt. You can use it as:
--
--   -- if needed (outside texlua):
--
--   -- require("lpeg")      -- mandate
--   -- require("md5")       -- handy
--   -- require("lfs")       -- recommended
--   -- require("slunicode") -- sort of obsolete
--
--   -- the library:
--
--   require("mtxlibs")
--
-- An alternative is to merge all libraries into this one so that you don't have to
-- distribute them.
--
--   mtxlibs --selfmerge
--
-- If you need additional libraries, you can do something like this:
--
--   lua mtxlibs.lua  --selfmerge  my-web-project.lua  trac-lmx util-jsn
--   lua mtxlibs.lua  --selfmerge  my-sql-project.lua  util-sql util-sql-imp-library util-sql-imp-client
--
-- That way you only need to update one file in a project and are not dependent on changes
-- in the core ConTeXT libraries. The libraries are maintained as part of ConTeXt and used
-- in projects so relative stable. The code works in Lua 5.1 as well as in 5.2. Not all
-- functionality makes sense for users who are not familiar with ConTeXt but for instance
-- trackers and loggers are included because that way we have can provide users with a
-- consistent ecosystem.
--
-- Much of the provided functionality is described in cld-mkiv.pdf and related manuals, on
-- contextgarden.net as well in articles. The XML subsystem is described in its own manual.
-- Templates and SQL (not preloaded) is also has its own manual.
--
-- The next section contains the merged code, with each block ending uop in its own
-- closure. The code gets somewhat compacted to save space and speed up loading.
--
-- There are some dependencies between the several modules. Also, quite some functions are added
-- to the regular Lua namespaces. In due time I'll isolate them in their own namespaces but with
-- the for context handy option to expose them in the normal ones. I might make the dependencies
-- less but it probably makes no sense to waste time on them.

xpcall(function() local _, t = require("lpeg")      if t then lpeg     = t end return  end,function() end)
xpcall(function() local _, t = require("md5")       if t then md5      = t end return  end,function() end)
xpcall(function() local _, t = require("lfs")       if t then lfs      = t end return  end,function() end)
xpcall(function() local _, t = require("slunicode") if t then unicode  = t end return  end,function() end)

-- begin library merge

-- end library merge

local gsub, gmatch, match, find = string.gsub, string.gmatch, string.match, string.find
local concat = table.concat

local ownname = arg and arg[0] or 'mtxlibs.lua'
local ownpath = gsub(match(ownname,"^(.+)[\\/].-$") or ".","\\","/")
local owntree = ownpath

local ownlibs = {

    "l-lua.lua",
    "l-macro.lua",
    "l-sandbox.lua",
    "l-package.lua",
    "l-lpeg.lua",
    "l-function.lua",
    "l-string.lua",
    "l-table.lua",
    "l-io.lua",
    "l-number.lua",
    "l-set.lua",
    "l-os.lua",
    "l-file.lua",     -- limited functionality when no lfs
 -- "l-gzip.lua",
    "l-md5.lua",      -- not loaded when no md5 library
    "l-sha.lua",      -- not loaded when no sha2 library
    "l-url.lua",
    "l-dir.lua",      -- limited functionality when no lfs
    "l-boolean.lua",
    "l-unicode.lua",  -- nowadays independent of slunicode
    "l-math.lua",

    "util-str.lua",
    "util-tab.lua",
    "util-fil.lua",
    "util-sac.lua",
    "util-sto.lua",
 -- "util-lua.lua", -- no need for compiling
    "util-prs.lua",
 -- "util-fmt.lua", -- no need for table formatters
 -- "util-deb.lua", -- no need for debugging (and tracing)

    "util-soc-imp-reset",
    "util-soc-imp-socket",
    "util-soc-imp-copas",
    "util-soc-imp-ltn12",
 -- "util-soc-imp-mbox",
    "util-soc-imp-mime",
    "util-soc-imp-url",
    "util-soc-imp-headers",
    "util-soc-imp-tp",
    "util-soc-imp-http",
    "util-soc-imp-ftp",
    "util-soc-imp-smtp",

    "trac-set.lua",
    "trac-log.lua",
 -- "trac-pro.lua",  -- not relevant outside context
    "trac-inf.lua",

    "util-mrg.lua",
    "util-tpl.lua",
    "util-sbx.lua",

    "util-env.lua",
 -- "luat-env.lua",  -- not relevant outside context

    "lxml-tab.lua",
    "lxml-lpt.lua",
    "lxml-mis.lua",
    "lxml-aux.lua",
    "lxml-xml.lua",

    "trac-xml.lua",  -- handy for helpinfo
}

package.path = "t:/sources/?.lua;t:/sources/?;" .. package.path

local ownlist = {
    '.',
    ownpath ,
    ownpath .. "/../sources", -- HH's development path
    --
    owntree .. "/../../texmf-local/tex/context/base/mkiv",
    owntree .. "/../../texmf-context/tex/context/base/mkiv",
    owntree .. "/../../texmf/tex/context/base/mkiv",
    owntree .. "/../../../texmf-local/tex/context/base/mkiv",
    owntree .. "/../../../texmf-context/tex/context/base/mkiv",
    owntree .. "/../../../texmf/tex/context/base/mkiv",
    --
    owntree .. "/../../texmf-local/tex/context/base",
    owntree .. "/../../texmf-context/tex/context/base",
    owntree .. "/../../texmf/tex/context/base",
    owntree .. "/../../../texmf-local/tex/context/base",
    owntree .. "/../../../texmf-context/tex/context/base",
    owntree .. "/../../../texmf/tex/context/base",
}

if ownpath == "." then table.remove(ownlist,1) end

own = {
    name = ownname,
    path = ownpath,
    tree = owntree,
    list = ownlist,
    libs = ownlibs,
}

local function locate_libs()
    local name = ownlibs[1]
    local done = false
    for i=1,#ownlist do
        local path = ownlist[i]
        local filename = path .. "/" .. name
        local f = io.open(filename)
        if f then
            f:close()
            package.path = package.path .. ";" .. path .. "/?.lua" -- in case l-* does a require
            done = path
            break
        end
    end
    locate_libs = function() return done end
    return done
end

local function load_libs()
    local found = locate_libs()
    if found then
        for i=1,#ownlibs do
            local basename = ownlibs[i]
            local filename = found .. "/" .. basename
            local codeblob = loadfile(filename)
            if codeblob then
                package.preload[basename] = codeblob() or true
            end
        end
    end
end

if not unicode then
    load_libs()
end

local merger = utilities and utilities.merger

if not merger then
    return
end

local arguments = environment.arguments
local files     = environment.files

local ownname   = file.basename(environment.ownname)

local helpinfo = [[
usage: mtxlibs [options]

--selfmerge
--selfmerge targetfile extralibs
--selfclean

and in a lua file:

require("mtxlibs")
]]

local application = logs.application {
    name     = "mtxlibs",
    banner   = "ConTeXt Basic Lua Libraries 1.00",
    helpinfo = helpinfo,
}

local report = application.report

if ownname == "mtxrun" or ownname == "mtxrun.lua" then
    -- we're using mtxrun
    ownname = "mtxlibs.lua"
elseif ownname == "mtxlibs" or ownname == "mtxlibs.lua" then
    -- we're using lua
    ownname = "mtxlibs.lua"
else
    report("usage : lua mtxlibs.lua ...")
    report("      : mtxrun --script mtxlibs.lua ...")
    return
end

if arguments.selfmerge then

    report("merging libraries")
    local found = locate_libs()
    if found then
        local target = files[1]
        if target == ownname then
            report("target cannot be this file")
            return
        elseif target then
            report("target: %s",target)
            for i=1,#files do
                ownlibs[#ownlibs+1] = file.addsuffix(files[i],"lua")
            end
        end
        merger.selfmerge(ownname,ownlibs,{ found },target)
        report("done")
    else
        report("no libraries found")
    end

elseif arguments.selfclean then

    report("cleaning libraries")
    merger.selfclean(ownname)
    report("done")

else -- if arguments.help or files[1] == "help" then

    application.help()

end