Module:TitleAnchor

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

local yesno = require('Module:Yesno')

function p.main(frame) local args = frame:getParent.args local title = args.t or ' ' local loc = location(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'

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

function p._main(title,color,bg,loc,comma,commacolor,space) local titleid = title

-- If comma needs colouring -- Edit a comma into the title before adding to span if comma and commacolor then if loc == 'prefix' then title = title..', ' elseif loc == 'suffix' 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 = '#5E6E77',					padding = '3px 2px' }) end

-- for Title [Name] if loc == 'prefix' 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 == 'suffix' 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 nothing 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 -- 		titleid = titleid:gsub('','') -- Uppercase first letter titleid = titleid:sub(1,1):upper..titleid:sub(2) ret:attr('id',titleid) end

return ret end

-- Function to parse the loc parameter -- Allows whole word or just first letter to work function location(arg) if arg then arg = arg:lower end

if arg == 'prefix' or arg == 'p' then return 'prefix' elseif arg == 'suffix' or arg == 's' then return 'suffix' else return nil end end return p