Модуль:Ftl: различия между версиями
Материал из Space Station 14 Вики
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 (обсуждение | вклад) мНет описания правки |
||
| (не показано 8 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
-- Загрузка данных | |||
local data = mw.loadData("Модуль:IanComradeBot/ftl/ru-RU.json/data") | |||
local p = {} | local p = {} | ||
-- | -- Функция для получения таблицы данных | ||
function p.getData() | |||
return data | |||
return | |||
end | end | ||
-- Поиск текста по ключу | -- Поиск текста по ключу (без учета регистра) | ||
local function findTextByKey(data, key) | local function findTextByKey(data, key) | ||
for | local searchKey = string.lower(key) | ||
if | for categoryName, category in pairs(data) do | ||
for k, value in pairs(category) do | |||
if string.lower(k) == searchKey then | |||
return value | |||
end | |||
end | end | ||
end | end | ||
| Строка 18: | Строка 22: | ||
end | end | ||
-- Поиск ключа по тексту | -- Поиск ключа по тексту (без учета регистра для текста) | ||
local function findKeyByText(data, text) | local function findKeyByText(data, text) | ||
local searchText = string.lower(text) | |||
for categoryName, category in pairs(data) do | for categoryName, category in pairs(data) do | ||
for key, value in pairs(category) do | for key, value in pairs(category) do | ||
if value == | if string.lower(value) == searchText then | ||
return key | return key | ||
end | end | ||
| Строка 28: | Строка 33: | ||
end | end | ||
return nil | return nil | ||
end | |||
-- Получение всех строк из категории (без учета регистра) | |||
local function getCategoryStrings(data, categoryName) | |||
local searchCategory = string.lower(categoryName) | |||
local matchedCategory = nil | |||
for catName, catData in pairs(data) do | |||
if string.lower(catName) == searchCategory then | |||
matchedCategory = catData | |||
break | |||
end | |||
end | |||
if not matchedCategory then | |||
return nil | |||
end | |||
local result = {} | |||
for _, value in pairs(matchedCategory) do | |||
if type(value) == "string" then | |||
table.insert(result, value) | |||
end | |||
end | |||
return result | |||
end | end | ||
| Строка 37: | Строка 67: | ||
if not mode or not param then | if not mode or not param then | ||
return "Ошибка: Не указаны все необходимые параметры." | return "Ошибка: Не указаны все необходимые параметры." | ||
end | end | ||
| Строка 51: | Строка 75: | ||
local result = findKeyByText(data, param) | local result = findKeyByText(data, param) | ||
return result or "Ошибка: Текст не найден." | return result or "Ошибка: Текст не найден." | ||
elseif mode == "categories" then | |||
local strings = getCategoryStrings(data, param) | |||
if not strings or #strings == 0 then | |||
return "Ошибка: Категория не найдена или пуста." | |||
end | |||
local output = {} | |||
for _, value in ipairs(strings) do | |||
table.insert(output, "<li>" .. mw.text.encode(value) .. "</li>") | |||
end | |||
return table.concat(output) | |||
else | else | ||
return "Ошибка: Неизвестный режим работы." | return "Ошибка: Неизвестный режим работы." | ||
Текущая версия от 22:21, 22 февраля 2025
Для документации этого модуля может быть создана страница Модуль:Ftl/doc
-- Загрузка данных
local data = mw.loadData("Модуль:IanComradeBot/ftl/ru-RU.json/data")
local p = {}
-- Функция для получения таблицы данных
function p.getData()
return data
end
-- Поиск текста по ключу (без учета регистра)
local function findTextByKey(data, key)
local searchKey = string.lower(key)
for categoryName, category in pairs(data) do
for k, value in pairs(category) do
if string.lower(k) == searchKey then
return value
end
end
end
return nil
end
-- Поиск ключа по тексту (без учета регистра для текста)
local function findKeyByText(data, text)
local searchText = string.lower(text)
for categoryName, category in pairs(data) do
for key, value in pairs(category) do
if string.lower(value) == searchText then
return key
end
end
end
return nil
end
-- Получение всех строк из категории (без учета регистра)
local function getCategoryStrings(data, categoryName)
local searchCategory = string.lower(categoryName)
local matchedCategory = nil
for catName, catData in pairs(data) do
if string.lower(catName) == searchCategory then
matchedCategory = catData
break
end
end
if not matchedCategory then
return nil
end
local result = {}
for _, value in pairs(matchedCategory) do
if type(value) == "string" then
table.insert(result, value)
end
end
return result
end
-- Основная функция модуля
function p.main(frame)
local mode = frame.args[1]
local param = frame.args[2]
if not mode or not param then
return "Ошибка: Не указаны все необходимые параметры."
end
if mode == "translation" then
local result = findTextByKey(data, param)
return result or "Ошибка: Ключ не найден."
elseif mode == "key" then
local result = findKeyByText(data, param)
return result or "Ошибка: Текст не найден."
elseif mode == "categories" then
local strings = getCategoryStrings(data, param)
if not strings or #strings == 0 then
return "Ошибка: Категория не найдена или пуста."
end
local output = {}
for _, value in ipairs(strings) do
table.insert(output, "<li>" .. mw.text.encode(value) .. "</li>")
end
return table.concat(output)
else
return "Ошибка: Неизвестный режим работы."
end
end
return p