summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/lua/mtx-package.lua
blob: 06c89907adf9847d0ca75073950a308d40d0e499 (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
if not modules then modules = { } end modules ['mtx-package'] = {
    version   = 1.002,
    comment   = "companion to mtxrun.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format, gsub, gmatch = string.format, string.gsub, string.gmatch

scripts         = scripts         or { }
messages        = messages        or { }
scripts.package = scripts.package or { }

function scripts.package.merge_luatex_files(name,strip)
    local oldname = resolvers.find_file(name) or ""
    oldname = file.replacesuffix(oldname,"lua")
    if oldname == "" then
        logs.simple("missing '%s'",name)
    else
        local newname = file.removesuffix(oldname) .. "-merged.lua"
        local data = io.loaddata(oldname) or ""
        if data == "" then
            logs.simple("missing '%s'",newname)
        else
            logs.simple("loading '%s'",oldname)
            local collected = { }
            collected[#collected+1] = format("-- merged file : %s\n",newname)
            collected[#collected+1] = format("-- parent file : %s\n",oldname)
            collected[#collected+1] = format("-- merge date  : %s\n",os.date())
            -- loadmodule can have extra arguments
            for lib in gmatch(data,"loadmodule *%([\'\"](.-)[\'\"]") do
                if file.basename(lib) ~= file.basename(newname) then
                    local fullname = resolvers.find_file(lib) or ""
                    if fullname == "" then
                        logs.simple("missing '%s'",lib)
                    else
                        logs.simple("fetching '%s'",fullname)
                        local data = io.loaddata(fullname)
                        if strip then
                            data = gsub(data,"%-%-%[%[ldx%-%-.-%-%-%ldx%]%]%-%-[\n\r]*","")
                            data = gsub(data,"%-%-%~[^\n\r]*[\n\r]*","\n")
                            data = gsub(data,"%s+%-%-[^\n\r]*[\n\r]*","\n")
                            data = gsub(data,"[\n\r]+","\n")
                        end
                        collected[#collected+1] = "\ndo -- begin closure to overcome local limits and interference\n\n"
                        collected[#collected+1] = data
                        collected[#collected+1] = "\nend -- closure\n"
                    end
                end
            end
            logs.simple("saving '%s'",newname)
            io.savedata(newname,table.concat(collected))
        end
    end
end

logs.extendbanner("Package Tools 0.1",true)

messages.help = [[
--merge               merge 'loadmodule' into merge file
]]

if environment.argument("merge") then
    scripts.package.merge_luatex_files(environment.files[1] or "")
else
    logs.help(messages.help)
end