---------------------------------------------------------------------------------- Labelled list ---- ---- This module does the core work of creating a hatnote composed of a list ---- prefixed by a colon-terminated label, i.e. "LABEL: [andList of pages]", ---- for and similar templates. ----------------------------------------------------------------------------------
local mHatnote = require('Module:Hatnote')local mHatlist = require('Module:Hatnote list')local mArguments --initialize lazilylocal yesno --initialize lazilylocal p =
-- Defaults global to this modulelocal defaults =
-- Localizable message stringslocal msg =
-- Helper function that pre-combines display parameters into page arguments.-- Also compresses sparse arrays, as a desirable side-effect.function p.preprocessDisplays (args, prefixes) -- Prefixes specify which parameters, in order, to check for display options -- They each have numbers auto-appended, e.g. 'label1', 'label 1', & 'l1' prefixes = prefixes or defaults.prefixes local indices = local sparsePages = for k, v in pairs(args) do if type(k)
--Helper function to get a page target from a processed page string--e.g. "Page|Label" → "Page" or "Target" → "Target"local function getTarget(pagename) local pipe = string.find(pagename, '|') return string.sub(pagename, 0, pipe and pipe - 1 or nil)end
-- Produces a labelled pages-list hatnote.-- The main frame (template definition) takes 1 or 2 arguments, for a singular-- and (optionally) plural label respectively:-- * -- The resulting template takes pagename & label parameters normally.function p.labelledList (frame) mArguments = require('Module:Arguments') yesno = require('Module:Yesno') local labels = labels[2] = frame.args[2] or labels[1] labels[3] = frame.args[3] --no defaulting labels[4] = frame.args[4] --no defaulting local template = frame:getParent:getTitle local args = mArguments.getArgs(frame,) local pages = p.preprocessDisplays(args) local options = return p._labelledList(pages, labels, options)end
function p._labelledList (pages, labels, options) if options.ifexists then for k = #pages, 1, -1 do --iterate backwards to allow smooth removals local v = pages[k] local title = mw.title.new(getTarget(v), namespace) if (v
nil or not title.exists then table.remove(pages, k) end end end labels = labels or label = (#pages
1 and (labels[3] or labels[1] or defaults.label) or (labels[4] or labels[2] or defaults.label) ) or defaults.label end end if #pages
return p