Модуль:MenuBar: відмінності між версіями
Admin (обговорення | внесок) (Створена сторінка: local p = {} local page_cache = {} 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) if not title or not title.exists then page_cache[page_name] = nil return nil end local content = title:getContent() page_cache[page_name] = content return content end local function get_section_content(content, s...) |
Admin (обговорення | внесок) Немає опису редагування |
||
| Рядок 1: | Рядок 1: | ||
local p = {} | local p = {} | ||
local page_cache = {} | local page_cache = {} | ||
| Рядок 7: | Рядок 6: | ||
return page_cache[page_name] | return page_cache[page_name] | ||
end | 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 | ||
| Рядок 13: | Рядок 11: | ||
return nil | return nil | ||
end | end | ||
local content = title:getContent() | local content = title:getContent() | ||
page_cache[page_name] = content | page_cache[page_name] = content | ||
| Рядок 21: | Рядок 18: | ||
local function get_section_content(content, section_start) | local function get_section_content(content, section_start) | ||
local section_end = mw.ustring.find(content, "\n==[^=]", section_start + 1) | local section_end = mw.ustring.find(content, "\n==[^=]", section_start + 1) | ||
if section_end then | if section_end then | ||
return mw.ustring.sub(content, section_start, section_end - 1) | return mw.ustring.sub(content, section_start, section_end - 1) | ||
| Рядок 49: | Рядок 45: | ||
function p.section_menu(frame) | function p.section_menu(frame) | ||
local page_name = frame.args.page or mw.title.getCurrentTitle().text | local page_name = frame.args.page or mw.title.getCurrentTitle().text | ||
local content = get_page_content(page_name) | local content = get_page_content(page_name) | ||
if not content then | if not content then return "" end | ||
local sections = {} | local sections = {} | ||
table.insert(sections, { | -- Перший пункт — завжди "Подія" | ||
table.insert(sections, { label = "Подія", anchor = "top", badge = nil }) | |||
for line in mw.ustring.gmatch(content, "[^\n]+") do | for line in mw.ustring.gmatch(content, "[^\n]+") do | ||
if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then | if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then | ||
local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$") | local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$") | ||
if section_name and section_name ~= "" then | if section_name and section_name ~= "" then | ||
local processed = frame:preprocess(section_name) | local processed = frame:preprocess(section_name) | ||
processed = mw.text.trim(processed) | processed = mw.text.trim(processed) | ||
local anchor = mw.ustring.gsub(processed, " ", "_") | local anchor = mw.ustring.gsub(processed, " ", "_") | ||
local | local badge = nil | ||
local section_pos = mw.ustring.find(content, line, 1, true) | local section_pos = mw.ustring.find(content, line, 1, true) | ||
if section_pos then | if section_pos then | ||
local section_content = get_section_content(content, section_pos) | local section_content = get_section_content(content, section_pos) | ||
if mw.ustring.match(processed, "Цікаві факти") then | if mw.ustring.match(processed, "Цікаві факти") then | ||
local | local n = count_facts(section_content) | ||
if | if n > 0 then badge = tostring(n) end | ||
elseif mw.ustring.match(processed, "Нагороди") then | elseif mw.ustring.match(processed, "Нагороди") then | ||
local | local n = count_awards(section_content) | ||
if | if n > 0 then badge = tostring(n) end | ||
end | end | ||
end | end | ||
if processed ~= "" then | if processed ~= "" then | ||
table.insert(sections, { | table.insert(sections, { label = processed, anchor = anchor, badge = badge }) | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
if #sections == 0 then | if #sections == 0 then return "" end | ||
local parts = {} | |||
table.insert(parts, '<div class="l-box">') | |||
for i, s in ipairs(sections) do | |||
local last = (i == #sections) and " l-box-item-last" or "" | |||
local badge_html = s.badge | |||
and (' <span class="mcc-badge">' .. s.badge .. '</span>') | |||
or "" | |||
table.insert(parts, | |||
'<div class="l-box-item' .. last .. '" data-target="' .. s.anchor .. '">' | |||
.. '<span>' .. s.label .. '</span>' | |||
.. badge_html | |||
.. '</div>' | |||
) | |||
end | end | ||
table.insert(parts, '</div>') | |||
return table.concat(parts, '\n') | |||
table.insert( | |||
return table.concat( | |||
end | end | ||
return p | return p | ||
Поточна версія на 14:53, 6 квітня 2026
Документацію для цього модуля можна створити у Модуль:MenuBar/документація
local p = {}
local page_cache = {}
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)
if not title or not title.exists then
page_cache[page_name] = nil
return nil
end
local content = title:getContent()
page_cache[page_name] = content
return content
end
local function get_section_content(content, section_start)
local section_end = mw.ustring.find(content, "\n==[^=]", section_start + 1)
if section_end then
return mw.ustring.sub(content, section_start, section_end - 1)
else
return mw.ustring.sub(content, section_start)
end
end
local function count_facts(section_content)
local count = 0
for line in mw.ustring.gmatch(section_content, "[^\n]+") do
if mw.ustring.match(line, "^%*[^%*]") then
count = count + 1
end
end
return count
end
local function count_awards(section_content)
local count = 0
for _ in mw.ustring.gmatch(section_content, "%[%[File:[^%]]+%.png") 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 return "" end
local sections = {}
-- Перший пункт — завжди "Подія"
table.insert(sections, { label = "Подія", anchor = "top", badge = nil })
for line in mw.ustring.gmatch(content, "[^\n]+") do
if mw.ustring.match(line, "^==[^=]") and mw.ustring.match(line, "[^=]==$") then
local section_name = mw.ustring.match(line, "^==%s*(.-)%s*==$")
if section_name and section_name ~= "" then
local processed = frame:preprocess(section_name)
processed = mw.text.trim(processed)
local anchor = mw.ustring.gsub(processed, " ", "_")
local badge = nil
local section_pos = mw.ustring.find(content, line, 1, true)
if section_pos then
local section_content = get_section_content(content, section_pos)
if mw.ustring.match(processed, "Цікаві факти") then
local n = count_facts(section_content)
if n > 0 then badge = tostring(n) end
elseif mw.ustring.match(processed, "Нагороди") then
local n = count_awards(section_content)
if n > 0 then badge = tostring(n) end
end
end
if processed ~= "" then
table.insert(sections, { label = processed, anchor = anchor, badge = badge })
end
end
end
end
if #sections == 0 then return "" end
local parts = {}
table.insert(parts, '<div class="l-box">')
for i, s in ipairs(sections) do
local last = (i == #sections) and " l-box-item-last" or ""
local badge_html = s.badge
and (' <span class="mcc-badge">' .. s.badge .. '</span>')
or ""
table.insert(parts,
'<div class="l-box-item' .. last .. '" data-target="' .. s.anchor .. '">'
.. '<span>' .. s.label .. '</span>'
.. badge_html
.. '</div>'
)
end
table.insert(parts, '</div>')
return table.concat(parts, '\n')
end
return p