Content deleted Content added
Jmorgan (WMF) (talk | contribs) No edit summary |
Jmorgan (WMF) (talk | contribs) add some docstrings |
||
Line 21: | Line 21: | ||
function getNumberedArgValues(values, numargs, args, arg_value) |
function getNumberedArgValues(values, numargs, args, arg_value) |
||
-- populates a table of numbered arguments passed from the calling |
|||
-- template with their values |
|||
local i = 2 |
local i = 2 |
||
for k,v in ipairs(numargs) do |
for k,v in ipairs(numargs) do |
||
Line 53: | Line 55: | ||
function makeLinkField(title, values, field, field_div) |
function makeLinkField(title, values, field, field_div) |
||
-- makes a formatted div for links |
-- makes a formatted div for links. |
||
field_div:cssText(field.style) |
field_div:cssText(field.style) |
||
field_div |
field_div |
||
Line 68: | Line 70: | ||
function makeButtonField(title, field, field_div) |
function makeButtonField(title, field, field_div) |
||
-- makes a formatted div for |
-- makes a formatted div for ui buttons |
||
field_div:cssText(field.style) |
field_div:cssText(field.style) |
||
field_div |
field_div |
||
Line 146: | Line 148: | ||
function makeInfobox(frame, args, data) |
function makeInfobox(frame, args, data) |
||
-- builds the infobox. Some content sections are required, others |
|||
-- are optional. Optional sections are defined in the stylesheet. |
|||
local box = mw.html.create('div'):cssText(data.styles.box.outer) |
local box = mw.html.create('div'):cssText(data.styles.box.outer) |
||
local inner_box = mw.html.create('div'):cssText(data.styles.box.inner) |
local inner_box = mw.html.create('div'):cssText(data.styles.box.inner) |
||
Line 182: | Line 186: | ||
function getPortalData(args) |
function getPortalData(args) |
||
-- loads the relevant stylesheet, if a sub-template was called with a portal |
|||
-- argument and a stylesheet exists with the same name. For example, calling |
|||
-- {{#invoke:Probox/Idealab|main|portal=Idealab}} would load the |
|||
-- Module:Probox/Idealab stylesheet |
|||
local data = {} |
local data = {} |
||
if (args.portal and mw.title.makeTitle( 'Module', 'Probox/' .. args.portal).exists) then |
if (args.portal and mw.title.makeTitle( 'Module', 'Probox/' .. args.portal).exists) then |
Revision as of 17:49, 13 May 2014
![](https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Test_Template_Info-Icon_-_Version_%282%29.svg/50px-Test_Template_Info-Icon_-_Version_%282%29.svg.png)
This module implements the {{Probox}} template. Please see the template documentation page for usage instructions and the project page for examples.
--
-- This module implements {{Probox}}
--
local getArgs = require('Module:Arguments').getArgs
local p = {}
function getArgNums(prefix, args)
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local nums = {}
for k, v in pairs(args) do
local num = tostring(k):match('^' .. prefix .. '([2-9]%d*)$')
if num then
table.insert(nums, tonumber(num))
end
end
table.sort(nums)
return nums
end
function getNumberedArgValues(values, numargs, args, arg_value)
-- populates a table of numbered arguments passed from the calling
-- template with their values
local i = 2
for k,v in ipairs(numargs) do
values[i] = args[arg_value .. v]
i = i + 1
end
return values
end
function makeTextField(title, values, field, field_div)
-- makes a formatted text field for output, based on parameter
-- values or on default values provided for that field
-- type in the stylesheet
-- local field_div = mw.html.create('div')
field_div:cssText(field.style)
if field.vtype == "text" then
field_div
:tag('span')
:cssText(field.style2)
:wikitext(title)
:done()
end
for k,v in pairs(values) do
field_div
:tag('span')
:cssText(field.style3)
:wikitext(v)
:done()
end
return field_div
end
function makeLinkField(title, values, field, field_div)
-- makes a formatted div for links.
field_div:cssText(field.style)
field_div
:tag('span')
:cssText(field.style3)
if field.vtype2 == "wikitext" then
field_div:wikitext("[[" .. values[1] .. "|<span style='" .. field.style2 .. "'>" .. title .."</span>]]")
elseif field.vtype2 == "icon" then
field_div:wikitext("[[File:" .. values[1] .. "|" .. field.alignment .. "|" .. field.width .."|link=" .. field.link .. "]]")
end
field_div:done()
return field_div
end
function makeButtonField(title, field, field_div)
-- makes a formatted div for ui buttons
field_div:cssText(field.style)
field_div
:tag('span')
:cssText(field.style2)
:addClass(field.class)
:wikitext(title)
:done()
return field_div
end
function makeImageField(values, field, field_div)
-- makes a formatted image field for output, based on parameter values
-- provided in the calling template or its parent template, or on default
-- values provided in the stylesheet
-- local field_div = mw.html.create('div')
field_div:cssText(field.style)
field_div
:tag('span')
:cssText(field.style3)
:wikitext("[[File:" .. values[1] .. "|" .. field.width .."]]")
:done()
return field_div
end
function makeSection(frame, args, data, box_sec)
-- return a div for a section of the box including child divs
-- for each content field in that section
local sec_div = mw.html.create('div')
local sec_fields = {}
for k,v in pairs(data.fields) do
if data.fields[k].section == box_sec then
sec_div:cssText(data.styles.section[box_sec])
local field = data.fields[k]
local title
local values = {}
local field_div = mw.html.create('div')
if field.vtype == "image" then
if args[k] then
values[1] = args[k]
else
break --do nothing with this field
end
field_div = makeImageField(values, field, field_div)
elseif (field.vtype == "title" or field.vtype == "text") then
title = mw.text.trim(frame:expandTemplate{title=args.translations, args={field.key}})
if args[k] then
values[1] = args[k]
local numargs = getArgNums(k, args)
if table.getn(numargs) > 0 then -- if there are numbered arguments
values = getNumberedArgValues(values, numargs, args, k)
end
elseif field.default then
values[1] = mw.text.trim(frame:expandTemplate{title=args.translations, args={field.default}})
end
field_div = makeTextField(title, values, field, field_div)
elseif field.vtype == "link" then
title = mw.text.trim(frame:expandTemplate{title=args.translations, args={field.key}})
values[1] = field.wikilink
field_div = makeLinkField(title, values, field, field_div)
elseif field.vtype == "ui_button" then
title = mw.text.trim(frame:expandTemplate{title=args.translations, args={field.key}})
field_div = makeButtonField(title, field, field_div)
else -- unsupported field type
end
field_div:done()
sec_fields[field.rank] = field_div
-- else -- ignore
end
end
for k,v in ipairs(sec_fields) do
sec_div:node(v)
end
sec_div:allDone()
return sec_div
end
function makeInfobox(frame, args, data)
-- builds the infobox. Some content sections are required, others
-- are optional. Optional sections are defined in the stylesheet.
local box = mw.html.create('div'):cssText(data.styles.box.outer)
local inner_box = mw.html.create('div'):cssText(data.styles.box.inner)
if data.sections.above == true then
local sec_top = makeSection(frame, args, data, "above")
box:node(sec_top)
end
if data.sections.nav == true then
local sec_nav = makeSection(frame, args, data, "nav")
box:node(sec_nav)
end
local sec_head = makeSection(frame, args, data, "head")
inner_box:node(sec_head)
local sec_main = makeSection(frame, args, data, "main")
inner_box:node(sec_main)
if data.sections.sub == true then
inner_box:tag('div'):wikitext("participants"):done()
local sec_sub = makeSection(frame, args, data, "sub")
inner_box:node(sec_sub)
end
if data.sections.cta == true then
local sec_cta = makeSection(frame, args, data, "cta")
inner_box:tag('div'):cssText("font-style:italic"):wikitext("get involved!"):done()
inner_box:node(sec_cta)
inner_box:tag('div'):cssText("clear:both"):done() --clears buttons in the cta sections
end
inner_box:allDone()
box:node(inner_box)
if data.sections.below == true then
local sec_bottom = makeSection(frame, args, data, "below")
box:node(sec_bottom)
end
box:allDone()
return box
end
function getPortalData(args)
-- loads the relevant stylesheet, if a sub-template was called with a portal
-- argument and a stylesheet exists with the same name. For example, calling
-- {{#invoke:Probox/Idealab|main|portal=Idealab}} would load the
-- Module:Probox/Idealab stylesheet
local data = {}
if (args.portal and mw.title.makeTitle( 'Module', 'Probox/' .. args.portal).exists) then
data = mw.loadData("Module:Probox/" .. args.portal)
else
data = mw.loadData("Module:Probox/Defaults")
end
return data
end
function addCategories(infobox, data, args) --sloppy right now
if data.categories.default then
infobox = infobox .. data.categories.default
end
local cat_arg
local cat_val
for k,v in pairs(data.categories) do --need to make this recursive?
if args[k] then
for l, w in pairs(data.categories[k]) do
if args[k] == l then
infobox = infobox .. w
break
end
end
end
end
return infobox
end
function p.main(frame)
local args = getArgs(frame)
local data = getPortalData(args)
if not (args.translations and mw.title.new(args.translations).exists) then --works; cmt out for bug check
args.translations = "Probox/Content"
end
local box = makeInfobox(frame, args, data)
local infobox = tostring(box)
if data.categories then
infobox = addCategories(infobox, data, args)
end
-- local infobox = test(frame, args)
return infobox
end
return p