Module:PadUtils: Difference between revisions
Appearance
GlifterPad (talk | contribs) No edit summary |
GlifterPad (talk | contribs) No edit summary |
||
| Line 1: | Line 1: | ||
-- Module:PadUtils | |||
local p = {} | local p = {} | ||
-- Split | -- Split helper (ignores empty parts) | ||
local function split(text, delim) | local function split(text, delim) | ||
local out = {} | local out = {} | ||
| Line 12: | Line 13: | ||
end | end | ||
-- Build a UL/OL list of links to subpages: | -- Build a UL/OL list of <a> links that point to subpages of `base`. | ||
-- base = page | -- Args: | ||
-- floors | -- base : page prefix; default = current page (FULLPAGENAME) | ||
-- ol | -- floors : "A;B;C" or "Target|Label;Target2|Label2" | ||
-- before | -- ol : "1"/"true" to use <ol>; otherwise <ul> | ||
-- before : optional text prepended to each label (display only) | |||
-- after : optional text appended to each label (display only) | |||
function p.dungeonFloors(frame) | function p.dungeonFloors(frame) | ||
local args = frame.args | local args = frame.args | ||
| Line 25: | Line 28: | ||
local after = args.after or "" | local after = args.after or "" | ||
if #floors == 0 then return "" end | if #floors == 0 then | ||
return "" | |||
end | |||
local items = {} | local items = {} | ||
for _, | for _, item in ipairs(floors) do | ||
-- | -- Support "Target|Label" per item; default label = target | ||
local | local target, label = item:match("^(.-)|(.*)$") | ||
table.insert(items, '<li>' .. | if not target then | ||
target = item | |||
label = item | |||
end | |||
target = mw.text.trim(target) | |||
label = mw.text.trim(label) | |||
-- Build full subpage title and URL | |||
local titleText = base .. "/" .. target | |||
local titleObj = mw.title.new(titleText) | |||
local url = titleObj and titleObj:localUrl() or ("/wiki/" .. mw.uri.encode(titleText)) | |||
-- Escape label for HTML; add optional before/after decorations (display only) | |||
local linkText = before .. label .. after | |||
table.insert(items, '<li><a href="' .. url .. '">' .. mw.text.encode(linkText) .. '</a></li>') | |||
end | end | ||
return | |||
return '<' .. listTag .. '>' .. table.concat(items) .. '</' .. listTag .. '>' | |||
end | end | ||
return p | return p | ||
Revision as of 04:36, 22 August 2025
Documentation for this module may be created at Module:PadUtils/doc
-- Module:PadUtils
local p = {}
-- Split helper (ignores empty parts)
local function split(text, delim)
local out = {}
if not text or text == "" then return out end
for part in mw.text.gsplit(text, delim, true) do
part = mw.text.trim(part)
if part ~= "" then table.insert(out, part) end
end
return out
end
-- Build a UL/OL list of <a> links that point to subpages of `base`.
-- Args:
-- base : page prefix; default = current page (FULLPAGENAME)
-- floors : "A;B;C" or "Target|Label;Target2|Label2"
-- ol : "1"/"true" to use <ol>; otherwise <ul>
-- before : optional text prepended to each label (display only)
-- after : optional text appended to each label (display only)
function p.dungeonFloors(frame)
local args = frame.args
local base = args.base and mw.text.trim(args.base) or mw.title.getCurrentTitle().fullText
local floors = split(args.floors or "", ";")
local listTag = (args.ol == "1" or args.ol == "true") and "ol" or "ul"
local before = args.before or ""
local after = args.after or ""
if #floors == 0 then
return ""
end
local items = {}
for _, item in ipairs(floors) do
-- Support "Target|Label" per item; default label = target
local target, label = item:match("^(.-)|(.*)$")
if not target then
target = item
label = item
end
target = mw.text.trim(target)
label = mw.text.trim(label)
-- Build full subpage title and URL
local titleText = base .. "/" .. target
local titleObj = mw.title.new(titleText)
local url = titleObj and titleObj:localUrl() or ("/wiki/" .. mw.uri.encode(titleText))
-- Escape label for HTML; add optional before/after decorations (display only)
local linkText = before .. label .. after
table.insert(items, '<li><a href="' .. url .. '">' .. mw.text.encode(linkText) .. '</a></li>')
end
return '<' .. listTag .. '>' .. table.concat(items) .. '</' .. listTag .. '>'
end
return p