پرش به محتوا

پودمان:AlborzInfoBox

از ویکی البرز
نسخهٔ تاریخ ‏۳ ژوئیهٔ ۲۰۲۶، ساعت ۱۸:۵۹ توسط 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

-------------------------------------------------------
-- GET ARGS
-------------------------------------------------------
local function getArgs(frame)
    local args = {}
    local function addArgs(argTable)
        for k, v in pairs(argTable or {}) do
            if v and v ~= "" then
                local nkey = norm(k)
                args[nkey] = v
                args[k] = v
            end
        end
    end
    addArgs(frame.args)
    local parent = frame:getParent()
    if parent then addArgs(parent.args) end
    return args
end

-------------------------------------------------------
-- ADD 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(mw.text.encode(value))
end

-------------------------------------------------------
-- BADGE (نسخهٔ حرفه‌ای‌تر)
-------------------------------------------------------
local function addBadge(root, rawType)
    if not rawType or rawType == "" then return end

    -- نرمالایز اضافی برای badge
    rawType = norm(rawType)

    local badgeMap = {
        city = "شهر",
        village = "روستا",
        county = "شهرستان",
        district = "بخش",
        ruraldistrict = "دهستان",
        mountain = "کوه",
        river = "رودخانه",
        waterfall = "آبشار",
        cave = "غار",
        park = "پارک",
    }

    local label = badgeMap[rawType] or rawType

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

-------------------------------------------------------
-- RENDER
-------------------------------------------------------
local function render(args)
    local rawType = norm(args["نوع"]) or ""
    local layout = data.layouts[rawType] or data.layouts.city

    local root = mw.html.create("table")
        :addClass("infobox alborz-infobox")
        :addClass("infobox-type-" .. (rawType ~= "" and rawType or "city"))

    ---------------------------------------------------
    -- TITLE
    ---------------------------------------------------
    local name = args[norm("نام")] 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(name))

    ---------------------------------------------------
    -- BADGE
    ---------------------------------------------------
    addBadge(root, rawType)

    ---------------------------------------------------
    -- IMAGE
    ---------------------------------------------------
    local img = args[norm("تصویر")]
    if img and img ~= "" then
        img = mw.text.trim(img)
        root:tag("tr")
            :tag("td")
                :attr("colspan", "2")
                :wikitext("[[پرونده:" .. mw.uri.encode(img, "WIKI") .. "|250px]]")
    end

    ---------------------------------------------------
    -- CAPTION
    ---------------------------------------------------
    local caption = args[norm("توضیح تصویر")]
    if caption and caption ~= "" then
        root:tag("tr")
            :tag("td")
                :attr("colspan", "2")
                :css("font-size", "90%")
                :css("text-align", "center")
                :wikitext(mw.text.encode(caption))
    end

    ---------------------------------------------------
    -- FIELDS
    ---------------------------------------------------
    for _, key in ipairs(layout) do
        local field = data.fields[key]
        if field then
            local argKey = norm(field.arg)
            local value = args[argKey] or args[field.arg]

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

            addRow(root, field.label, value)
        end
    end

    return tostring(root)
end

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

return p