پرش به محتوا

پودمان:AlborzInfoBox

از ویکی البرز
نسخهٔ تاریخ ‏۴ ژوئیهٔ ۲۰۲۶، ساعت ۰۷:۳۱ توسط Modir (بحث | مشارکت‌ها) (خنثی‌سازی نسخهٔ 10486 از Modir (بحث))

توضیحات این پودمان می‌تواند در پودمان:AlborzInfoBox/توضیحات قرار گیرد.

local p = {}

local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')

-------------------------------------------------------
-- NORMALIZE
-------------------------------------------------------

local function norm(s)
    if not s then
        return nil
    end

    s = mw.text.trim(s)
    s = mw.ustring.gsub(s, "[\u200C\u200D]", "")
    s = mw.text.trim(s)

    return s
end

-------------------------------------------------------
-- ARG HELPERS
-------------------------------------------------------

local function getArgs(frame)
    local args = {}

    local function add(tbl)
        if not tbl then return end

        for k, v in pairs(tbl) do
            if v and v ~= "" then
                local nk = norm(k)
                args[nk] = mw.text.trim(v)
                args[k] = mw.text.trim(v)
            end
        end
    end

    add(frame.args)

    local parent = frame:getParent()
    if parent then
        add(parent.args)
    end

    return args
end

local function getArg(args, ...)
    local keys = {...}

    for i = 1, #keys do
        local k = keys[i]
        if k then
            local nk = norm(k)
            if args[nk] and args[nk] ~= "" then
                return args[nk]
            end
        end
    end

    return nil
end

-------------------------------------------------------
-- TABLE BASE
-------------------------------------------------------

local function newTable(rawType)
    return mw.html.create("table")
        :addClass("infobox")
        :addClass("alborz-infobox")
        :addClass("infobox-type-" .. (rawType or "generic"))
end

-------------------------------------------------------
-- TITLE
-------------------------------------------------------

local function addTitle(root, args)
    local title =
        getArg(args, "نام", "نام رسمی", "نام‌رسمی")
        or mw.title.getCurrentTitle().text

    root:tag("tr")
        :tag("th")
            :attr("colspan", "2")
            :css("text-align", "center")
            :css("font-size", "115%")
            :wikitext(mw.text.encode(title))
end
-------------------------------------------------------
-- BADGE MAP (ساده و سریع)
-------------------------------------------------------

local badgeMap = {
    city = "شهر",
    village = "روستا",
    county = "شهرستان",
    district = "بخش",
    ruraldistrict = "دهستان",

    mountain = "کوه",
    river = "رودخانه",
    waterfall = "آبشار",
    cave = "غار",
    park = "پارک",

    historic = "بنای تاریخی",
    religious = "بنای مذهبی"
}

local function addBadge(root, rawType)
    if not rawType or rawType == "" then
        return
    end

    rawType = norm(rawType)
    local label = badgeMap[rawType] or rawType

    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css("text-align", "center")
            :css("padding", "6px 0")
            :tag("span")
                :css("display", "inline-block")
                :css("padding", "2px 10px")
                :css("border", "1px solid #c0d4e8")
                :css("border-radius", "4px")
                :css("background", "#f0f6ff")
                :css("font-size", "87%")
                :css("font-weight", "bold")
                :wikitext(label)
end

-------------------------------------------------------
-- IMAGE
-------------------------------------------------------

local function addImage(root, args)
    local image = getArg(args, "تصویر", "پرونده", "image")

    if not image then
        return
    end

    image = mw.text.trim(image)

    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css("text-align", "center")
            :wikitext("[[پرونده:" .. image .. "|250px]]")
end

-------------------------------------------------------
-- CAPTION
-------------------------------------------------------

local function addCaption(root, args)
    local caption =
        getArg(args,
            "توضیح تصویر",
            "عنوان تصویر",
            "برچسب تصویر")

    if not caption then
        return
    end

    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css("text-align", "center")
            :css("font-size", "90%")
            :css("padding", "3px 0 6px 0")
            :wikitext(mw.text.encode(caption))
end

-------------------------------------------------------
-- ROW
-------------------------------------------------------

local function addRow(root, label, value)
    if not value or value == "" then
        return
    end

    local tr = root:tag("tr")

    tr:tag("th")
        :wikitext(label)

    tr:tag("td")
        :wikitext(value)
end
-------------------------------------------------------
-- ROWS
-------------------------------------------------------

local function addRows(root, args, layout)
    for _, key in ipairs(layout) do

        local field = data.fields[key]

        if field then

            local value =
                getArg(args, field.arg)

            if (not value or value == "") and field.default then
                value = field.default
            end

            if value and value ~= "" then
                addRow(root, field.label, value)
            end

        end
    end
end

-------------------------------------------------------
-- RENDER
-------------------------------------------------------

local function render(args)

    local rawType = getArg(args, "نوع") or "city"
    local layout = data.layouts[rawType] or data.layouts.city

    local root = newTable(rawType)

    addTitle(root, args)
    addBadge(root, rawType)
    addImage(root, args)
    addCaption(root, args)
    addRows(root, args, layout)

    return tostring(root)
end

-------------------------------------------------------
-- MAIN
-------------------------------------------------------

function p.main(frame)
    return render(getArgs(frame))
end

return p