| 1 | --[[ |
| 2 | Copyright 2016,2019 ARATA Mizuki |
| 3 | |
| 4 | This file is part of ClutTeX. |
| 5 | |
| 6 | ClutTeX is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | ClutTeX is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with ClutTeX. If not, see <http://www.gnu.org/licenses/>. |
| 18 | ]] |
| 19 | |
| 20 | local string_gsub = string.gsub |
| 21 | local os_execute = os.execute |
| 22 | |
| 23 | -- s: string |
| 24 | local function escape(s) |
| 25 | return '"' .. string_gsub(string_gsub(s, '(\\*)"', '%1%1\\"'), '(\\+)$', '%1%1') .. '"' |
| 26 | end |
| 27 | |
| 28 | -- TEST CODE |
| 29 | assert(escape([[Hello world!]]) == [["Hello world!"]]) |
| 30 | assert(escape([[Hello" world!]]) == [["Hello\" world!"]]) |
| 31 | assert(escape([[Hello\" world!"]]) == [["Hello\\\" world!\""]]) |
| 32 | -- END TEST CODE |
| 33 | |
| 34 | local function has_command(name) |
| 35 | local result = os_execute("where " .. escape(name) .. " > NUL 2>&1") |
| 36 | -- Note that os.execute returns a number on Lua 5.1 or LuaTeX |
| 37 | return result == 0 or result == true |
| 38 | end |
| 39 | |
| 40 | return { |
| 41 | escape = escape, |
| 42 | has_command = has_command, |
| 43 | } |