« Module:Comic books » : différence entre les versions

De Semantic MediaWiki - Sandbox

Aucun résumé des modifications
(Contenu remplacé par « local p = {} function p.main( frame ) local comicBook = require( 'Module:Comic books/class' ):new() return comicBook:renderPage() end return p »)
 
(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 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 infoboxConfig = {
-- note the absence of field 'title'
-- 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 = 'main character', label = 'Protagonist' },
{ argument = 'publisher', label = 'Published by' },
{ argument = 'writer', label = 'Author' },
{ argument = 'artist', label = 'Artist(s)' },
{ argument = 'publication year', label = '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!' )
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 _, 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( '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
end


return p
return p

Dernière version du 25 décembre 2016 à 14:00

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 = {}

function p.main( frame )
	local comicBook = require( 'Module:Comic books/class' ):new()
	return comicBook:renderPage()
end

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