پرش به محتوا

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

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۷: خط ۷:
-------------------------------------------------------
-------------------------------------------------------
local function norm(s)
local function norm(s)
     if not s then return nil end
     if not s then
        return nil
    end
 
     s = mw.text.trim(s)
     s = mw.text.trim(s)
     s = mw.ustring.gsub(s, "[\u200C\u200D]", "") -- حذف ZWNJ و ZWJ
     s = mw.ustring.gsub(s, "[\u200C\u200D]", "")
     s = mw.text.trim(s)
     s = mw.text.trim(s)
     return s
     return s
end
end


-------------------------------------------------------
-------------------------------------------------------
-- GET ARG (قوی)
-- GET ARG
-------------------------------------------------------
-------------------------------------------------------
local function getArg(args, field)
local function getArg(args, field)
     if not field then return nil end
     if not field then
        return nil
    end


     local candidates = {}
     local candidates = {}
   
 
    -- arg تکی
     if field.arg then
     if field.arg then  
         table.insert(candidates, field.arg)
         table.insert(candidates, field.arg)  
     end
     end
   
 
    -- args چندگانه
     if field.args then
     if field.args then
         for _, a in ipairs(field.args) do  
         for _, a in ipairs(field.args) do
             table.insert(candidates, a)  
             table.insert(candidates, a)
         end
         end
     end
     end


     for _, name in ipairs(candidates) do
     for _, name in ipairs(candidates) do
         local normalized = norm(name)
         local value = args[norm(name)]
        local val = args[normalized]
         if value and value ~= "" then
         if val and val ~= "" then  
             return value
             return val
         end
         end
     end
     end
     return nil
     return nil
end
end
خط ۴۹: خط ۵۳:
local function getArgs(frame)
local function getArgs(frame)
     local args = {}
     local args = {}
     local function add(tbl)
     local function add(tbl)
         if not tbl then return end
         if not tbl then
            return
        end
 
         for k, v in pairs(tbl) do
         for k, v in pairs(tbl) do
             if v and v ~= "" then
             if v and v ~= "" then
                 local normKey = norm(k)
                 args[norm(k)] = mw.text.trim(v)
                args[normKey] = mw.text.trim(v)
             end
             end
         end
         end
     end
     end
     add(frame.args)
     add(frame.args)
     local parent = frame:getParent()
     local parent = frame:getParent()
     if parent then add(parent.args) end
     if parent then
        add(parent.args)
    end
 
     return args
     return args
end
end
خط ۶۸: خط ۸۰:
-------------------------------------------------------
-------------------------------------------------------
local function addRow(root, label, value)
local function addRow(root, label, value)
     if not value or value == "" then return end
     if not value or value == "" then
        return
    end
 
     local tr = root:tag("tr")
     local tr = root:tag("tr")
     tr:tag("th"):wikitext(label)
     tr:tag("th"):wikitext(label)
     if value:find("%[%[") or value:find("{{") or value:find("|") then
 
         tr:tag("td"):wikitext(value)
     local td = tr:tag("td")
 
    local isWikitext =
        value:find("%[%[.-%]%]") or
        value:find("{{.-}}") or
        value:find("<[^>]+>") or
        value:find("^%*") or
        value:find("^#") or
         value:find("\n")
 
    if isWikitext then
        td:wikitext(value)
     else
     else
         tr:tag("td"):wikitext(mw.text.encode(value))
         td:wikitext(mw.text.encode(value))
     end
     end
end
end
خط ۹۲: خط ۱۱۸:
     waterfall = "آبشار",
     waterfall = "آبشار",
     park = "پارک",
     park = "پارک",
     historic = "بنای تاریخی",
     historic = "بنای تاریخی"
    ["بنای تاریخی"] = "بنای تاریخی"
}
}


local function addBadge(root, rawType)
local function addBadge(root, typeKey)
     if not rawType or rawType == "" then return end
     if not typeKey then
    rawType = norm(rawType)
        return
     local label = badgeMap[rawType] or "بنای تاریخی"
    end
 
     local label = badgeMap[typeKey] or "مکان"


     root:tag("tr")
     root:tag("tr")
خط ۱۱۹: خط ۱۴۶:


-------------------------------------------------------
-------------------------------------------------------
-- Image & Caption
-- IMAGE
-------------------------------------------------------
-------------------------------------------------------
local function addImage(root, args)
local function addImage(root, args)
     local image = args[norm("تصویر")] or args[norm("پرونده")]
     local image = args[norm("تصویر")] or args[norm("پرونده")]
     if not image or image == "" then return end
 
     if not image or image == "" then
        return
    end
 
     image = mw.text.trim(image)
     image = mw.text.trim(image)
    image = mw.ustring.gsub(image, "^پرونده:", "")
    image = mw.ustring.gsub(image, "^File:", "")
     root:tag("tr")
     root:tag("tr")
         :tag("td")
         :tag("td")
