Dokumentaciju za ovaj modul možete napraviti na stranici Modul:Interwiki/dok

-- This is the interwiki library. It is not meant to be used directly in articles, but to be used by other Lua modules.

local interwikiTable = mw.loadData( 'Module:InterwikiTable' )
local libraryUtil = require( 'libraryUtil' )

local iw = {}

local function getProjectCode( prefix )
    for code, t in pairs( interwikiTable ) do
        for _, interwikiPrefix in ipairs( t.iw_prefix ) do
            if prefix == interwikiPrefix then
                return code
            end
        end
    end
    return nil
end

function iw.isKnownPrefix( prefix )
    return getProjectCode( prefix ) and true or false
end

function iw.isValidLanguageCode( code )
    return mw.language.isKnownLanguageTag( code )
end

function iw.new( s )
    local obj = {}
    local data = {}
    local checkSelf = libraryUtil.makeCheckSelfFunction( 'Module:Interwiki', 'interwiki', obj, 'interwiki object' )
    local projectCode, language
    
    -- Get the project code and the language from the input.
    s = mw.ustring.match( s, '^:*(.-):*$' ) or s -- Trim colons from the outside.
    s = mw.ustring.lower( s )
    local pref1, pref2 = mw.ustring.match( s, '^(%w+):(%w+)$' )
    if pref1 and pref2 then
        local pref1Lang = iw.isValidLanguageCode( pref1 )
        if pref1Lang then
            local pref2Code = getProjectCode( pref2 )
            if pref2Code then
                language = pref1
                projectCode = getProjectCode( pref2 )
            end
        else
            local pref2Lang = iw.isValidLanguageCode( pref2 )
            if pref2Lang then
                local pref1Code = getProjectCode( pref1 )
                if pref1Code then
                    language = pref2
                    projectCode = getProjectCode( pref1 )
                end            
            end
        end
    elseif iw.isValidLanguageCode( s ) then
        language = s
    elseif interwikiTable[ s ] then
        projectCode = s
    else
        projectCode = getProjectCode( s )
    end
    
    -- Get properties and methods from the interwiki table.
    local iwTable
    if projectCode then
        iwTable = interwikiTable[ projectCode ]
        data.domain = iwTable.domain
        data.titlePrefix = iwTable.title_prefix
        data.takesLanguagePrefix = iwTable.takesLanguagePrefix
    end
    if language then
        data.language = language
    end
        
    function data:getPrefixes()
        checkSelf( self, 'getPrefixes' )
        if not iwTable then
            return nil
        end
        local t = {}
        for i, iwPrefix in ipairs( iwTable.iw_prefix ) do
            table.insert( t, iwPrefix )
        end
        return t
    end
    
    -- Specify which fields are read-only, and prepare the metatable.
    local readOnlyFields = {
        domain = true,
        titlePrefix = true,
        takesLanguagePrefix = true,
        language = true
    }
    for k, v in pairs( data ) do
        readOnlyFields[ k ] = true
    end
 
    local function pairsfunc( t, k )
        local v
        repeat
            k = next( readOnlyFields, k )
            if k == nil then
                return nil
            end
            v = t[ k ]
        until v ~= nil
        return k, v
    end
 
    return setmetatable( obj, {
        __pairs = function ( t )
            return pairsfunc, t, nil
        end,
        __index = data,
        __newindex = function( t, key, value )
            if readOnlyFields[ key ] then
                error( 'index "' .. key .. '" is read-only', 2 )
            else
                rawset( t, key, value )
            end
        end,
        __tostring = function( t )
            local ret = {}
            local prefixes = t:getPrefixes() or {}
            local prefix = prefixes[ 1 ]
            table.insert( ret, prefix )
            local lang = t.language
            table.insert( ret, language )
            return table.concat( ret, ':' )
        end
    } )
end

function iw.test()
    local new = iw.new( ':ja:wikibooks:' )
    return tostring( new )
end

return iw