https://londonhuawiki.wpi.edu/index.php?title=Module:Hatnote_list&feed=atom&action=historyModule:Hatnote list - Revision history2024-03-29T12:02:56ZRevision history for this page on the wikiMediaWiki 1.31.8https://londonhuawiki.wpi.edu/index.php?title=Module:Hatnote_list&diff=1104&oldid=prevDfmurphy: 1 revision imported2017-04-12T17:49:24Z<p>1 revision imported</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">Revision as of 17:49, 12 April 2017</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Dfmurphyhttps://londonhuawiki.wpi.edu/index.php?title=Module:Hatnote_list&diff=1103&oldid=prevNihiltres: Updated from sandbox2016-06-25T06:18:53Z<p>Updated from sandbox</p>
<p><b>New page</b></p><div>--------------------------------------------------------------------------------<br />
-- Module:Hatnote list --<br />
-- --<br />
-- This module produces and formats lists for use in hatnotes. In particular, --<br />
-- it implements the for-see list, i.e. lists of "For X, see Y" statements, --<br />
-- as used in {{about}}, {{redirect}}, and their variants. Also introduced --<br />
-- are andList & orList helpers for formatting lists with those conjunctions. --<br />
--------------------------------------------------------------------------------<br />
<br />
local mArguments --initialize lazily<br />
local mHatnote = require('Module:Hatnote')<br />
local libraryUtil = require('libraryUtil')<br />
local checkType = libraryUtil.checkType<br />
local p = {}<br />
<br />
--------------------------------------------------------------------------------<br />
-- List stringification helper functions<br />
--<br />
-- These functions are used for stringifying lists, usually page lists inside<br />
-- the "Y" portion of "For X, see Y" for-see items.<br />
--------------------------------------------------------------------------------<br />
<br />
--default options table used across the list stringification functions<br />
local stringifyListDefaultOptions = {<br />
conjunction = "and",<br />
separator = ",",<br />
altSeparator = ";",<br />
space = " ",<br />
formatted = false<br />
}<br />
<br />
-- Stringifies a list generically; probably shouldn't be used directly<br />
function stringifyList(list, options)<br />
-- Type-checks, defaults, and a shortcut<br />
checkType("stringifyList", 1, list, "table")<br />
if #list == 0 then return nil end<br />
checkType("stringifyList", 2, options, "table", true)<br />
options = options or {}<br />
for k, v in pairs(stringifyListDefaultOptions) do<br />
if options[k] == nil then options[k] = v end<br />
end<br />
local s = options.space<br />
-- Format the list if requested<br />
if options.formatted then list = mHatnote.formatPages(unpack(list)) end<br />
-- Set the separator; if any item contains it, use the alternate separator<br />
local separator = options.separator<br />
--searches display text only<br />
function searchDisp(t, f)<br />
return string.find(string.sub(t, (string.find(t, '|') or 0) + 1), f)<br />
end<br />
for k, v in pairs(list) do<br />
if searchDisp(v, separator) then<br />
separator = options.altSeparator<br />
break<br />
end<br />
end<br />
-- Set the conjunction, apply Oxford comma, and force a comma if #1 has "§"<br />
local conjunction = s .. options.conjunction .. s<br />
if #list == 2 and searchDisp(list[1], "§") or #list > 2 then<br />
conjunction = separator .. conjunction<br />
end<br />
-- Return the formatted string<br />
return mw.text.listToText(list, separator .. s, conjunction)<br />
end<br />
<br />
--DRY function<br />
function conjList (conj, list, fmt)<br />
return stringifyList(list, {conjunction = conj, formatted = fmt})<br />
end<br />
<br />
-- Stringifies lists with "and" or "or"<br />
function p.andList (...) return conjList("and", ...) end<br />
function p.orList (...) return conjList("or", ...) end<br />
<br />
--------------------------------------------------------------------------------<br />
-- For see<br />
--<br />
-- Makes a "For X, see [[Y]]." list from raw parameters. Intended for the<br />
-- {{about}} and {{redirect}} templates and their variants.<br />
--------------------------------------------------------------------------------<br />
<br />
--default options table used across the forSee family of functions<br />
local forSeeDefaultOptions = {<br />
andKeyword = 'and',<br />
title = mw.title.getCurrentTitle().text,<br />
otherText = 'other uses',<br />
forSeeForm = 'For %s, see %s.'<br />
}<br />
<br />
--Collapses duplicate punctuation<br />
function punctuationCollapse (text)<br />
local replacements = {<br />
["%.%.$"] = ".",<br />
["%?%.$"] = "?",<br />
["%!%.$"] = "!",<br />
["%.%]%]%.$"] = ".]]",<br />
["%?%]%]%.$"] = "?]]",<br />
["%!%]%]%.$"] = "!]]"<br />
}<br />
for k, v in pairs(replacements) do text = string.gsub(text, k, v) end<br />
return text<br />
end<br />
<br />
-- Structures arguments into a table for stringification, & options<br />
function p.forSeeArgsToTable (args, from, options)<br />
-- Type-checks and defaults<br />
checkType("forSeeArgsToTable", 1, args, 'table')<br />
checkType("forSeeArgsToTable", 2, from, 'number', true)<br />
from = from or 1<br />
checkType("forSeeArgsToTable", 3, options, 'table', true)<br />
options = options or {}<br />
for k, v in pairs(forSeeDefaultOptions) do<br />
if options[k] == nil then options[k] = v end<br />
end<br />
-- maxArg's gotten manually because getArgs() and table.maxn aren't friends<br />
local maxArg = 0<br />
for k, v in pairs(args) do<br />
if type(k) == 'number' and k > maxArg then maxArg = k end<br />
end<br />
-- Structure the data out from the parameter list:<br />
-- * forTable is the wrapper table, with forRow rows<br />
-- * Rows are tables of a "use" string & a "pages" table of pagename strings<br />
-- * Blanks are left empty for defaulting elsewhere, but can terminate list<br />
local forTable = {}<br />
local i = from<br />
local terminated = false<br />
-- Loop to generate rows<br />
repeat<br />
-- New empty row<br />
local forRow = {}<br />
-- On blank use, assume list's ended & break at end of this loop<br />
forRow.use = args[i]<br />
if not args[i] then terminated = true end<br />
-- New empty list of pages<br />
forRow.pages = {}<br />
-- Insert first pages item if present<br />
table.insert(forRow.pages, args[i + 1])<br />
-- If the param after next is "and", do inner loop to collect params<br />
-- until the "and"'s stop. Blanks are ignored: "1|and||and|3" → {1, 3}<br />
while args[i + 2] == options.andKeyword do<br />
if args[i + 3] then <br />
table.insert(forRow.pages, args[i + 3])<br />
end<br />
-- Increment to next "and"<br />
i = i + 2<br />
end<br />
-- Increment to next use<br />
i = i + 2<br />
-- Append the row<br />
table.insert(forTable, forRow)<br />
until terminated or i > maxArg<br />
<br />
return forTable<br />
end<br />
<br />
-- Stringifies a table as formatted by forSeeArgsToTable<br />
function p.forSeeTableToString (forSeeTable, options)<br />
-- Type-checks and defaults<br />
checkType("forSeeTableToString", 1, forSeeTable, "table")<br />
checkType("forSeeTableToString", 2, options, "table", true)<br />
options = options or {}<br />
for k, v in pairs(forSeeDefaultOptions) do<br />
if options[k] == nil then options[k] = v end<br />
end<br />
-- Stringify each for-see item into a list<br />
local strList = {}<br />
for k, v in pairs(forSeeTable) do<br />
local useStr = v.use or options.otherText<br />
local pagesStr = p.andList(v.pages, true) or<br />
mHatnote._formatLink(mHatnote.disambiguate(options.title))<br />
local forSeeStr = string.format(options.forSeeForm, useStr, pagesStr)<br />
forSeeStr = punctuationCollapse(forSeeStr)<br />
table.insert(strList, forSeeStr)<br />
end<br />
-- Return the concatenated list<br />
return table.concat(strList, ' ')<br />
end<br />
<br />
-- Produces a "For X, see [[Y]]" string from arguments. Expects index gaps<br />
-- but not blank/whitespace values. Ignores named args and args < "from".<br />
function p._forSee (args, from, options)<br />
local forSeeTable = p.forSeeArgsToTable(args, from, options)<br />
return p.forSeeTableToString(forSeeTable, options)<br />
end<br />
<br />
-- As _forSee, but uses the frame.<br />
function p.forSee (frame, from, options)<br />
mArguments = require('Module:Arguments')<br />
return p._forSee(mArguments.getArgs(frame), from, options)<br />
end<br />
<br />
return p</div>Nihiltres