Title: Module:Labelled list hatnote - freem URL Source: https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote Markdown Content: _Documentation for this module may be created at [Module:Labelled list hatnote/doc](https://freemwiki.com/index.php?title=Module:Labelled\_list\_hatnote/doc&action=edit&redlink=1 "Module:Labelled list hatnote/doc (page does not exist)")_ [](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-1)--------------------------------------------------------------------------------[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-2)-- Labelled list --[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-3)-- --[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-4)-- This module does the core work of creating a hatnote composed of a list --[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-5)-- prefixed by a colon-terminated label, i.e. "LABEL: [andList of pages]", --[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-6)-- for {{[see also](https://freemwiki.com/wiki/Template:See_also "Template:See also")}} and similar templates. --[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-7)--------------------------------------------------------------------------------[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-8)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-9)local mHatnote = require(['Module:Hatnote'](https://freemwiki.com/wiki/Module:Hatnote))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-10)local mHatlist = require(['Module:Hatnote list'](https://freemwiki.com/wiki/Module:Hatnote_list))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-11)local mArguments --initialize lazily[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-12)local yesno --initialize lazily[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-13)local p = {}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-14)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-15)-- Defaults global to this module[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-16)local defaults = {[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-17)label = 'See also', --Final fallback for label argument[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-18)labelForm = '%s: %s',[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-19)prefixes = {'label', 'label ', 'l'},[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-20)template = 'Module:Labelled list hatnote'[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-21)}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-22)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-23)-- Localizable message strings[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-24)local msg = {[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-25)errorSuffix = '#Errors',[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-26)noInputWarning = 'no page names specified',[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-27)noOutputWarning =[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-28)"'''[[%s]] — no output: none of the target pages exist.'''"[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-29)}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-30)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-31)-- Helper function that pre-combines display parameters into page arguments.[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-32)-- Also compresses sparse arrays, as a desirable side-effect.[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-33)function p.preprocessDisplays (args, prefixes)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-34)-- Prefixes specify which parameters, in order, to check for display options[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-35)-- They each have numbers auto-appended, e.g. 'label1', 'label 1', & 'l1'[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-36)prefixes = prefixes or defaults.prefixes[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-37)local indices = {}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-38)local sparsePages = {}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-39)for k, v in pairs(args) do[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-40)if type(k) == 'number' then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-41)indices[#indices + 1] = k[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-42)local display[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-43)for i = 1, #prefixes do[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-44)display = args[prefixes[i] .. k][](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-45)if display then break end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-46)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-47)sparsePages[k] = display and[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-48)string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-49)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-50)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-51)table.sort(indices)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-52)local pages = {}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-53)for k, v in ipairs(indices) do pages[#pages + 1] = sparsePages[v] end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-54)return pages[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-55)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-56)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-57)--Helper function to get a page target from a processed page string[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-58)--e.g. "Page|Label" → "Page" or "Target" → "Target"[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-59)local function getTarget(pagename)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-60) local pipe = string.find(pagename, '|')[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-61)return string.sub(pagename, 0, pipe and pipe - 1 or nil)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-62)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-63)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-64)-- Produces a labelled pages-list hatnote.[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-65)-- The main frame (template definition) takes 1 or 2 arguments, for a singular[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-66)-- and (optionally) plural label respectively:[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-67)-- * {{#invoke:Labelled list hatnote|labelledList|Singular label|Plural label}}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-68)-- The resulting template takes pagename & label parameters normally.[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-69)function p.labelledList (frame)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-70)mArguments = require(['Module:Arguments'](https://freemwiki.com/wiki/Module:Arguments))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-71)yesno = require(['Module:Yesno'](https://freemwiki.com/wiki/Module:Yesno))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-72)local labels = {frame.args[1] or defaults.label}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-73)labels[2] = frame.args[2] or labels[1][](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-74)labels[3] = frame.args[3] --no defaulting[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-75)labels[4] = frame.args[4] --no defaulting[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-76)local template = frame:getParent():getTitle()[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-77)local args = mArguments.getArgs(frame, {parentOnly = true})[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-78)local pages = p.preprocessDisplays(args)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-79)local options = {[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-80)category = yesno(args.category),[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-81)extraclasses = frame.args.extraclasses,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-82)ifexists = yesno(frame.args.ifexists),[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-83)namespace = frame.args.namespace or args.namespace,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-84)selfref = yesno(frame.args.selfref or args.selfref),[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-85)template = template[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-86)}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-87)return p._labelledList(pages, labels, options)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-88)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-89)local function exists(title)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-90)local success, result = pcall(function() return title.exists end)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-91)if success then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-92)return result[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-93)else[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-94)return true[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-95)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-96)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-97)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-98)function p._labelledList (pages, labels, options)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-99)local removednonexist = false[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-100)if options.ifexists then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-101)for k = #pages, 1, -1 do --iterate backwards to allow smooth removals[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-102)local v = pages[k][](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-103)if mw.ustring.sub(mw.text.trim(v), 1, 1) ~= "#" then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-104)local title = mw.title.new(getTarget(v), namespace)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-105)if (v == '') or (title == nil) or not exists(title) then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-106)table.remove(pages, k)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-107)removednonexist = true[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-108)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-109)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-110)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-111)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-112)labels = labels or {}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-113)label = (#pages == 1 and labels[1] or labels[2]) or defaults.label[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-114)for k, v in pairs(pages) do [](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-115)if mHatnote.findNamespaceId(v) ~= 0 then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-116)label =[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-117)([](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-118)#pages == 1 and[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-119)(labels[3] or labels[1] or defaults.label) or[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-120)(labels[4] or labels[2] or defaults.label)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-121)) or defaults.label[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-122)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-123)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-124)if #pages == 0 then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-125)if removednonexist then[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-126)mw.addWarning([](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-127)string.format([](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-128)msg.noOutputWarning, options.template or defaults.template[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-129))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-130))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-131)return ''[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-132)else[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-133)return mHatnote.makeWikitextError([](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-134)msg.noInputWarning,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-135)(options.template or defaults.template) .. msg.errorSuffix,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-136)options.category[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-137))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-138)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-139)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-140)local text = string.format([](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-141)options.labelForm or defaults.labelForm,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-142)label,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-143)mHatlist.andList(pages, true)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-144))[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-145)local hnOptions = {[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-146)extraclasses = options.extraclasses,[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-147)selfref = options.selfref[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-148)}[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-149)return mHatnote._hatnote(text, hnOptions)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-150)end[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-151)[](https://freemwiki.com/index.php?oldid=579774&title=Module%3ALabelled_list_hatnote#L-152)return p