Módulo:Título

De RuneScape Wiki
Ir para: navegação, pesquisa

A documentação para este módulo pode ser criada em Módulo:Título/doc

--
-- Implements [[Template:TitleAnchor]]
--
local p = {}

local yesno = require('Module:Yesno')

local locs = {
	sim = 'p',
	['não'] = 's',
	nao = 's',
}

function p.main(frame)
	local args = frame:getParent().args
	local title = args.t or args[1] or '<title>'
	local loc = locs[string.lower(args.p or '')]
	local comma = args[',']
	local bg = yesno(args.bg)
	local ftitle = args.f or false

	-- default no colouring
	local commacolor = false
	if comma and comma:lower() == 'v' then
		comma = true
		commacolor = true
	elseif comma then
		comma = yesno(comma)
	end

	-- yesno defaults to false if the argument is nil, have it default to true
	local space = yesno(args.e or 'true')

	-- default colour black
	local color = args.c or 'black'

	return p._main(title, color, bg, loc, comma, commacolor, space, ftitle)
end

function p._main(title, color, bg, loc, comma, commacolor, space, ftitle)
	-- Copy title to a different variable before editing it
	local titleid = title

	-- If comma needs colouring
	-- Edit a comma into the title before adding to span
	if comma and commacolor then
		if loc == 'p' then
			title = title..', '
		elseif loc == 's' then
			title = ', '..title
		end
	end

	-- Tag to keep everything on one line
	local ret = mw.html.create('span')
		:css({ ['font-weight'] = 'bold',
			['white-space'] = 'nowrap',
			color = bg and '#FFFFFF' or '#000000' })

	-- Background for lighter coloured titles
	-- Also some padding so it isn't weird
	if bg then
		ret:css({ background = '#19364C',
			padding = '3px 2px' })
	end
	
	local tspan

	-- for "<title> [Nome]"
	if loc == 'p' then
		tspan = ret:tag('span')
		tspan:css('color',color)
			:wikitext(title)
			:done()
		if not commacolor then
			if comma then
				ret:wikitext(', ')
			elseif space then
				ret:wikitext(' ')
			end
		end
		ret:wikitext('[Nome]')

	-- for "[Nome] <title>"
	elseif loc == 's' then
		ret:wikitext('[Nome]')
		if not commacolor then
			if comma then
				ret:wikitext(', ')
			elseif space then
				ret:wikitext(' ')
			end
		end
		tspan=ret:tag('span')
		tspan:css('color',color)
			:wikitext(title)
			:done()

	-- Otherwise just do <title>
	else
		tspan=ret
		tspan:css('color',color)
			:wikitext(title)
	end
	if ftitle then
		tspan:css('border-bottom', 'dotted 1px grey')
			:attr('title', ftitle)
	end
	

	-- Don't add the anchor except on [[Titles]]
	if mw.title.getCurrentTitle().text == 'Titles' then
		-- Look for and strip various mark-ups for multi colored titles
		-- <span>
		titleid = titleid:gsub('</?span.->','')

		-- Uppercase first letter
		titleid = mw.text.split(titleid,'')
		titleid[1] = titleid[1]:upper()
		titleid = table.concat(titleid,'')

		ret:attr('id',titleid)
	end

	return ret
end

return p