Модуль:Loc: различия между версиями

мНет описания правки
Метка: ручная отмена
Нет описания правки
Строка 1: Строка 1:
-- Загрузка данных
local data = mw.loadData("Модуль:IanComradeBot/loc/data")
local data = mw.loadData("Модуль:IanComradeBot/loc/data")


local p = {}
local p = {}


-- Функция для получения таблицы данных
-- индекс для регистронезависимого поиска
function p.getData()
local lowerIndex = nil
     return data
 
local function tolower(s)
     return s and mw.ustring.lower(s) or s
end
end


-- Получение значения по ключу с поддержкой подключачей
local function buildLowerIndex()
local function getValueByKey(data, key, subkey)
    if lowerIndex then return end
    if not data[key] then
    lowerIndex = {}
         return nil
    for k, v in pairs(data) do
        if type(k) == "string" then
            local lk = tolower(k)
            if not lowerIndex[lk] then
                lowerIndex[lk] = v
            end
         end
     end
     end
   
end
    local value = data[key]
 
   
-- Возвращает значение по ключу
    -- Если значение - строка, возвращаем её
local function getValueByKeyRaw(value, subkey)
     if type(value) == "string" then
     if value == nil then return nil end
    local t = type(value)
    if t == "string" then
         return value
         return value
     end
     elseif t == "table" then
   
    -- Если значение - таблица (вложенная структура)
    if type(value) == "table" then
         if subkey and value[subkey] then
         if subkey and value[subkey] then
             return value[subkey]
             return value[subkey]
         end
         end
        return nil
     end
     end
   
     return nil
     return nil
end
end


-- Поиск текста по ключу (без учета регистра)
-- Основной поиск
local function findTextByKey(data, key, subkey)
local function findTextByKey(key, subkey)
     local searchKey = string.lower(key)
    if not key then return nil end
     for k, value in pairs(data) do
 
        if string.lower(k) == searchKey then
     local direct = data[key]
            local result = getValueByKey(data, k, subkey)
     local result = getValueByKeyRaw(direct, subkey)
            if result then
    if result then return result end
                return result
 
            end
    buildLowerIndex()
        end
    local lk = tolower(key)
    end
    local v = lowerIndex[lk]
    result = getValueByKeyRaw(v, subkey)
    if result then return result end
 
     return nil
     return nil
end
end
Строка 54: Строка 63:
     end
     end


     local result = findTextByKey(data, key, subkey)
     local result = findTextByKey(key, subkey)


     if not result then
     if not result then
    return key
        return key
     end
     end
 
 
     return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}")
     return frame:preprocess("{{#invoke:Loc/Marking|main|<nowiki>".. result .. "</nowiki>}}")
end
end
Строка 71: Строка 80:
     end
     end


     local result = findTextByKey(data, key, subkey)
     local result = findTextByKey(key, subkey)
     if not result then
     if not result then
    return key
        return key
     end
     end