|
|
(2 versions intermédiaires par le même utilisateur non affichées) |
Ligne 1 : |
Ligne 1 : |
| local p = {} | | local p = {} |
|
| |
| local getArgs = require( 'Module:Arguments' ).getArgs
| |
|
| |
| local category = 'Characters'
| |
|
| |
| local pageHeadline = 'This is a character'
| |
|
| |
| local parameters = {
| |
| name = 'has name',
| |
| sex = 'has sex',
| |
| ability = 'has ability',
| |
| creator = 'was created by',
| |
| ['wikipedia page'] = 'is described on'
| |
| }
| |
|
| |
| local infoboxConfig = {
| |
| -- note the absence of field 'name'
| |
| -- note also that this table is not indexed by a string (like parameters), because the order matters
| |
| -- defining this as an array means: lua does not maintain the order of items but accesses them randomly
| |
| { argument = 'sex', label = 'Sex' },
| |
| { argument = 'creator', label = 'Created by' },
| |
| { argument = 'wikipedia page', label = 'Wikipedia' },
| |
| { argument = 'ability' },
| |
| }
| |
|
| |
| local linkFields = {
| |
| -- if a field is set to true, it will be linked
| |
| -- if it is set to a string, #formlink will be used
| |
| creator = 'Person',
| |
| }
| |
|
| |
| local mandatory = { 'ability', 'sex' }
| |
| local listFields = { 'ability' }
| |
| local delimiter = ','
| |
|
| |
|
| function p.main( frame ) | | function p.main( frame ) |
| local args = getArgs( frame, { parentOnly = true } ) | | local character = require( 'Module:Character/class' ):new() |
| return p._main( args )
| | return character:renderPage() |
| end
| |
| | |
| function p._main( args )
| |
| local output = '==' .. pageHeadline .. ' ==\n'
| |
|
| |
| -- get the correct data from arguments
| |
| local data = {}
| |
| for k, _ in pairs( parameters ) do
| |
| data[k] = args[k]
| |
| end
| |
|
| |
| -- set defaults
| |
| if not data.name then
| |
| data.name = mw.title.getCurrentTitle().text
| |
| end
| |
|
| |
| -- check mandatory
| |
| for _, m in pairs( mandatory ) do
| |
| if not args[m] then
| |
| output = output .. formatError( 'Mandatory argument "' .. m .. '" is missing!' )
| |
| end
| |
| end
| |
| | |
| -- convert listfields
| |
| for _, lf in pairs( listFields ) do
| |
| if data[lf] then
| |
| data[lf] = mw.text.split( data[lf], delimiter, true )
| |
| -- clear empty fields
| |
| for k, v in pairs( data[lf] ) do
| |
| if #v == 0 then
| |
| data[lf][k] = nil
| |
| end
| |
| end
| |
| end
| |
| if data[lf] and #data[lf] > 0 then
| |
| if #data[lf] == 1 then
| |
| data[lf] = data[lf][1]
| |
| end
| |
| else
| |
| data[lf] = nil
| |
| end
| |
| end
| |
|
| |
| -- save semantic data
| |
| local semanticData = {}
| |
| for arg, property in pairs( parameters ) do
| |
| if data[arg] then
| |
| if type(data[arg]) == 'table' then
| |
| table.insert( semanticData, property .. '=' .. table.concat( data[arg], delimiter ) )
| |
| table.insert( semanticData, '+sep=' .. delimiter )
| |
| --[[
| |
| -- instead of using a delimiter with +set, you could do something like this:
| |
| for _, v in pairs( data[arg] ) do
| |
| table.insert( semanticData, property .. '=' .. v )
| |
| end
| |
| --]]
| |
| else
| |
| table.insert( semanticData, property .. '=' .. data[arg] )
| |
| end
| |
| end
| |
| end
| |
| if #semanticData > 0 then
| |
| mw.smw.set( semanticData )
| |
| else
| |
| output = output .. formatError( 'There was no semantic data to store!' )
| |
| end
| |
|
| |
| -- rewrite some fields
| |
| if data['wikipedia page'] then
| |
| data['wikipedia page'] = '[' .. data['wikipedia page'] .. ' external]'
| |
| end
| |
|
| |
| -- display the infobox
| |
| local ibArgs = {
| |
| bodyclass = 'infobox_comic_book',
| |
| aboveclass = 'objtitle titletext',
| |
| headerclass = 'headertext',
| |
| labelstyle = 'width: 30%;',
| |
| datastyle = 'width: 70%;',
| |
| title = data.title,
| |
| subheader = 'Comic book',
| |
| }
| |
| local counter = 1 -- needed to set the correct arguments for the ibArgs table
| |
| for _, row in pairs ( infoboxConfig ) do
| |
| if row.argument and data[row.argument] then
| |
| if row.label then
| |
| ibArgs['label' .. counter] = row.label
| |
| end
| |
| ibArgs['data' .. counter] = getPrintout( row.argument, data[row.argument] )
| |
| counter = counter + 1
| |
| end
| |
| end
| |
| | |
| output = require( 'Module:Infobox' ).infobox( ibArgs ) .. '\n'
| |
| .. output
| |
|
| |
| return addCategory( category, output )
| |
| end
| |
| | |
| function addCategory( category, text )
| |
| local text = text
| |
| if not in_array( mw.title.getCurrentTitle().namespace, { 10, 11, 828, 829 } ) then
| |
| text = text .. '[[Category:' .. category .. ']]'
| |
| end
| |
| return text
| |
| end
| |
| | |
| | |
| function formatError( text )
| |
| return '<div style="color:red; font-weight:700">' .. text .. '</div>\n'
| |
| end
| |
| | |
| function getPrintout( field, data )
| |
| --[[
| |
| the printout of field depends on certain things:
| |
| * is it a table (aka list) or a single item
| |
| * should it be linked (aka is it present in linkFields)
| |
| * if so, should it be form linked? (aka not simply set to true but to a string) | |
| --]]
| |
|
| |
| local data = data
| |
| if type( data ) == 'table' then
| |
| for k, v in pairs( data ) do
| |
| data[k] = getPrintout( field, v )
| |
| end
| |
| return mw.text.listToText( data, ', ', ' and ' )
| |
| end
| |
|
| |
| if linkFields[field] then
| |
| if type( linkFields[field] ) == 'string' then
| |
| local frame = mw.getCurrentFrame()
| |
| local args = {
| |
| form = linkFields[field],
| |
| ['link text'] = data,
| |
| ['existing page link text'] = data
| |
| }
| |
| return frame:callParserFunction{ name='#formredlink:target=' .. data, args=args }
| |
| else
| |
| return '[[' .. data .. ']]'
| |
| end
| |
| else
| |
| return data
| |
| end
| |
| end
| |
| | |
| function in_array( needle, haystack )
| |
| local invertedHaystack = {}
| |
| for _, v in pairs( haystack ) do
| |
| invertedHaystack[v] = true
| |
| end
| |
| return invertedHaystack[needle]
| |
| end | | end |
|
| |
|
| return p | | return p |