Hoppa till innehållet

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 arg = args[ 1 ]
	if arg and arg ~= '' then
		local template = '{{ISBN|' .. arg .. '}}'
		local copy = arg
		copy = copy:gsub( '-', '' )
		copy = copy:gsub( ' ', '' )
		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 frame:preprocess( template .. '[[Kategori:Sidor med felaktigt ISBN]]' )
			end
		elseif length == 13 then
			local t = {}
			for i = 1, length do
				t[ i ] = copy:sub( i, i )
			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 frame:preprocess( template .. '[[Kategori:Sidor med felaktigt ISBN]]' )
			end
		else
			return frame:preprocess( template .. '[[Kategori:Sidor med felaktigt ISBN]]' )
		end
		return frame:preprocess( template )
	else
		return '<span class="error">[[Mall:ISBN]] anropad utan argument</span>[[Kategori:Sidor som anropar Mall:ISBN utan argument]]'
	end
end
return p