پرش به محتوا

پودمان: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 ARG
-------------------------------------------------------
local function getArg(args, field)
    if not field then return nil end
    if field.args then
        for i = 1, #field.args do
            local val = args[norm(field.args[i])]
            if val and val ~= "" then return val end
        end
    end
    if field.arg then
        return args[norm(field.arg)]
    end
    return nil
end

-------------------------------------------------------
-- GET ARGS
-------------------------------------------------------
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
                args[norm(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

-------------------------------------------------------
-- 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 badgeMap = {
    city = "شهر", village = "روستا", county = "شهرستان",
    district = "بخش", ruraldistrict = "دهستان", mountain = "کوه",
    cave = "غار", river = "رودخانه", waterfall = "آبشار",
    park = "پارک", historic = "بنای تاریخی"
}

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 8px 0")
            :tag("span")
                :css("display", "inline-block")
                :css("background", "#e8f0f8")
                :css("color", "#1e4b7a")
                :css("font-size", "87%")
                :css("font-weight", "bold")
                :css("padding", "3px 12px")
                :css("border-radius", "5px")
                :css("border", "1px solid #b0c8e0")
                :wikitext(label)
end

-------------------------------------------------------
-- IMAGE
-------------------------------------------------------
local function addImage(root, args)
    local image = args[norm("تصویر")] or args[norm("پرونده")]
    if not image then return end
    image = mw.text.trim(image)
    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css("text-align", "center")
            :wikitext("[[پرونده:" .. mw.uri.encode(image, "WIKI") .. "|250px]]")
end

-------------------------------------------------------
-- CAPTION
-------------------------------------------------------
local function addCaption(root, args)
    local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")]
    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 8px 0")
            :wikitext(mw.text.encode(caption))
end

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

-------------------------------------------------------
-- TITLE (وسط چین قوی)
-------------------------------------------------------
local function addTitle(root, args)
    local title = args[norm("نام")] or args[norm("نام رسمی")] or args[norm("نام‌رسمی")]
        or mw.title.getCurrentTitle().text

    root:tag("tr")
        :tag("th")
            :attr("colspan", "2")
            :css("text-align", "center")
            :css("font-size", "118%")
            :css("padding", "8px 4px")
            :wikitext(mw.text.encode(title))
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)
            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 = norm(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

function p.main(frame)
    local args = getArgs(frame)
    return render(args)
end

return p