Module:LCCN
Jump to navigation
Jump to search
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
Usage
This module implements Template:LCCN and is not suitable for direct use.
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 [[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 ("[[LCCN (identifier)|LCCN]] [%s %s-%s]%s"):format( url, left_part, right_part, title and (' – ' .. title) or '') end return p