|
|
(8 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 pageHeadline = 'This is a comic book'
| |
|
| |
| local parameters = {
| |
| title = 'has comic book title',
| |
| ['main character'] = 'has main character',
| |
| publisher = 'has comic book publisher',
| |
| writer = 'has comic book writer',
| |
| artist = 'has comic book artist',
| |
| ['publication year'] = 'was published in year'
| |
| }
| |
|
| |
| local labels = {
| |
| -- note the absence of field 'title'
| |
| ['main character'] = 'Protagonist',
| |
| publisher = 'Published by',
| |
| writer = 'Author',
| |
| artist = 'Artist(s)',
| |
| ['publication year'] = 'Publication year'
| |
| }
| |
|
| |
| local linkFields = {
| |
| -- if a field is set to true, it will be linked
| |
| -- if it is set to a string, #formlink will be used
| |
| ['main character'] = 'character',
| |
| publisher = true,
| |
| writer = 'Person',
| |
| artist = true,
| |
| }
| |
|
| |
| local mandatory = { 'main character', 'writer' }
| |
| local listFields = { 'main character', 'artist' }
| |
| local delimiter = ','
| |
|
| |
|
| function p.main( frame ) | | function p.main( frame ) |
| local args = getArgs( frame, { parentOnly = true } ) | | local comicBook = require( 'Module:Comic books/class' ):new() |
| return p._main( args )
| | return comicBook: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.title then
| |
| data.title = 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!' ) .. '\n'
| |
| 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] > 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!' ) .. '\n'
| |
| 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 argument, label in pairs( labels ) do
| |
| if data[argument] then
| |
| ibArgs['label' .. counter] = label
| |
| ibArgs['data' .. counter] = getPrintout( argument, data[argument] )
| |
| counter = counter +1
| |
| end
| |
| end
| |
| | |
| output = require( 'Module:Infobox' ).infobox( ibArgs ) .. '\n'
| |
| .. output
| |
|
| |
| return output .. '[[Category:Comic books]]'
| |
| end
| |
| | |
| function formatError( text )
| |
| return '<span style="color:red; font-weight:700">' .. text .. '</span>'
| |
| 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 |