Module:User:Ruakh
Jump to navigation
Jump to search
- This module sandbox lacks a documentation subpage. You may create it.
- Useful links: subpage list • links • transclusions • testcases • user page • user talk page • userspace
This is a private module sandbox of Ruakh, for his own experimentation. Items in this module may be added and removed at Ruakh's discretion; do not rely on this module's stability.
local p = {}
p.json = require('Module:Json')
function p.echo(frame)
return frame:getParent().args[1]
end
function p.main(frame)
if type(p[frame.args[1]]) == 'function' then
return p[frame.args[1]](unpack(frame.args, 2))
else
return p[frame.args[1]][frame.args[2]]
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 [[Module:translations/data]]'s 'interwiki_langs' table); the value is
-- the corresponding site prefix ("Wikimedia language code").
function p.formatLangCodeToPrefixMapAsJson()
all_language_data = mw.loadData("Module:languages/data/all")
interwiki_langs = mw.loadData("Module:translations/data").interwiki_langs
local merged = {}
for lang_code, lang_info in pairs(all_language_data) do
if type(lang_code) ~= 'string' then
error('all_language_data contains non-string key: ' .. lang_code)
end
if type(lang_info) ~= 'table' then
error('all_language_data contains non-table value: ' .. lang_info)
end
if lang_info.wikimedia_codes then
merged[lang_code] = string.gsub(lang_info.wikimedia_codes, "%s*,.*", "")
end
end
for key, value in pairs(interwiki_langs) do
if type(key) ~= 'string' then
error('interwiki_langs contains non-string key: ' .. key)
end
if type(value) ~= 'string' then
error('interwiki_langs contains non-string value: ' .. value)
end
if merged[value] == nil then
merged[key] = value
else
merged[key] = merged[value]
end
end
return p.json.jsonObjectFromTable(merged, p.json.jsonStringFromString)
end
function p.faithfulJsonValueFromValue(val)
if val == nil then
return 'null'
elseif val == true then
return 'true'
elseif val == false then
return 'false'
elseif type(val) == 'number' then
return p.json.jsonNumberFromNumber(val)
elseif type(val) == 'string' then
return p.json.jsonStringFromString(val)
elseif type(val) == 'table' then
local ret = {}
for key, value in pairs(val) do
table.insert(ret, ', ')
table.insert(ret, p.json.jsonStringFromString(p.faithfulJsonValueFromValue(key)))
table.insert(ret, ': ')
table.insert(ret, p.faithfulJsonValueFromValue(value))
end
if # ret == 0 then
return '{}'
end
ret[1] = '{ '
table.insert(ret, ' }')
return table.concat(ret)
else
error('Unsupported type: ' .. t)
end
end
function p.formatEntryNameRulesAsJson()
local result = {}
-- normal languages:
for langCode, langData in pairs(mw.loadData("Module:languages/data/all")) do
result[langCode] = langData.entry_name
end
-- etymology languages (which are also supported by {{t}}/{{t+}}):
for langCode, langInfo in pairs(mw.loadData("Module:etymology languages/data")) do
result[langCode] = result[langInfo[5]]
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