Module:PadUtils: Difference between revisions
Appearance
GlifterPad (talk | contribs) Created page with "local p = {} -- Split a string by a delimiter (keeps empty=false) 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 links to subpages: -- base = page prefix (default: FULLPAGENAME) -- floors = "A;B;C" -- ol = "1" o..." |
GlifterPad (talk | contribs) No edit summary |
||
| Line 33: | Line 33: | ||
table.insert(items, '<li>' .. link .. '</li>') | table.insert(items, '<li>' .. link .. '</li>') | ||
end | end | ||
return string.format('<%s>%s</%s>', listTag, table.concat(items), listTag) | return frame:preprocess( string.format('<%s>%s</%s>', listTag, table.concat(items), listTag) ) | ||
end | end | ||
return p | return p | ||
Revision as of 04:34, 22 August 2025
Documentation for this module may be created at Module:PadUtils/doc
local p = {}
-- Split a string by a delimiter (keeps empty=false)
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 links to subpages:
-- base = page prefix (default: FULLPAGENAME)
-- floors = "A;B;C"
-- ol = "1" or "true" to use <ol> instead of <ul>
-- before/after = optional text around each anchor text (rarely needed)
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 _, name in ipairs(floors) do
-- MediaWiki will auto-handle spaces→underscores; keep display text as-is
local link = string.format('[[%s/%s|%s%s%s]]', base, name, before, name, after)
table.insert(items, '<li>' .. link .. '</li>')
end
return frame:preprocess( string.format('<%s>%s</%s>', listTag, table.concat(items), listTag) )
end
return p