mtx-package.lua /size: 3915 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mtx-package'] = {
2    version   = 1.002,
3    comment   = "companion to mtxrun.lua",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local format, gsub, gmatch = string.format, string.gsub, string.gmatch
10
11local helpinfo = [[
12<?xml version="1.0"?>
13<application>
14 <metadata>
15  <entry name="name">mtx-package</entry>
16  <entry name="detail">Distribution Related Goodies</entry>
17  <entry name="version">0.10</entry>
18 </metadata>
19 <flags>
20  <category name="basic">
21   <subcategory>
22    <flag name="merge"><short>merge 'loadmodule' into merge file</short></flag>
23   </subcategory>
24  </category>
25 </flags>
26</application>
27]]
28
29local application = logs.application {
30    name     = "mtx-package",
31    banner   = "Distribution Related Goodies 0.10",
32    helpinfo = helpinfo,
33}
34
35local report = application.report
36
37scripts         = scripts         or { }
38messages        = messages        or { }
39scripts.package = scripts.package or { }
40
41function scripts.package.merge_luatex_files(name)
42    local oldname = resolvers.findfile(name) or ""
43    oldname = file.replacesuffix(oldname,"lua")
44    if oldname == "" then
45        report("missing %q",name)
46    else
47        local newname = file.removesuffix(oldname) .. "-merged.lua"
48        local data = io.loaddata(oldname) or ""
49        if data == "" then
50            report("missing %q",newname)
51        else
52            report("loading %q",oldname)
53            local collected = { }
54            collected[#collected+1] = format("-- merged file : %s\n",newname)
55            collected[#collected+1] = format("-- parent file : %s\n",oldname)
56            collected[#collected+1] = format("-- merge date  : %s\n",os.date())
57            -- loadmodule can have extra arguments
58            for lib in gmatch(data,"loadmodule *%([\'\"](.-)[\'\"]") do -- todo: not -- lines
59                if file.basename(lib) ~= file.basename(newname) then
60                    local fullname = resolvers.findfile(lib) or ""
61                    if fullname == "" then
62                        report("missing %q",lib)
63                    else
64                        report("fetching %q",fullname)
65                        local data = io.loaddata(fullname)
66                        collected[#collected+1] = "\ndo -- begin closure to overcome local limits and interference\n\n"
67                        collected[#collected+1] = utilities.merger.compact(data)
68                        collected[#collected+1] = "\nend -- closure\n"
69                    end
70                end
71            end
72            collected = table.concat(collected)
73            if environment.argument("stripcontext") then
74                local stripped = 0
75                local eol      = lpeg.patterns.eol
76                local space    = lpeg.patterns.space^0
77                local start    = eol * lpeg.P("if context then") * space * eol
78                local stop     = eol * (lpeg.P("else") + lpeg.P("end")) * space * eol
79                local noppes   = function()
80                    stripped = stripped + 1
81                    return "\n--removed\n"
82                end
83                local pattern = lpeg.Cs((start * ((1-stop)^1/noppes) * stop + lpeg.P(1))^0)
84                collected = lpeg.match(pattern,collected)
85                if stripped > 0 then
86                    report("%i context specific sections stripped",stripped)
87                end
88            end
89            report("saving %q (%i bytes)",newname,#collected)
90            io.savedata(newname,collected)
91        end
92    end
93end
94
95if environment.argument("merge") then
96    scripts.package.merge_luatex_files(environment.files[1] or "")
97elseif environment.argument("exporthelp") then
98    application.export(environment.argument("exporthelp"),environment.files[1])
99else
100    application.help()
101end
102