FANDOM


--
-- Implements [[Template:TitleAnchor]]
--
local p = {}
 
local yesno = require('Module:Yesno')
local hc = require('Module:Paramtest').has_content
 
local locs = {
	p = 'p',
	P = 'p',
	s = 's',
	S = 's'
}
 
 
function parseColor(col)
	local r,g,b = col:match('#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])')
	local cstr
	if r ~= nil then
		r = tonumber(r .. r, 16)
		g = tonumber(g .. g, 16)
		b = tonumber(b .. b, 16)
	else
		r,g,b = col:match('#([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F])([0-9a-fA-F][0-9a-fA-F]')
		if r ~= nil then
			return nil
		end
	end
 
	if r == nil then
		return ''
	end
	local yiq = (r * 299 + g * 587 + b * 114)/1000
 
	if yiq > 128 then
		return 'rs-title-darkbg', yiq
	else
		return 'rs-title-lightbg', yiq
	end
 
end
 
p.parseColor = parseColor
 
 
function p.main(frame)
	local args = frame:getParent().args
	local title = args.t or '<title>'
	local loc = locs[args.loc]
	local comma = args[',']
	local bg = string.lower(args.bg or 'no') == 'yes'
 
	-- default no colouring
	local commacolor = false
	if comma and comma:lower() == 'c' 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.space or 'true')
 
	-- default colour black
	local color = args.c or 'black'
 
	local bgclass = parseColor(color)
 
	local link = false
	if hc(args.link) then
		link = args.link
	end
 
	return p._main(title, color, bg, loc, comma, commacolor, space, bgclass, link)
end
 
function p._main(title,color, bg, loc, comma, commacolor, space, bgclass, link)
	-- 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' })
		:addClass(bgclass)
 
	-- Background for lighter coloured titles
	-- Also some padding so it isn't weird
	if bg then
		ret:css({ background = '#5E6E77',
			padding = '1px 2px' })
	end
 
	-- for "<title> [Name]"
	if loc == 'p' then
		ret:tag('span')
			:css('color',color)
			:wikitext(title)
			:done()
		if not commacolor then
			if comma then
				ret:wikitext(', ')
			elseif space then
				ret:wikitext(' ')
			end
		end
		ret:wikitext('[Name]')
 
	-- for "[Name] <title>"
	elseif loc == 's' then
		ret:wikitext('[Name]')
		if not commacolor then
			if comma then
				ret:wikitext(', ')
			elseif space then
				ret:wikitext(' ')
			end
		end
		ret:tag('span')
			:css('color',color)
			:wikitext(title)
			:done()
 
	-- Otherwise just do <title>
	else
		ret:css('color',color)
			:wikitext(title)
	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
 
	if link then
		ret = string.format('[[%s|%s]]', link, tostring(ret))
	end
	return ret
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.