Jump to content

Module:Italic title/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 17:24, 30 July 2015 (questionably resolve use of titles with first letter = apostrophe). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module implements {{italic title}}, {{italic title prefixed}},
-- and {{lcfirstitalictitle}}.

local yesno = require('Module:Yesno')

local p = {}

-------------------------------------------------------------------------------
-- Helper functions
-------------------------------------------------------------------------------

local function makeTitle(title)
	if title == nil then
		return mw.title.getCurrentTitle()
	elseif type(title) == 'string' then
		title = mw.title.new(title)
		if not title then
			error(string.format("invalid page name '%s'", title), 2)
		end
		return title
	elseif type(title) == 'table'
		and type(title.prefixedText) == 'string'
		and type(title.getContent) == 'function'
	then
		return title
	else
		error('title must be nil, a string, or a mw.title object', 2)
	end
end

local function italicize(s)
	return string.format("<i>%s</i>", s)
end

local function parseTitle(t)
	local title = makeTitle(t.title)

	-- Find disambiguation text
	local before, dab
	if t.checkDab ~= false then
		before, dab = mw.ustring.match(title.text, '^(.+) (%([^%(%)]+%))$')
	end
	before = before or title.text
	
	-- Find prefix and main part
	local prefix, main
	if t.prefixPatterns then
		for _, pattern in ipairs(prefixPatterns) do
			prefix, main = mw.ustring.match(before, pattern)
			if prefix and main then
				break
			end
		end
	elseif t.start and t.start >= 2 then
		prefix = mw.ustring.sub(before, 1, t.start - 1)
		main = mw.ustring.sub(before, t.start, -1)
	end
	main = main or before

	-- Find namespace text
	local namespace
	if title.namespace ~= 0 then
		namespace = title.nsText
	end

	return {
		namespace = namespace,
		prefix = prefix,
		main = main,
		dab = dab
	}
end

local function expandDisplaytitle(s, dupeHandling, frame)
	frame = frame or mw.getCurrentFrame()
	return frame:callParserFunction('DISPLAYTITLE', s, dupeHandling)
end

local function fetchArgs(frame, wrappers)
	return require('Module:Arguments').getArgs(frame, {
		wrappers = wrappers
	})
end

-------------------------------------------------------------------------------
-- Template:Italic title
-------------------------------------------------------------------------------

function p._italic_title(args, title)
	local parsed = parseTitle{
		title = title,
		checkDab = not yesno(args.force or args.all),
	}
	local ret = {}
	ret[#ret + 1] = parsed.namespace and parse.namespace .. ':'
	ret[#ret + 1] = parsed.prefix
	ret[#ret + 1] = parsed.main and italicize(parsed.main)
	ret[#ret + 1] = parsed.dab and ' ' .. parsed.dab
	return expandDisplayTitle(table.concat(ret), args[1])
end

function p.italic_title(frame)
	local args = fetchArgs(frame, 'Template:Italic title')
	return p._italic_title(args)
end

p.main = p.italic_title -- Temporary alias so that the transition doesn't cause script errors

-------------------------------------------------------------------------------
-- Template:Lcfirstitalictitle
-------------------------------------------------------------------------------

function p._lcfirstitalictitle(args)
	-- @TODO: Check how this template actually works.
	local parsed = parseTitle{
		title = title,
		start = 2,
	}

	-- Find the main italicized part, and make the first character lower case.
	local prefix = parsed.prefix and mw.ustring.lower(parsed.prefix) or ''
	local main = parsed.main or ''
	main = italicize(prefix .. main)

	local ret = {}
	ret[#ret + 1] = parsed.namespace and parse.namespace .. ':'
	ret[#ret + 1] = main
	ret[#ret + 1] = parsed.dab and ' ' .. parsed.dab
	return expandDisplayTitle(table.concat(ret), args[1])
end

function p.lcfirstitalictitle(frame)
	local args = fetchArgs(frame, 'Template:Lcfirstitalictitle')
	return p._lcfirstitalictitle(args)
end

-------------------------------------------------------------------------------
-- Template:Italic title prefixed
-------------------------------------------------------------------------------

function p._italic_title_prefixed(args)
	-- @TODO: Check how this template actually works.
	local start = tonumber(args[1])
	local parsed = parseTitle{
		title = title,
		start = start and start + 1,
	}
	local ret = {}
	ret[#ret + 1] = parsed.namespace and parse.namespace .. ':'
	ret[#ret + 1] = parsed.prefix
	ret[#ret + 1] = parsed.main and italicize(parsed.main)
	ret[#ret + 1] = parsed.dab and ' ' .. parsed.dab
	return expandDisplayTitle(table.concat(ret), args[2])
end

function p.italic_title_prefixed(frame)
	local args = fetchArgs(frame, 'Template:Italic title prefixed')
	return p._italic_title_prefixed(args)
end

return p