Modulo:Tabella animanga/sandbox

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
require('strict')
local getArgs = require('Modulo:Arguments').getArgs

local p = {}
local root -- radice del markup html
local row
local args -- argomenti passati al template
local tipo
local checkTitles

local function checkParameters()
	if tipo == 'volume' then
		args['episodi arco narrativo'] = args['volumi arco narrativo']
		args['numero episodio'] = args['numero volume']
		args['numero episodio2'] = args['numero volume2']
		args['larghezza numero episodio'] = args['larghezza numero volume']
		args['didascalia numero episodio'] = args['didascalia numero volume']
		args['larghezza numero episodio2'] = args['larghezza numero volume2']
		args['didascalia numero episodio2'] = args['didascalia numero volume2']
		args['cella titolo'] = nil
	elseif tipo == 'episodio' then
		args['isbn giappone'] = nil
		args['isbn giappone nota'] = nil
		args['isbn italia'] = nil
		args['isbn italia nota'] = nil
		args['capitoli'] = nil
	end
end

-- usa il template ISBN e concatena note
local function get_isbn(isbn)
	local nota = args[isbn .. ' nota'] or ''
	isbn = args[isbn]
	-- verifica la presenza di trattini usati come placeholder
	if isbn == '-' or isbn == '—' then return isbn end
	isbn = isbn and
			mw.getCurrentFrame():expandTemplate {
				title = 'ISBN',
				args = { isbn }
			} .. ' ' .. nota
	
	return isbn or ''
end

local function buildHeaderN(n)
	if not args['numero episodio' .. n] then return end
	local rows = (args['data giappone'] or
			args['data italia']) and '2'
	rows = rows or '1'
	local style = {
		['white-space'] = 'nowrap',
		width = args['larghezza numero episodio' .. n] or 'auto'
	}
	local text = n == '2' and '<br />[[Lingua italiana|It]]' or
			args['numero episodio2'] and '<br />[[Lingua giapponese|Ja]]'
	text = args['didascalia numero episodio' .. n] or
			'Nº' .. (text or '')
	root
		:tag('th')
			:attr('rowspan', rows)
			:css(style)
			:wikitext(text)
			:done()
end

local function buildHeaderTitle()
	-- titolo obbligatorio nel caso di Episodio Anime
	if not checkTitles and tipo ~= 'episodio' then return end
	local rows = (args['data giappone'] or
			args['data italia']) and '2'
	rows = rows or '1'
	local style = {
		background = '#eaecf0',
		['text-align'] = 'left',
		width = args['larghezza titolo'] or 'auto'
	}
	local textIt = '<b>[[Lingua italiana|Titolo italiano]]</b> ' ..
			(args['didascalia titolo italiano nota'] or '')
	textIt = args['didascalia titolo italiano'] or
			textIt
	textIt = (args['cella titolo'] or
			args['titolo italiano']) and
			textIt or ''
	
	local textJa = "'''[[Lingua giapponese|Giapponese]]''' " ..
			"「'''''[[Kanji]]'''''」 - " ..
			"'''''[[Rōmaji]]'''''"
	textJa = args['didascalia titolo giapponese'] or
			textJa
	
	local textTr = ' - <b>' .. (args['didascalia titolo traduzione'] or
			'[[Traduzione|Traduzione letterale]]') .. '</b>'
	textJa = textJa .. (args['titolo traduzione'] and
			textTr or '')
	textJa = (textIt ~= '' and '<br/>' or '') .. textJa
	textJa = args['titolo kanji'] and
			textJa or ''
	
	local text = textIt .. textJa
	root
		:tag('td')
			:attr('rowspan', rows)
			:css(style)
			:wikitext(text)
			:done()
end

local function buildHeaderDate(lg, cols)
	if not args['data ' .. lg] then return end
	
	local text = lg == 'giappone' and
			'giapponese|Giapponese' or
			'italiana|Italiano'
	text = args['didascalia data ' .. lg] or
			'[[Lingua ' .. text .. ']]'
	local style = args['larghezza data ' .. lg] or
			(tipo == 'episodio' and '15%')
	style = style and ('width:' .. style) or ''
	row
		:tag('th')
			:attr('colspan', 1 + cols)
			:cssText(style)
			:wikitext(text)
			:done()
end

local function buildArc()
	if not args['arco narrativo'] then return end
	row = root:tag('tr'):tag('td')
	local text = "'''" .. args['arco narrativo'] .. "'''"
	local ep = tipo == 'volume' and 'volumi' or 'episodi'
	local counter = args['episodi arco narrativo']
	ep = counter == '1' and tipo or ep
	ep = counter and (' (' .. counter .. ' ' .. ep .. ')') or ''
	text = text .. ep
	row
		:cssText('text-align:center')
		:attr('colspan', '7')
		:wikitext(text)
		:done()
end

local function buildNumber(number, rows)
	if not number then return end
	row
		:tag('td')
			:attr('rowspan', rows)
				:cssText('text-align:center')
				:wikitext(number)
				:done()
end

local function buildTitleList()
	local msg = {}
	local titleIt
	local titleJa
	local titleRo
	local titleTr
	-- div che crea una barra per episodi con titoli multipli
	local div = '<div style="border-top:1px solid #ccc;height:5px;"></div>'
	local span = '<span class="t_nihongo_kanji" lang="ja">「'
	
	local index = 1
	while true do
		index = index == 1 and '' or index
		titleIt = args['titolo italiano' .. tostring(index)]
		titleJa = args['titolo kanji' .. tostring(index)]
		titleRo = args['titolo romaji' .. tostring(index)]
		titleTr = args['titolo traduzione' .. tostring(index)]
		if titleIt or (titleJa and titleJa ~= '-') then
			msg[#msg+1] = index ~= '' and div or ''
			if titleIt then
				msg[#msg+1] = "'''''" .. titleIt .. "'''''"
				if titleJa or titleRo then
					msg[#msg+1] = '<br />'
				end
			end
			if titleJa and titleJa ~= '-' then
				msg[#msg+1] = span .. titleJa .. '」</span>'
				if titleRo then
					msg[#msg+1] = '&nbsp;-&nbsp;' .. "''" .. titleRo .. "''"
				end
				if titleTr then
					msg[#msg+1] = '&nbsp;–&nbsp;' .. '"' .. titleTr .. '"'
				end
			end
		else
			break
		end
		index = index == '' and 1 or index
		index = index + 1
	end
	return table.concat(msg)
end

local function buildDate(lg)
	local style = {
		['white-space'] = 'nowrap',
		['text-align'] = 'center'
	}
	if args['data ' .. lg] then
		local isbn = get_isbn('isbn ' .. lg)
		row
			:tag('td')
				:css(style)
				:wikitext(args['data ' .. lg] .. (checkTitles and
						('<br />' .. isbn) or ''))
				:done()
		if not checkTitles and isbn ~= '' then
			row
				:tag('td')
					:css(style)
					:wikitext(isbn)
					:done()
		end
	end
end

local function buildChapterList()
	if not args.capitoli then return end
	row = root:tag('tr'):tag('td')
	row
		:attr('colspan', '7')
		:wikitext("'''Capitoli'''\n<div style='padding:0 1em'>\n" .. args.capitoli .. '\n</div>')
		:done()
end

local function buildSummary()
	if not args.trama then return end
	row = root:tag('tr'):tag('td')
	local style = {
		['line-height'] = '1.4em'
	}
	local text = tipo == 'volume' and
			"'''Trama'''\n" or ''
	text = text .. '<div style="font-size:0.95em">\n' .. args.trama .. '\n</div>'
	row
		:attr('colspan', '7')
		:css(style)
		:wikitext(text)
		:done()
end

local function renderHeader()
	buildHeaderN('')
	buildHeaderN('2')
	buildHeaderTitle()
	if args['data giappone'] or args['data italia'] then
		local text = tipo == 'episodio' and 'In onda' or
				'Data di prima pubblicazione'
		text = args['didascalia data'] or text
		-- verifica il numero di colonne per Volume Manga
		local colsJa = not checkTitles and args['isbn giappone'] and 1 or 0
		local colsIt = not checkTitles and args['isbn italia'] and 1 or 0
		root
			:tag('th')
				:attr('colspan', 2 + colsJa + colsIt)
				:wikitext(text)
				:done()
		row = root:tag('tr')
		buildHeaderDate('giappone', colsJa)
		buildHeaderDate('italia', colsIt)
	end
end

local function renderBody()
	buildArc()
	row = root:tag('tr')
	local ep = tipo == 'episodio' and 'ep' or 'vol'
	ep = ep .. (args['numero episodio'] or '')
	local rows = 1
	row
		:attr('id', ep)
	buildNumber(args['numero episodio'], rows)
	buildNumber(args['numero episodio2'], rows)
	-- titolo obbligatorio nel caso di Episodio Anime
	if checkTitles or tipo == 'episodio' then
		local text = args['cella titolo']
		text = text or buildTitleList()
		row
			:tag('td')
				:wikitext(text)
				:done()
	end
	buildDate('giappone')
	buildDate('italia')
	buildChapterList()
	buildSummary()
end

-- Entry-point per i template Episodio Anime e Volume Manga
function p.main(frame)
	args = getArgs(frame, {parentOnly = not frame.args[1]})
	tipo = frame.args[1]
	-- gestisce i parametri a seconda del template
	checkParameters()
	-- variabile che cambia l'aspetto di Volume Manga
	checkTitles = args['titolo italiano'] or
			args['titolo kanji']
	local pre = ''
	local post = ''
	local width = 'width:' .. (args.larghezza or 'auto')
	local pos = args['posizione template']
	-- costruisce e ritorna la tabella
	root = mw.html.create('')
	if pos == 'testa' or pos == 'unico' then
		pre = '<table class="wikitable" style="' .. width .. '">'
		renderHeader()
	end
	if pos == 'coda' or pos == 'unico' then
		post = '</table>'
	end
	renderBody()
	return pre .. tostring(root) .. post
end

return p