Saltar para o conteúdo

Módulo:Ext scan link

Wikisource, a biblioteca livre
Documentação do módulo[ver] [editar] [histórico] [purgar]

Logic for {{ext scan link}}

--[=[
Logic for [[Template:Ext scan link]]
]=]

local p = {} --p stands for package
local getArgs = require('Módulo:Arguments').getArgs


function generate_links_from_args(args)
	local links = {}
	
	local i = 1
	while args[i] ~= nil do
		
		local link = {
			href = args[i],
			filename = args['filename' .. i],
			display = args['display' .. i]
		}
		
		table.insert(links, link)
		i = i + 1
	end
	return links
end

function format_links(links, single_link_text, multi_link_prefix, sep)
	local out = mw.html.create('span')
		:addClass('wst-ext-scan-links')
		:wikitext('(')
	
	if #links > 1 then
		out:wikitext(multi_link_prefix)
	end

	for k, link in pairs(links) do
		
		if k > 1 then
			out:wikitext(sep)
		end
		
		local display = link.display
		if display == nil then
			if #links == 1 then
				display = single_link_text
			else
				display = tostring(k)
			end
		end

		local span = out:tag('span')
			:addClass('wst-ext-scan-link')
			:wikitext('[' .. link.href .. ' ' .. display .. ']')
			
		if link.filename then
			span:attr('data-upload-filename', link.filename)	
		end
	end
	
	out:wikitext(')')
	return out
end

--[=[
Main entry point
]=]
function p.ext_scan_link(frame)
	local args = getArgs(frame)

	for k, v in pairs(args) do
		if string.match(k, "^https?://") ~= nil then
			local err = ""
			err = err .. "<strong class='error'>"
			err = err .. "Missing = before parameter: '" .. k .. "'='" .. v .. "'"
			err = err .. "</strong>"
			err = err .. "[[Category:" .. "Pages using ext scan link with unescaped equals signs" .. "]]"
			return err
		end
	end

	
	local links = generate_links_from_args(args)
	local out = format_links(links,
		'Digitalização em site externo',
		'Digitalização em site externo (em várias partes): ',
		', ')
	return out
end

return p