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 13:07, 4 March 2015 (make a new parseTitle function to separate title-parsing from italicisation). 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')

-------------------------------------------------------------------------------
-- 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("''%s''", s)
end

-------------------------------------------------------------------------------
-- Exports
-------------------------------------------------------------------------------

local p = {}

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

	-- Find disambiguation text
	local before, dab
	if t.checkDab 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 then
		prefix = mw.ustring.sub(before, 1, t.start)
		main = mw.ustring.sub(before, t.start + 1, -1)
	end
	main = main or before

	local namespace
	if title.namespace ~= 0 then
		namespace = title.nsText
	end

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

function p._main(args)
end

function p.italic_title(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Italic title'
	})
	-- Call displaytitle with the text we generated.
	return mw.getCurrentFrame():callParserFunction(
		'DISPLAYTITLE',
		p._main(args),
		args[1]
	)
end

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

function p.lcfirstitalictitle(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Lcfirstitalictitle'
	})
	-- Call displaytitle with the text we generated.
	local lang = mw.language.getContentLanguage()
	return mw.getCurrentFrame():callParserFunction(
		'DISPLAYTITLE',
		lang:lcfirst(p._main(args)),
		args[1] -- TODO: what are original parameters in Template:Lcfirstitalictitle?
	)
end

function p.italic_title_prefixed(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Italic title prefixed'
	})
	-- Call displaytitle with the text we generated.
	return mw.getCurrentFrame():callParserFunction(
		'DISPLAYTITLE',
		p._main(args, tonumber(args[1])),
		args[2] -- TODO: what are original parameters in Template:Italic title prefixed?
	)
end

return p