The editors' meeting has been canceled for technical reasons.

Module:Labelled list hatnote: Difference between revisions

Jump to navigation Jump to search
m 1 revision imported
No edit summary
 
Line 1: Line 1:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--                              Labelled list                                --
--                              Labelled list                                --
--                                                                            --
-- This module generates a hatnote with a colon-terminated label, e.g.,      --
-- This module does the core work of creating a hatnote composed of a list    --
-- "LABEL: [andList of pages]", for {{see also}} and similar templates.       --
-- prefixed by a colon-terminated label, i.e. "LABEL: [andList of pages]",   --
-- for {{see also}} and similar templates.                                   --
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local mHatnote = require('Module:Hatnote')
local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
local mHatlist = require('Module:Hatnote list')
local mArguments --initialize lazily
local mArguments
local p = {}
local p = {}


-- Defaults global to this module
-- Defaults for this module
-- 本地化注意
local defaults = {
local defaults = {
label = '参见', --Final fallback for label argument
    label = 'See also', -- Fallback for the label argument
labelForm = '%s:%s',
    labelForm = '%s: %s', -- Format for the label
prefixes = {'label', 'label ', 'l'},
    prefixes = {'label', 'label ', 'l'}, -- Prefixes for display parameters
template = 'Module:Labelled list hatnote'
    template = 'Module:Labelled list hatnote'
}
}


-- Helper function that pre-combines display parameters into page arguments.
-- Preprocess display parameters and combine them with page arguments.
-- Also compresses sparse arrays, as a desirable side-effect.
local function preprocessDisplays(args, prefixes)
function p.preprocessDisplays (args, prefixes)
    prefixes = prefixes or defaults.prefixes
-- Prefixes specify which parameters, in order, to check for display options
    local pages = {}
-- They each have numbers auto-appended, e.g. 'label1', 'label 1', & 'l1'
    for k, v in pairs(args) do
prefixes = prefixes or defaults.prefixes
        if type(k) == 'number' then
local pages = {}
            local display
for k, v in pairs(args) do
            for _, prefix in ipairs(prefixes) do
if type(k) == 'number' then
                display = args[prefix .. k]
local display
                if display then break end
for i = 1, #prefixes do
            end
display = args[prefixes[i] .. k]
            local page = display and string.format('%s|%s', v:gsub('|.*$', ''), display) or v
if display then break end
            table.insert(pages, page)
end
        end
local page = display and
    end
string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
    return pages
pages[#pages + 1] = page
end
end
return pages
end
end


-- Produces a labelled pages-list hatnote.
-- Main entry point for the module.
-- The main frame (template definition) takes 1 or 2 arguments, for a singular
function p.labelledList(frame)
-- and (optionally) plural label respectively:
    mArguments = mArguments or require('Module:Arguments')
-- * {{#invoke:Labelled list hatnote|labelledList|Singular label|Plural label}}
    local args = mArguments.getArgs(frame, {parentOnly = true})
-- The resulting template takes pagename & label parameters normally.
    local labels = {frame.args[1] or defaults.label, frame.args[2] or frame.args[1] or defaults.label}
function p.labelledList (frame)
    local pages = preprocessDisplays(args)
mArguments = require('Module:Arguments')
    local options = {
local labels = {frame.args[1] or defaults.label}
        extraclasses = frame.args.extraclasses,
labels[2] = frame.args[2] or labels[1]
        category = args.category,
local template = frame:getParent():getTitle()
        selfref = frame.args.selfref or args.selfref,
local args = mArguments.getArgs(frame, {parentOnly = true})
        template = frame:getParent():getTitle()
local pages = p.preprocessDisplays(args)
    }
local options = {
    return p._labelledList(pages, labels, options)
extraclasses = frame.args.extraclasses,
category = args.category,
selfref = frame.args.selfref or args.selfref,
template = template
}
return p._labelledList(pages, labels, options)
end
end


function p._labelledList (pages, labels, options)
-- Generates the hatnote text and wraps it.
labels = labels or {}
function p._labelledList(pages, labels, options)
if #pages == 0 then
    if #pages == 0 then
-- 本地化注意
        return mHatnote.makeWikitextError(
return mHatnote.makeWikitextError(
            'Page name not specified',
'未指定页面名称',
            (options.template or defaults.template) .. '#error',
(options.template or defaults.template) .. '#错误',
            options.category
options.category
        )
)
    end
end
 
label = (#pages == 1 and labels[1] or labels[2]) or defaults.label
    local label = (#pages == 1 and labels[1] or labels[2]) or defaults.label
local text = string.format(
    local text = string.format(
options.labelForm or defaults.labelForm,
        options.labelForm or defaults.labelForm,
label,
        label,
mHatlist.andList(pages, true)
        mHatlist.andList(pages, true)
)
    )
local hnOptions = {
    return mHatnote._hatnote(text, {extraclasses = options.extraclasses, selfref = options.selfref})
extraclasses = options.extraclasses,
selfref = options.selfref
}
return mHatnote._hatnote(text, hnOptions)
end
end


return p
return p