Документация
--
-- Реализует шаблон {{Userbox}}
-- Основа позаимствована из Module:Navbox.
--
local p = {}
local getArgs -- lazily initialized
local templateStyles = 'Шаблон:Песочница/Iniquity/styles.css'
local args
--
-- Render caption box
--
local function renderCaptionDiv(mainBox)
if not args.cap then return end
caption = mainBox:tag('div')
:addClass('ts-userbox-caption')
:css('text-align', args['cap-a'])
:css('background', args['cap-c'])
:css('color', args['cap-fc'])
if args['cap-s'] then
caption:css('font-size', args['cap-s'] .. 'pt')
end
caption
:cssText(args['cap-style'])
:wikitext(args.cap)
end
--
-- Render main box with IDs and info cells
--
local function renderMainCell(mainBox)
local baseBox = mainBox:tag('div')
:addClass('ts-userbox-main')
if args.id or args.id1 or args[3] then
local firstID = baseBox:tag('div')
:addClass('ts-userbox-idCell')
:css('text-align', args['id1-a'] or args['id-a'])
:css('background', args['id1-c'] or args['id-c'] or args[1])
:css('color', args['id1-fc'] or args['id-fc'])
if args['id1-s'] or args['id-s'] then
firstID:css('font-size', (args['id1-s'] or args['id-s']) .. 'pt')
end
firstID
:cssText(args['id1-style'] or args['id-style'])
:wikitext(args.id or args.id1 or args[3])
end
if not (args.info or args[4]) then
local info = baseBox:tag('div')
:addClass('ts-userbox-info')
:tag('p')
:wikitext('info')
else
local info = baseBox:tag('div')
:addClass('ts-userbox-info')
:css('text-align', args['info-a'])
:css('background', args['info-c'] or args[2])
:css('color', args['info-fc'])
if args['info-s'] then
info:css('font-size', args['info-s'] .. 'pt')
end
info
:cssText(args['info-style'])
:tag('p')
:wikitext(args.info or args[4])
end
if args.id2 or args[5] then
local secondID = baseBox:tag('div')
:addClass('ts-userbox-idCell')
:css('text-align', args['id2-a'] or args['id-a'])
:css('background', args['id2-c'] or args['id-c'] or args[1])
:css('color', args['id2-fc'] or args['id-fc'])
if args['id2-s'] or args['id-s'] then
secondID:css('font-size', (args['id2-s'] or args['id-s']) .. 'pt')
end
secondID
:cssText(args['id2-style'] or args['id-style'])
:wikitext(args.id2 or args[5])
end
end
--
-- Tracking categories
--
-- local function hasBackgroundColors()
-- return mw.ustring.match(titlestyle or '','background') or mw.ustring.match(groupstyle or '','background') or mw.ustring.match(basestyle or '','background')
-- end
local function isIllegible()
local styleratio = require('Module:Color contrast')._styleratio
for key, style in pairs(args) do
if tostring(key):match("style$") or tostring(key):match("^стиль") then
if styleratio{mw.text.unstripNoWiki(style)} < 4.5 then
return true
end
end
end
return false
end
local function getTrackingCategories()
local cats = {}
if isIllegible() then table.insert(cats, 'Потенциально нечитаемые навигационные шаблоны') end
return cats
end
local function renderTrackingCategories(builder)
local title = mw.title.getCurrentTitle()
if title.namespace ~= 10 then return end -- not in template space
local subpage = title.subpageText
if subpage == 'doc' or subpage == 'песочница' or subpage == 'тесты' then return end
for i, cat in ipairs(getTrackingCategories()) do
builder:wikitext('[[Категория:' .. cat .. ']]')
end
end
--
-- Main userbox container
--
local function renderMainTable()
local mainBox = mw.html.create('div')
:addClass('ts-userbox')
:addClass('nocolbreak')
if args.float then
mainBox:addClass('t' .. args.float)
else
mainBox:addClass('tright')
end
mainBox
:css('border-color', args[1] or args['border-c'] or args['id1-c'] or args['id-c'] or args['id2-c'] or args[2] or args['info-c'])
:css('border-size', args['border-s'])
renderCaptionDiv(mainBox)
renderMainCell(mainBox)
return mainBox
end
function p._userbox(userboxArgs)
args = userboxArgs
-- render the main body of the userbox
local mainBox = renderMainTable()
renderTrackingCategories(mainBox)
return tostring(mainBox)
end
function p.userbox(frame)
if not getArgs then
getArgs = require('Module:Arguments').getArgs
end
args = getArgs(frame, {wrappers = {'Участник:Iniquity/Черновик', 'Шаблон:Подгруппы навигационной таблицы'}})
-- Read the arguments in the order they'll be output in, to make references number in the right order.
local _
_ = args.cap
_ = args.id
_ = args.info
_ = args.id2
return frame:extensionTag{ name = 'templatestyles', args = { src = templateStyles } } .. p._userbox(args)
end
return p