Module:Cite web

-- from Wookipedia:Module:Cite web -- This module implements Template:Cite_web.

local p = {}

-- Lazily load a mw.language object. local lang

-- NS_MAIN holds the namespace number for the Main namespace. local NS_MAIN = 0 -- NS_FILE holds the namespace number for the File namespace. local NS_FILE = 6 -- NS_USER holds the namespace number for the User namespace. TESTING ONLY --local NS_USER = 2 -- NS_MODULE holds the namespace number for the Module namespace. TESTING ONLY --local NS_MODULE = 828

-- get the current page local currentPageTitle = mw.title.getCurrentTitle -- get the current page's namespace local namespace = currentPageTitle.namespace -- Avoid rendering tracking categories outside of main or file namespaces local isAllowedNamespace = (namespace == NS_MAIN or namespace == NS_FILE) and true

-- Formats all error messages and adds appropriate tracking category local function makeWikitextError(msg) -- styling for the error message local ret = ' Template:Cite_web error: '..msg..'. '	-- add appropriate tracking category if isAllowedNamespace then ret = ret..'' end return ret end

-- Formats all dates including wookieepedia linking local function makeDateLink(date) lang = lang or mw.language.getContentLanguage local success, dateLink = pcall(		lang.formatDate, lang,		'F j, Y',		date	) return dateLink end

function p._main(args) -- Capture variables local url = args.url local title = args.title -- Validate input if not url then -- missing url parameter return makeWikitextError("missing url parameter must be specified") end if not title then -- missing title parameter return makeWikitextError("missing title parameter must be specified") end -- Capture remaing variables local author = args.author local citeDate = args.date local citeFormat = args.format local work = args.work local publisher = args.publisher local pages = args.pages local language = args.language local quote = args.quote local archiveDate = args.archivedate local archiveUrl = args.archiveurl local archiveFile = args.archivefile local noBackup = args.nobackup local noLive = args.nolive local noArchive = args.noarchive local ret = {} -- URL -- check for any badly formatted urls if not mw.ustring.find(url, 'http://') and not mw.ustring.find(url, 'https://') then ret[#ret + 1] = makeWikitextError("badly formatted url parameter") if isAllowedNamespace then ret[#ret + 1] = '' return table.concat(ret) end end -- build the left-most url/archive links if url and title then if archiveUrl then -- use archiveUrl ret[#ret + 1] = '['..archiveUrl..' '..title..']' elseif archiveDate then -- use archiveDate to build a Wayback url ret[#ret + 1] = ..title.. elseif url then -- if no archive links, then just use the url ret[#ret + 1] = '['..url..' '..title..']' end end

-- Language if language then -- ensure language value is using title case e.g. upper case first letter language = string.gsub(" "..language, "%W%l", string.upper):sub(2) ret[#ret + 1] = '&#32;('..language..')' end

-- Format or Pages if citeFormat or pages then -- opening (		ret[#ret + 1] = '&#32;(' -- Format if citeFormat then ret[#ret + 1] = citeFormat end -- add a space if both if citeFormat and pages then ret[#ret + 1] = '&#32;' end -- Pages if pages then ret[#ret + 1] = pages end -- closing )		ret[#ret + 1] = ')' end -- Author if author then ret[#ret + 1] = '&#32;by '..author end

-- Publisher if publisher then ret[#ret + 1] = ',&#32;published by '..publisher end -- Work if work then ret[#ret + 1] = "&#32;on "..work else -- if work parameter hasn't been provided, we can create a link to the website's homepage automatically -- grab the website's homepage - including http(s):// local website = url:match('^(%w+://[^/]+)') -- grab the website's domain name which we'll use for the website title local domain = url:match('^%w+://([^/]+)') -- create a link to the website's homepage ret[#ret + 1] = "&#32;on ["..website..' '..domain.."]" end -- Date if citeDate then -- add a formatted data ret[#ret + 1] = '&#32;('..makeDateLink(citeDate)..')' -- check if date contains letters -- we only want dates formatted as YYYY-MM-DD e.g. 2020-11-23)		if mw.ustring.find(citeDate, 'a') or mw.ustring.find(citeDate, 'e') or mw.ustring.find(citeDate, 'u') then   		-- any dates containing the above letters, e.g. Jan 12, 2019    		-- add the appropriate tracking category    		if isAllowedNamespace then				ret[#ret + 1] = ''			end        end	end	-- Quote	if quote then		ret[#ret + 1] = '&#58;&#32;"'..quote..'"'	end

-- Start of the right-most url/archive links ret[#ret + 1] = '&#32; ('

if noBackup then -- noBackup i.e. when an archive link cannot be produced because the original content is no longer available ret[#ret + 1] = 'content obsolete and backup link not available' else if noLive then -- original content is no longer available (but archive link may exist) ret[#ret + 1] = 'content now obsolete;&#32;' elseif noArchive then -- original content still live and no archive yet ret[#ret + 1] = 'content has no archive;&#32;' end if archiveFile then ret[#ret + 1] =	'screenshot' elseif archiveUrl or archiveDate then -- add url link to output ret[#ret + 1] =	'archived from ['..url..' the original]' else -- format unverifie backup link message local verifyBackupLink = ' \'\'\'not verified!\'\'\' ' -- create a temporary Wayback link from the url ret[#ret + 1] = 'backup link&#32;'..verifyBackupLink -- add a tracking category for the missing archival links if isAllowedNamespace then ret[#ret + 1] = '' end end -- Full Wayback URL given if archiveUrl and mw.ustring.find(archiveUrl, 'web.archive.org/web') then -- so return it with appropriate tracking categories if isAllowedNamespace then ret[#ret + 1] = '' end -- grab the date value from the Wayback url local waybackDate = archiveUrl:match('https://web.archive.org/web/(%d+)/http') -- add Wayback date to output ret[#ret + 1] =	'&#32;on '..makeDateLink(waybackDate) elseif archiveDate then -- add archiveDate to output ret[#ret + 1] =	'&#32;on '..makeDateLink(archiveDate) end end -- End of the right-most url/archive links ret[#ret + 1] = ') '	-- final output	return table.concat(ret) end

-- this has slightly slower performance than the next function --local getArgs = require('Module:Arguments').getArgs --function p.main(frame) --	local args = getArgs(frame) --	return p._main(args) --end

function p.main(frame) local args = {} for k, v in pairs(frame:getParent.args) do		v = v:match('^%s*(.-)%s*$') -- trim whitespace if v ~= '' then args[k] = v		end end return p._main(args) end

return p