Différences entre les versions de « Module:Comic books »
De Semantic MediaWiki - Sandbox
Ligne 46 : | Ligne 46 : | ||
for _, m in pairs( mandatory ) do | for _, m in pairs( mandatory ) do | ||
if not args[m] then | if not args[m] then | ||
− | + | output = output .. formatError( 'Mandatory argument "' .. m .. '" is missing!' ) .. '\n' | |
end | end | ||
end | end | ||
Ligne 67 : | Ligne 67 : | ||
table.insert( semanticData, property .. '=' .. table.concat( data[arg], delimiter ) ) | table.insert( semanticData, property .. '=' .. table.concat( data[arg], delimiter ) ) | ||
table.insert( semanticData, '+sep=' .. 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 | else | ||
table.insert( semanticData, property .. '=' .. data[arg] ) | table.insert( semanticData, property .. '=' .. data[arg] ) | ||
Ligne 75 : | Ligne 81 : | ||
mw.smw.set( semanticData ) | mw.smw.set( semanticData ) | ||
else | else | ||
− | output = output .. formatError( 'There was no semantic data to store | + | output = output .. formatError( 'There was no semantic data to store!' ) .. '\n' |
end | end | ||
Ligne 108 : | Ligne 114 : | ||
function formatError( text ) | function formatError( text ) | ||
return '<span style="color:red; font-weight:700">' .. text .. '</span>' | return '<span style="color:red; font-weight:700">' .. text .. '</span>' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
return p | return p |
Version du 15 décembre 2016 à 10:32
It uses the class on Module:Comic books/class to process its arguments, store them semantically and displays an infobox.
This module stores its configuration on Module:Comic books/config.
local p = {}
local getArgs = require( 'Module:Arguments' ).getArgs
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 = {
['main character'] = 'Protagonist',
publisher = 'Published by',
writer = 'Author',
artist = 'Artist(s)',
['publication year'] = 'Publication year'
}
local mandatory = { 'main character', 'writer' }
local listFields = { 'main character', 'artist' }
local delimiter = ','
function p.main( frame )
local args = getArgs( frame, { parentOnly = true } )
return p._main( args )
end
function p._main( args )
local output = '== This is a comic book ==\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 )
end
if #data[lf] == 1 then
data[lf] = data[lf][1]
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_name',
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
if type( data[argument] ) == 'table' then
ibArgs['data' .. counter] = mw.text.listToText( data[argument] )
else
ibArgs['data' .. counter] = data[argument]
end
counter = counter +1
end
end
output = output .. require( 'Module:Infobox' ).infobox( ibArgs )
return output .. '[[Category:Comic books]]'
end
function formatError( text )
return '<span style="color:red; font-weight:700">' .. text .. '</span>'
end
return p