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:32, 18 November 2021 (sensible variable names, read each arg once only). 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
	local id = args[1] or args.id or ''
	local title = blank_to_nil(args[2] or args.title or args.name)
	
	if args.long == "yes" then
		return "Catalog record for [https://lccn.loc.gov/" ..
			(blank_to_nil(id) or ("Name?" .. mw.title.getCurrentTitle().partialUrl())) ..
			" " .. (title or mw.title.getCurrentTitle().text) ..
			"] at the United States [[LCCN (identifier)|Library of Congress]]"
	end
	local letter_width = (id:match("^%s*[0-9][0-9]") and id:len() < 10) and 0 or 2
	
	local left_part = id:sub(1, letter_width + 2)
	local right_part = expr(padright(id, letter_width + 8, '.00000'):sub(letter_width + 3, letter_width + 8))
	
	return ("[[LCCN (identifier)|LCCN]]&nbsp;[https://lccn.loc.gov/%s%s %s-%s]"):format(
		left_part, padleft(right_part, 6, 0), left_part, right_part,
		title and ('&nbsp;&ndash;&nbsp;' .. title) or '')
end

return p