Module:LCCN

From imedwiki
Jump to navigation Jump to search

Usage

This module implements Template:LCCN and is not suitable for direct use.

This module (and the documentation) are based (partially) on Module:LCCN from the free encyclopedia wikipedia and is licensed under GNU license for free documentation and the Creative Commons Attribution/Share Alike. On wikipedia there is a List of authors accessible. More about importing from wikipedia on page Imedwiki:Importing from wikipedia.

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:getParent().args
	local id = blank_to_nil(args[1] or args.id)
	local title = blank_to_nil(args[2] or args.title or args.name)
	local left_part, right_part, url
	if id then
		local letter_width = (id:match("^%s*[0-9][0-9]") and id:len() < 10) and 0 or 2
	
		left_part = id:sub(1, letter_width + 2)
		right_part = expr(padright(id, letter_width + 8, '.00000'):sub(letter_width + 3, letter_width + 8))
		url = "https://lccn.loc.gov/" .. left_part .. padleft(right_part, 6, 0)
	else
		url = "https://lccn.loc.gov/Name?" .. mw.title.getCurrentTitle():partialUrl()
	end
		
	if args.long == "yes" then
		return ("Catalog record for [%s %s] at the United States [[Wikipedia:LCCN (identifier)|Library of Congress]]"):format(
			url, title or mw.title.getCurrentTitle().text)
	end
	
	if not left_part then
		error("Template:LCCN: You must provide an ID.")
	end
	
	return ("[[Wikipedia:LCCN (identifier)|LCCN]]&nbsp;[%s %s-%s]%s"):format(
		url, left_part, right_part,
		title and ('&nbsp;&ndash;&nbsp;' .. title) or '')
end

return p