Jump to content

Module:Folger Shakespeare

From Wikipedia, the free encyclopedia

-- Modules we need
local getArgs = require('Module:Arguments').getArgs
local warn = require('Module:Warning')

-- Configuration
local cfg = mw.loadData('Module:Folger Shakespeare/config');
local _plays = mw.loadData('Module:Folger Shakespeare/plays');

-- Turn plays table inside out so we can look up by alias too
local plays = {}
for _, play in pairs(_plays) do
	for _, key in pairs(play.keys) do
		plays[key] = {
			["title"] = play.title,
			["folgerid"] = play.folgerid,
			["wikilink"] = play.wikilink
		}
	end
end


-- This module's function lookup table, used by the calling context
local p = {}

function p.sfd(args)
	local play  = plays[args[1]]
	local act   = tonumber(args[2])
	local scene = tonumber(args[3])
	local line  = args[4]

	-- Check the play argument
	if play == nil or play == "" then
		warn("No play argument provided, or the play provided was not recognised.")
		return
	end

	-- Check the act argument
	if act == nil or act == "" then
		warn("No act argument provided, or the act given was not numerical.")
		return
	end

	-- Check the scene argument
	if scene == nil or scene == "" then
		warn("No scene argument provided, or the scene given was not numerical.")
		return
	end

	-- Check the line argument
	if line == nil or line == "" then
		warn("No line argument provided.")
		return
	end

	local display_line = line
	if mw.ustring.match(line, '^%s*%d+[-–]%d+%s*$') then
		line = mw.ustring.match(line, '^%s*(%d+)[-–]%d+%s*$')
	elseif mw.ustring.match(line, '^%s*%d+%s*$') then
		line = mw.ustring.match(line, '^%s*(%d+)%s*$')
	else
		warn(("Could not coerce %s into a valid line specification."):format(line))
		return
	end

	local location = mw.ustring.format('line-%d.%d.%d', act, scene, line)
	local url = mw.ustring.format(cfg.url_pattern, play.folgerid, act, scene, location)

	local   id = play.title .. act .. '_' .. scene .. '_' .. display_line
	local name = 'FOOTNOTE' .. id

	local location_link = mw.ustring.format(cfg.location_format, url, act, scene, display_line)
	local cite = mw.ustring.format("''%s'', %s", play.title, location_link)

	local result = mw.getCurrentFrame():extensionTag{
		name = 'ref',
		args = {name = name},
		content = cite,
	}
	return result
end

function p.supported_plays(frame)
	local t = mw.html.create('table')
		:addClass('wikitable')
		:addClass('sortable')
	t:tag('tr')
		:tag('th'):wikitext('Play')
		:tag('th'):wikitext('Aliases')
	for _, play in ipairs(_plays) do
		local playlink = mw.ustring.format("[[%s|%s]]", play.wikilink, play.title)
		local row = t:tag('tr')
		row:tag('td'):wikitext(playlink)
		local aliasList = frame:expandTemplate{
			title = 'hlist',
			args = play.keys
		}
		row:tag('td'):wikitext(aliasList)
	end
	return t
end

function p.main(frame)
	local args = getArgs(frame)
	return p.sfd(args)
end

return p