FANDOM


-- <nowiki>Simple module to demostrate how to create a portable infobox + lua (hybrid)
 
local portable = {}
local getArgs = require("Dev:Arguments").getArgs
local testStructure = {
    title = { default = "Standard title"},
    structure ={
        {
            default ="Example.png",
            type = "image"
        },
        {
            label ="testLabel",
            default ="testValue",
            format = "",
            source = "testSource",
            type = "data",
            group = ""
        },
        {
            label ="testLabel2",
            default ="testValue2",
            format = "",
            source = "testSource2",
            type = "data",
            group = ""
        },
    }
}
 
function portable.main(frame)
    local args = getArgs(frame)
 
    if args.test then
        return createPortable(testStructure)    
    end
end
 
function createPortable(dataDB)
    local portableOutput = ""
 
    for dataNode,content in pairs(dataDB) do
        mw.log(dataNode)
        if dataNode == "title" then
             portableOutput = portableOutput .. createTitle(dataDB[dataNode].default)
        end
 
        if dataNode =="structure" then
            for _, attribute in ipairs(dataDB.structure) do
                if attribute.type == "data" then
                    portableOutput = portableOutput .. createDataItem(attribute.source,
                     attribute.label, attribute.default, attribute.dataFormat)
                end
                if attribute.type == "image" then
                    portableOutput = portableOutput .. createImageItem(attribute.source, attribute.default,
                        attribute.caption, attribute.alt)
                end
            end
        end
 
    end
 
    local portableInfobox = preprocess("{{#tag:infobox|".. portableOutput.. "}}")
 
    return portableInfobox 
end
 
function createTitle(defaultText)
    local data = [[
    <title>
        <default>]] ..(defaultText or "") ..[[</default>
    </title>
    ]] 
    return data
end
 
function createImageItem(source, default, caption, alt)
    local data =
     [[<image source= "]] .. (source or "") .. [[">
        <caption>]] .. (caption or "") .. [[</caption>
        <default>]] ..(default or "") ..[[</default>
        <alt>]] .. (alt or "") ..[[</alt>
    </image>
    ]] 
    return data
end
 
function createDataItem(source, label, default, dataFormat)
    local data = [[
    <data source= "]] .. source .. [["> 
        <label>]] .. label .. [[</label>
        <default>]] ..default ..[[</default>
        <format>]] .. (dataFormat or "") ..[[</format>
    </data>
    ]] 
    return data
end
 
function preprocess(text)
    if mw.getCurrentFrame() then
       return mw.getCurrentFrame():preprocess(text) 
    end
 
    return text
end
return portable

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.