モジュール:Labelled list hatnote
表示
モジュールの解説[作成]
local mHatnote = require('Module:Hatnote')
local mHatlist = require('Module:Hatnote list')
local mArguments -- initialize lazily
local p = {}
-- Defaults global to this module
local defaults = {
label = 'を参照', -- Final fallback for label argument
mainLabelForm = '→詳細は%s%s', -- Label format for main template
labelForm = '→%s%s', -- Label format for see_also template
prefixes = {'label', 'label ', 'l'},
template = 'Module:Labelled list hatnote'
}
-- 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 = prefixes or defaults.prefixes
local pages = {}
for k, v in pairs(args) do
if type(k) == 'number' then
local display
for i = 1, #prefixes do
display = args[prefixes[i] .. k]
if display then break end
end
local page = display and
string.format('%s|%s', string.gsub(v, '|.*$', ''), display) or v
pages[#pages + 1] = page
end
end
return pages
end
-- Produces a labelled pages-list hatnote.
function p.labelledList(frame)
mArguments = require('Module:Arguments')
local template = frame:getParent():getTitle()
if template ~= 'Template:Main' and template ~= 'Template:Main/sandbox' then
template = 'Template:See Also' -- Default template if not Main
end
local args = mArguments.getArgs(frame, {parentOnly = true})
local pages = p.preprocessDisplays(args)
local labels
local labelForm
if template == 'Template:Main' or template == 'Template:Main/sandbox' then
labels = {defaults.label}
labelForm = frame.args.labelForm or defaults.mainLabelForm
else
labels = {frame.args[1] or defaults.label}
labels[2] = frame.args[2] or labels[1]
labelForm = frame.args.labelForm or defaults.labelForm
end
local options = {
extraclasses = frame.args.extraclasses,
category = args.category,
selfref = frame.args.selfref or args.selfref,
template = template,
labelForm = labelForm
}
return p._labelledList(pages, labels, options)
end
function p._labelledList(pages, labels, options)
labels = labels or {}
if #pages == 0 then
return mHatnote.makeWikitextError(
'ページ名が指定されていません',
(options.template or defaults.template) .. '#エラー',
options.category
)
end
local label = (#pages == 1 and labels[1] or labels[2]) or defaults.label
local text = string.format(
options.labelForm or defaults.labelForm,
mHatlist.andList(pages, true),
label
)
local hnOptions = {
extraclasses = options.extraclasses,
selfref = options.selfref
}
-- Render the HTML div with the specified style and classes
local html = string.format(
'<div class="rellink %s" style="margin-bottom: 0.5em; padding-left: 2em; font-size: 90%%;">%s</div>',
options.extraclasses or '',
mHatnote._hatnote(text, hnOptions)
)
return html
end
return p