Module:Hatnote list: Difference between revisions
m
19 revisions imported from wikipedia:Module:Hatnote_list: All aboard the import train again. Originally imported from English Wikipedia.
(Updated from sandbox) |
m (19 revisions imported from wikipedia:Module:Hatnote_list: All aboard the import train again. Originally imported from English Wikipedia.) |
||
(18 intermediate revisions by 10 users not shown) | |||
Line 9:
local mArguments --initialize lazily
local mFormatLink = require('Module:Format link')
local mHatnote = require('Module:Hatnote')
local libraryUtil = require('libraryUtil')
Line 26 ⟶ 27:
separator = ",",
altSeparator = ";",
space = " ",
formatted = false
}
--Searches display text only
local function searchDisp(haystack, needle)
return string.find(
string.sub(haystack, (string.find(haystack, '|') or 0) + 1), needle
)
end▼
-- Stringifies a list generically; probably shouldn't be used directly
local function stringifyList(list, options)
-- Type-checks, defaults, and a shortcut
checkType("stringifyList", 1, list, "table")
Line 40 ⟶ 49:
end
local s = options.space
-- Format the list if requested
if options.formatted then
list = mFormatLink.formatPages(
{categorizeMissing = mHatnote.missingTargetCat}, list
)
end
-- Set the separator; if any item contains it, use the alternate separator
local separator = options.separator
for k, v in pairs(list) do
if
separator = options.altSeparator
break
Line 50 ⟶ 65:
-- 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 57 ⟶ 72:
end
--DRY function
function p.
return stringifyList(list, {conjunction =
end
-- Stringifies
function p.
function p.orList (...) return p.conjList("or", ...) end
▲end
--------------------------------------------------------------------------------
Line 76 ⟶ 90:
--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
local function punctuationCollapse (text)
local replacements = {
["%.%.$"] = ".",
["%?%.$"] = "?",
Line 88 ⟶ 106:
["%!%]%]%.$"] = "!]]"
}
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 106 ⟶ 126:
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] or options.otherText
if not args[i] then terminated = true end
-- New empty list of pages
forRow.pages = {}
-- Insert first pages item if present
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 143 ⟶ 165:
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 152 ⟶ 174:
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
local forSeeStr = string.format(options.forseeForm, useStr, pagesStr)▼
mFormatLink._formatLink{
categorizeMissing = mHatnote.missingTargetCat,
forSeeStr = string.gsub(forSeeStr, k, v)▼
link = mHatnote.disambiguate(options.title)
}
table.insert(strList, forSeeStr)▼
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 169 ⟶ 196:
-- 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 176 ⟶ 202:
end
--
function p.forSee (frame, from, options)
mArguments = require('Module:Arguments')
|