Модуль:FetchData2: відмінності між версіями

нема опису редагування
Немає опису редагування
Немає опису редагування
Рядок 16: Рядок 16:
     text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
     text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
     return mw.text.trim(text)
     return mw.text.trim(text)
end
--- Парсить комірки з рядка таблиці
local function parse_table_cells(row_text)
    local cells = {}
    local current_cell = ""
    local in_link = false
    local i = 1
   
    while i <= #row_text do
        local char = row_text:sub(i, i)
        local next_char = row_text:sub(i + 1, i + 1)
       
        -- Відслідковуємо вікі-посилання
        if char == "[" and next_char == "[" then
            in_link = true
            current_cell = current_cell .. "[["
            i = i + 2
        elseif char == "]" and next_char == "]" then
            in_link = false
            current_cell = current_cell .. "]]"
            i = i + 2
        -- Якщо знайшли роздільник комірок
        elseif char == "|" and not in_link then
            if next_char == "|" then
                -- || - роздільник комірок
                table.insert(cells, mw.text.trim(current_cell))
                current_cell = ""
                i = i + 2
            else
                -- | на початку рядка - також роздільник
                if current_cell ~= "" then
                    table.insert(cells, mw.text.trim(current_cell))
                    current_cell = ""
                end
                i = i + 1
            end
        else
            current_cell = current_cell .. char
            i = i + 1
        end
    end
   
    -- Додаємо останню комірку
    if current_cell ~= "" then
        table.insert(cells, mw.text.trim(current_cell))
    end
   
    return cells
end
end


Рядок 87: Рядок 38:
      
      
     -- Знаходимо таблицю
     -- Знаходимо таблицю
     local table_content = mw.ustring.match(content, "{|.-(.-|})")  
     local table_start = mw.ustring.find(content, "{|")
     if not table_content then  
    local table_end = mw.ustring.find(content, "|}", table_start)
   
     if not table_start or not table_end then
         return error_output("Table Missing", page_title)
         return error_output("Table Missing", page_title)
     end
     end
      
      
    local table_content = mw.ustring.sub(content, table_start, table_end + 1)
   
    -- Екрануємо спеціальні символи в імені гравця
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
     local player_pattern = "%[%[" .. escaped .. "%]%]"
     local player_pattern = "%[%[" .. escaped .. "%]%]"
      
      
     -- Парсимо рядки таблиці
     -- Шукаємо рядки, які починаються з |-
     for row in mw.ustring.gmatch(table_content, "|\n(.-)\n|-") do
    local rows = {}
         row = row .. "\n" -- Додаємо перенос для коректної обробки
     for row in mw.ustring.gmatch(table_content, "|-\n(.-)\n|-") do
          
         table.insert(rows, row)
        -- Перевіряємо, чи є ім'я гравця в цьому рядку
    end
   
    -- Останній рядок (до |})
    local last_row = mw.ustring.match(table_content, "|-\n(.-)%s*|}")
    if last_row then
         table.insert(rows, last_row)
    end
   
    -- Обробляємо кожен рядок
    for _, row in ipairs(rows) do
         if mw.ustring.find(row, player_pattern) then
         if mw.ustring.find(row, player_pattern) then
             local cells = parse_table_cells(row)
            -- Розбиваємо рядок на комірки
             local cells = {}
           
            -- Видаляємо початковий |
            row = mw.ustring.gsub(row, "^%s*|%s*", "")
              
              
             if column_index <= #cells and cells[column_index] then
            -- Розбиваємо по ||
            for cell in mw.ustring.gmatch(row, "([^|]+)") do
                -- Пропускаємо порожні комірки від подвійних ||
                local trimmed = mw.text.trim(cell)
                if trimmed ~= "" then
                    table.insert(cells, trimmed)
                end
            end
           
            -- Повертаємо потрібну колонку
             if column_index <= #cells then
                 return clean_wikilinks(cells[column_index])
                 return clean_wikilinks(cells[column_index])
            else
                return error_output("Column Out of Range",
                    string.format("Requested col %d, found %d cells", column_index, #cells))
             end
             end
         end
         end
     end
     end
      
      
     return nil
     return error_output("Player Not Found", player_name)
end
end