Difference between revisions of "Module:Hatnote list"

From Timelines
Jump to: navigation, search
m (Derped on that comment fix before. Removed more incorrect/obsolete comment text.)
(Simplified a few things, mostly re: calls to mw.listToText().)
Line 13: Line 13:
 
local p = {}
 
local p = {}
  
function p.andList (andTable)
+
function p.andList (list)
 
-- Stringifies a list with "and"
 
-- Stringifies a list with "and"
local andString = (#andTable > 2 and mw.text.listToText(andTable, nil, ', and ')) or
+
return mw.text.listToText(list, nil, (#list > 2 and ',' or '') .. ' and ')
mw.text.listToText(andTable)
 
return andString
 
 
end
 
end
  
function p.orList (orTable)
+
function p.orList (list)
 
-- Stringifies a list with "or"
 
-- Stringifies a list with "or"
local orString = (#andTable > 2 and mw.text.listToText(andTable, nil, ', or ')) or
+
return mw.text.listToText(list, nil, (#list > 2 and ',' or '') .. ' or ')
mw.text.listToText(andTable, nil, ' or ')
 
return orString
 
 
end
 
end
  
Line 104: Line 100:
 
table.insert(strList, string.format(options.forseeForm, useStr, pagesStr))
 
table.insert(strList, string.format(options.forseeForm, useStr, pagesStr))
 
end
 
end
return mw.text.listToText(strList, ' ', ' ')
+
return table.concat(strList, ' ')
 
end
 
end
  
 
return p
 
return p

Revision as of 19:47, 28 April 2016

Documentation for this module may be created at Module:Hatnote list/doc

--------------------------------------------------------------------------------
-- For see
--
-- Makes a "For X, see [[Y]]." 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 (list)
	-- Stringifies a list with "and"
	return mw.text.listToText(list, nil, (#list > 2 and ',' or '') .. ' and ')
end

function p.orList (list)
	-- Stringifies a list with "or"
	return mw.text.listToText(list, nil, (#list > 2 and ',' or '') .. ' or ')
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 [[Y]]" 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[k] == nil then options[k] = 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
	-- Rows are tables of a "use" string and a "pages" table of pagename strings
	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[i] or options.otherText
		if not args[i] then terminated = true end
		-- New empty list of pages
		forRow.pages = {}
		-- If there's not at least one page listed, use the default.
		table.insert(forRow.pages, args[i + 1] 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[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
		-- 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 table.concat(strList, ' ')
end

return p