پرش به محتوا

پودمان:AlborzInfoBox: تفاوت میان نسخه‌ها

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۴: خط ۴:


-------------------------------------------------------
-------------------------------------------------------
-- NORMALIZE
-- NORMALIZE (cached-safe)
-------------------------------------------------------
-------------------------------------------------------
local function norm(s)
local function norm(s)
     if not s then
     if not s then return nil end
        return nil
    end
 
     s = mw.text.trim(s)
     s = mw.text.trim(s)
     s = mw.ustring.gsub(s, "[\u200C\u200D]", "")
     s = mw.ustring.gsub(s, "[\u200C\u200D]", "")
     s = mw.text.trim(s)
     return mw.text.trim(s)
 
    return s
end
end


-------------------------------------------------------
-------------------------------------------------------
-- GET ARG
-- ARG HANDLING (optimized)
-------------------------------------------------------
-------------------------------------------------------
local function getArg(args, field)
local function collectArgs(frame)
     if not field then
     local args = {}
        return nil
    end


     local candidates = {}
     local function push(tbl)
 
        if not tbl then return end
    if field.arg then
         for k, v in pairs(tbl) do
        table.insert(candidates, field.arg)
             if v and v ~= "" then
    end
                args[norm(k)] = mw.text.trim(v)
 
            end
    if field.args then
         for _, a in ipairs(field.args) do
             table.insert(candidates, a)
         end
         end
     end
     end


     for _, name in ipairs(candidates) do
     push(frame.args)
        local value = args[norm(name)]
    push(frame:getParent() and frame:getParent().args)
        if value and value ~= "" then
            return value
        end
    end


     return nil
     return args
end
end


-------------------------------------------------------
-------------------------------------------------------
-- GET ARGS
-- GET FIELD VALUE
-------------------------------------------------------
-------------------------------------------------------
local function getArgs(frame)
local function getValue(args, field)
     local args = {}
     if not field then return nil end


     local function add(tbl)
     local names = {}
        if not tbl then
    if field.arg then table.insert(names, field.arg) end
             return
    if field.args then
        for _, v in ipairs(field.args) do
             table.insert(names, v)
         end
         end
    end


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


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


-------------------------------------------------------
-------------------------------------------------------
-- ADD ROW
-- ROW RENDERER (clean)
-------------------------------------------------------
-------------------------------------------------------
local function addRow(root, label, value)
local function addRow(root, label, value, raw)
     if not value or value == "" then
     if not value or value == "" then return end
        return
    end


     local tr = root:tag("tr")
     local tr = root:tag("tr")
خط ۸۹: خط ۶۹:
     local td = tr:tag("td")
     local td = tr:tag("td")


    local isWikitext =
     if raw then
        value:find("%[%[.-%]%]") or
        value:find("{{.-}}") or
        value:find("<[^>]+>") or
        value:find("^%*") or
        value:find("^#") or
        value:find("\n")
 
     if isWikitext then
         td:wikitext(value)
         td:wikitext(value)
     else
     else
خط ۱۲۱: خط ۹۳:
}
}


local function addBadge(root, typeKey)
local function addBadge(root, t)
    if not typeKey then
     local label = badgeMap[t] or "مکان"
        return
    end
 
     local label = badgeMap[typeKey] or "مکان"


     root:tag("tr")
     root:tag("tr")
         :tag("td")
         :tag("td")
             :attr("colspan", "2")
             :attr("colspan", "2")
             :css("text-align", "center")
             :css({
            :css("padding", "6px 0 8px 0")
                ["text-align"] = "center",
                ["padding"] = "6px 0 8px 0"
            })
             :tag("span")
             :tag("span")
                 :css("display", "inline-block")
                 :css({
                :css("background", "#e8f0f8")
                    display = "inline-block",
                :css("color", "#1e4b7a")
                    background = "#e8f0f8",
                :css("font-size", "87%")
                    color = "#1e4b7a",
                :css("font-weight", "bold")
                    ["font-size"] = "87%",
                :css("padding", "3px 12px")
                    ["font-weight"] = "bold",
                :css("border-radius", "5px")
                    padding = "3px 12px",
                :css("border", "1px solid #b0c8e0")
                    ["border-radius"] = "5px",
                    border = "1px solid #b0c8e0"
                })
                 :wikitext(label)
                 :wikitext(label)
