summaryrefslogtreecommitdiff
path: root/tex/context/base/core-ctx.lua
blob: e6fb7bb5fd8b0dcd96ba171a03ae44ea77ecba42 (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
if not modules then modules = { } end modules ['core-ctx'] = {
    version   = 1.001,
    comment   = "companion to core-ctx.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local trace_prepfiles = false  trackers.register("system.prepfiles", function(v) trace_prepfiles = v end)

local report_prepfiles = logs.reporter("system","prepfiles")

commands       = commands or { }
local commands = commands

local list, suffix, islocal, found = { }, "prep", false, false

function commands.loadctxpreplist()
    local ctlname = file.replacesuffix(tex.jobname,"ctl")
    if lfs.isfile(ctlname) then
        local x = xml.load(ctlname)
        if x then
            islocal = xml.found(x,"ctx:preplist[@local=='yes']")
--~             if trace_prepfiles then
                if islocal then
                    report_prepfiles("loading ctx log file (local)") -- todo: m!system
                else
                    report_prepfiles("loading ctx log file (specified)") -- todo: m!system
                end
--~             end
            for e in xml.collected(x,"ctx:prepfile") do
                local name = xml.text(e)
                if islocal then
                    name = file.basename(name)
                end
                local done = e.at['done'] or 'no'
                if trace_prepfiles then
                    report_prepfiles("registering %s -> %s",done)
                end
                found = true
                list[name] = done -- 'yes' or 'no'
            end
        end
    end
end

-- -- --

local function found(name) -- used in resolve
    local prepname = name .. "." .. suffix
    if list[name] and lfs.isfile(prepname) then
        if trace_prepfiles then
            report_prepfiles("preprocessing: using %s",prepname)
        end
        return prepname
    end
    return false
end

local function resolve(name) -- used a few times later on
    local filename = file.collapsepath(name)
    local prepname = islocal and found(file.basename(name))
    if prepname then
        return prepname
    end
    prepname = found(filename)
    if prepname then
        return prepname
    end
    return false
end

--~ support.doiffileexistelse(name)

local processfile       = commands.processfile
local doifinputfileelse = commands.doifinputfileelse

function commands.processfile(name,maxreadlevel) -- overloaded
    local prepname = resolve(name)
    if prepname then
        return processfile(prepname,0)
    end
    return processfile(name,maxreadlevel)
end

function commands.doifinputfileelse(name,depth)
    local prepname = resolve(name)
    if prepname then
        return doifinputfileelse(prepname,0)
    end
    return doifinputfileelse(name,depth)
end

function commands.preparedfile(name)
    return resolve(name) or name
end