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 01:46, 4 March 2015 (split the parsing part into a p.parseTitle function). 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 libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg

local function italicize(s, start)
	return string.format(
		"%s''%s''",
		mw.ustring.sub(s, 1, start),
		mw.ustring.sub(s, start + 1, -1)
	)
end

local p = {}

function p.parseTitle(t)
	checkType('parseTitle', 1, t, 'table')
	checkTypeForNamedArg('parseTitle', 'args', t.args, 'table', true)
	checkTypeForNamedArg('parseTitle', 'frame', t.frame, 'table', true)
	checkTypeForNamedArg('parseTitle', 'title', t.title, 'table', true)
	checkTypeForNamedArg('parseTitle', 'start', t.start, 'number', true)
	local args = t.args or {}
	local frame = t.frame or mw.getCurrentFrame()
	local title = t.title or mw.title.getCurrentTitle()
	local start = t.start or 0

	-- Parse the title into a prefix and the final parentheses.
	local prefix, parentheses = mw.ustring.match(title.text, '^(.+) (%([^%(%)]+%))$')

	-- Italicize the relevant part.
	local result
	if prefix and parentheses and not yesno(args.all or args.force) then
		result = italicize(prefix, start)
		result = result .. ' ' .. parentheses
	else
		result = italicize(title.text, start)
	end

	-- Add the namespace if we're not in mainspace.
	if title.namespace ~= 0 then
		result = title.nsText .. ':' .. result
	end
	
	return result
end

function p._main(args)
	return p.parseTitle{args = 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',
		_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(_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',
		_main(args, tonumber(args[1])),
		args[2] -- TODO: what are original parameters in Template:Italic title prefixed?
	)
end

return p