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

-- will move

commands.writestatus = ctx.writestatus

function commands.doifelse(b)
    tex.sprint(tex.texcatcodes,(b and "\\firstoftwoarguments") or "\\secondoftwoarguments")
end
function commands.doif(b)
    tex.sprint(tex.texcatcodes,(b and "\\firstofoneargument") or "\\gobbleoneargument")
end
function commands.doifnot(b)
    tex.sprint(tex.texcatcodes,(b and "\\gobbleoneargument") or "\\firstofoneargument")
end
cs.testcase = commands.doifelse

local format = string.format

function commands. def(cs,value) tex.sprint(tex.ctxcatcodes,format( "\\def\\%s{%s}",cs,value)) end
function commands.edef(cs,value) tex.sprint(tex.ctxcatcodes,format("\\edef\\%s{%s}",cs,value)) end
function commands.gdef(cs,value) tex.sprint(tex.ctxcatcodes,format("\\gdef\\%s{%s}",cs,value)) end
function commands.xdef(cs,value) tex.sprint(tex.ctxcatcodes,format("\\xdef\\%s{%s}",cs,value)) end

function commands.cs(cs,args) tex.sprint(tex.ctxcatcodes,format("\\csname %s\\endcsname %s",cs,args or"")) end

-- main code

local function find_file(name,maxreadlevel)
    local function exists(n)
        if io.exists(n) then
            return n
        else
            n = file.addsuffix(n,'tex')
            if io.exists(n) then
                return n
            end
        end
        return nil
    end
    if input.aux.qualified_path(name) then
        return name
    else
        -- not that efficient, too many ./ lookups
        local n = "./" .. name
        local found = exists(n)
        if found then
            return found
        else
            for i=1,maxreadlevel or 0 do
                n = "../" .. n
                found = exists(n)
                if found then
                    return found
                end
            end
        end
        return input.find_file(texmf.instance,name) or ""
    end
end

function commands.processfile(name,maxreadlevel)
    name = find_file(name,maxreadlevel)
    if name ~= "" then
    --  tex.sprint(tex.ctxcatcodes,string.format("\\input %s\\relax",name))
        tex.print(tex.ctxcatcodes,string.format("\\input %s",name))
    end
end

function commands.doifinputfileelse(name,maxreadlevel)
    commands.doifelse(find_file(name,maxreadlevel) ~= "")
end

function commands.locatefilepath(name,maxreadlevel)
    tex.sprint(tex.texcatcodes,file.dirname(find_file(name,maxreadlevel)))
end

function commands.usepath(paths,maxreadlevel)
    input.register_extra_path(texmf.instance,paths)
    tex.sprint(tex.texcatcodes,table.concat(texmf.instance.extra_paths or {}, ""))
end

function commands.usesubpath(subpaths,maxreadlevel)
    input.register_extra_path(texmf.instance,nil,subpaths)
    tex.sprint(tex.texcatcodes,table.concat(texmf.instance.extra_paths or {}, ""))
end

function commands.usezipfile(name,tree)
    if tree and tree ~= "" then
        input.usezipfile(texmf.instance,string.format("zip:///%s?tree=%s",name,tree))
    else
        input.usezipfile(texmf.instance,string.format("zip:///%s",name))
    end
end

-- for the moment here, maybe a module

--~ <?xml version='1.0' standalone='yes'?>
--~ <exa:variables xmlns:exa='htpp://www.pragma-ade.com/schemas/exa-variables.rng'>
--~ 	<exa:variable label='mode:pragma'>nee</exa:variable>
--~ 	<exa:variable label='mode:variant'>standaard</exa:variable>
--~ </exa:variables>

local function convertexamodes(str)
    local x, t = xml.convert(str), { }
    for e, d, k in xml.elements(x,"exa:variable") do
        local dk = d[k]
        local label = dk.at and dk.at.label
        if label and label ~= "" then
            local data = xml.content(dk)
            local mode = label:match("^mode:(.+)$")
            if mode then
                tex.sprint(tex.ctxcatcodes,string.format("\\enablemode[%s:%s]",mode,data))
            end
            if data:find("{}") then
                t[#t+1] = string.format("%s={%s}",mode,data)
            else
                t[#t+1] = string.format("%s=%s",mode,data)
            end
        end
    end
    if #t > 0 then
        tex.sprint(tex.ctxcatcodes,string.format("\\setvariables[exa:variables][%s]",table.concat(t,",")))
    end
end

-- we need a system file option: ,. .. etc + paths but no tex lookup so input.find_file is wrong here

function commands.loadexamodes(filename)
    if not filename or filename == "" then
        filename = file.stripsuffix(tex.jobname)
    end
    filename = input.find_file(texmf.instance,file.addsuffix(filename,'ctm')) or ""
    if filename ~= "" then
        commands.writestatus("examodes","loading " .. filename) -- todo: message system
        convertexamodes(io.loaddata(filename))
    else
        commands.writestatus("examodes","no mode file " .. filename) -- todo: message system
    end
end