-- extract_dictionary.lua
-- dictionary.lua -> temiz JSON (NO trailing comma)

local function escape(str)
    if not str then return " " end
    str = tostring(str)
    if str:match("^%s*$") then return " " end

    str = str:gsub("\\", "\\\\")
    str = str:gsub('"', '\\"')
    str = str:gsub("\r\n", "\n")
    str = str:gsub("\n", "\\n")
    str = str:gsub("\r", "")
    return str
end

dofile(arg[1])

local out = io.open(arg[2], "w")
out:write("{\n")

local first = true
for k, v in pairs(dictionary) do
    if not first then
        out:write(",\n")
    end
    first = false

    out:write(string.format(
        '  "%s": "%s"',
        escape(k),
        escape(v)
    ))
end

out:write("\n}\n")
out:close()