Jump to content

Module:Character cell

From Semantic Stargate Wiki
Documentation icon Module documentation[create]
local p = {}

local function makeDiv(params)
    local size = params.size or ""
    local deceased = params.deceased or ""
    local catlink = params.catlink or ""
    local link = params.link or ""
    local image = params.image or ""
    local text = params.text or ""
    local smalltext = params.smalltext or ""
    local smallnonlinktext = params.smallnonlinktext or ""
    local unlinkedtext = params.unlinkedtext or ""
    local related = params.related or ""
    local lines = tonumber(params.lines) or 2
    local namespace = mw.title.getCurrentTitle().nsText
    
    local isSmall = size == "small"

    local width = isSmall and "60px" or "100px"
    local height = isSmall and "75px" or "127px"
    local fontsize = isSmall and "80px" or "125px"
    local imgsize = isSmall and "60px" or "100px"
    local padding = isSmall and "2px" or "5px"
    local margin = isSmall and "2px" or "5px"

    local opacityStyle = deceased ~= "" and 'filter:alpha(opacity=50);opacity:.50;' or ''
    local linkTarget = catlink ~= "" and (namespace == "Category" and catlink or link) or link
    local displayText = smalltext ~= "" and smalltext or text
    local finalText = deceased ~= "" and "<span style='color:silver;'><i>[" .. linkTarget .. "|" .. displayText .. "]</i></span>" or "[[" .. linkTarget .. "|" .. displayText .. "]]"

    local linesBreak = string.rep("<br>&nbsp;", lines > 0 and lines - 1 or 1)

    local html = mw.html.create("div")
        :css("border", "1px gray solid")
        :css("padding", padding)
        :css("margin", margin)
        :css("width", width)
        :css("background-color", "white")
        :tag("div")
            :css("position", "relative")
            :css("height", height)
            :css("overflow", "hidden")
            :node(
                mw.html.create("div")
                :css("position", "absolute")
                :css("top", "0px")
                :css("left", "0px")
                :css("font-size", fontsize)
                :css("width", width)
                :css("overflow", "hidden")
                :css("line-height", height)
                :css("z-index", "3")
                :wikitext("[[" .. linkTarget .. "|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]]")
            )
            :node(
                mw.html.create("div")
                :css("position", "absolute")
                :css("top", "0px")
                :css("left", "0px")
                :css("z-index", "2")
                :cssText(opacityStyle)
                :wikitext("[[File:" .. image .. "|" .. link .. "|" .. imgsize .. "]]")
            )
        :done()
        :node(
            mw.html.create("div")
                :css("position", "relative")
                :css("width", "100%")
                :css("text-align", "center")
                :wikitext(linesBreak)
                :node(mw.html.create("div")
                    :css("position", "absolute")
                    :css("top", "0px")
                    :css("left", "0px")
                    :css("width", "100%")
                    :css("text-align", "center")
                    :wikitext(finalText ..
                        (unlinkedtext ~= "" and unlinkedtext or "") ..
                        (related ~= "" and "<br><small>''[[" .. related .. "/Related Articles|related pages]]''</small>" or "")
                    )
                )
        )

    return tostring(html)
end

function p.render(frame)
    local args = frame:getParent().args
    return makeDiv(args)
end

return p