local p = {}
p.json = require('Module:Json')
local toJSON = require("Module:JSON").toJSON
function p.echo(frame)
return frame:getParent().args
end
function p.main(frame)
if type(p]) == 'function' then
return p](unpack(frame.args, 2))
else
return p]]
end
end
-- Returns a JSON object that contains cases where {{t+|foo|...}} links to a site
-- other than foo.wiktionary.org. The key is the language code used in the translation
-- template (typically a "Wiktionary language code", though there are a few exceptions
-- defined in ]'s 'interwiki_langs' table); the value is
-- the corresponding site prefix ("Wikimedia language code").
function p.formatLangCodeToPrefixMapAsJson()
local get_lang = require("Module:languages").getByCode
local merged = {}
local interwiki_langs = require("Module:translations/data").interwiki_langs
for key, value in pairs(interwiki_langs) do
if type(key) ~= 'string' then
error('interwiki_langs contains non-string key: ' .. key)
elseif type(value) ~= 'string' then
error('interwiki_langs contains non-string value: ' .. value)
end
merged = value
end
local all_language_data = require("Module:languages/data/all/additional")
for lang_code in pairs(all_language_data) do
if type(lang_code) ~= 'string' then
error('all_language_data contains non-string key: ' .. lang_code)
elseif merged == nil then
local lang = get_lang(lang_code, nil, true)
if lang then
local first_code = lang:getWikimediaLanguageCodes()
if first_code ~= lang_code then
merged = first_code
end
end
end
end
return toJSON(merged, {ascii = true})
end
-- This JSONifies all string keys before the main table is JSONified (i.e. they're
-- processed twice), which mimics how the old function did things.
local function JSONify_keys(val, spec, seen)
-- Memoize with `seen`, to avoid duplication/loops.
local seen_val = seen
if seen_val ~= nil then
return seen_val
end
local new = {}
for k, v in pairs(val) do
if type(k) == "string" then
k = toJSON(k, spec)
end
if type(v) == "table" then
local new_v = JSONify_keys(v, spec, seen)
v, seen = new_v, new_v
end
new = v
end
return new
end
function p.faithfulJsonValueFromValue(val)
local spec = {ascii = true}
if type(val) == "table" then
val = JSONify_keys(val, spec, {})
end
return toJSON(val, spec)
end
function p.formatEntryNameRulesAsJson()
local get_lang = require("Module:languages").getByCode
local all_language_data = require("Module:languages/data/all/additional")
local result = {}
for lang_code in pairs(all_language_data) do
if type(lang_code) ~= 'string' then
error('all_language_data contains non-string key: ' .. lang_code)
end
local lang = get_lang(lang_code, nil, true)
if lang then
result = lang:getData().entry_name
end
end
return p.faithfulJsonValueFromValue(result)
end
function p.listArgPairs(frame)
local ret = ''
for key, value in pairs(frame.args) do
ret = ret .. key .. '=>' .. value .. '<br />'
end
return ret
end
function p.strlen(s)
return s:len()
end
function p.substring(s, i, j)
return s:sub(i, j)
end
function p.bytewiseEscape(s)
s = s:gsub('%p', '\\%0')
s = s:gsub('\n', '\\n')
s = s:gsub('', function (c)
return string.format('\\x%02X', c:byte(1))
end)
s = s:gsub('^ ', '\\x20')
s = s:gsub(' ', ' \\x20')
s = s:gsub(' $', '\\x20')
return s
end
return p