end
end


-------------------------------------------------------
-------------------------------------------------------
-- IMAGE
-- IMAGE (safer, no encoding)
-------------------------------------------------------
-------------------------------------------------------
local function addImage(root, args)
local function addImage(root, args)
     local image = args[norm("تصویر")] or args[norm("پرونده")]
     local img = args[norm("تصویر")] or args[norm("پرونده")]
 
     if not img or img == "" then return end
     if not image or image == "" then
        return
    end


     image = mw.text.trim(image)
     img = mw.text.trim(img)
 
     img = mw.ustring.gsub(img, "^[Ff]ile:", "")
     image = mw.ustring.gsub(image, "^پرونده:", "")
     img = mw.ustring.gsub(img, "^پرونده:", "")
     image = mw.ustring.gsub(image, "^File:", "")


     root:tag("tr")
     root:tag("tr")
خط ۱۶۴: خط ۱۳۲:
             :attr("colspan", "2")
             :attr("colspan", "2")
             :css("text-align", "center")
             :css("text-align", "center")
             :wikitext("[[پرونده:" .. mw.uri.encode(image, "WIKI") .. "|250px]]")
             :wikitext("[[File:" .. img .. "|250px]]")
end
end


خط ۱۷۱: خط ۱۳۹:
-------------------------------------------------------
-------------------------------------------------------
local function addCaption(root, args)
local function addCaption(root, args)
     local caption =
     local cap =
         args[norm("توضیح تصویر")]
         args[norm("توضیح تصویر")] or
         or args[norm("عنوان تصویر")]
         args[norm("عنوان تصویر")] or
         or args[norm("زیرنویس")]
         args[norm("زیرنویس")]


     if not caption or caption == "" then
     if not cap or cap == "" then return end
        return
    end


     root:tag("tr")
     root:tag("tr")
         :tag("td")
         :tag("td")
             :attr("colspan", "2")
             :attr("colspan", "2")
             :css("text-align", "center")
             :css({
             :css("font-size", "90%")
                ["text-align"] = "center",
             :css("padding", "3px 0 8px 0")
                ["font-size"] = "90%",
             :wikitext(mw.text.encode(caption))
                padding = "3px 0 8px 0"
            })
            :wikitext(mw.text.encode(cap))
end
 
-------------------------------------------------------
-- TITLE
-------------------------------------------------------
local function addTitle(root, args)
    local title =
        args[norm("نام")] or
        args[norm("نام رسمی")] or
        mw.title.getCurrentTitle().text
 
    root:tag("tr")
        :tag("th")
             :attr("colspan", "2")
             :css({
                ["text-align"] = "center",
                ["font-size"] = "118%",
                padding = "8px 4px"
            })
             :wikitext(mw.text.encode(title))
end
end
-------------------------------------------------------
-------------------------------------------------------
-- TABLE
-- TABLE
-------------------------------------------------------
-------------------------------------------------------
local function newTable(typeKey)
local function newTable(typeKey)
    typeKey = typeKey or "city"
     return mw.html.create("table")
     return mw.html.create("table")
         :addClass("infobox")
         :addClass("infobox")
خط ۲۰۱: خط ۱۸۸:


-------------------------------------------------------
-------------------------------------------------------
-- TITLE
-- TYPE NORMALIZATION
-------------------------------------------------------
-------------------------------------------------------
local function addTitle(root, args)
local typeAliases = {
     local title =
    ["شهرستان"] = "county",
        args[norm("نام")]
     ["شهر"] = "city",
        or args[norm("نام رسمی")]
    ["روستا"] = "village",
        or args[norm("نام‌رسمی")]
    ["بخش"] = "district",
        or mw.title.getCurrentTitle().text
    ["دهستان"] = "ruraldistrict",
    ["کوه"] = "mountain",
    ["رودخانه"] = "river",
    ["آبشار"] = "waterfall",
    ["بنای تاریخی"] = "historic"
}


    root:tag("tr")
