Modul:Check isxn

Documentation icon Moduldokumentation
Utilstrækkelig vejledning
Dette modul bør have en (bedre) vejledning, helst med eksempler på anvendelse.
Hvis andre moduler er nyere og/eller bedre, bør der henvises til dem.

Brug

{{#invoke:Check isxn|function_name}}

-- This template is a copy of the ISXN validation code from [[Module:Citation/CS1]]-- which allows for validating ISBN, ISMN, and ISSN without invoking a citation templatelocal p = {}--[[--------------------------< IS _ V A L I D _ I S X N >-----------------------------------------------------ISBN-10 and ISSN validator code calculates checksum across all isbn/issn digits including the check digit. ISBN-13 is checked in check_isbn().If the number is valid the result will be 0. Before calling this function, issbn/issn must be checked for length and stripped of dashes,spaces and other non-isxn characters.]]local function is_valid_isxn (isxn_str, len)local temp = 0;isxn_str = { isxn_str:byte(1, len) };-- make a table of byte values '0' → 0x30 .. '9'  → 0x39, 'X' → 0x58len = len+1;-- adjust to be a loop counterfor i, v in ipairs( isxn_str ) do-- loop through all of the bytes and calculate the checksumif v == string.byte( "X" ) then-- if checkdigit is X (compares the byte value of 'X' which is 0x58)temp = temp + 10*( len - i );-- it represents 10 decimalelsetemp = temp + tonumber( string.char(v) )*(len-i);endendreturn temp % 11 == 0;-- returns true if calculation result is zeroend--[[--------------------------< IS _ V A L I D _ I S X N  _ 1 3 >----------------------------------------------ISBN-13 and ISMN validator code calculates checksum across all 13 isbn/ismn digits including the check digit.If the number is valid, the result will be 0. Before calling this function, isbn-13/ismn must be checked for lengthand stripped of dashes, spaces and other non-isxn-13 characters.]]local function is_valid_isxn_13 (isxn_str)local temp=0;isxn_str = { isxn_str:byte(1, 13) };-- make a table of byte values '0' → 0x30 .. '9'  → 0x39for i, v in ipairs( isxn_str ) dotemp = temp + (3 - 2*(i % 2)) * tonumber( string.char(v) );-- multiply odd index digits by 1, even index digits by 3 and sum; includes check digitendreturn temp % 10 == 0;-- sum modulo 10 is zero when isbn-13/ismn is correctend--[[--------------------------< C H E C K _ I S B N >------------------------------------------------------------Determines whether an ISBN string is valid]]local function check_isbn( isbn_str, error_string )if nil ~= isbn_str:match("[^%s-0-9X]") then -- fail if isbn_str contains anything but digits, hyphens, or the uppercase Xreturn error_string;endisbn_str = isbn_str:gsub( "-", "" ):gsub( " ", "" );-- remove hyphens and spaceslocal len = isbn_str:len(); if len ~= 10 and len ~= 13 thenreturn error_string;endif len == 10 thenif isbn_str:match( "^%d*X?$" ) == nil then return error_string; endreturn is_valid_isxn(isbn_str, 10) and '' or error_string;elselocal temp = 0;if isbn_str:match( "^97[89]%d*$" ) == nil then -- isbn13 begins with 978 or 979; ismn begins with 979return error_string; endreturn is_valid_isxn_13 (isbn_str) and '' or error_string;endend--[[--------------------------< C H E C K _ I S M N >------------------------------------------------------------Determines whether an ISMN string is valid.  Similar to isbn-13, ismn is 13 digits begining 979-0-... and uses thesame check digit calculations.  See http://www.ismn-international.org/download/Web_ISMN_Users_Manual_2008-6.pdfsection 2, pages 9–12.]]local function check_ismn (id, error_string)local text;local valid_ismn = true;id=id:gsub( "[%s-–]", "" );-- strip spaces, hyphens, and endashes from the ismnif 13 ~= id:len() or id:match( "^9790%d*$" ) == nil then-- ismn must be 13 digits and begin 9790valid_ismn = false;elsevalid_ismn=is_valid_isxn_13 (id);-- validate ismnendreturn valid_ismn and '' or error_stringend--[[--------------------------< I S S N >----------------------------------------------------------------------Validate and format an issn.  This code fixes the case where an editor has included an ISSN in the citation but has separated the two groups of fourdigits with a space.  When that condition occurred, the resulting link looked like this:|issn=0819 4327 gives: [http://www.worldcat.org/issn/0819 4327 0819 4327]  -- can't have spaces in an external linkThis code now prevents that by inserting a hyphen at the issn midpoint.  It also validates the issn for length and makes sure that the checkdigit agreeswith the calculated value.  Incorrect length (8 digits), characters other than 0-9 and X, or checkdigit / calculated value mismatch will all cause a check issnerror message.]]local function check_issn(id, error_string)local issn_copy = id;-- save a copy of unadulterated issn; use this version for display if issn does not validatelocal text;local valid_issn = true;if not id:match ('^%d%d%d%d%-%d%d%d[%dX]$') thenreturn error_string;endid=id:gsub( "[%s-–]", "" );-- strip spaces, hyphens, and endashes from the issnif 8 ~= id:len() or nil == id:match( "^%d*X?$" ) then-- validate the issn: 8 digits long, containing only 0-9 or X in the last positionvalid_issn=false;-- wrong length or improper characterelsevalid_issn=is_valid_isxn(id, 8);-- validate issnendreturn valid_issn and '' or error_stringend------------------------------< E N T R Y   P O I N T S >--------------------------------------------------====function p.check_isbn(frame)return check_isbn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')endfunction p.check_ismn(frame)return check_ismn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')endfunction p.check_issn(frame)return check_issn(frame.args[1] or frame:getParent().args[1], frame.args['error'] or frame:getParent().args['error'] or 'error')endreturn p