Modul:Format link: Unterschied zwischen den Versionen

K
15 Versionen von wpen:Module:Format_link importiert
(Further copied getArgs from Module:Hatnote and organized into helper and main functions)
K (15 Versionen von wpen:Module:Format_link importiert)
 
(13 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 11: Zeile 11:
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg
local mArguments -- lazily initialise [[Module:Arguments]]
local mArguments -- lazily initialise [[Module:Arguments]]
local mError -- lazily initialise [[Module:Error]]
local yesno -- lazily initialise [[Module:Yesno]]
local yesno -- lazily initialise [[Module:Yesno]]


Zeile 26: Zeile 27:
end
end


local function italicize(s)
local function removeInitialColon(s)
-- Italicize a string.
-- Removes the initial colon from a string, if present.
return '<i>' .. s .. '</i>'
return s:match('^:?(.*)')
end
end


local function maybeItalicize(s, shouldItalicize)
local function maybeItalicize(s, shouldItalicize)
-- italicize s if s is a string and the shouldItalicize parameter is true.
-- Italicize s if s is a string and the shouldItalicize parameter is true.
if s and shouldItalicize then
if s and shouldItalicize then
return italicize(s)
return '<i>' .. s .. '</i>'
else
else
return s
return s
Zeile 77: Zeile 78:
section = section,
section = section,
display = display,
display = display,
}
end
local function formatDisplay(parsed, options)
-- Formats a display string based on a parsed link table (matching the
-- output of parseLink) and an options table (matching the input options for
-- _formatLink).
local page = maybeItalicize(parsed.page, options.italicizePage)
local section = maybeItalicize(parsed.section, options.italicizeSection)
if (not section) then
return page
elseif (not page) then
return mw.ustring.format('§&nbsp;%s', section)
else
return mw.ustring.format('%s §&nbsp;%s', page, section)
end
end
local function missingArgError(target)
mError = require('Module:Error')
return mError.error{message =
'Error: no link or target specified! ([[' .. target .. '#Errors|help]])'
}
}
end
end
Zeile 88: Zeile 111:
yesno = require('Module:Yesno')
yesno = require('Module:Yesno')
local args = getArgs(frame)
local args = getArgs(frame)
local link = args[1]
local link = args[1] or args.link
if not link then
local target = args[3] or args.target
return p.makeWikitextError(
if not (link or target) then
'no link specified',
return missingArgError('Template:Format link')
'Template:Format link#Errors',
args.category
)
end
end
return p._formatLink{
return p._formatLink{
link = link,
link = link,
display = args[2],
display = args[2] or args.display,
target = target,
italicizePage = yesno(args.italicizepage),
italicizePage = yesno(args.italicizepage),
italicizeSection = yesno(args.italicizesection),
italicizeSection = yesno(args.italicizesection),
categorizeMissing = args.categorizemissing
}
}
end
end
Zeile 107: Zeile 130:
-- The formatLink export function, for use in modules.
-- The formatLink export function, for use in modules.
checkType('_formatLink', 1, options, 'table')
checkType('_formatLink', 1, options, 'table')
checkTypeForNamedArg('_formatLink', 'link', options.link, 'string', false)
local function check(key, expectedType) --for brevity
checkTypeForNamedArg(
checkTypeForNamedArg(
'_formatLink',
'_formatLink', key, options[key], expectedType or 'string', true
'display',
)
options.display,
end
'string',
check('link')
true
check('display')
)
check('target')
checkTypeForNamedArg(
check('italicizePage', 'boolean')
'_formatLink',
check('italicizeSection', 'boolean')
'italicizePage',
check('categorizeMissing')
options.italicizePage,
 
'boolean',
-- Normalize link and target and check that at least one is present
true
if options.link == '' then options.link = nil end
)
if options.target == '' then options.target = nil end
checkTypeForNamedArg(
if not (options.link or options.target) then
'_formatLink',
return missingArgError('Module:Format link')
'italicizeSection',
end
options.italicizeSection,
'boolean',
true
)


local parsed = parseLink(options.link)
local parsed = parseLink(options.link)
local display = options.display or parsed.display
local display = options.display or parsed.display
local catMissing = options.categorizeMissing
-- Deal with the case where we don't have to pipe the link
local category = ''
if not display and not parsed.section and not options.italicizePage then
 
return string.format('[[:%s]]', parsed.link)
-- Find the display text
if not display then display = formatDisplay(parsed, options) end
 
-- Handle the target option if present
if options.target then
local parsedTarget = parseLink(options.target)
parsed.link = parsedTarget.link
parsed.page = parsedTarget.page
end
end
 
-- Find the display text for piped links
-- Test if page exists if a diagnostic category is specified
if not display then
if catMissing and (mw.ustring.len(catMissing) > 0) then
local page = maybeItalicize(parsed.page, options.italicizePage)
local title = nil
local section = maybeItalicize(parsed.section, options.italicizeSection)
if parsed.page then title = mw.title.new(parsed.page) end
if not page then
if title and (not title.isExternal) then
display = string.format('§&nbsp;%s', section)
local success, exists = pcall(function() return title.exists end)
elseif section then
if success and not exists then
display = string.format('%s §&nbsp;%s', page, section)
category = mw.ustring.format('[[Category:%s]]', catMissing)
else
end
display = page
end
end
end
end
return string.format('[[:%s|%s]]', parsed.link, display)
-- Format the result as a link
if parsed.link == display then
return mw.ustring.format('[[:%s]]%s', parsed.link, category)
else
return mw.ustring.format('[[:%s|%s]]%s', parsed.link, display, category)
end
end
end
--------------------------------------------------------------------------------
-- Derived convenience functions
--------------------------------------------------------------------------------
function p.formatPages(options, pages)
-- Formats an array of pages using formatLink and the given options table,
-- and returns it as an array. Nil values are not allowed.
local ret = {}
for i, page in ipairs(pages) do
ret[i] = p._formatLink{
link = page,
categorizeMissing = options.categorizeMissing,
italicizePage = options.italicizePage,
italicizeSection = options.italicizeSection
}
end
return ret
end
return p