local function resolveType(t)
        :tag("th")
    t = norm(t) or "city"
            :attr("colspan", "2")
    return typeAliases[t] or t
            :css("text-align", "center")
            :css("font-size", "118%")
            :css("padding", "8px 4px")
            :wikitext(mw.text.encode(title))
end
end


-------------------------------------------------------
-------------------------------------------------------
-- ADD ROWS
-- RENDER ROWS
-------------------------------------------------------
-------------------------------------------------------
local function addRows(root, args, layout)
local function renderRows(root, args, layout)
     for _, key in ipairs(layout) do
     for _, key in ipairs(layout) do
         local field = data.fields[key]
         local field = data.fields[key]
         if field then
         if field then
             local value = getArg(args, field)
             local value = getValue(args, field)
 
            if (not value or value == "") and field.default then
                value = field.default
            end


             if value and value ~= "" then
             if value and value ~= "" then
                 addRow(root, field.label, value)
                 addRow(root, field.label, value, field.raw)
             end
             end
         end
         end
خط ۲۴۱: خط ۲۲۴:


-------------------------------------------------------
-------------------------------------------------------
-- RENDER
-- MAIN RENDER
-------------------------------------------------------
-------------------------------------------------------
local function render(args)
local function render(args)
    if not data or not data.fields then
        return "Infobox data not found"
    end


     local rawTypeInput = norm(args["نوع"]) or "city"
     local typeKey = resolveType(args["نوع"])
 
     local internalType = (data.typeMap and data.typeMap[typeKey]) or typeKey
    -- تبدیل مقدار واردشده به کلید داخلی
     local layout = (data.layouts and data.layouts[internalType]) or (data.layouts and data.layouts.city) or {}
     local internalType = data.typeMap[rawTypeInput] or rawTypeInput
 
     local layout = data.layouts[internalType] or data.layouts.city


    -- استفاده از کلید داخلی برای CSS
     local root = newTable(internalType)
     local root = newTable(internalType)


     addTitle(root, args)
     addTitle(root, args)
    -- استفاده از کلید داخلی برای Badge
     addBadge(root, internalType)
     addBadge(root, internalType)
     addImage(root, args)
     addImage(root, args)
     addCaption(root, args)
     addCaption(root, args)
     addRows(root, args, layout)
     renderRows(root, args, layout)


     return tostring(root)
     return tostring(root)
خط ۲۶۸: خط ۲۴۷:


-------------------------------------------------------
-------------------------------------------------------
-- MAIN
-- EXPORT
-------------------------------------------------------
-------------------------------------------------------
function p.main(frame)
function p.main(frame)
     local args = getArgs(frame)
     return render(collectArgs(frame))
    return render(args)
end
end


return p
return p

نسخهٔ ‏۴ ژوئیهٔ ۲۰۲۶، ساعت ۱۵:۱۷

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

local p = {}

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

-------------------------------------------------------
-- NORMALIZE (cached-safe)
-------------------------------------------------------
local function norm(s)
    if not s then return nil end
    s = mw.text.trim(s)
    s = mw.ustring.gsub(s, "[\u200C\u200D]", "")
    return mw.text.trim(s)
end

