Modul:Parts

Fra Wikipedia, den frie encyklopedi
Moduldokumentasjon

Parts er et bibliotek for å formatere en liste av signaturer for refererte elementer. Oppførselen som konstrueres består av etiketten og beskrivelsen, med en eventuell nettstedlenke som går til aktuell artikkel. En tilsvarende modul på nnwiki konstruerer i tillegg en lenke til spesialsiden «AboutTopic» for elementet.

Modulen er antatt å være spesifikk for de enkelte elementene som brukes, og vil derfor ikke ta ekstra parametre.

Bruk[rediger kilde]

Kallet er uten ekstra parametre

{{#invoke:Parts|list}}

Dette er eneste form som så langt gir mening.

local parts = {}

function parts.makeList( data )
	local html = mw.html.create( 'div' ) -- this is the outer containing node
	local ul = true
	local last = ''
	for i,item in ipairs( data ) do
		local id = item[1]
		local label = item[2] or item[4] or item[1] or "?" -- should never reach "?"
		local description = item[3]
		local sitelink = item[4]
		local codePoint = mw.ustring.upper( mw.ustring.sub( label, 1, 1 ) )

		if last ~= codePoint then -- the char is different from the last one
			-- create a dd-element and use the code point as header
			local dd = mw.html.create( 'dd' )
			html:node ( mw.html.create( 'dl' )
				:node( mw.html.create( 'dt' ):node( mw.html.create( 'span' ):attr( 'id', codePoint ) ):wikitext( codePoint ) )
				:node( dd ) )
			-- create a new ul-element as container for new signatures
			ul = mw.html.create( 'ul' ) -- this is the list node that has to be recreated for each char in the alphabet
			dd:node( ul )
			last = codePoint
		end

		-- add the signature to the containing ul-element
		ul:node( mw.html.create( 'li' )
			:node( mw.html.create( 'span' ):attr( 'id', label ) )
			:wikitext( sitelink
				-- use the sitelink
				and ( string.format( "''[[%s|%s]]''[[File:Blue pencil.svg|11px|link=d:%s]]", sitelink, label, id ) .. ( description and string.format( ' – %s', description ) or '' ) )
				-- do not use the sitelink
				or ( string.format( "''%s''[[File:Blue pencil.svg|11px|link=d:%s]]", label, id ) .. ( description and string.format( ' – %s', description ) or '' ) )))
	end
	return html
end

function parts.list( frame )
	local entityId = frame.args['root'] or frame:getParent().args['root'] or mw.wikibase.getEntityIdForCurrentPage()
	if not entityId or not string.find( entityId, '^Q%d+$' ) then
		local html = mw.html.create('span')
			:wikitext( 'Missing entity id for root' )
		return html
	end

	local listId = frame.args['entries'] or frame:getParent().args['entries']
	if not listId or not string.find( listId, '^P%d+$' ) then
		local html = mw.html.create('span')
			:wikitext( 'Missing property id for part list' )
		return html
	end

	local data,_ = require 'Module:SignatureList'( entityId, listId )

	return parts.makeList( data )
end

function parts.makeTBody( data, ... )
	local html = mw.html.create() -- this is the outer containing node
	--html:addClass( 'wikitable' )
	for i,item in ipairs( data ) do
		local id = item[1]
		local label = item[2] or item[4] or item[1] or "?" -- should never reach "?"
		local description = item[3]
		local sitelink = item[4]
		local entity = mw.wikibase.getEntity( id )

		local tr = mw.html.create( 'tr' )
		local th = mw.html.create( 'th' )
		th:node( mw.html.create( 'span' )
			:attr( 'id', label ) )
			:wikitext( sitelink
				-- use the sitelink
				and ( string.format( "[[%s|%s]][[File:Blue pencil.svg|11px|link=d:%s]]", sitelink, label, id ))
				-- do not use the sitelink
				or ( string.format( "%s[[File:Blue pencil.svg|11px|link=d:%s]]", label, id ) ))
		tr:node( th )
		for _,v in ipairs( { ... }) do
			--local snak = (((entity.claims or {})[v] or {})[1] or {}) -- ['mainsnak']
			local snaks = {}
			for _,w in pairs( (entity.claims or {})[v] or {} ) do
				table.insert( snaks, w.mainsnak )
			end
			local td = mw.html.create( 'td' )
			:wikitext( snaks and mw.wikibase.formatValues( snaks ) or '' )
			tr:node( td )
		end
		html:node( tr )
	end
	return html
end

function parts.table( frame )
	local entityId = frame.args['root'] or frame:getParent().args['root'] or mw.wikibase.getEntityIdForCurrentPage()
	if not entityId or not string.find( entityId, '^Q%d+$' ) then
		local html = mw.html.create('span')
			:wikitext( 'Missing entity id for root' )
		return html
	end

	local listId = frame.args['entries'] or frame:getParent().args['entries'] or 'P527'
	if not listId or not string.find( listId, '^P%d+$' ) then
		local html = mw.html.create('span')
			:wikitext( 'Missing property id for part list' )
		return html
	end

	local columnIds = {}
	for i,v in ipairs( frame.args ) do
		table.insert( columnIds, v )
	end
	if frame:getParent() then
		for i,v in ipairs( frame:getParent().args ) do
			table.insert( columnIds, v )
		end
	end
	
	local data,_ = require 'Module:SignatureList'( entityId, listId )
	
	return parts.makeTBody( data, unpack( columnIds ) )
end

return parts