summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/latexconfig/lualatexquotejobname.lua
blob: 9009864e52c508db16bf2d411625a57d1b409b32 (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
-- $Id: lualatexquotejobname.tex 22957 2011-06-13 20:49:26Z mpg $
-- Manuel Pegourie-Gonnard, originally written 2010. WTFPL v2.
--
-- Used in lualatexquotejobname.tex
--
-- Try to replicate web2c's function normalize_quotes() (in lib/texmfmp.c,
-- non-XeTeX version) as closely as possible (that is, exactly except error
-- handling).
--
-- Cache the results of previous calls, not so much for the speed gain which
-- probably doesn't matter, but to avoid repeated error messages.
local jobname_cache = {}
callback.register('process_jobname', function(jobname)
    -- use a cached version if available
    local cached = jobname_cache[jobname]
    if cached ~= nil then return cached end
    -- remove the quotes in jobname
    local clean, n_quotes = jobname:gsub([["]], [[]])
    -- complain if they wasn't an even number of quotes (aka unbalanced)
    if n_quotes % 2 ~= 0 then
        -- this code is executed when jobname is first expanded, which is
        -- probably too late for an early exit as in web2c's normalize_quotes()
        -- so, just throw an error without exiting
        texio.write_nl('! Unbalanced quotes in jobname: ' .. jobname )
    end
    -- add quotes around the clean up jobname if necessary
    if jobname:find(' ') then
        clean = '"' .. clean .. '"'
    end
    -- remember the result before returning
    jobname_cache[jobname] = clean
    return clean
end)