Misplaced Pages

Module:Hatnote list: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 13:51, 21 April 2016 view sourceNihiltres (talk | contribs)Edit filter managers, Administrators83,814 edits Moved form of for-see statement into options← Previous edit Revision as of 15:29, 27 April 2016 view source Nihiltres (talk | contribs)Edit filter managers, Administrators83,814 editsm Changed some variable and structure names: forRow's second item is a table called "pages" instead of a table called "see" now; more logical namingNext edit →
Line 78: Line 78:
if not args then terminated = true end if not args then terminated = true end
-- New empty list of pages -- New empty list of pages
forRow.see = {} forRow.pages = {}
-- If there's not at least one page listed, assume the list's ended, use -- If there's not at least one page listed, assume the list's ended, use
-- the default, and break at end of this loop-through. -- the default, and break at end of this loop-through.
table.insert(forRow.see, args or (options.title .. options.disambiguator)) table.insert(forRow.pages, args or (options.title .. options.disambiguator))
-- If the option after next is "and", do an inner loop where we collect -- If the option after next is "and", do an inner loop where we collect
-- items following "and"'s until the "and"'s stop. If there's a blank -- items following "and"'s until the "and"'s stop. If there's a blank
Line 87: Line 87:
while args == 'and' do while args == 'and' do
if args then if args then
table.insert(forRow.see, args) table.insert(forRow.pages, args)
end end
-- Increment to the next "and" -- Increment to the next "and"
Line 102: Line 102:
for k, v in pairs(forTable) do for k, v in pairs(forTable) do
local useStr = v.use local useStr = v.use
local seeStr = p.andList(mHatnote.formatPages(unpack(v.see))) local pagesStr = p.andList(mHatnote.formatPages(unpack(v.pages)))
table.insert(strList, string.format(options.forseeForm, useStr, seeStr)) table.insert(strList, string.format(options.forseeForm, useStr, pagesStr))
end end
return mw.text.listToText(strList, ' ', ' ') return mw.text.listToText(strList, ' ', ' ')

Revision as of 15:29, 27 April 2016

Module documentation[view] [edit] [history] [purge]
WarningThis Lua module is used in MediaWiki:Wantedpages-summary, and on approximately 1,180,000 pages, or roughly 2% of all pages.
Changes to it can cause immediate changes to the Misplaced Pages user interface.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them.
This module depends on the following other modules:

Usage from wikitext

This module is not designed be used directly from wikitext even though forSee does take an initial frame argument and could potentially be used from wikitext, e.g.:

  • {{hatnote|PREFIX {{#invoke:Hatnote list|forSee|{{tl|For}}|Module:For|{{tl|About}}|Module:About}} POSTFIX}} → PREFIX Lua error at line 104: attempt to call field 'formatPages' (a nil value). POSTFIX

Usage from Lua

To call the module, use
local mHatList = require('Module:Hatnote list')
or similar, then access its methods through the mHatList variable (or whatever was used).

andList

andList takes a list in table form, and returns

Cite error: A <ref> tag is missing the closing </ref> (see the help page).</ref>a string with the list separated with "and" and commas as appropriate.

orList

orList takes a list in table form, and returns a string with the list separated with "or" and commas as appropriate.

forSee

_forSee takes three arguments: a table of trimmed arguments with blanks removed, a "from" number with the index to start at, and an options table, and returns a string with a number of "For X, see ]" sentences. The links are formatted using the methods from Module:Hatnote.

As a convenience, the forSee method (without the leading underscore) takes the same arguments except with a frame instead of an args table, using getArgs() from Module:Arguments to preprocess the arguments.

The above documentation is transcluded from Module:Hatnote list/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

--------------------------------------------------------------------------------
-- For see
--
-- Makes a "For X, see ]." list from raw parameters. Intended for the
-- {{about}} and {{redirect}} templates and their variants. Also incidentally
-- introduces andList & orList helpers, useful for other hatnote lists.
--------------------------------------------------------------------------------

local mArguments --initialize lazily
local mHatnote = require('Module:Hatnote')
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}

function p.andList (andTable)
	-- Stringifies a list with "and"
	local andString = (#andTable > 2 and mw.text.listToText(andTable, nil, ', and ')) or
		mw.text.listToText(andTable)
	return andString
end

function p.orList (orTable)
	-- Stringifies a list with "or"
	local orString = (#andTable > 2 and mw.text.listToText(andTable, nil, ', or ')) or
		mw.text.listToText(andTable, nil, ' or ')
	return orString
end

function p.forSee (frame, from, options)
	-- Calls _forSee but pulls from the frame.
	mArguments = require('Module:Arguments')
	getArgs = mArguments.getArgs

	local args = getArgs(frame)
	return p._forSee(args, from, options)
end

function p._forSee (args, from, options)
	-- Produces a "For X, see ]" string from arguments. Expects index gaps
	-- but not blank or whitespace values; those should be filtered. Ignores
	-- arguments less than "from", and named arguments.

	-- Type-checks and defaults
	checkType("_forSee", 1, args, 'table')
	checkType("_forSee", 2, from, 'number', true)
	from = from or 1
	checkType("_forSee", 3, options, 'table', true)
	options = options or {}
	local defaultOptions = {
		disambiguator = ' (disambiguation)',
		forseeForm = 'For %s, see %s.',
		title = mw.title.getCurrentTitle().text,
		otherText = 'other uses'
	}
	for k, v in pairs(defaultOptions) do
		if options == nil then options = v end
	end

	-- maxArg's gotten manually because getArgs() and table.maxn aren't friends
	local maxArg = 0
	for k, v in pairs(args) do
		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
	-- Each row's a table with one "use" string and one "see" table
	local forTable = {}
	local i = from
	local terminated = false
	-- Repeat to generate and append each row
	repeat
		-- New empty row
		local forRow = {}
		-- If there's a blank use, assume the list's ended, use the default,
		-- and break at the end of this loop-through.
		forRow.use = args or options.otherText
		if not args then terminated = true end
		-- New empty list of pages
		forRow.pages = {}
		-- If there's not at least one page listed, assume the list's ended, use
		-- the default, and break at end of this loop-through.
		table.insert(forRow.pages, args or (options.title .. options.disambiguator))
		-- If the option after next is "and", do an inner loop where we collect
		-- items following "and"'s until the "and"'s stop. If there's a blank
		-- where we'd expect an item, ignore it: "1|and||and|3" → {1, 3}
		while args == 'and' do
			if args then 
				table.insert(forRow.pages, args)
			end
			-- Increment to the next "and"
			i = i + 2
		end
		-- Increment to the next use
		i = i + 2
		-- Add the row to the table
		table.insert(forTable, forRow)
	until terminated or i > maxArg

	-- Stringify the table, which is easy because it's structured now
	local strList = {}
	for k, v in pairs(forTable) do
		local useStr = v.use
		local pagesStr = p.andList(mHatnote.formatPages(unpack(v.pages)))
		table.insert(strList, string.format(options.forseeForm, useStr, pagesStr))
	end
	return mw.text.listToText(strList, ' ', ' ')
end

return p
Category: