Ir para o conteúdo

Módulo:Documento

Wikisource, a biblioteca livre
-- Limpa e separar todos os argumentos da função
function clean_args(args)
	local cleaned = {}
	for k,v in pairs(args) do
		v = mw.text.trim(v)
		if v == '' then
			v = nil
		end
		cleaned[k] = v
	end
	return cleaned
end

function normalize_title(title)
	if title.isRedirect then
		return normalize_title(title.redirectTarget)
	else
		return title
	end
end

function date_link(date)
	if tonumber(date) ~= nil then
        if tonumber(date) < 0 then
		  date = tostring(math.abs(tonumber(date))) .. ' a.C.'
	    end
	   local category_date = mw.title.makeTitle('Categoria',  date)
	   if category_date then 
		  return '[[:' .. category_date.fullText .. '|' .. date .. ']]'
	   else
		  return date
	   end
    else
        return date
    end
end

function analyser_title(title)
    	--    [[doc_title|doc_text]] doc_comment
	local doc_title = title
	local doc_text = title
	local doc_comment = ''
    	local item_id = nil

	if mw.ustring.match(title .. ' ', '^%s*%[%[[^%]]*%]%][^%[]*$') then
		for c1, c2, c3 in mw.ustring.gmatch( title .. ' ', '%[%[([^|]*)|(.*)%]%](.*)') do
			doc_title = c1
			doc_text = c2
			doc_comment = c3 --string.sub(c3,1,string.len (c3) - 1)
		end
		for c, c0  in mw.ustring.gmatch( title .. ' ', '%[%[([^|]*)%]%](.*)') do
			doc_title = c
			doc_text = c
			doc_comment = c0  --string.sub(c0,1,string.len (c0) - 1)
		end
	end
	local mw_title = mw.title.new(doc_title)
	if mw_title then
		doc_title = normalize_title(mw_title)
		item_id = mw.wikibase.getEntityIdForTitle(doc_title.fullText)
	end
	return {doc_title, doc_text, doc_comment, item_id}
end

function new_row(style, sort)
	local html = '\n| '
	if style then 
		html = html .. 'style="' .. style .. '" '
	end
	if sort then
		html = html .. 'data-sort-value="' .. mw.text.nowiki(sort) .. '" '
	end
	return html .. '|'
end

function error_message(text)
	-- Return a html formated version of text stylized as an error.
	local html = mw.html.create('div')
	html:addClass('error')
		:wikitext(text)
		:wikitext("[[Categoria:Páginas com uso incorreto do modelo Documento]]")
	return tostring(html)
end

local p = {}

