Modul:ISBN

Från Wikipedia

Dokumentation [visa] [redigera] [historik] [rensa sidcachen]


Den här modulen används av mallen {{ISBN}}.

p = {}
p.ISBN = function( frame )
	local pframe = frame:getParent()
	local args = pframe.args
	local arg1 = args[ 1 ]
	arg1 = arg1:gsub('x','X')                  -- change 'x' to 'X' in order to avoid search failure in Special:Bokkällor (or Lua error)
	local arg = string.gsub(arg1,"%(.*%)","")  -- remove possible parenthesis, e.g. "(inb.)", in the parameter value
	if arg and arg ~= '' then
		local link = '[[Special:Bokkällor/' .. arg1 .. '|ISBN ' .. arg1 .. ']]'
		local copy = arg
		copy = copy:gsub( '[^0-9X]', '')       -- remove everything that is not a digit or X in order to avoid Lua error) − 
		local length = copy:len()
		if length == 10 then
			local t = {}
			for i = 1, length do
				if copy:sub( i, i ) == 'X' then
					t[ i ] = 10
				else
					t[ i ] = copy:sub( i, i )
				end
			end
			local t2 = {}
			local multiplier = 10
			for i = 1, #t do
				t2[ i ] = t[ i ] * multiplier
				multiplier = multiplier -1
			end
			local sum = 0
			for i = 1, #t2 do
				sum = sum + t2[ i ]
			end
			local remainder = sum % 11
			if remainder ~= 0 then
				return link ..  ' ' .. frame:preprocess('{{error|(Felaktigt ISBN-10-nummer)}}') .. '[[Kategori:Sidor med felaktigt ISBN]]'
			end
		elseif length == 13 then
			local t = {}
			for i = 1, length do
				if copy:sub( i, i ) == 'X' then
					t[ i ] = 10                    -- just to avoid Lua error if someone gives a ISBN-13 containing a 'X'
				else
					t[ i ] = copy:sub( i, i )
				end
			end
			local t2 = {}
			for i = 1, #t do
				if i % 2 == 0 then
					t2[ i ] = t[ i ] * 3
				else
					t2[ i ] = t[ i ]
				end
			end
			local sum = 0
			for i = 1, #t2 do
				sum = sum + t2[ i ]
			end
			local remainder = sum % 10
			if remainder ~= 0 then
				return link .. ' ' .. frame:preprocess('{{error|(Felaktigt ISBN-13-nummer)}}') .. '[[Kategori:Sidor med felaktigt ISBN]]'
			end
		else
			return link .. ' ' .. frame:preprocess('{{error|(Felaktig längd på ISBN-nummer)}}') .. '[[Kategori:Sidor med felaktigt ISBN]]'
		end
		return link
	else
		return '<span class="error">[[Mall:ISBN]] anropad utan argument</span>[[Kategori:Sidor som anropar Mall:ISBN utan argument]]'
	end
end
return p