Module:Citation/CS1/Identifiers: Difference between revisions

Synch from sandbox;
m>Trappist the monk
m (oclc to 10 digit max;)
m>Trappist the monk
(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 return false; end -- fail if isbn_str contains anything but digits, hyphens, or the uppercase X
if nil ~= isbn_str:match("[^%s-0-9X]") then
isbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" ); -- remove hyphens and spaces
return false, 'invalid character'; -- fail if isbn_str contains anything but digits, hyphens, or the uppercase X
end
isbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" ); -- remove hyphens and spaces
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 return false; end
if isbn_str:match( "^%d*X?$" ) == nil then -- fail if isbn_str has 'X' anywhere but last position
return is_valid_isxn(isbn_str, 10);
return false, 'invalid form';
end
return is_valid_isxn(isbn_str, 10), 'checksum';
else
else
local temp = 0;
if isbn_str:match( "^%d+$" ) == nil then
if isbn_str:match( "^97[89]%d*$" ) == nil then return false; end -- isbn13 begins with 978 or 979; ismn begins with 979
return false, 'invalid character'; -- fail if isbn13 is not all digits
return is_valid_isxn_13 (isbn_str);
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
return is_valid_isxn_13 (isbn_str), 'checksum';
end
end
end
end
Line 723: Line 732:
elseif k == 'ISBN' then
elseif k == 'ISBN' then
local ISBN = internal_link_id( handler );
local ISBN = internal_link_id( handler );
if not check_isbn( v ) and not is_set(options.IgnoreISBN) then
local check;
ISBN = ISBN .. set_error( 'bad_isbn', {}, false, " ", "" );
local err_type = '';
-- if not check_isbn( v ) and not is_set(options.IgnoreISBN) then
-- ISBN = ISBN .. set_error( 'bad_isbn', {}, false, " ", "" );
-- 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 } );
Anonymous user