Module:Hatnote list: Difference between revisions

m
4 revisions imported from wikipedia:Module:Hatnote_list
(Removed default texts set in forSeeArgsToTable(); unset items in table are now empty rather than filled with defaults; defaults are filled in forSeeTableToString().)
m (4 revisions imported from wikipedia:Module:Hatnote_list)
 
(13 intermediate revisions by 9 users not shown)
Line 26:
separator = ",",
altSeparator = ";",
space = " ",
formatted = false
}
 
Line 40 ⟶ 41:
end
local s = options.space
-- Format the list if requested
if options.formatted then list = mHatnote.formatPages(unpack(list)) end
-- Set the separator; if any item contains it, use the alternate separator
local separator = options.separator
--searches display text only
function searchDisp(t, f)
return string.find(string.sub(t, (string.find(t, '|') or 0) + 1), f)
end
for k, v in pairs(list) do
if string.findsearchDisp(v, separator) then
separator = options.altSeparator
break
Line 50 ⟶ 57:
-- Set the conjunction, apply Oxford comma, and force a comma if #1 has "§"
local conjunction = s .. options.conjunction .. s
if #list == 2 and string.findsearchDisp(list[1], "§") or #list > 2 then
conjunction = separator .. conjunction
end
Line 57 ⟶ 64:
end
 
--DRY function
-- Stringifies a list with "and"
function p.andListconjList (conj, list, fmt)
return stringifyList(list, {conjunction = "and"conj, formatted = fmt})
end
 
-- Stringifies a listlists with "and" or "or"
function p.orListandList (list...) return conjList("and", ...) end
function p.orList (...) return conjList("or", ...) end
return stringifyList(list, {conjunction = "or"})
end
 
--------------------------------------------------------------------------------
Line 76 ⟶ 82:
--default options table used across the forSee family of functions
local forSeeDefaultOptions = {
andKeyword = 'and',
title = mw.title.getCurrentTitle().text,
otherText = 'other uses',
forseeFormforSeeForm = 'For %s, see %s.',
punctuationCollapseReplacements = {
 
--Collapses duplicate punctuation
function punctuationCollapse (text)
local replacements = {
["%.%.$"] = ".",
["%?%.$"] = "?",
Line 87 ⟶ 98:
["%!%]%]%.$"] = "!]]"
}
for k, v in pairs(replacements) do text = string.gsub(text, k, v) end
return text
end
 
-- Structures arguments into a table for stringification, & options
Line 105 ⟶ 118:
if type(k) == 'number' and k > maxArg then maxArg = k end
end
-- Structure the data out from the parameter list:
-- * forTable is the wrapper table, with forRow rows
-- * Rows are tables of a "use" string and& a "pages" table of pagename strings
-- * Blanks are left empty for defaulting elsewhere, but can terminate list
local forTable = {}
local i = from
local terminated = false
-- If there is extra text, and no arguments are given, give nil value
-- Repeat to generate and append each row
-- to not produce default of "For other uses, see foo (disambiguation)"
if options.extratext and i > maxArg then return nil end
-- RepeatLoop to generate and append each rowrows
repeat
-- New empty row
local forRow = {}
-- If there's aOn blank use, assume the list's ended, leave& break at end of emptythis forloop
-- defaulting elsewhere, and break at the end of this loop-through.
forRow.use = args[i]
if not args[i] then terminated = true end
-- New empty list of pages
forRow.pages = {}
-- Try to insertInsert first pages item; empty is ignored/defaultedif elsewherepresent
table.insert(forRow.pages, args[i + 1])
-- If the optionparam after next is "and", do an inner loop whereto wecollect collectparams
-- itemsuntil followingthe "and"'s untilstop. theBlanks are ignored: "1|and||and|3"'s stop. If there's a{1, blank3}
while args[i + 2] == 'and'options.andKeyword do
-- where we'd expect an item, ignore it: "1|and||and|3" → {1, 3}
while args[i + 2] == 'and' do
if args[i + 3] then
table.insert(forRow.pages, args[i + 3])
end
-- Increment to the next "and"
i = i + 2
end
-- Increment to the next use
i = i + 2
-- AddAppend the row to the table
table.insert(forTable, forRow)
until terminated or i > maxArg
Line 142 ⟶ 157:
end
 
-- TakesStringifies a table as formatted by forSeeArgsToTable and stringifies it
function p.forSeeTableToString (forSeeTable, options)
-- Type-checks and defaults
checkType("forSeeTableToString", 1, forSeeTable, "table", true)
checkType("forSeeTableToString", 2, options, "table", true)
options = options or {}
Line 151 ⟶ 166:
if options[k] == nil then options[k] = v end
end
-- FormatStringify each for-see item and makeinto a table containing themlist
local strList = {}
forif k, v in pairs(forSeeTable) dothen
for k, v in pairs(forSeeTable) do
local useStr = v.use or options.otherText
local pagesStr = p.andList(mHatnote.formatPages(unpack(v.pages))) or
local pagesStr = p.andList(v.pages, true) or mHatnote._formatLink(mHatnote.disambiguate(options.title))
local forSeeStr = string.format(options.forseeFormforSeeForm, useStr, pagesStr)
forSeeStr = string.gsubpunctuationCollapse(forSeeStr, k, v)
for k, v in pairs(options.punctuationCollapseReplacements) do
table.insert(strList, forSeeStr)
forSeeStr = string.gsub(forSeeStr, k, v)
end
table.insert(strList, forSeeStr)
end
if options.extratext then table.insert(strList, punctuationCollapse(options.extratext..'.')) end
-- Return the concatenated list
return table.concat(strList, ' ')
Line 168 ⟶ 183:
 
-- Produces a "For X, see [[Y]]" string from arguments. Expects index gaps
-- but not blank or /whitespace values;. thoseIgnores shouldnamed beargs filtered.and Ignoresargs < "from".
-- arguments less than "from", and named arguments.
function p._forSee (args, from, options)
local forSeeTable = p.forSeeArgsToTable(args, from, options)
Line 175 ⟶ 189:
end
 
-- CallsAs _forSee, but pulls fromuses the frame.
function p.forSee (frame, from, options)
mArguments = require('Module:Arguments')