Siirry sisältöön

Moduuli:ISBN-lista

Wikipedia

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:ISBN-lista/ohje

-- This module implements {{ISBN-lista}}

local p = {}

-- check for separator or isbn character
local function isvalidisbnchar( ch )
	-- dash is allowed in isbn
	if (ch == '-') then
		return true
	end

	-- space is allowed in isbn but old cases have used it as separator
	-- -> disallow until those are changed to proper separator?
	--if (ch == ' ') then
	--	return true
	--end
	
	-- only upper-case X is allowed in isbn, but x has been used as well
	if (ch == 'X' or ch == 'x') then
		return true
	end

	-- all numbers are allowed
	if (tonumber(ch) ~= nil) then
		return true
	end

	-- otherwise, not allowed in isbn
	return false
end


-- jos vain yhdessä parametrissa kaikki perättäin, parsi luetteloksi.
-- erotinmerkkejä ainakin pilkku, <br>
--
-- huom: ohita "ISBN" merkkijonot jos on (ei välttämättä ole, joissakin voi olla)
--
-- huom2: vanha ei osaa käsitellä jos isbn-tunnisteessa on välilyöntejä viivojen sijaan,
-- mutta isbn-standardin mukaan välilyöntikin on sallittu isbn:ssä -> tunnisteiden erottelu?
--
local function splitarg( par )
	local ret = {}
	
	local plen = par:len()
	local pos = 1

	local start	= nil
	while (pos <= plen ) do
		local ch = par:sub(pos, pos)
		-- find first number
		if ( tonumber(ch) ~= nil and start == nil) then
			start = pos
			-- skip ahead?
		elseif (start ~= nil and isvalidisbnchar(ch) == false) then
			-- not a part of isbn -> end
			ret[#ret+1] = mw.text.trim(par:sub( start, pos-1 ))
			start = nil -- reset
			
			-- if there is reference -> locate end
			--if (par:match("<ref>")) then
			--par:find("</ref>")
			--end
		end
		pos = pos +1 
	end
	if (start ~= nil) then
		ret[#ret+1] = mw.text.trim(par:sub( start, plen ))
	end
	
	return ret
end

-- lista, jossa kaikki eroteltu |a|b|c...
-- tulosteena lista
local function buildList( args )
    -- Get the list items.
    local listItems = args
    if #listItems == 0 then
        return ''
    end
    -- jos pelkkä viiva ei tehdä mitään
    if (args[1] == "-") then
        return ''
    end
    -- jos vain yksi parametri, pilkottava ensimmäisestä
    if #listItems == 1 then
    	listItems = splitarg(args[1])
    	-- ustring-versio on hidas, %p katkaisee myös väliviivoista
		--listItems = mw.text.split(args[1], '%s')
    end

	-- muotoillut linkit hakutoimintoon
	local wiki = {}	
	local function insert( value )
		wiki[#wiki+1] = value
	end

	-- tarkistusnumerot
	local Isxn = require('Moduuli:ISxN')
	
	local errorsfound = false
	local k = 1
	while (k <= #listItems) do
		local formattedisbn;
		local isbnid = listItems[k]
		
		-- skip empty
		if (mw.ustring.len( isbnid ) > 1) then
			if Isxn._check_isbn(isbnid) then
				-- pelkkä tunniste (ei alkuosaa)
				formattedisbn = '[[Toiminnot:Kirjalähteet/' .. isbnid .. '|' .. isbnid ..']]'
			else
				errorsfound = true
				formattedisbn = "<span class='error'>Virhe: ".. isbnid .."</span>"
			end
			
			-- pilkku edellisen jälkeen
			if (k > 1) then
				insert(', ')
			end
			insert(formattedisbn)
		end

		k = k+1
	end

	-- append error category
	if (errorsfound == true) then
		local errorcat = "[[Luokka:Sivut, joissa on virheellinen ISBN-tunniste]]"
		insert(errorcat)
	end

	return table.concat( wiki )
end

function p.main( frame )
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( k ) == 'number' or v ~= '' then
            args[ k ] = v
        end
    end
    return buildList( args )
end

return p