Module:Ext scan link

Module documentation[view] [edit] [history] [purge]

Logic for {{ext scan link}}

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

local p = {} --p stands for package
local getArgs = require('Module: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
			error("Missing = before parameter: " .. k .. "=" .. v, 0)
		end
	end

	
	local links = generate_links_from_args(args)
	local out = format_links(links,
		'external scan',
		'external scans (multiple parts): ',
		', ')
	return out
end

return p