mtxlibs.lua /size: 8258 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mtxlibs'] = {
2    version   = 1.001,
3    comment   = "a reasonable subset of mtxrun preloaded libraries",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- This file can be used to load a the (relevant) helper libraries that are also used
10-- in ConTeXt. You can use it as:
11--
12--   -- if needed (outside texlua):
13--
14--   -- require("lpeg")      -- mandate
15--   -- require("md5")       -- handy
16--   -- require("lfs")       -- recommended
17--   -- require("slunicode") -- sort of obsolete
18--
19--   -- the library:
20--
21--   require("mtxlibs")
22--
23-- An alternative is to merge all libraries into this one so that you don't have to
24-- distribute them.
25--
26--   mtxlibs --selfmerge
27--
28-- If you need additional libraries, you can do something like this:
29--
30--   lua mtxlibs.lua  --selfmerge  my-web-project.lua  trac-lmx util-jsn
31--   lua mtxlibs.lua  --selfmerge  my-sql-project.lua  util-sql util-sql-imp-library util-sql-imp-client
32--
33-- That way you only need to update one file in a project and are not dependent on changes
34-- in the core ConTeXT libraries. The libraries are maintained as part of ConTeXt and used
35-- in projects so relative stable. The code works in Lua 5.1 as well as in 5.2. Not all
36-- functionality makes sense for users who are not familiar with ConTeXt but for instance
37-- trackers and loggers are included because that way we have can provide users with a
38-- consistent ecosystem.
39--
40-- Much of the provided functionality is described in cld-mkiv.pdf and related manuals, on
41-- contextgarden.net as well in articles. The XML subsystem is described in its own manual.
42-- Templates and SQL (not preloaded) is also has its own manual.
43--
44-- The next section contains the merged code, with each block ending uop in its own
45-- closure. The code gets somewhat compacted to save space and speed up loading.
46--
47-- There are some dependencies between the several modules. Also, quite some functions are added
48-- to the regular Lua namespaces. In due time I'll isolate them in their own namespaces but with
49-- the for context handy option to expose them in the normal ones. I might make the dependencies
50-- less but it probably makes no sense to waste time on them.
51
52xpcall(function() local _, t = require("lpeg")      if t then lpeg     = t end return  end,function() end)
53xpcall(function() local _, t = require("md5")       if t then md5      = t end return  end,function() end)
54xpcall(function() local _, t = require("lfs")       if t then lfs      = t end return  end,function() end)
55xpcall(function() local _, t = require("slunicode") if t then unicode  = t end return  end,function() end)
56
57-- begin library merge
58
59-- end library merge
60
61local gsub, gmatch, match, find = string.gsub, string.gmatch, string.match, string.find
62local concat = table.concat
63
64local ownname = arg and arg[0] or 'mtxlibs.lua'
65local ownpath = gsub(match(ownname,"^(.+)[\\/].-$") or ".","\\","/")
66local owntree = ownpath
67
68local ownlibs = {
69
70    "l-bit32.lua",
71    "l-lua.lua",
72    "l-macro.lua",
73    "l-sandbox.lua",
74    "l-package.lua",
75    "l-lpeg.lua",
76    "l-function.lua",
77    "l-string.lua",
78    "l-table.lua",
79    "l-io.lua",
80    "l-number.lua",
81    "l-set.lua",
82    "l-os.lua",
83    "l-file.lua",     -- limited functionality when no lfs
84 -- "l-gzip.lua",
85    "l-md5.lua",      -- not loaded when no md5 library
86    "l-sha.lua",      -- not loaded when no sha2 library
87    "l-url.lua",
88    "l-dir.lua",      -- limited functionality when no lfs
89    "l-boolean.lua",
90    "l-unicode.lua",  -- nowadays independent of slunicode
91    "l-math.lua",
92
93    "util-str.lua",
94    "util-tab.lua",
95    "util-fil.lua",
96    "util-sac.lua",
97    "util-sto.lua",
98 -- "util-lua.lua", -- no need for compiling
99    "util-prs.lua",
100 -- "util-fmt.lua", -- no need for table formatters
101 -- "util-deb.lua", -- no need for debugging (and tracing)
102
103    "util-soc-imp-reset",
104    "util-soc-imp-socket",
105    "util-soc-imp-copas",
106    "util-soc-imp-ltn12",
107 -- "util-soc-imp-mbox",
108    "util-soc-imp-mime",
109    "util-soc-imp-url",
110    "util-soc-imp-headers",
111    "util-soc-imp-tp",
112    "util-soc-imp-http",
113    "util-soc-imp-ftp",
114    "util-soc-imp-smtp",
115
116    "trac-set.lua",
117    "trac-log.lua",
118 -- "trac-pro.lua",  -- not relevant outside context
119    "trac-inf.lua",
120
121    "util-mrg.lua",
122    "util-tpl.lua",
123    "util-sbx.lua",
124
125    "util-env.lua",
126 -- "luat-env.lua",  -- not relevant outside context
127
128    "lxml-tab.lua",
129    "lxml-lpt.lua",
130    "lxml-mis.lua",
131    "lxml-aux.lua",
132    "lxml-xml.lua",
133
134    "trac-xml.lua",  -- handy for helpinfo
135}
136
137package.path = "t:/sources/?.lua;t:/sources/?;" .. package.path
138
139local ownlist = {
140    '.',
141    ownpath ,
142    ownpath .. "/../sources", -- HH's development path
143    --
144    owntree .. "/../../texmf-local/tex/context/base/mkiv",
145    owntree .. "/../../texmf-context/tex/context/base/mkiv",
146    owntree .. "/../../texmf/tex/context/base/mkiv",
147    owntree .. "/../../../texmf-local/tex/context/base/mkiv",
148    owntree .. "/../../../texmf-context/tex/context/base/mkiv",
149    owntree .. "/../../../texmf/tex/context/base/mkiv",
150    --
151    owntree .. "/../../texmf-local/tex/context/base",
152    owntree .. "/../../texmf-context/tex/context/base",
153    owntree .. "/../../texmf/tex/context/base",
154    owntree .. "/../../../texmf-local/tex/context/base",
155    owntree .. "/../../../texmf-context/tex/context/base",
156    owntree .. "/../../../texmf/tex/context/base",
157}
158
159if ownpath == "." then table.remove(ownlist,1) end
160
161own = {
162    name = ownname,
163    path = ownpath,
164    tree = owntree,
165    list = ownlist,
166    libs = ownlibs,
167}
168
169local function locate_libs()
170    local name = ownlibs[1]
171    local done = false
172    for i=1,#ownlist do
173        local path = ownlist[i]
174        local filename = path .. "/" .. name
175        local f = io.open(filename)
176        if f then
177            f:close()
178            package.path = package.path .. ";" .. path .. "/?.lua" -- in case l-* does a require
179            done = path
180            break
181        end
182    end
183    locate_libs = function() return done end
184    return done
185end
186
187local function load_libs()
188    local found = locate_libs()
189    if found then
190        for i=1,#ownlibs do
191            local basename = ownlibs[i]
192            local filename = found .. "/" .. basename
193            local codeblob = loadfile(filename)
194            if codeblob then
195                package.preload[basename] = codeblob() or true
196            end
197        end
198    end
199end
200
201if not unicode then
202    load_libs()
203end
204
205local merger = utilities and utilities.merger
206
207if not merger then
208    return
209end
210
211local arguments = environment.arguments
212local files     = environment.files
213
214local ownname   = file.basename(environment.ownname)
215
216local helpinfo = [[
217usage: mtxlibs [options]
218
219--selfmerge
220--selfmerge targetfile extralibs
221--selfclean
222
223and in a lua file:
224
225require("mtxlibs")
226]]
227
228local application = logs.application {
229    name     = "mtxlibs",
230    banner   = "ConTeXt Basic Lua Libraries 1.00",
231    helpinfo = helpinfo,
232}
233
234local report = application.report
235
236if ownname == "mtxrun" or ownname == "mtxrun.lua" then
237    -- we're using mtxrun
238    ownname = "mtxlibs.lua"
239elseif ownname == "mtxlibs" or ownname == "mtxlibs.lua" then
240    -- we're using lua
241    ownname = "mtxlibs.lua"
242else
243    report("usage : lua mtxlibs.lua ...")
244    report("      : mtxrun --script mtxlibs.lua ...")
245    return
246end
247
248if arguments.selfmerge then
249
250    report("merging libraries")
251    local found = locate_libs()
252    if found then
253        local target = files[1]
254        if target == ownname then
255            report("target cannot be this file")
256            return
257        elseif target then
258            report("target: %s",target)
259            for i=1,#files do
260                ownlibs[#ownlibs+1] = file.addsuffix(files[i],"lua")
261            end
262        end
263        merger.selfmerge(ownname,ownlibs,{ found },target)
264        report("done")
265    else
266        report("no libraries found")
267    end
268
269elseif arguments.selfclean then
270
271    report("cleaning libraries")
272    merger.selfclean(ownname)
273    report("done")
274
275else -- if arguments.help or files[1] == "help" then
276
277    application.help()
278
279end
280