Moduł:Information

Z Nonsensopedii, polskiej encyklopedii humoru
Wersja z dnia 13:38, 9 lis 2020 autorstwa Ostrzyciel (dyskusja • edycje) (+śledzenie linków w podpisie)

Moduł generujący tabelkę w szablonie {{Information}} i ustawiający odpowiednie własności semantyczne. Instrukcję korzystania znajdziesz na stronie Nonsensopedia:Opisywanie grafik.

Zobacz też: Moduł:InformationProvider, który pobiera informacje o plikach.


local tools = require('Moduł:Narzędzia')
local linki = require('Moduł:Linki')
local data
local props = {}
local rows = {}

-- funkcje lokalne

-- funkcja pomocnicza do scalania tabel atrybutów
local function appendProps(key, value)
	props[key] = props[key] or {}
	table.insert(props[key], value)
end

local function addRow(id, name, content, trid, tdstyle)
	local row = '<tr style="vertical-align: top"'
	if not content then content = '' end
	
	if trid then
		row = row .. '; id="' .. trid .. '">'
	else
		row = row .. '>'
	end
	
	row = row .. '<td id="' .. id .. '" style="background: #ccf; text-align: right; padding-right: 0.4em; width: 15%; font-weight:bold">'
	row = row .. name .. '</td>'
	
	if tdstyle then
		row = row .. '<td style="' .. tdstyle .. '">' .. content .. '</td></tr>'
	else
		row = row .. '<td>' .. content .. '</td></tr>'
	end
	
	table.insert(rows, row)
end

local function getTable()
	local s = 
		'<table class="information-template toccolours" style="width: 100%;" cellpadding="4">'
		.. table.concat(rows)
		.. '</table>'
	return s
end

local function processParts(frame, args)
	local prows = {}
	
	local function pPart(num)
		local row = '<tr><td>' .. args['psource' .. num] .. '</td>'
		row = row .. '<td>' .. (args['pauthor' .. num] or '') .. '</td>'
		local plic = (args['plic' .. num] or 'nolic')
		plic = frame:expandTemplate{ title=plic, args={ mini=1 } }
		row = row .. '<td style="padding: 0;">' .. plic .. '</td></tr>'
		table.insert(prows, row)
	end
	
	if args['psource'] ~= nil then pPart('') end
	
	for i = 1, 50 do
		if args['psource' .. i] ~= nil then pPart(i)
		else break end
	end
	
	if #prows == 0 then return end
	
	local s = [[<table class="info-inner"><tr>
<td style="background: #ccf; font-weight:bold">Źródło</td>
<td style="background: #ccf; font-weight:bold">Autor</td>
<td style="background: #ccf; font-weight:bold">Licencja</td>
</tr>]]
	s = s .. table.concat(prows) .. '</table>'
	addRow('fileinfotpl_parts', 'Składowe', s, 'fileinforow_parts', 'padding: 0')
end

-- funkcje eksportowane
local p = {}

-- zrób cuda
function p.information(frame)
	local args = tools.getArgs(frame)
	local demo = args['demo'] ~= nil
	
	-- caption
	local captionLinksCount = 0
	-- stosujemy podpis jako domyślny opis dla MMV, a w razie jego braku bierzemy pole description
	local descriptionTag = 'fileinfotpl_desc'
	if args['caption'] or demo then
		appendProps('Ma podpis pliku', args['caption'])
		addRow('fileinfotpl_desc', 'Podpis', args['caption'])
		descriptionTag = 'fileinfotpl_full_desc'
		
		local captionLinks = linki.wLink(args['caption'])
		for k, v in pairs(captionLinks) do
			appendProps('Linkuje w podpisie', k)
			captionLinksCount = captionLinksCount + 1
		end
	else
		appendProps('Nie ma podpisu pliku', true)
	end
	appendProps('Liczba linków w podpisie', captionLinksCount)
	
	-- description
	if args['description'] then
		appendProps('Ma opis', args['description'])
		addRow(descriptionTag, 'Opis', args['description'])
	else
		appendProps('Nie ma opisu', true)
		addRow(descriptionTag, 'Opis', "''Brak''")
	end
	
	-- składowe
	processParts(frame, args)
	
	-- data
	if args['date'] or demo then
		if not data then data = require('Module:Data') end		-- lazy
		local dSMW, dClean = data.processToSMW(args['date'])
		appendProps('Ma opis daty', dClean)
		addRow('fileinfotpl_date', 'Data', dSMW)
	else
		appendProps('Nie ma daty', true)
	end
	
	-- źródło
	if args['source'] then
		local source = require('Module:Źródło')
		local aprops, s = source.process(args['source'])
		if not demo then mw.smw.set(aprops) end
		addRow('fileinfotpl_source', 'Źródło', args['source'])
	else
		addRow('fileinfotpl_source', 'Źródło',
			"'''Nie podano źródła.''' Uzupełnij <code>Source=</code> (''Praca własna'', adres URL strony lub pliku)")
		if not demo then appendProps('Nie ma podanego źródła', true) end
	end
	
	-- author
	if args['author'] or demo then
		local autor = require('Module:Autor')
		local aprops, s = autor.process(args['author'])
		if not demo then mw.smw.set(aprops) end
		addRow('fileinfotpl_author', 'Autor', s)
	end
	
	-- permission
	if args['permission'] or demo then
		-- TODO: smw i te sprawy
		addRow('fileinfotpl_perm', 'Licencja', args['permission'])
	end
	
	-- confirmation
	if args['confirmation'] or demo then
		appendProps('Ma potwierdzenie licencji', args['confirmation'])
		addRow('fileinfotpl_confirmation', 'Potwierdzenie licencji', args['confirmation'])
	end
	
	-- other versions
	local ov = args['other_versions']
	if (ov and ov ~= '' and ov ~= '-' and ov ~= 'none') or demo then
		local ovs = linki.wLink(ov or '')
		for l, v in pairs(ovs) do
			if mw.ustring.find(l, ':') == 1 then
				appendProps('Ma inną wersję', mw.ustring.gsub(l, ';', '\\;') .. ';' .. mw.ustring.gsub(v, ';', '\\;'))
			elseif #l > 0 then
				appendProps('Ma inną wersję', mw.ustring.gsub(l, ';', '\\;'))
			end
		end
		
		addRow('fileinfotpl_ver', 'Inne wersje', ov)
	end
	
	-- location
	local loc = args['location']
	if loc then
		local locModule = require('Moduł:Lokalizacja')
		local _, text, geoprops = locModule.parseLocation(frame, loc, true)
		
		for key, val in pairs(geoprops) do
			appendProps(key, val)
		end
		
		addRow('fileinfotpl_loc', 'Lokalizacja', text)
	end
	
	if not demo then mw.smw.set(props) end
	return getTable()
end

return p