Jump to content

Module:Internet Archive small links

From Wikisource

require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local make_style_string = require('Module:Optional style').make_style_string

local function IAlink(id, suffix)
	local add
	if suffix then
		add = " " .. suffix
	else
		add = ""
	end
	return "[https://archive.org/details/" .. (id or "") .. " IA" .. add .. "]"
end

function p.links(frame)
	local args = getArgs(frame)
	
	-- make links
	local start = tonumber(args.start) or 1
	local prefix = args.prefix or ''
	local suffix = args.suffix or ''
	local numbers = yesno(args.numbers or 'yes')
	local display = args.display
	
	local style = make_style_string({['font-size'] = '83%', ['style'] = args.style})
	
	local links = {}
	for i, id in ipairs(args) do
		if numbers then
			links[i] = IAlink(id, i + start - 1)
		else
			links[i] = IAlink(id, display)
		end
	end
	
	local link_string = '<span title="Copy of this work at the Internet Archive" ' .. style .. '">' .. prefix .. table.concat(links, ", ") .. suffix .. '</span>'
	
	-- tracking cat
	local cat
	local page = mw.title.getCurrentTitle()
	if (page.nsText == 'Template' or page.nsText == 'Module') and page.rootText == 'Internet Archive small links' then
		cat = ''
	else
		cat = '[[Category:Pages linking to scanned works at Internet Archive]]'
	end
	
	return link_string .. cat
end

return p