Module:LCCN: Difference between revisions
Appearance
Content deleted Content added
switch from "https://lccn.loc.gov/$1" to "https://www.loc.gov/item/$1" links and add tracking when no "id" provided for the strange case where "long=yes" resulting in "https://lccn.loc.gov/Name?$1" links at Special:WhatLinksHere/Module:LCCN/doc |
better focus tracking |
||
Line 53: | Line 53: | ||
local left_part, right_part, url |
local left_part, right_part, url |
||
if not id then |
if not id then |
||
⚫ | |||
url = "https://lccn.loc.gov/Name?" .. mw.title.getCurrentTitle():partialUrl() |
url = "https://lccn.loc.gov/Name?" .. mw.title.getCurrentTitle():partialUrl() |
||
else |
else |
||
Line 64: | Line 63: | ||
if args.long == "yes" then |
if args.long == "yes" then |
||
if not id then |
|||
⚫ | |||
end |
|||
return ("Catalog record for [%s %s] at the United States [[LCCN (identifier)|Library of Congress]]"):format( |
return ("Catalog record for [%s %s] at the United States [[LCCN (identifier)|Library of Congress]]"):format( |
||
url, title or mw.title.getCurrentTitle().text) |
url, title or mw.title.getCurrentTitle().text) |
Revision as of 03:45, 13 May 2024
![]() | 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. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
Usage
This module implements Template:LCCN and is not suitable for direct use.
require('strict')
local p = {}
local function blank_to_nil(s)
if s and #s > 0 then return s end
end
local function pad(padlen, ...)
padlen = tonumber(padlen) or 0
if 1 > select('#', ...) then
return ("0"):rep(padlen)
end
local padsrc = tostring((...))
local srclen = mw.ustring.len(padsrc)
if 1 > padlen or 1 > srclen then
return ""
end
return mw.ustring.sub(mw.ustring.rep(padsrc, math.ceil(padlen / srclen)), 1, padlen)
end
local function padleft(str, ...)
str = tostring(str)
if 1 > select('#', ...) then
return str
end
local strlen = mw.ustring.len(str)
if 1 > strlen then
return pad((...), select(2, ...))
end
return pad((tonumber((...)) or 0) - strlen, select(2, ...)) .. str
end
local function padright(str, ...)
str = tostring(str)
if 1 > select('#', ...) then
return str
end
local strlen = mw.ustring.len(str)
if 1 > strlen then
return pad((...), select(2, ...))
end
return str .. pad((tonumber((...)) or 0) - strlen , select(2, ...))
end
local function expr(...)
return mw.ext.ParserFunctions.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 not id then
url = "https://lccn.loc.gov/Name?" .. mw.title.getCurrentTitle():partialUrl()
else
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://www.loc.gov/item/" .. left_part .. padleft(right_part, 6, 0)
end
if args.long == "yes" then
if not id then
mw.title.new(frame:getTitle()):subPageTitle('doc'):getContent() --XXX: quick and dirty transclusion tracking
end
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