Jump to content

Module:Sandbox/GKFX

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by GKFX (talk | contribs) at 18:15, 18 November 2021 (Refactor to one function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

function blank_to_nil(s)
	if s and #s > 0 then return s end
end

function padleft(...)
	return mw.getCurrentFrame():callParserFunction('padleft', { ... })	
end

function padright(...)
	return mw.getCurrentFrame():callParserFunction('padright', { ... })	
end

function expr(...)
	return mw.getCurrentFrame():callParserFunction('#expr', { ... })
end

function p.main(frame)
	local args = frame.args
	if args.long == "yes" then
		return "Catalog record for [https://lccn.loc.gov/" ..
			(blank_to_nil(args[1] or args.id) or ("Name?" .. mw.title.getCurrentTitle().partialUrl())) ..
			" " .. (args[2] or args.title or args.name or mw.title.getCurrentTitle().text) ..
			"] at the United States [[LCCN (identifier)|Library of Congress]]"
	end
	local a1 = args[1] or args.id or ''
	local b1 = ((args[1] or ''):match("^%s*[0-9][0-9]") and (args[1] or ''):len() < 10) and 0 or 2
	local title = args[2] or args.title or args.name or ''
	
	local a2 = a1:sub(1, 2 + b1)
	local b2 = expr(padright(a1, b1 + 8, '.00000'):sub(b1 + 3, b1 + 8))
	
	return "[[LCCN (identifier)|LCCN]]&nbsp;[https://lccn.loc.gov/" ..
		a2 .. padleft(b2, 6, 0) .. ' ' .. a2 .. '-' .. b2 .. ']' ..
		(title:len() == 0 and '' or '&nbsp;&ndash;&nbsp;' .. title)
end

return p