Module:CCE page link

From Wikisource
Jump to navigation Jump to search

--[=[
A module to generate lists of links to Catalog of Copyright Entries (CCE) volumes,
using the underlying data resource at [[Module:CCE page link/data]].
]=]

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

local DATA = mw.loadData('Module:CCE page link/data')

local function ia_link(id, offset, page, mode, text)

	if offset ~= nil then
		page = 'n' .. page + offset
	end

	return '[https://archive.org/details/' .. id .. '/page/' .. page .. '/mode/' .. mode .. ' ' .. text .. ']'
end

local function gb_link(id, prefix, page, text)
	return '[' .. 'https://books.google.com/books?id=' .. id .. '&pg=' .. prefix .. page .. ' ' .. text .. ']'
end

--[=[
Make a list (table) of links to a scan, given a "scan" object, which can be
an IA or Google Books scan descriptor, and a list of page numbers (which are
assumed to be accessible by the URL, so it might be n16 for an IA scan that doesn't
have all the pages numbered)
]=]
function make_index_list(scan, pages)
	local links = {}
	
	if scan == null or pages == nil then
		return links
	end

	if scan.src == 'ia' then
		for i, v in pairs(pages) do
			local letter = string.char( 64 + i )
			if v == 0 then
				table.insert(links, "''" .. letter .. "''" )
			else
				table.insert(links, ia_link( scan.id, scan.offset, v, '1up', letter ) )
			end
		end
	elseif scan.src == 'gb' then
		for i, v in pairs(pages) do
			local letter = string.char( 64 + i )
			if v == 0 then
				table.insert(links, "''" .. letter .. "''" )
			else
				local prefix = scan.prefix or 'PA'
				table.insert(links, gb_link( scan.id, prefix, v, letter ) )
			end
		end
	else
		error('Unknown vol type: ' .. vol.scan[1])
	end	
	
	return links
end

--[=[
Generate a year's row for a table of data.

Arguments:
	1: year - the year of the data to look up (e.g. 1975)
    2: type - the registration type (e.g. book)
]=]
function p.year_row(frame)
	local args = getArgs(frame)
	
	local year = args[1]
	local type = args[2]
	
	-- no data for this year or type
	if not DATA.years[year] or not DATA.years[year][type] then
		return "|-\n|\n"
	end

	local ydata = DATA.years[year][type]
	local rows = 2
	
	local upenn_type = type
	local upenn = "[http://onlinebooks.library.upenn.edu/cce/" .. year .. "r.html#" .. upenn_type .. " UPenn]"

	s = "|-\n"
	s = s .. "| rowspan=" .. rows .. " | " .. year .. "\n"
	s = s .. "| rowspan=" .. rows .. " | " .. (year - 28) .. "\n"
	s = s .. "| rowspan=" .. rows .. " | " .. upenn .. "\n"
	
	for k, part in pairs(ydata) do
		if k > 1 then
			s = s .. "|-\n"
		end
		
		local pg = ""
		if part.pg then
			pg = "[https://www.gutenberg.org/ebooks/" .. part.pg .. ".txt.utf-8" .. " PG]"
		end
		s = s .. "| " .. pg .. "\n"
		
		local commons = ""
		if part.commons then
			commons = "[[:File:" .. part.commons .. "|File]] / [[Index:" .. part.commons .. "|Index]]"
		end
		s = s .. "| " .. commons .. "\n"
		
		local az = ''
		if part.renewals then
			local scan = DATA.volumes[part.renewals.vol]
			
			if scan then
				local renewal_list = make_index_list(scan.scans[1], part.renewals.az)
				az = table.concat(renewal_list, ' ')
			end
		end
		s = s .. "|" .. az .. "\n"
		
		az = ''
		if part.current then
			local scan = DATA.volumes[part.current.vol]
			
			if scan then
				local renewal_list = make_index_list(scan.scans[1], part.current.az)
				az = table.concat(renewal_list, ' ')
			end
		end
		s = s .. "|" .. az .. "\n"
	end
	
	return s
end
return p