|
Мітки: Очищення Ручний відкіт |
| (Не показано 50 проміжних версій цього користувача) |
| Рядок 1: |
Рядок 1: |
| local function fetch_data_from_table(page_title, player_name, column_index)
| |
| local title = mw.title.new(page_title)
| |
| if not title then return nil end
| |
|
| |
|
| local content = title:getContent()
| |
| if not content then return nil end
| |
|
| |
| -- Витягуємо будь-яку wikitable
| |
| local table_content = mw.ustring.match(content, "{|.-|}")
| |
| if not table_content then return nil end
| |
|
| |
| -- Робимо імʼя безпечним у RegEx
| |
| local escaped_player = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
| |
|
| |
| -- Шукаємо у форматах:
| |
| -- [[Player]], [[Player|текст]], Player
| |
| local player_pattern =
| |
| "%[%[" .. escaped_player .. "%]%]" ..
| |
| "|%[%[" .. escaped_player .. "|.-%]%]" ..
| |
| "|" .. escaped_player
| |
|
| |
| -- Знайдемо весь рядок, де є гравець
| |
| local row = nil
| |
| for line in mw.ustring.gmatch(table_content, "[^\n]+") do
| |
| if mw.ustring.match(line, player_pattern) then
| |
| row = line
| |
| break
| |
| end
| |
| end
| |
| if not row then return nil end
| |
|
| |
| -- Забираємо все після "|-" якщо є
| |
| row = mw.ustring.gsub(row, "^%s*|%-", "")
| |
|
| |
| -- Розбиваємо рядок на комірки
| |
| local cells = {}
| |
| for cell in mw.ustring.gmatch(row, "%s*|%s*([^|]+)") do
| |
| table.insert(cells, mw.text.trim(cell))
| |
| end
| |
|
| |
| -- Вибираємо потрібну колонку
| |
| local result = cells[column_index]
| |
| if not result then return nil end
| |
|
| |
| -- Прибираємо вікі-посилання
| |
| result = mw.ustring.gsub(result, "%[%[([^%]|]+)|?([^%]]*)%]%]", "%2")
| |
| result = mw.text.trim(result)
| |
| if result == "" then
| |
| result = mw.ustring.gsub(result, "%[%[([^%]]+)%]%]", "%1")
| |
| end
| |
|
| |
| return result
| |
| end
| |