Module:LCCN

local p = {}

function blank_to_nil(s) if s and #s &#x3e; 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 &#x3c; 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 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 ("LCCN&#x26;nbsp;[%s %s-%s]%s"):format(		url, left_part, right_part,		title and ('&#x26;nbsp;&#x26;ndash;&#x26;nbsp;' .. title) or '') end

return p