Модуль:Loc: различия между версиями
Pok (обсуждение | вклад) Новая страница: «local p = {} -- Загрузка данных local function loadData(filePath) local page = mw.title.new(filePath) local content = page and page:getContent() return content and mw.text.jsonDecode(content) or nil end -- Поиск текста по ключу local function findTextByKey(data, key) for _, category in pairs(data) do if category[key] then return category[key] end end return nil end -- По...» |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 37 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
local JsonPaths = require('Module:JsonPaths') | |||
local getArgs = require('Module:Arguments').getArgs | |||
local data = mw.loadData(JsonPaths.get("loc.json")) | |||
local p = {} | local p = {} | ||
local function tolower(s) | |||
local function | return s and mw.ustring.lower(s) or s | ||
return | |||
end | end | ||
local complexLowerIndex = nil | |||
local function | |||
for | local function buildComplexIndex() | ||
if | if complexLowerIndex then return end | ||
complexLowerIndex = {} | |||
for k, v in pairs(data) do | |||
if type(v) == "table" and type(k) == "string" then | |||
local lk = tolower(k) | |||
if not complexLowerIndex[lk] then | |||
complexLowerIndex[lk] = v | |||
end | |||
end | end | ||
end | end | ||
end | end | ||
local function getValueFromEntry(entry, subkey) | |||
local function | if entry == nil then return nil end | ||
local t = type(entry) | |||
if t == "string" then | |||
return entry | |||
elseif t == "table" then | |||
if subkey then | |||
return entry[subkey] | |||
else | |||
return entry["_value"] | |||
end | end | ||
end | end | ||
| Строка 30: | Строка 39: | ||
end | end | ||
local function findTextByKeyOptimized(key, subkey) | |||
function | if not key then return nil end | ||
if | local direct = data[key] | ||
local r = getValueFromEntry(direct, subkey) | |||
if r then return r end | |||
buildComplexIndex() | |||
local lk = tolower(key) | |||
local complexEntry = complexLowerIndex[lk] | |||
if complexEntry then | |||
local rr = getValueFromEntry(complexEntry, subkey) | |||
if rr then return rr end | |||
end | |||
return nil | |||
end | |||
function p.GetString(frame) | |||
local args = getArgs(frame, { removeBlanks = false }) | |||
local key = args[1] | |||
local subkey = args[2] or "_value" | |||
local noFormat = args.noFormat or "" | |||
if not key then | |||
return "Ошибка: Не указаны все необходимые параметры." | return "Ошибка: Не указаны все необходимые параметры." | ||
end | end | ||
local result = findTextByKeyOptimized(key, subkey) | |||
if not | if not result then | ||
return | return key | ||
end | |||
if noFormat == "" then | |||
return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}") | |||
else | |||
return frame:preprocess("{{#invoke:Loc/Marking|main|noFormat=1|<nowiki>".. result .. "</nowiki>}}") | |||
end | |||
end | |||
function p.GetRawString(frame) | |||
local args = getArgs(frame, { removeBlanks = false }) | |||
local key = args[1] | |||
local subkey = args[2] or "_value" | |||
local noFormat = args.noFormat or "" | |||
if not key then | |||
return "Ошибка: Не указаны все необходимые параметры." | |||
end | end | ||
local result = findTextByKeyOptimized(key, subkey) | |||
if not result then | |||
return key | |||
return | |||
end | end | ||
if noFormat == "" then | |||
return result | |||
else | |||
return frame:preprocess("<nowiki>".. result .. "</nowiki>") | |||
end | |||
end | end | ||
return p | return p | ||