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:10, 18 November 2021 (Working copy of original templates). 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.core(a, b, title)
	return "[[LCCN (identifier)|LCCN]] [https://lccn.loc.gov/" ..
		a .. padleft(b, 6, 0) .. ' ' .. a .. '-' .. b .. ']' ..
		(title:len() == 0 and '' or ' – ' .. title)
end

function p.prepare(a, b, title)
	return p.core(
		a:sub(1, 2 + b),
		expr(padright(a, b + 8, '.00000'):sub(b + 3):sub(1, 6)),
		title)
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]]"
	else
		return p.prepare(
			args[1] or args.id or '',
			((args[1] or ''):match("^%s*[0-9][0-9]") and (args[1] or ''):len() < 10) and 0 or 2,
			args[2] or args.title or args.name or ''
		)
	end
end

return p