Modul:Sandkasse/Infoboks svævefly

Fra Wikipedia, den frie encyklopædi
Documentation icon Moduldokumentation[opret]
local p = {}

local function debug( obj, name, pattern, visited )
    local pattern = pattern or '\n<br/>%s = %s ';
    local name = name or 'obj';
    visited = visited or {};
    if type(obj) == 'table' then
        if ( visited[obj] ) then
            -- avoid displaying the same table again, eg metatables
            return string.format( pattern, name, '(table, see ' .. visited[obj] .. ')' );
        else
            visited[obj] = name;
        end;
        local content = '';
        for k, v in pairs(obj) do
            content = content .. debug( v, name .. '[' .. tostring( k ) .. ']', pattern, visited );
        end;
        local meta = getmetatable(obj);
        if meta == nil then
            meta = '';
        else
            meta = debug(meta, name .. ' metatable', pattern, visited);
        end;
        if content == '' then
            return string.format( pattern, name, '(table, empty)' ) .. meta;
        else
            return content .. meta;
        end;
    else
        return string.format( pattern, name, '(' .. type(obj) .. ') ' .. tostring(obj) );
    end
end


-- from http://lua-users.org/wiki/SimpleLuaClasses, reformatted
-- class.lua
-- Compatible with Lua 5.1 (not 5.0).
function class(base, init)
    local c = {}    -- a new class instance
    if not init and type(base) == 'function' then
        init = base
        base = nil
    elseif type(base) == 'table' then
        -- our new class is a shallow copy of the base class!
        for i,v in pairs(base) do
            c[i] = v
        end
        c._base = base
    end
    -- the class will be the metatable for all its objects,
    -- and they will look up their methods in it.
    c.__index = c

    -- expose a constructor which can be called by <classname>(<args>)
    local mt = {}
    mt.__call = function(class_tbl, ...)
        local obj = {}
        setmetatable(obj,c)
        if init then
            init(obj,...)
        else 
            -- make sure that any stuff from the base class is initialized!
            if base and base.init then
                base.init(obj, ...)
            end
        end
        return obj
    end
    c.init = init
    c.is_a = function(self, klass)
        local m = getmetatable(self)
        while m do 
            if m == klass then return true end
            m = m._base
        end
        return false
    end
    setmetatable(c, mt)
    return c
end

-- java-like baseclass
Object = class(function(me)
    local mt = getmetatable(me);
    me._hashcode = mt._hashcodeNext or 1;
    mt._hashcodeNext = me._hashcode + 1;
end);
function Object:extend(init)
    return class(self, init);
end
function Object.hashcode(me)
    return me._hashcode;
end
function Object.toString(me)
    return 'Object@' .. tostring( me:hashcode() );
end

-- inspireret af http://da.wikipedia.org/wiki/Skabelon:Infoboks-en
Infoboks = Object:extend(function(me, config)
    me._config = {};
    if config then
        for k,v in pairs(config) do
            me._config[k] = v;
        end;
    end;
    me._content = {};
end);
function Infoboks.add(me, line, config)
    if not line then return me; end;
    if type(line) ~= 'table' then return me; end;
    if line.heading then
        table.insert(me._content, "<TR><TH COLSPAN='2'>'''" .. line.heading .. "'''</TH></TR>");
    end;
    if line.data or line.param or line.parameter or line.prop or line.property then
        local data, param, prop, extra = line.data, line.param or line.parameter, line.prop or line.property
        local row = '<tr>';
        if line.label then
            row = row .. '<th>' .. line.label .. '</th><td>' .. line.data .. '</td>'
        else
            row = row .. '<td colspan="2" align="center">' .. line.data .. '</td>';
        end;
        row = row .. '</tr>';
        table.insert(me._content, row);
    end;
    -- table.insert(me._content, debug(line, 'line' ) )
    return me;
end
function Infoboks.toString(me)
    -- check for content
    local content = table.concat( me._content, '\n' );
    if content == '' then
        return me._config.categoryEmpty or '';
    end;
    -- start table
    local ret = '<table class="infobox';
    if me._config.bodyclass then ret = ret .. ' ' .. me._config.bodyclass; end;
    ret = ret .. '" cellspacing="5" style="width: 22em; text-align: left; font-size: 88%; line-height: 1.5em;';
    if me._config.float then
        ret = ret .. ' float: ' .. me._config.float .. '; clear: ';
        if me._config.float == 'none' then
            ret = ret .. 'both';
        else
            ret = ret .. me._config.float;
        end;
        ret = ret .. "; margin: 0 0 1em 1em;"
    end;
    ret = ret .. '>\n';
    -- title(s)
    if me._config.title then
        ret = ret .. '<caption';
        if me._config.titleclass then ret = ret .. ' class="' .. me._config.titleclass .. '"'; end;
        ret = ret .. '" style="font-size: 125%; font-weight: bold;';
        if me._config.titlestyle then ret = ret .. ' ' .. me._config.titlestyle; end;
        ret = ret .. '">' .. me._config.title .. '</caption>\n';
    end;
    if me._config.above then
        ret = ret .. '<tr><th colspan="2"';
        if me._config.aboveclass then ret = ret .. ' class="' .. me._config.aboveclass .. '"'; end;
        ret = ret .. ' style="text-align:center; font-size:125%; font-weight:bold;';
        if me._config.abovestyle then ret = ret .. ' ' .. me._config.abovestyle; end;
        ret = ret .. '">' .. me._config.above .. ' </th></tr>\n';
    end;
    ret = ret .. content;
    ret = ret .. '\n</table>';
    return ret;
end


function p.hello(frame)
    local entity = mw.wikibase.getEntityObject()
    local entityStatus
    local box = Infoboks( { categoryEmpty='[[Kategori:Sider med tom infoboks]]', above='Svævefly<br/>ASK 21', abovestyle='background: #87CEEB; font-color: #ffffff;' } );
    box:add{heading='Konstruktion', label='Designer', data='Otto Lilienthal'};
    return box:toString();
--    local o = { Object(), Object(), box };
--    return '<table class="infobox" style="font-size: 90%; float:right; width: 260px;>"'
--        .. '\n<tr><td colspan="2" align="center" style="background: #87CEEB; font-size: larger;"><font color="#ffffff">Svævefly<br/>'
--        .. 'Sandkasse/Infoboks svævefly' .. '</td></tr>'
--        .. debug( o, 'object', '\n<tr valing="top"><td> %s </td><td> %s </td></tr>' )
--        .. '\n<tr><td colspan="2">object[1]:toString() = "' .. o[1]:toString() .. '"</td></tr>'
--        .. '\n<tr><td colspan="2">object[2]:toString() = "' .. o[2]:toString() .. '"</td></tr>'
--        .. debug( entity, 'entity', '\n<tr valing="top"><td> %s </td><td> %s </td></tr>' )
--        .. debug( frame, 'frame', '\n<tr valing="top"><td> %s </td><td> %s </td></tr>' )
--        .. debug( { test = 'test' }, 'testtable', '\n<tr valing="top"><td> %s </td><td> %s </td></tr>' )
--        .. '\n</table>'
end

return p