Jump to content

Module:Italic title/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 05:39, 8 July 2016 (split logic out into functions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements {{italic title}}.

local p = {}

-- Parses a title object into its namespace text, title, and disambiguation
-- text.
-- Param: options - a table of options with the following keys:
--     title - the title object to parse
--     ignoreDab - ignore any disambiguation parentheses
-- Returns a table with keys "namespace", "title", and "dab". Both namespace and
-- dab may be nil.
local function parseTitle(options)
	assert(type(options) == 'table', 'options was not a table')
	assert(type(options.title) == 'table', 'options.title was not a table')
	local ret = {}
	local title = options.title

	-- Title and dab text
	local prefix, parentheses
	if not options.ignoreDab then
		prefix, parentheses = mw.ustring.match(
			title.text,
			'^(.+) %(([^%(%)]+)%)$'
		)
	end
	if prefix and parentheses then
		ret.title = prefix
		ret.dab = parentheses
	else
		ret.title = title.text
	end

	-- Namespace
	local namespace = mw.site.namespaces[title.namespace].name
	if namespace and #namespace >= 1 then
		ret.namespace = namespace
	end

	return ret
end

-- Italicizes a string
-- Param: s - the string to italicize
-- Returns string.
local function italicize(s)
	assert(type(s) == 'string', 's was not a string')
	assert(s ~= '', 's was the empty string')
	return string.format('<i>%s</i>', s)
end

-- Escape characters in a string that are magic in Lua patterns.
-- Param: pattern - the pattern to escape
-- Returns string.
local function escapeMagicCharacters(s)
	assert(type(s) == 'string', 's was not a string')
	return s:gsub('%p', '%%%0')
end

-- Italicizes a parsed title.
-- Param: options - a table of options with the following keys:
--     parsedTitle - a table as returned by the parseTitle function (table, required)
--     substring - a substring of the title to italicize (string)
--     title - whether to italicize the whole title (boolean)
--     dab - whether to italicize the disambiguation text (boolean)
--     namespace - whether to italicize the namespace text (boolean)
-- Returns a parsedTitle table.
local function italicizeParsedTitle(options)
	assert(type(options) == 'table', 'options was not a table')
	assert(type(options.parsedTitle) == 'table', 'options.parsedTitle was not a table')
	assert(type(options.parsedTitle.title) == 'string', 'options.parsedTitle.title was not a string')
	assert(options.substring == nil or type(options.substring) == 'string' and options.substring ~= '', 'invalid options.substring')
	local ret = {}

	-- Make a shallow copy of the parsedTitle table so that we don't alter data
	-- that might be used by other functions.
	for k, v in pairs(options.parsedTitle) do
		ret[k] = v
	end

	-- Substrings
	if options.substring then
		local pattern = escapeMagicCharacters(options.substring)
		ret.title = ret.title:gsub(pattern, italicize)

	-- Whole title
	elseif options.title then
		ret.title = italicize(ret.title)
	end

	-- Disambiguation text
	if options.dab then
		ret.dab = italicize(ret.dab)
	end
	
	-- Namespace
	if options.namespace then
		ret.namespace = italicize(ret.namespace)
	end

	return ret
end

-- Renders a parsed title table into a page name
-- Param: parsedTitle - a parsedTitle table.
-- Returns string
local function renderParsedTitle(parsedTitle)
	local ret = ''
	if parsedTitle.namespace then
		ret = ret .. parsedTitle.namespace .. ':'
	end
	ret = ret .. parsedTitle.title
	if parsedTitle.dab then
		ret = ret .. ' (' .. parsedTitle.dab .. ')'
	end
	return ret
end

local function getArgs(wrapper)
	assert(type(wrapper) == 'string', 'wrapper was not a string')
	return require('Module:Arguments').getArgs(frame, {
		wrappers = wrapper
	})
end

-- Main function for {{italic title}}
function p._main(args, frame, title)
	assert(type(args) == 'table', 'args was not a table')
	frame = frame or mw.getCurrentFrame()
	title = title or mw.title.getCurrentTitle()
	local parsedTitle = parseTitle{
		title = title,
		ignoreDab = args.all == 'yes'
	}
	parsedTitle = italicizeParsedTitle{
		parsedTitle = parsedTitle,
		substring = args.string,
		title = true
	}
	return renderParsedTitle(parsedTitle)
	--[[
	return frame:callParserFunction(
		'DISPLAYTITLE',
		renderParsedTitle(parsedTitle),
		args[1]
	)
	]]
end

function p.main(frame)
	return p._main(getArgs('Template:Italic title'), frame)
end

return p