Différences entre les versions de « Module:Comic books »

De Semantic MediaWiki - Sandbox

Ligne 95 : Ligne 95 :
 
ibArgs['data' .. counter] = mw.text.listToText( data[argument] )
 
ibArgs['data' .. counter] = mw.text.listToText( data[argument] )
 
else
 
else
ibArgs['label' .. counter] = data[argument]
+
ibArgs['data' .. counter] = data[argument]
 
end
 
end
 
counter = counter +1
 
counter = counter +1

Version du 15 décembre 2016 à 10:18

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
			return formatError( 'Mandatory argument "' .. m .. '" is missing. Aborting!' )
		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 )
			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. Aborting!' )
	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

function in_array( needle, haystack )
	local invertedHaystack = {}
	for _, v in pairs( haystack ) do
		invertedHaystack[v] = true
	end
	return invertedHaystack[needle]
end

return p
Les cookies nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de cookies.