Módulo:Título/Sandbox
A documentação para este módulo pode ser criada em Módulo:Título/Sandbox/doc
-- <nowiki>
--
-- 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') == 'sim'
local nome = args.nome or '[Nome]'
-- default no colouring
local commacolor = false
local dos = false
if comma and comma:lower() == 'cdos' then
comma = true
commacolor = true
dos = true
elseif comma and comma:lower() == 'dos' then
comma = true
dos = true
elseif 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.s 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, nome, dos)
end
function p._main(title,color, bg, loc, comma, commacolor, space, bgclass, link, nome, dos)
-- 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 and dos then
if loc == 'p' then
title = title..' dos '
elseif loc == 's' then
title = ' dos '..title
end
elseif 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' })
:addClass('rs-title')
:addClass(bgclass)
-- Background for lighter coloured titles
-- Also some padding so it isn't weird
if bg then
ret:css({ background = 'rgba(0,0,0,0.4)',
padding = '2px 5px',
['border-radius'] = '1.5px' })
end
-- for "<title> [Nome]"
if loc == 'p' then
ret:tag('span')
:css('color',color)
:wikitext(title)
:done()
if dos and not commacolor then
ret:wikitext(' dos ')
elseif 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 dos and not commacolor then
ret:wikitext(' dos ')
elseif 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 == 'no' then
ret = ret
elseif link then
ret = string.format('[[%s|%s]]', link, tostring(ret))
else
ret = string.format('[[%s|%s]]', titleid, tostring(ret))
end
return ret
end
return p