|
|
| Рядок 1: |
Рядок 1: |
| local p = {} | | local p = {} |
|
| |
|
| local page_cache = {}
| | function p.section_menu(frame) |
| | | local page_name = frame.args.page or mw.title.getCurrentTitle().text |
| local function get_page_content(page_name)
| |
| if page_cache[page_name] then | |
| return page_cache[page_name]
| |
| end
| |
| | | |
| local title = mw.title.new(page_name) | | local title = mw.title.new(page_name) |
| if not title or not title.exists then | | if not title or not title.exists then |
| page_cache[page_name] = nil
| | return "" |
| return nil | |
| end | | end |
| | | |
| local content = title:getContent() | | local content = title:getContent() |
| page_cache[page_name] = content
| |
| return content
| |
| end
| |
|
| |
| local function get_section_content(content, section_start)
| |
| local next_section = mw.ustring.find(content, "\n==[^=]", section_start + 1)
| |
| if next_section then
| |
| return mw.ustring.sub(content, section_start, next_section - 1)
| |
| else
| |
| return mw.ustring.sub(content, section_start)
| |
| end
| |
| end
| |
|
| |
| local function count_table_rows(section_content)
| |
| local table_start = mw.ustring.find(section_content, "{|")
| |
| if not table_start then return 0 end
| |
|
| |
| local table_end = mw.ustring.find(section_content, "|}", table_start)
| |
| if not table_end then return 0 end
| |
|
| |
| local table_content = mw.ustring.sub(section_content, table_start, table_end)
| |
|
| |
| local count = 0
| |
| for _ in mw.ustring.gmatch(table_content, "|-") do
| |
| count = count + 1
| |
| end
| |
|
| |
| return math.max(0, count - 1)
| |
| end
| |
|
| |
| local function count_bullet_points(section_content)
| |
| local count = 0
| |
| for _ in mw.ustring.gmatch(section_content, "\n%*") do
| |
| count = count + 1
| |
| end
| |
| return count
| |
| end
| |
|
| |
| function p.section_menu(frame)
| |
| local page_name = frame.args.page or mw.title.getCurrentTitle().text
| |
| local content = get_page_content(page_name)
| |
|
| |
| if not content then | | if not content then |
| return "" | | return "" |
| Рядок 63: |
Рядок 16: |
| local sections = {} | | local sections = {} |
| | | |
| for line in mw.ustring.gmatch(content, "[^\n]+") do | | for section_name in mw.ustring.gmatch(content, "\n==%s*([^=]+)%s*==") do |
| if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
| | section_name = mw.text.trim(section_name) |
| local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
| | section_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "") |
|
| | section_name = mw.ustring.gsub(section_name, "%s*%(.-%)", "") |
| if section_name and section_name ~= "" then
| | section_name = mw.text.trim(section_name) |
| local display_name = mw.ustring.gsub(section_name, "%{%{.-%}%}", "")
| | |
| display_name = mw.ustring.gsub(display_name, "%s*%([^%)]*%)%s*", "")
| | if section_name ~= "" then |
| display_name = mw.text.trim(display_name)
| | table.insert(sections, section_name) |
|
| |
| local anchor = display_name
| |
| local section_pos = mw.ustring.find(content, line, 1, true)
| |
| local count = nil
| |
|
| |
| if section_pos then
| |
| local section_content = get_section_content(content, section_pos)
| |
|
| |
| if mw.ustring.match(display_name, "Нагороди") or mw.ustring.match(display_name, "Сезони") then
| |
| count = count_table_rows(section_content)
| |
| elseif mw.ustring.match(display_name, "Цікаві факти") then
| |
| count = count_bullet_points(section_content)
| |
| end
| |
| end
| |
|
| |
| if count and count > 0 then
| |
| display_name = display_name .. " (" .. count .. ")"
| |
| end
| |
|
| |
| anchor = mw.ustring.gsub(anchor, " ", "_")
| |
|
| |
| if display_name ~= "" then
| |
| table.insert(sections, {
| |
| display = display_name,
| |
| anchor = anchor
| |
| })
| |
| end
| |
| end
| |
| end | | end |
| end | | end |
| Рядок 106: |
Рядок 31: |
| end | | end |
| | | |
| local html_parts = {} | | local html = '<div class="l-box" style="position:fixed; top:20px; left:20px; z-index:100; border-radius:16px; background-color:#23232c; width:220px; overflow:hidden;">' |
|
| | html = html .. '<div style="padding:15px 20px; font-weight:bold; font-size:15px; color:#fff; border-bottom:1px solid rgba(255,255,255,0.1);">Зміст</div>' |
| table.insert(html_parts, '<div class="l-box" style="position:fixed; top:20px; left:20px; z-index:100; border-radius:16px; background-color:#23232c; width:220px; box-sizing:border-box; overflow:hidden;">')
| |
| table.insert(html_parts, '<div style="padding:15px 20px; font-weight:bold; font-size:15px; color:#fff; border-bottom:1px solid rgba(255,255,255,0.1);">Зміст</div>') | |
| | | |
| for i, section in ipairs(sections) do | | for i, name in ipairs(sections) do |
| | local anchor = mw.ustring.gsub(name, " ", "_") |
| local border = "" | | local border = "" |
| if i < #sections then | | if i < #sections then |
| border = "border-bottom:1px solid rgba(255,255,255,0.05);" | | border = "border-bottom:1px solid rgba(255,255,255,0.05);" |
| end | | end |
| | | html = html .. '<div class="l-box-item" style="padding:12px 20px;' .. border .. '">[[#' .. anchor .. '|' .. name .. ']]</div>' |
| table.insert(html_parts, string.format(
| |
| '<div class="l-box-item" style="padding:12px 20px; %s">[[#%s|%s]]</div>',
| |
| border, section.anchor, section.display
| |
| ))
| |
| end | | end |
| | | |
| table.insert(html_parts, '</div>') | | html = html .. '</div>' |
| | | |
| return table.concat(html_parts, '\n') | | return html |
| end | | end |
|
| |
|
| return p | | return p |