Module:Section link: Difference between revisions

fix error placement and tweak default conjunction
(create replacement for Template:Section link)
 
(fix error placement and tweak default conjunction)
Line 4:
 
local p = {}
 
local function makeWikitextError(msg)
return string.format(
'<strong class="error">[[Template:Section link]] error: %s.</strong>',
msg
)
end
 
local function makeSectionLink(page, section, display)
Line 32 ⟶ 25:
options = options or {}
title = title or mw.title.getCurrentTitle()
if #sections < 1 then
return makeWikitextError('at least one section must be specified')
end
 
-- Make the link(s).
Line 48 ⟶ 38:
return makeSectionLink(linkPage, section, display)
else
-- Multiple sections. First, makevalidate the listsections of linkstable.
if #sections < 1 then
return makeWikitextError error('at least one section must be specified', 2)
end
 
-- Make the list of links.
local ret = {}
for i, section in ipairs(sections) do
ret[i] = makeSectionLink(page, section)
end
 
-- Assemble the list of links into a string with mw.text.listToText.
-- We use the default argumentsseparator for mw.text.listToText, unlessbut wea havecustom
-- conjunction. There is also a special case conjunction if we only
-- exactly two sections, in which case we use a custom conjunction.
-- have two links.
local conjunction
if #sections == 2 then
conjunction = '&#8203; and '
else
conjunction = ', and '
end
ret = mw.text.listToText(ret, nil, conjunction)
Line 69 ⟶ 67:
end
ret = intro .. ret
 
return ret
end
Line 107 ⟶ 105:
end
sections = compressArray(sections)
 
return p._main(page, sections, options)
end