function p.document(frame)
	parentFrame = frame:getParent()
    	args = clean_args(parentFrame.args)
    	--args = clean_args(frame.args)

	--Rendu
	local html = '\n|- style="vertical-align:top;" '
	
	if args.data and (args.galeria or args['edições'] or args['traduções']) then
		html = html .. new_row('white-space:nowrap;min-width:5em', nil)
	else
		html = html .. new_row('white-space:nowrap;', nil)
	end
	if args.data then
		html = html .. date_link(args.data) .. ' :'
	elseif args.datas then
		local formatted = {}
		local node = mw.html.create('div')
			:css('line-height', '90%')
			:css('font-size', '100%')
			:css('float', 'left')
		for k,data in pairs(mw.text.split(args.datas, '/')) do
			formatted[k] = date_link(data)
		end
		node:wikitext(table.concat(formatted, '<br/>') ..' :')
		html = html .. tostring(node)
	end

	local item_id = nil
	if args['edições'] then
        local doc_info = analyser_title(args['edições'])
        local doc_title = doc_info[1]
		local doc_text = doc_info[2]
		local doc_comment = doc_info[3]
		item_id = doc_info[4]
		html = html .. '<div style="float:right">[[Image:Disambig.svg|Várias edições disponíveis|link=|20px]]&nbsp;</div>'
		html = html .. new_row('padding-right: 2em;', args['título alpha'])
		if doc_title and doc_title.exists then
			html = html .. '[[' .. doc_title.fullText .. '|' .. doc_text .. ']]' .. doc_comment .. '&nbsp;'
		else
			html = html .. doc_text .. doc_comment .. '&nbsp;'
		end
	elseif args['traduções'] then
        local doc_info = analyser_title(args['traduções'])
        local doc_title = doc_info[1]
		local doc_text = doc_info[2]
		local doc_comment = doc_info[3]
		item_id = doc_info[4]
		html = html .. '<div style="float:right">[[Image:Disambig.svg|Várias traduções disponíveis|link=|20px]]&nbsp;</div>'
		html = html .. new_row('padding-right: 2em;', args['título alpha']) 
		if doc_title and doc_title.exists then
			html = html .. '[[' .. doc_title.fullText .. '|' .. doc_text .. ']]' .. doc_comment .. '&nbsp;'
		else
			html = html .. doc_text .. doc_comment .. '&nbsp;'
		end
	elseif args['título'] then
        local doc_info = analyser_title(args['título'])
        local doc_title = doc_info[1]
		local doc_text = doc_info[2]
		local doc_comment = doc_info[3]
		item_id = doc_info[4]
		if args.galeria then
			html = html .. '<div style="float:right">[[Image:Open book nae 02.svg|20px|link=Galeria:' .. args.galeria .. '|Transcrição do livro]]&nbsp;</div>'
		end			
		html = html .. new_row('padding-right:2em;', args['título alpha'])
		if doc_title and doc_title.exists then
			html = html .. '[[' .. doc_title.fullText .. '|' .. doc_text .. ']]' .. doc_comment .. '&nbsp;'
		else
			html = html .. doc_text .. doc_comment .. '&nbsp;'
		end
	else
		return '|-'
	end
	if args.progresso == '0' then
		html = html .. frame:expandTemplate{title = '000%'}
	elseif args.progresso == '1' then
		html = html .. frame:expandTemplate{title = '025%'}
	elseif args.progresso == '2' then
		html = html .. frame:expandTemplate{title = '050%'}
	elseif args.progresso == '3' then
		html = html .. frame:expandTemplate{title = '075%'}
	elseif args.progresso == '4' then
		html = html .. frame:expandTemplate{title = '100%'}
	elseif args.progresso == '5' then
		html = html .. frame:expandTemplate{title = 'Validado'}
	elseif args.progresso then
		mw.addWarning( 'Este valor' .. mw.text.jsonEncode(args.progresso) .. ' do parâmetro de progresso não é compatível com o modelo de documento.' )
	end	
	if args[1] == 'epub' or args[1] == 'exportar' then
		html = html .. '&nbsp;' .. frame:expandTemplate{title = args[1], args = {args['título']}}
	elseif args[1] then
		html = html .. error_message('Parâmetro com nome desconhecido: ' .. args[1])
	end
	if args['epub'] then
		html = html .. '&nbsp;' .. frame:expandTemplate{title = 'Epub', args = {args.epub}}
	end
	if args['exportar'] then
		html = html .. '&nbsp;' .. frame:expandTemplate{title = 'Exportar', args = {args.exportar}}
	end
	if item_id then
		html = html .. ' [[Image:Blue pencil.svg|10px|link=d:' .. item_id .. '|Visualização e edição de dados no Wikidata]]'
	end
	if args.tn then
		html = html .. new_row('font-size:90%; padding-right:2em:', args['tn alpha']) .. args.tn
	end
	
	if args.autor then
		local autor_title = args.autor
		local autor_text = args.autor
		for c1, c2 in mw.ustring.gmatch( args.autor, '%[%[([^|]*)|(.*)%]%]') do
			autor_title = c1
			autor_text = c2
		end
		for c in mw.ustring.gmatch( args.autor, '%[%[([^|]*)%]%]') do
			autor_title = c
			autor_text = c
		end
    	html = html .. new_row('font-size:90%; padding-right:2em;', args['autor alpha'])
		local autor_title = mw.title.new(autor_title, 'Autor')
		if autor_title and autor_title.exists then
			html = html .. '[[' .. autor_title.fullText .. '|' .. autor_text .. ']]'
		else
			html = html .. autor_text
		end
	end
	
	if args['gênero'] then
		html = html .. new_row('font-size:90%; padding-right:2em;', nil) .. args['gênero']
	end
	
	if args.scan then
		html = html .. new_row('font-size:90%; padding-right:2em;', nil) .. '<span class="plainlink">' .. args.scan .. '</span>'
	end
	
	if args.notas then
		html = html .. new_row('font-size:90%; padding-right:2em;', args['notas alpha']) .. args.notas
	end
	
	return html
end


return p