-------------------------------------------------------
-- ARG HANDLING (optimized)
-------------------------------------------------------
local function collectArgs(frame)
    local args = {}

    local function push(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

    push(frame.args)
    push(frame:getParent() and frame:getParent().args)

    return args
end

-------------------------------------------------------
-- GET FIELD VALUE
-------------------------------------------------------
local function getValue(args, field)
    if not field then return nil end

    local names = {}
    if field.arg then table.insert(names, field.arg) end
    if field.args then
        for _, v in ipairs(field.args) do
            table.insert(names, v)
        end
    end

    for _, n in ipairs(names) do
        local v = args[norm(n)]
        if v and v ~= "" then
            return v
        end
    end

    return field.default
end

-------------------------------------------------------
-- ROW RENDERER (clean)
-------------------------------------------------------
local function addRow(root, label, value, raw)
    if not value or value == "" then return end

    local tr = root:tag("tr")
    tr:tag("th"):wikitext(label)

    local td = tr:tag("td")

    if raw then
        td:wikitext(value)
    else
        td:wikitext(mw.text.encode(value))
    end
end

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

local function addBadge(root, t)
    local label = badgeMap[t] or "مکان"

    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css({
                ["text-align"] = "center",
                ["padding"] = "6px 0 8px 0"
            })
            :tag("span")
                :css({
                    display = "inline-block",
                    background = "#e8f0f8",
                    color = "#1e4b7a",
                    ["font-size"] = "87%",
                    ["font-weight"] = "bold",
                    padding = "3px 12px",
                    ["border-radius"] = "5px",
                    border = "1px solid #b0c8e0"
                })
                :wikitext(label)
end

-------------------------------------------------------
-- IMAGE (safer, no encoding)
-------------------------------------------------------
local function addImage(root, args)
    local img = args[norm("تصویر")] or args[norm("پرونده")]
    if not img or img == "" then return end

    img = mw.text.trim(img)
    img = mw.ustring.gsub(img, "^[Ff]ile:", "")
    img = mw.ustring.gsub(img, "^پرونده:", "")

    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css("text-align", "center")
            :wikitext("[[File:" .. img .. "|250px]]")
end

-------------------------------------------------------
-- CAPTION
-------------------------------------------------------
local function addCaption(root, args)
    local cap =
        args[norm("توضیح تصویر")] or
        args[norm("عنوان تصویر")] or
        args[norm("زیرنویس")]

    if not cap or cap == "" then return end

    root:tag("tr")
        :tag("td")
            :attr("colspan", "2")
            :css({
                ["text-align"] = "center",
                ["font-size"] = "90%",
                padding = "3px 0 8px 0"
            })
            :wikitext(mw.text.encode(cap))
end

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

    root:tag("tr")
        :tag("th")
            :attr("colspan", "2")
            :css({
                ["text-align"] = "center",
                ["font-size"] = "118%",
                padding = "8px 4px"
            })
            :wikitext(mw.text.encode(title))
end

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

-------------------------------------------------------
-- TYPE NORMALIZATION
-------------------------------------------------------
local typeAliases = {
    ["شهرستان"] = "county",
    ["شهر"] = "city",
    ["روستا"] = "village",
    ["بخش"] = "district",
    ["دهستان"] = "ruraldistrict",
    ["کوه"] = "mountain",
    ["رودخانه"] = "river",
    ["آبشار"] = "waterfall",
    ["بنای تاریخی"] = "historic"
}

local function resolveType(t)
    t = norm(t) or "city"
    return typeAliases[t] or t
end

-------------------------------------------------------
-- RENDER ROWS
-------------------------------------------------------
local function renderRows(root, args, layout)
    for _, key in ipairs(layout) do
        local field = data.fields[key]
        if field then
            local value = getValue(args, field)

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

-------------------------------------------------------
-- MAIN RENDER
-------------------------------------------------------
local function render(args)
    if not data or not data.fields then
        return "Infobox data not found"
    end

    local typeKey = resolveType(args["نوع"])
    local internalType = (data.typeMap and data.typeMap[typeKey]) or typeKey
    local layout = (data.layouts and data.layouts[internalType]) or (data.layouts and data.layouts.city) or {}

    local root = newTable(internalType)

    addTitle(root, args)
    addBadge(root, internalType)
    addImage(root, args)
    addCaption(root, args)
    renderRows(root, args, layout)

    return tostring(root)
end

-------------------------------------------------------
-- EXPORT
-------------------------------------------------------
function p.main(frame)
    return render(collectArgs(frame))
end

return p