summaryrefslogtreecommitdiff
path: root/support/cluttex/src/texrunner/safename.lua
blob: 0014b59b929ce31952d5dd319a3246354ec9a391 (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
--[[
  Copyright 2019 ARATA Mizuki

  This file is part of ClutTeX.

  ClutTeX is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  ClutTeX is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with ClutTeX.  If not, see <http://www.gnu.org/licenses/>.
]]

local string = string
local table = table

local function dounsafechar(c)
  if c == " " then
    return "_"
  else
    return string.format("_%02x", c:byte(1))
  end
end

local function escapejobname(name)
  return (string.gsub(name, "[%s\"$%%&'();<>\\^`|]", dounsafechar))
end

local function handlespecialchar(s)
  return (string.gsub(s, "[%\\%%^%{%}%~%#]", "~\\%1"))
end

local function handlespaces(s)
  return (string.gsub(s, "  +", function(s) return string.rep(" ", #s, "~") end))
end

local function handlenonascii(s)
  return (string.gsub(s, "[\x80-\xFF]+", "\\detokenize{%1}"))
end

local function safeinput(name, engine)
  local escaped = handlespaces(handlespecialchar(name))
  if engine.name == "pdftex" or engine.name == "pdflatex" then
    escaped = handlenonascii(escaped)
  end
  if name == escaped then
    return string.format("\\input\"%s\"", name)
  else
    return string.format("\\begingroup\\escapechar-1\\let~\\string\\edef\\x{\"%s\" }\\expandafter\\endgroup\\expandafter\\input\\x", escaped)
  end
end

return {
  escapejobname = escapejobname,
  safeinput = safeinput,
}