پرش به محتوا

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

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۱: خط ۱:
local p = {}
local p = {}
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')


خط ۵: خط ۶:
-- NORMALIZE
-- NORMALIZE
-------------------------------------------------------
-------------------------------------------------------
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]", "")
     s = mw.ustring.gsub(s, "[\u200C\u200D]", "")
     s = mw.text.trim(s)
     s = mw.text.trim(s)
     return s
     return s
end
end


-------------------------------------------------------
-------------------------------------------------------
-- GET ARGS
-- ARG HELPERS
-------------------------------------------------------
-------------------------------------------------------
local function getArgs(frame)
local function getArgs(frame)
     local args = {}
     local args = {}
     local function addArgs(argTable)
 
         for k, v in pairs(argTable or {}) do
     local function add(tbl)
        if not tbl then return end
 
         for k, v in pairs(tbl) do
             if v and v ~= "" then
             if v and v ~= "" then
                 local nkey = norm(k)
                 local nk = norm(k)
                 args[nkey] = v
                 args[nk] = mw.text.trim(v)
                 args[k] = v
                 args[k] = mw.text.trim(v)
             end
             end
         end
         end
     end
     end
     addArgs(frame.args)
 
     add(frame.args)
 
     local parent = frame:getParent()
     local parent = frame:getParent()
     if parent then addArgs(parent.args) end
     if parent then
        add(parent.args)
    end
 
     return args
     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
end


-------------------------------------------------------
-------------------------------------------------------
-- ADD ROW
-- TABLE BASE
-------------------------------------------------------
-------------------------------------------------------
local function addRow(root, label, value)
 
     if not value or value == "" then return end
local function newTable(rawType)
    local tr = root:tag("tr")
     return mw.html.create("table")
    tr:tag("th"):wikitext(label)
        :addClass("infobox")
    tr:tag("td"):wikitext(mw.text.encode(value))
        :addClass("alborz-infobox")
        :addClass("infobox-type-" .. (rawType or "generic"))
end
end


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


    -- نرمالایز اضافی برای badge
     rawType = norm(rawType)
     rawType = norm(rawType)
    local badgeMap = {
        city = "شهر",
        village = "روستا",
        county = "شهرستان",
        district = "بخش",
        ruraldistrict = "دهستان",
        mountain = "کوه",
        river = "رودخانه",
        waterfall = "آبشار",
        cave = "غار",
        park = "پارک",
    }
     local label = badgeMap[rawType] or rawType
     local label = badgeMap[rawType] or rawType


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


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


     local root = mw.html.create("table")
local function addImage(root, args)
        :addClass("infobox alborz-infobox")
     local image = getArg(args, "تصویر", "پرونده", "image")
         :addClass("infobox-type-" .. (rawType ~= "" and rawType or "city"))
 
    if not image then
         return
    end
 
    image = mw.text.trim(image)


    ---------------------------------------------------
    -- TITLE
    ---------------------------------------------------
    local name = args[norm("نام")] or mw.title.getCurrentTitle().text
     root:tag("tr")
     root:tag("tr")
         :tag("th")
         :tag("td")
             :attr("colspan", "2")
             :attr("colspan", "2")
             :css("text-align", "center")
             :css("text-align", "center")
             :css("font-size", "115%")
             :wikitext("[[پرونده:" .. image .. "|250px]]")
            :wikitext(mw.text.encode(name))
end
 
-------------------------------------------------------
-- CAPTION
-------------------------------------------------------


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


    ---------------------------------------------------
     if not caption then
    -- IMAGE
         return
    ---------------------------------------------------
    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
     end


     ---------------------------------------------------
     root:tag("tr")
    -- CAPTION
        :tag("td")
    ---------------------------------------------------
            :attr("colspan", "2")
    local caption = args[norm("توضیح تصویر")]
            :css("text-align", "center")
     if caption and caption ~= "" then
            :css("font-size", "90%")
         root:tag("tr")
            :css("padding", "3px 0 6px 0")
            :tag("td")
            :wikitext(mw.text.encode(caption))
                :attr("colspan", "2")
end
                :css("font-size", "90%")
 
                :css("text-align", "center")
-------------------------------------------------------
                :wikitext(mw.text.encode(caption))
-- ROW
-------------------------------------------------------
 
local function addRow(root, label, value)
     if not value or value == "" then
         return
     end
     end


     ---------------------------------------------------
     local tr = root:tag("tr")
    -- FIELDS
 
    ---------------------------------------------------
    tr:tag("th")
        :wikitext(label)
 
    tr:tag("td")
        :wikitext(value)
end
-------------------------------------------------------
-- ROWS
-------------------------------------------------------
 
local function addRows(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 argKey = norm(field.arg)
 
             local value = args[argKey] or args[field.arg]
             local value =
                getArg(args, field.arg)


             if (not value or value == "") and field.default then
             if (not value or value == "") and field.default then
خط ۱۴۹: خط ۲۱۵:
             end
             end


             addRow(root, field.label, value)
             if value and value ~= "" then
                addRow(root, field.label, value)
            end
 
         end
         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)
     return tostring(root)
end
end
-------------------------------------------------------
-- MAIN
-------------------------------------------------------


function p.main(frame)
function p.main(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

-------------------------------------------------------
-- 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