پرش به محتوا

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

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


local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')
--------------------------------------------------------------------------------
-- DEBUG TOGGLE
--------------------------------------------------------------------------------
local DEBUG = true
local function log(t)
    if DEBUG then
        mw.logObject(t)
    end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۲۰: خط ۹:
local function render(args)
local function render(args)


    log(args)
     local layout = data.layouts[args["نوع"] or "city"]
 
     local layoutName = args["نوع"] or "city"
    local layout = data.layouts[layoutName]
 
     if not layout then
     if not layout then
         layout = data.layouts.city
         layout = data.layouts.city
        log("Fallback to city layout")
    else
        log("Layout: " .. layoutName)
     end
     end


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


     --------------------------------------------------------------------------
     --------------------------------------------------------------------------
خط ۶۸: خط ۵۱:


     --------------------------------------------------------------------------
     --------------------------------------------------------------------------
     -- Rows DEBUG MODE
     -- Rows (FIXED PART)
     --------------------------------------------------------------------------
     --------------------------------------------------------------------------


خط ۷۶: خط ۵۹:
         local field = data.fields[key]
         local field = data.fields[key]


         if not field then
         if field then
            log("Missing field: " .. tostring(key))
        else
            log("Checking field: " .. key)


             local value = nil
             local value = nil


            -- 🔴 FIX اصلی اینجاست:
             if field.arg then
             if field.arg then
                 value = args[field.arg]
                 value = args[field.arg]
            end
            -- fallback مهم (برای جلوگیری از خالی شدن داده‌ها)
            if (not value or value == "") and field.label then
                value = args[field.label]
             end
             end


             if value and value ~= "" then
             if value and value ~= "" then
                log("FOUND VALUE: " .. key .. " = " .. value)


                 local tr = root:tag("tr")
                 local tr = root:tag("tr")


                 tr:tag("th"):wikitext(field.label)
                 tr:tag("th")
                    :wikitext(field.label)


                 tr:tag("td"):wikitext(value)
                 tr:tag("td")
                    :wikitext(value)


            else
                log("EMPTY: " .. key .. " (arg=" .. tostring(field.arg) .. ")")
             end
             end
         end
         end

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

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

local p = {}

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

--------------------------------------------------------------------------------
-- Render
--------------------------------------------------------------------------------

local function render(args)

    local layout = data.layouts[args["نوع"] or "city"]
    if not layout then
        layout = data.layouts.city
    end

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

    --------------------------------------------------------------------------
    -- Title
    --------------------------------------------------------------------------

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

    root:tag("tr")
        :tag("th")
            :attr("colspan","2")
            :wikitext(title)

    --------------------------------------------------------------------------
    -- Image
    --------------------------------------------------------------------------

    if args["تصویر"] and args["تصویر"] ~= "" then

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

        if args["توضیح تصویر"] and args["توضیح تصویر"] ~= "" then

            root:tag("tr")
                :tag("td")
                    :attr("colspan","2")
                    :wikitext(args["توضیح تصویر"])

        end
    end

    --------------------------------------------------------------------------
    -- Rows (FIXED PART)
    --------------------------------------------------------------------------

    for i = 1, #layout do

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

        if field then

            local value = nil

            -- 🔴 FIX اصلی اینجاست:
            if field.arg then
                value = args[field.arg]
            end

            -- fallback مهم (برای جلوگیری از خالی شدن داده‌ها)
            if (not value or value == "") and field.label then
                value = args[field.label]
            end

            if value and value ~= "" then

                local tr = root:tag("tr")

                tr:tag("th")
                    :wikitext(field.label)

                tr:tag("td")
                    :wikitext(value)

            end
        end
    end

    return tostring(root)

end

--------------------------------------------------------------------------------
-- Main
--------------------------------------------------------------------------------

function p.main(frame)

    local args = {}

    local parent = frame:getParent() or frame

    for k, v in pairs(parent.args or {}) do
        if v and v ~= "" then
            args[k] = mw.text.trim(v)
        end
    end

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

    return render(args)

end

return p