Module:Hatnote list: Difference between revisions
m
19 revisions imported from wikipedia:Module:Hatnote_list
(Updated with "formatted" option for andList and orList from sandbox) |
Squareblob (talk | contribs) m (19 revisions imported from wikipedia:Module:Hatnote_list) |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 45:
-- Set the separator; if any item contains it, use the alternate separator
local separator = options.separator
--searches display text only
local 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
separator = options.altSeparator
break
Line 53 ⟶ 57:
-- Set the conjunction, apply Oxford comma, and force a comma if #1 has "§"
local conjunction = s .. options.conjunction .. s
if #list == 2 and
conjunction = separator .. conjunction
end
Line 60 ⟶ 64:
end
--DRY function
function
return stringifyList(list, {conjunction =
end
-- Stringifies
function p.
function p.orList (...) return conjList("or", ...) end
▲end
--------------------------------------------------------------------------------
Line 79 ⟶ 82:
--default options table used across the forSee family of functions
local forSeeDefaultOptions = {
andKeyword = 'and',
title = mw.title.getCurrentTitle().text,
otherText = 'other uses',
}▼
--Collapses duplicate punctuation
function punctuationCollapse (text)
local replacements = {
["%.%.$"] = ".",
["%?%.$"] = "?",
Line 90 ⟶ 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 108 ⟶ 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
-- * 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
repeat
-- New empty row
local forRow = {}
--
forRow.use = args[i]
if not args[i] then terminated = true end
-- New empty list of pages
forRow.pages = {}
--
table.insert(forRow.pages, args[i + 1])
-- If the
--
▲ while args[i + 2] == 'and' do
if args[i + 3] then
table.insert(forRow.pages, args[i + 3])
end
-- Increment to
i = i + 2
end
-- Increment to
i = i + 2
--
table.insert(forTable, forRow)
until terminated or i > maxArg
Line 145 ⟶ 157:
end
--
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 154 ⟶ 166:
if options[k] == nil then options[k] = v end
end
--
local strList = {}
for k, v in pairs(forSeeTable) do
local useStr = v.use or options.otherText
local pagesStr = p.andList(v.pages, true) or mHatnote._formatLink
local forSeeStr = string.format(options.
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 171 ⟶ 183:
-- Produces a "For X, see [[Y]]" string from arguments. Expects index gaps
-- but not blank
function p._forSee (args, from, options)
local forSeeTable = p.forSeeArgsToTable(args, from, options)
Line 178 ⟶ 189:
end
--
function p.forSee (frame, from, options)
mArguments = require('Module:Arguments')
|