پرش به محتوا

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

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


--------------------------------------------------------------------------------
-------------------------------------------------------
-- NORMALIZER
-- NORMALIZE
--------------------------------------------------------------------------------
-------------------------------------------------------


local function norm(s)
local function norm(s)
خط ۱۴: خط ۱۴:
end
end


--------------------------------------------------------------------------------
-------------------------------------------------------
-- GET ARGS
-- ARGS
--------------------------------------------------------------------------------
-------------------------------------------------------


local function getArgs(frame)
local function getArgs(frame)
خط ۳۹: خط ۳۹:
end
end


--------------------------------------------------------------------------------
-------------------------------------------------------
-- RENDER
-- RENDER
--------------------------------------------------------------------------------
-------------------------------------------------------


local function render(args)
local function render(args)


     -- FIX مهم: تشخیص نوع به شکل مقاوم
     -- 🔴 DEBUG SAFE TYPE
     local t = args[norm("نوع")] or args["نوع"] or "city"
     local rawType = args[norm("نوع")] or args["نوع"] or "city"
     local layout = data.layouts[t] or data.layouts.city
 
    -- اگر اشتباه تایپ شد، fallback
     local layout = data.layouts[rawType] or data.layouts.city


     local root = mw.html.create("table")
     local root = mw.html.create("table")
         :addClass("infobox alborz-infobox")
         :addClass("infobox alborz-infobox")


     --------------------------------------------------------------------
     ---------------------------------------------------
     -- TITLE
     -- TITLE
     --------------------------------------------------------------------
     ---------------------------------------------------


     local title = args["نام"] or mw.title.getCurrentTitle().text
     local title = args["نام"] or mw.title.getCurrentTitle().text
خط ۶۳: خط ۶۵:
             :wikitext(mw.text.encode(title))
             :wikitext(mw.text.encode(title))


     --------------------------------------------------------------------
     ---------------------------------------------------
     -- IMAGE
     -- IMAGE
     --------------------------------------------------------------------
     ---------------------------------------------------


     if args["تصویر"] and args["تصویر"] ~= "" then
     if args["تصویر"] and args["تصویر"] ~= "" then
خط ۷۴: خط ۷۶:
     end
     end


     --------------------------------------------------------------------
     ---------------------------------------------------
     -- IMAGE CAPTION (اصلاح مهم)
     -- CAPTION
     --------------------------------------------------------------------
     ---------------------------------------------------


     if args["توضیح تصویر"] and args["توضیح تصویر"] ~= "" then
     if args["توضیح تصویر"] and args["توضیح تصویر"] ~= "" then
خط ۸۷: خط ۸۹:
     end
     end


     --------------------------------------------------------------------
     ---------------------------------------------------
     -- FIELDS
    -- 🔴 FIX اصلی مشکل تو (حتماً اجرا می‌شود)
    --------------------------------------------------------------------
    ---------------------------------------------------
 
     if not layout or type(layout) ~= "table" then
        layout = data.layouts.city
    end
 
    ---------------------------------------------------
    -- FIELDS LOOP
    ---------------------------------------------------


     for i = 1, #layout do
     for i = 1, #layout do


         local field = data.fields[layout[i]]
        local key = layout[i]
         local field = data.fields[key]


         if field then
         if field then
خط ۱۰۹: خط ۱۲۰:
                 local tr = root:tag("tr")
                 local tr = root:tag("tr")


                 tr:tag("th")
                 tr:tag("th"):wikitext(field.label)
                    :wikitext(field.label)
                 tr:tag("td"):wikitext(mw.text.encode(value))
 
                 tr:tag("td")
                    :wikitext(mw.text.encode(value))
             end
             end
         end
         end
خط ۱۲۱: خط ۱۲۹:
end
end


--------------------------------------------------------------------------------
-------------------------------------------------------
-- MAIN
-- MAIN
--------------------------------------------------------------------------------
-------------------------------------------------------


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


return p
return p

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

توضیحات این پودمان می‌تواند در پودمان: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, "[‌ %s]", "")
    return s
end

-------------------------------------------------------
-- ARGS
-------------------------------------------------------

local function getArgs(frame)
    local args = {}

    for k, v in pairs(frame.args or {}) do
        if v and v ~= "" then
            args[norm(k)] = v
        end
    end

    local parent = frame:getParent()
    if parent then
        for k, v in pairs(parent.args or {}) do
            if v and v ~= "" then
                args[norm(k)] = v
            end
        end
    end

    return args
end

-------------------------------------------------------
-- RENDER
-------------------------------------------------------

local function render(args)

    -- 🔴 DEBUG SAFE TYPE
    local rawType = args[norm("نوع")] or args["نوع"] or "city"

    -- اگر اشتباه تایپ شد، fallback
    local layout = data.layouts[rawType] or data.layouts.city

    local root = mw.html.create("table")
        :addClass("infobox alborz-infobox")

    ---------------------------------------------------
    -- TITLE
    ---------------------------------------------------

    local title = args["نام"] or mw.title.getCurrentTitle().text

    root:tag("tr")
        :tag("th")
            :attr("colspan", "2")
            :wikitext(mw.text.encode(title))

    ---------------------------------------------------
    -- IMAGE
    ---------------------------------------------------

    if args["تصویر"] and args["تصویر"] ~= "" then
        root:tag("tr")
            :tag("td")
                :attr("colspan", "2")
                :wikitext("[[پرونده:" .. args["تصویر"] .. "|250px]]")
    end

    ---------------------------------------------------
    -- CAPTION
    ---------------------------------------------------

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

    ---------------------------------------------------
    -- 🔴 FIX اصلی مشکل تو (حتماً اجرا می‌شود)
    ---------------------------------------------------

    if not layout or type(layout) ~= "table" then
        layout = data.layouts.city
    end

    ---------------------------------------------------
    -- FIELDS LOOP
    ---------------------------------------------------

    for i = 1, #layout do

        local key = layout[i]
        local field = data.fields[key]

        if field then
            local value

            if field.arg then
                value = args[norm(field.arg)] or args[field.arg]
            end

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

            if value and value ~= "" then
                local tr = root:tag("tr")

                tr:tag("th"):wikitext(field.label)
                tr:tag("td"):wikitext(mw.text.encode(value))
            end
        end
    end

    return tostring(root)
end

-------------------------------------------------------
-- MAIN
-------------------------------------------------------

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

return p