Module:Citation

From Warwick Wiki
Revision as of 20:38, 23 August 2025 by AlfieG (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Powers {{citation}}.


local p = {}
local h = {}

function h.existsNotBlank(var)
	return var and var ~= ""
end

function p.citation(frame)
	local args = frame:getParent().args
	
	local authors = args["authors"] or ""
	if authors == "" then
		if h.existsNotBlank(args["author"]) or h.existsNotBlank(args["author1"]) then
			authors = args["author"] or args["author1"]
		elseif h.existsNotBlank(args["last"]) or h.existsNotBlank(args["last1"]) then
			if h.existsNotBlank(args["first"]) or h.existsNotBlank(args["first1"]) then
				authors = authors .. (args["first"] or args["first1"]) .. " "
			end
			authors = authors .. (args["last"] or args["last1"])
		end
		if h.existsNotBlank(args["author-link"]) or h.existsNotBlank(args["author-link1"]) then
			authors = "[[" .. (args["author-link"] or args["author-link1"]) .. "|" .. authors .. "]]"
		end
		
		local finished = false
		local i = 2
		if not (h.existsNotBlank(args["author" .. tostring(i)]) or h.existsNotBlank(args["last" .. tostring(i)])) then
			finished = true
		end
		while not finished do
			local displayText = ""
			if h.existsNotBlank(args["author" .. tostring(i)]) then
				displayText = displayText .. args["author" .. tostring(i)]
			elseif h.existsNotBlank(args["last" .. tostring(i)]) then
				if h.existsNotBlank(args["first" .. tostring(i)]) then
					displayText = displayText .. args["first" .. tostring(i)] .. " "
				end
				displayText = displayText .. args["last" .. tostring(i)]
			end
			if h.existsNotBlank(args["author-link" .. tostring(i)]) then
				authors = authors .. ", [[" .. args["first" .. tostring(i)] .. "|" .. displayText .. "]]"
			else
				authors = authors .. ", " .. displayText
			end
			i = i + 1
			if not (h.existsNotBlank(args["author" .. tostring(i)]) or h.existsNotBlank(args["last" .. tostring(i)])) then
				finished = true
			end
		end
	end
	
	local editors = args["editors"] or ""
	if editors == "" then
		if h.existsNotBlank(args["editor"]) or h.existsNotBlank(args["editor1"]) then
			editors = args["editor"] or args["editor1"]
		elseif h.existsNotBlank(args["editor-last"]) or h.existsNotBlank(args["editor-last1"]) then
			if h.existsNotBlank(args["editor-first"]) or h.existsNotBlank(args["editor-first1"]) then
				editors = editors .. (args["editor-first"] or args["editor-first1"]) .. " "
			end
			editors = editors .. (args["editor-last"] or args["editor-last1"])
		end
		if h.existsNotBlank(args["editor-link"]) or h.existsNotBlank(args["editor-link1"]) then
			editors = "[[" .. (args["editor-link"] or args["editor-link1"]) .. "|" .. editors .. "]]"
		end
		
		local finished = false
		local i = 2
		if not (h.existsNotBlank(args["editor" .. tostring(i)]) or h.existsNotBlank(args["last" .. tostring(i)])) then
			finished = true
		end
		while not finished do
			local displayText = ""
			if h.existsNotBlank(args["editor" .. tostring(i)]) then
				displayText = displayText .. args["editor" .. tostring(i)]
			elseif h.existsNotBlank(args["editor-last" .. tostring(i)]) then
				if h.existsNotBlank(args["editor-first" .. tostring(i)]) then
					displayText = displayText .. args["editor-first" .. tostring(i)] .. " "
				end
				displayText = displayText .. args["editor-last" .. tostring(i)]
			end
			if h.existsNotBlank(args["editor-link" .. tostring(i)]) then
				editors = editors .. ", [[" .. args["editor-first" .. tostring(i)] .. "|" .. displayText .. "]]"
			else
				editors = editors .. ", " .. displayText
			end
			i = i + 1
			if not (h.existsNotBlank(args["editor" .. tostring(i)]) or h.existsNotBlank(args["editor-last" .. tostring(i)])) then
				finished = true
			end
		end
	end
	
	local workAuthors = args["work-authors"] or ""
	if workAuthors == "" then
		if h.existsNotBlank(args["work-author"]) or h.existsNotBlank(args["work-author1"]) then
			workAuthors = args["work-author"] or args["work-author1"]
		elseif h.existsNotBlank(args["work-author-last"]) or h.existsNotBlank(args["work-author-last1"]) then
			if h.existsNotBlank(args["work-author-first"]) or h.existsNotBlank(args["work-author-first1"]) then
				workAuthors = workAuthors .. (args["work-author-first"] or args["work-author-first1"]) .. " "
			end
			workAuthors = workAuthors .. (args["work-author-last"] or args["work-author-last1"])
		end
		if h.existsNotBlank(args["work-author-link"]) or h.existsNotBlank(args["work-author-link1"]) then
			workAuthors = "[[" .. (args["work-author-link"] or args["work-author-link1"]) .. "|" .. workAuthors .. "]]"
		end
		
		local finished = false
		local i = 2
		if not (h.existsNotBlank(args["work-author" .. tostring(i)]) or h.existsNotBlank(args["work-author-last" .. tostring(i)])) then
				finished = true
		end
		while not finished do
			local displayText = ""
			if h.existsNotBlank(args["work-author" .. tostring(i)]) then
				displayText = displayText .. args["work-author" .. tostring(i)]
			elseif h.existsNotBlank(args["work-author-last" .. tostring(i)]) then
				if h.existsNotBlank(args["work-author-first" .. tostring(i)]) then
					displayText = displayText .. args["work-author-first" .. tostring(i)] .. " "
				end
				displayText = displayText .. args["work-author-last" .. tostring(i)]
			end
			if h.existsNotBlank(args["work-author-link" .. tostring(i)]) then
				workAuthors = workAuthors .. ", [[" .. args["work-author-first" .. tostring(i)] .. "|" .. displayText .. "]]"
			else
				workAuthors = workAuthors .. ", " .. displayText
			end
			i = i + 1
			if not (h.existsNotBlank(args["work-author" .. tostring(i)]) or h.existsNotBlank(args["work-author-last" .. tostring(i)])) then
				finished = true
			end
		end
	end
	
	local workDetails = args["work-details"] or ""
	if workDetails == "" then
		if h.existsNotBlank(args["volume"]) then
			workDetails = ", <span title=\"volume\">vol.</span> " .. args["volume"]
		end
		if h.existsNotBlank(args["issue"]) then
			workDetails = 	workDetails .. ", issue " .. args["issue"]
		end
		if h.existsNotBlank(args["edition"]) then
			workDetails = 	workDetails .. " (" .. args["edition"] .. "  <span title=\"edition\">ed.</span>)"
		end
	end
	
	local title = args["title"] or "<span class=\"cite-error\">Error: title required</span>"
	local work = args["work"] or args["part of"] or args["periodical"] or args["magazine"] or args["newspaper"] or ""
	local date = args["date"] or "undated"
	local publisher = args["publisher"] or ""
	local via = args["via"] or args["website-name"] or ""
	local publicationDate = args["publication-date"] or ""
	local url = args["url"] or ""
	local login = args["login"] or ""
	local accessDate = args["access-date"] or ""
	local archiveUrl = args["archive-url"] or ""
	local archiveDate = args["archive-date"] or "<span class=\"cite-error\">Error: please provide an archive-date alongside the archive-url</span>"
	local archiveCentre = args["archive-centre"] or ""
	local archiveCollection = args["archive-collection"] or ""
	local archiveRef = args["archive-ref"] or ""
	local archiveRefUrl = args["archive-ref-url"] or ""
	local isbn = args["isbn"] or ""
	local pages = args["page"] or args["pages"] or ""
		
	local titleItal = "''"
	if args["title-ital"] == "no" or ((work ~= "" or via ~= "") and args["title-ital"] ~= "yes") then
		titleItal = ""
	end
	local titleQuotes = ""
	if args["title-quotes"] == "yes" or ((work ~= "" or via ~= "") and args["title-quotes"] ~= "no") then
		titleQuotes = "\""
	end
	
	local workItal = "''"
	if args["work-ital"] == "no" then
		workItal = ""
	end
	local workQuotes = ""
	if args["work-quotes"] == "yes" then
		workQuotes = "\""
	end
	
	if publisher == "" and publicationDate ~= "" then
		date = date .. ", published " .. publicationDate
	end
	
	local titleLink = false
	if string.find(title, "%[%[.+%]%]") ~= nil then
		titleLink = true
	end
	
	if url ~= "" and not titleLink then
		if archiveUrl ~= "" then
			title = "[" .. archiveUrl .. " " .. title .. "]"
		else
			title = "[" .. url .. " " .. title .. "]"
		end
	end
	
	local citation = titleQuotes .. titleItal .. title .. titleItal .. titleQuotes
	if url ~= "" then
		if titleLink then
			if archiveUrl ~= "" then
				citation = citation .. " ([" .. archiveUrl .. " link]"
			else
				citation = citation .. " ([" .. url .. " link]"
			end
			if login ~= "" then
				citation = citation .. " ," .. login .. " login required)"
			else
				citation = citation .. ")"
			end
		elseif login ~= "" then
			citation = citation .. " (" .. login .. " login required)"
		end
	end
	if work == "" then
		citation = citation .. workDetails
	end
	if authors ~= "" then
		citation = citation .. " by " .. authors
		if date ~= "" then
			citation = citation .. " (" .. date .. ")"
		end
		if work == "" and editors ~= "" then
			citation = citation .. ", edited by " .. editors
		end
	elseif work == "" and editors ~= "" then
		citation = citation .. ", edited by " .. editors
		if date ~= "" then
			citation = citation .. " (" .. date .. ")"
		end
	elseif date ~= "" then
		citation = citation .. " (" .. date .. ")"
	end
	if publisher == "" and via ~= "" then
		citation = citation .. " via " .. via
	end
	if work ~= "" then
		citation = citation .. ", part of " .. workQuotes .. workItal .. work .. workItal .. workQuotes .. workDetails
		if workAuthors ~= "" then
			citation = citation .. " by " .. workAuthors
		end
		if editors ~= "" then
			citation = citation .. ", edited by " .. editors
		end
		if pages ~= "" then
			if string.find(pages, "-") or string.find(pages, ",") or string.find(pages, "&") or string.find(pages, "and") then
				citation = citation .. ", pages "
			else
				citation = citation .. ", page "
			end
			citation = citation .. pages
		end
	end
	if publisher ~= "" then
		citation = citation .. ", published by " .. publisher
		if via ~= "" then
			citation = citation .. " via " .. via
		end
		if publicationDate ~= "" then
			citation = citation .. " (" .. publicationDate .. ")"
		end
	end
	if archiveUrl ~= "" then
		citation = citation .. ". Archived from [" .. url .. " the original] on " .. archiveDate
	end
	if accessDate ~= "" then
		citation = citation .. ". Accessed on " .. accessDate
	end
	if archiveCentre ~= "" or archiveCollection ~= "" then
		citation = citation .. ". Available "
		if archiveCollection ~= "" then
			citation = citation .. "in the " .. archiveCollection
			if archiveCentre ~= "" then
				citation = citation .. ", " .. archiveCentre
			end
		elseif archiveCentre ~= "" then
			citation = citation .. "at the " .. archiveCentre
		end
		if archiveRef ~= "" then
			citation = citation .. ", "
			if archiveRefUrl ~= "" then
				citation = citation .. "[" .. archiveRefUrl .. " "
			end
			citation = citation .. archiveRef
			if archiveRefUrl ~= "" then
				citation = citation .. "]"
			end
		end
	end
	if isbn ~= "" then
		citation = citation .. ", [[Special:BookSources/" .. isbn .. "|ISBN " .. isbn .. "]]"
	end
	if work == "" and pages ~= "" then
		if string.find(pages, "-") or string.find(pages, ",") or string.find(pages, "&") or string.find(pages, "and") then
			citation = citation .. ". Pages "
		else
			citation = citation .. ". Page "
		end
		citation = citation .. pages
	end
	
	citation = citation .. "."
	
	return citation
end

return p