Module:Comic books

De Semantic MediaWiki - Sandbox

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 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 )
	local args = getArgs( frame, { parentOnly = true } )
	return p._main( args )
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!' )
		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
	
	-- 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 addCategory( 'Comic books', 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

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