Module:Citation/CS1/Identifiers: Difference between revisions
Content added Content deleted
m (1 revision imported: Math) |
(Synch from sandbox;) |
||
Line 1: | Line 1: | ||
local identifiers = {}; |
local identifiers = {}; |
||
Line 97: | Line 98: | ||
local function check_isbn( isbn_str ) |
local function check_isbn( isbn_str ) |
||
if nil ~= isbn_str:match("[^%s-0-9X]") then |
if nil ~= isbn_str:match("[^%s-0-9X]") then |
||
return false, 'invalid character'; -- fail if isbn_str contains anything but digits, hyphens, or the uppercase X |
|||
⚫ | |||
end |
|||
⚫ | |||
local len = isbn_str:len(); |
local len = isbn_str:len(); |
||
if len ~= 10 and len ~= 13 then |
if len ~= 10 and len ~= 13 then |
||
return false; |
return false, 'length'; -- fail if incorrect length |
||
end |
end |
||
if len == 10 then |
if len == 10 then |
||
if isbn_str:match( "^%d*X?$" ) == nil then |
if isbn_str:match( "^%d*X?$" ) == nil then -- fail if isbn_str has 'X' anywhere but last position |
||
return |
return false, 'invalid form'; |
||
end |
|||
return is_valid_isxn(isbn_str, 10), 'checksum'; |
|||
else |
else |
||
if isbn_str:match( "^%d+$" ) == nil then |
|||
⚫ | |||
return false, 'invalid character'; -- fail if isbn13 is not all digits |
|||
if isbn_str:match( "^97[89]%d*$" ) == nil then return false; end -- isbn13 begins with 978 or 979; ismn begins with 979 |
|||
end |
|||
⚫ | |||
if isbn_str:match( "^97[89]%d*$" ) == nil then |
|||
return false, 'invalid prefix'; -- fail when isbn13 does not begin with 978 or 979 |
|||
end |
|||
⚫ | |||
end |
end |
||
end |
end |
||
Line 668: | Line 677: | ||
prefix=handler.prefix, id=id, separator=handler.separator, encode=handler.encode}) .. err_msg; |
prefix=handler.prefix, id=id, separator=handler.separator, encode=handler.encode}) .. err_msg; |
||
return text; |
|||
end |
|||
--[[--------------------------< B I B C O D E >-------------------------------------------------------------------- |
|||
Validates (sort of) and formats a bibcode id. |
|||
Format for bibcodes is specified here: http://adsabs.harvard.edu/abs_doc/help_pages/data.html#bibcodes |
|||
But, this: 2015arXiv151206696F is apparently valid so apparently, the only things that really matter are length, 19 characters |
|||
and first four digits must be a year. This function makes these tests: |
|||
length must be 19 characters |
|||
characters in position |
|||
1–4 must be digits and must represent a year in the range of 1000 – next year |
|||
5 must be a letter |
|||
6–8 must be letter, ampersand, or dot (ampersand cannot directly precede a dot; &. |
|||
9 must be letter or dot |
|||
10–18 must be letter, digit, or dot |
|||
19 must be a letter or dot |
|||
]] |
|||
local function bibcode (id) |
|||
local handler = cfg.id_handlers['BIBCODE']; |
|||
local err_type; |
|||
local year; |
|||
local text = external_link_id({link=handler.link, label=handler.label, |
|||
prefix=handler.prefix, id=id, separator=handler.separator, encode=handler.encode}); |
|||
if 19 ~= id:len() then |
|||
err_type = 'length'; |
|||
else |
|||
year = id:match ("^(%d%d%d%d)[%a][%a&%.][%a&%.][%a&%.][%a%.][%a%d%.]+[%a%.]$") -- |
|||
if not year then -- if nil then no pattern match |
|||
err_type = 'value'; -- so value error |
|||
else |
|||
local next_year = tonumber(os.date ('%Y'))+1; -- get the current year as a number and add one for next year |
|||
year = tonumber (year); -- convert year portion of bibcode to a number |
|||
if (1000 > year) or (year > next_year) then |
|||
err_type = 'year'; -- year out of bounds |
|||
end |
|||
if id:find('&%.') then |
|||
err_type = 'journal'; -- journal abbreviation must not have '&.' (if it does its missing a letter) |
|||
end |
|||
end |
|||
end |
|||
if is_set (err_type) then -- if there was an error detected |
|||
text = text .. ' ' .. set_error( 'bad_bibcode', {err_type}); |
|||
end |
|||
return text; |
return text; |
||
end |
end |
||
Line 690: | Line 751: | ||
-- fallback to read-only cfg |
-- fallback to read-only cfg |
||
handler = setmetatable( { ['id'] = v }, fallback(k) ); |
handler = setmetatable( { ['id'] = v }, fallback(k) ); |
||
if handler.mode == 'external' then |
if handler.mode == 'external' then |
||
table.insert( new_list, {handler.label, external_link_id( handler ) } ); |
table.insert( new_list, {handler.label, external_link_id( handler ) } ); |
||
Line 697: | Line 758: | ||
elseif handler.mode ~= 'manual' then |
elseif handler.mode ~= 'manual' then |
||
error( cfg.messages['unknown_ID_mode'] ); |
error( cfg.messages['unknown_ID_mode'] ); |
||
elseif k == 'BIBCODE' then |
|||
table.insert( new_list, {handler.label, bibcode( v ) } ); |
|||
elseif k == 'DOI' then |
elseif k == 'DOI' then |
||
table.insert( new_list, {handler.label, doi( v, options.DoiBroken ) } ); |
table.insert( new_list, {handler.label, doi( v, options.DoiBroken ) } ); |
||
Line 723: | Line 786: | ||
elseif k == 'ISBN' then |
elseif k == 'ISBN' then |
||
local ISBN = internal_link_id( handler ); |
local ISBN = internal_link_id( handler ); |
||
local check; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- end |
|||
check, err_type = check_isbn( v ); |
|||
if not check then |
|||
if is_set(options.IgnoreISBN) then -- ISBN is invalid; if |ignore-isbn-error= set |
|||
add_maint_cat ('ignore_isbn_err'); -- ad a maint category |
|||
else |
|||
ISBN = ISBN .. set_error( 'bad_isbn', {err_type}, false, " ", "" ); -- else display an error message |
|||
end |
|||
elseif is_set(options.IgnoreISBN) then -- ISBN is OK; if |ignore-isbn-error= set |
|||
add_maint_cat ('ignore_isbn_err'); -- because |ignore-isbn-error= unnecessary |
|||
end |
end |
||
table.insert( new_list, {handler.label, ISBN } ); |
table.insert( new_list, {handler.label, ISBN } ); |