خط ۱۳۲: خط ۱۶۷:
end
end


-------------------------------------------------------
-- CAPTION
-------------------------------------------------------
local function addCaption(root, args)
local function addCaption(root, args)
     local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")] or args[norm("زیرنویس")]
     local caption =
     if not caption or caption == "" then return end
        args[norm("توضیح تصویر")]
        or args[norm("عنوان تصویر")]
        or args[norm("زیرنویس")]
 
     if not caption or caption == "" then
        return
    end
 
     root:tag("tr")
     root:tag("tr")
         :tag("td")
         :tag("td")
خط ۱۴۳: خط ۱۸۸:
             :wikitext(mw.text.encode(caption))
             :wikitext(mw.text.encode(caption))
end
end
-------------------------------------------------------
-------------------------------------------------------
-- Table
-- TABLE
-------------------------------------------------------
-------------------------------------------------------
local function newTable(rawType)
local function newTable(typeKey)
     rawType = rawType or "city"
     typeKey = typeKey or "city"
 
     return mw.html.create("table")
     return mw.html.create("table")
         :addClass("infobox")
         :addClass("infobox")
         :addClass("alborz-infobox")
         :addClass("alborz-infobox")
         :addClass("infobox-type-" .. norm(rawType))
         :addClass("infobox-type-" .. typeKey)
end
end


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


     root:tag("tr")
     root:tag("tr")
خط ۱۷۶: خط ۲۲۵:
     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 = getArg(args, field)
             if (not value or value == "") and field.default then
             if (not value or value == "") and field.default then
                 value = field.default
                 value = field.default
             end
             end
             if value and value ~= "" then
             if value and value ~= "" then
                 addRow(root, field.label, value)
                 addRow(root, field.label, value)
خط ۱۸۹: خط ۲۴۱:


-------------------------------------------------------
-------------------------------------------------------
-- RENDER (اصلاح شده)
-- RENDER
-------------------------------------------------------
-------------------------------------------------------
local function render(args)
local function render(args)
     local rawTypeInput = norm(args["نوع"]) or "city"
     local rawTypeInput = norm(args["نوع"]) or "city"
   
 
     -- تبدیل به کلید داخلی
     -- تبدیل مقدار واردشده به کلید داخلی
     local internalType = data.typeMap[rawTypeInput] or rawTypeInput
     local internalType = data.typeMap[rawTypeInput] or rawTypeInput
   
 
     local layout = data.layouts[internalType] or data.layouts.city
     local layout = data.layouts[internalType] or data.layouts.city


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


     addTitle(root, args)
     addTitle(root, args)
     addBadge(root, rawTypeInput)
 
    -- استفاده از کلید داخلی برای Badge
     addBadge(root, internalType)
 
     addImage(root, args)
     addImage(root, args)
     addCaption(root, args)
     addCaption(root, args)
خط ۲۱۰: خط ۲۶۷:
end
end


-------------------------------------------------------
-- MAIN
-------------------------------------------------------
function p.main(frame)
function p.main(frame)
     local args = getArgs(frame)
     local args = getArgs(frame)

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

توضیحات این پودمان می‌تواند در پودمان: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

    local candidates = {}

    if field.arg then
        table.insert(candidates, field.arg)
    end

    if field.args then
        for _, a in ipairs(field.args) do
            table.insert(candidates, a)
        end
    end

    for _, name in ipairs(candidates) do
        local value = args[norm(name)]
        if value and value ~= "" then
            return value
        end
    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)

    local td = tr:tag("td")

    local isWikitext =
        value:find("%[%[.-%]%]") or
        value:find("{{.-}}") or
        value:find("<[^>]+>") or
        value:find("^%*") or
        value:find("^#") or
        value:find("\n")

    if isWikitext 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, typeKey)
    if not typeKey then
        return
    end

    local label = badgeMap[typeKey] or "مکان"

    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 or image == "" then
        return
    end

    image = mw.text.trim(image)

    image = mw.ustring.gsub(image, "^پرونده:", "")
    image = mw.ustring.gsub(image, "^File:", "")

    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("عنوان تصویر")]
        or args[norm("زیرنویس")]

    if not caption or 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(typeKey)
    typeKey = typeKey or "city"

    return mw.html.create("table")
        :addClass("infobox")
        :addClass("alborz-infobox")
        :addClass("infobox-type-" .. typeKey)
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

-------------------------------------------------------
-- ADD 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 rawTypeInput = norm(args["نوع"]) or "city"

    -- تبدیل مقدار واردشده به کلید داخلی
    local internalType = data.typeMap[rawTypeInput] or rawTypeInput

    local layout = data.layouts[internalType] or data.layouts.city

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

    addTitle(root, args)

    -- استفاده از کلید داخلی برای Badge
    addBadge(root, internalType)

    addImage(root, args)
    addCaption(root, args)
    addRows(root, args, layout)

    return tostring(root)
end

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

return p