Module:StoredCites

From PHIGHTING! Wiki

Renders a citation from a list by a simple index key. See Template:RefLink for more comprehensive usage documentation.


-- Originally imported from Module:Availability on Nookipedia 
-- even though I hacked like most of it out but still have me inspiration
-- Credit to Nookipedia User:PanchamBro 
-- (Re?)Written by mh:meta:User:PixDeVl(@pixldev on Discord) 
local p = {}
local getArgs = require('Module:Arguments').getArgs
local data = mw.loadData("Module:StoredCites/Data")

function setDefault (t, d)
	local mt = {__index = function () return d end}
	setmetatable(t, mt)
end

-- Imported function from https://www.lua.org/pil/13.4.3.html --
function copyTable(t)
	local t2 = {}
	for k,v in pairs(t) do
    	t2[k] = v
	end
	return t2
end

function p.main(frame)
    local args      = getArgs(frame)
    local ref_id     = tonumber(args[1]) or args[1] or ''

    return p.renderRef(ref_id)
end

function p.renderRef (ref_id)
	local function isEmpty(s)
		return s == nil or s == ''
	end
	local print = ''
	local info = data[ref_id]
	assert(ref_id, "No Ref Id provided. Silly")
	assert(info, "Nothing found for the given ref_id " .. ref_id)
	print = print .. '<b>' .. info.author .. '</b>' .. ', ' .. '<b>' .. info.date .. '</b>' .. ' ( ' 
	if string.find(info.link, "https?://") then
		print = print .. '[' .. info.link .. ' Link] '
	else
		print = print .. '[[' .. info.link .. '|Link]] '
	end
	if not isEmpty(info.image) then
		print = print .. '&#124; [' .. info.image .. ' Image]'
	end
	print = print .. ' ) - ' .. info.quote

	return print
end
return p