Module:Project infobox
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Project infobox/doc
-- The infobox behind {{Project}}. The wiki has no ParserFunctions, so the
-- optional rows are left out here rather than with {{#if:}}.
local p = {}
-- A bare Discord URL becomes a readable link: an invite such as
-- https://discord.gg/abc reads as discord.gg/abc, and a channel link, whose
-- long numeric path would not fit the box, reads as "Discord channel".
-- Anything else is used as written, so a labelled [url text] link still works.
local function discord(v)
if not v:match('^https?://%S+$') then
return v
end
if v:match('^https?://[%w.]*discord%.com/channels/') then
return string.format('[%s Discord channel]', v)
end
return string.format('[%s %s]', v, (v:gsub('^https?://', ''):gsub('/$', '')))
end
-- The third field, when present, formats the value before it is shown.
local rows = {
{ 'status', 'Status' },
{ 'type', 'Type' },
{ 'maintainers', 'Maintainers' },
{ 'started', 'Started' },
{ 'platform', 'Runs on' },
{ 'source', 'Source' },
{ 'docs', 'Docs' },
{ 'discord', 'Discord', discord },
}
local function value(args, key)
local v = args[key]
if v then
v = mw.text.trim(v)
if v ~= '' then
return v
end
end
return nil
end
function p.main(frame)
local args = frame:getParent().args
local title = mw.title.getCurrentTitle()
local name = value(args, 'name') or title.text
local box = mw.html.create('table'):addClass('pb-project')
box:tag('tr'):tag('th'):attr('colspan', '2'):addClass('pb-project-title'):wikitext(name)
local image = value(args, 'image')
if image then
local cell = box:tag('tr'):tag('td'):attr('colspan', '2'):addClass('pb-project-image')
cell:wikitext(string.format('[[File:%s|%s|alt=%s]]', image, value(args, 'image_size') or '220px', name))
local caption = value(args, 'caption')
if caption then
cell:tag('div'):addClass('pb-project-caption'):wikitext(caption)
end
end
for _, row in ipairs(rows) do
local v = value(args, row[1])
if v and row[3] then
v = row[3](v)
end
if v then
local tr = box:tag('tr')
tr:tag('th'):attr('scope', 'row'):wikitext(row[2])
tr:tag('td'):wikitext(v)
end
end
local out = frame:extensionTag('templatestyles', '', { src = 'Template:Project/styles.css' }) .. tostring(box)
-- Only articles are listed, so the template's own page and its docs stay out.
if title.namespace == 0 then
out = out .. '[[Category:Projects]]'
local category = value(args, 'category')
if category then
out = out .. '[[Category:' .. category .. ']]'
end
end
return out
end
return p