پرش به محتوا

پودمان:Citation

از ویکی البرز
نسخهٔ تاریخ ‏۳۰ ژوئن ۲۰۲۶، ساعت ۲۰:۲۹ توسط Modir (بحث | مشارکت‌ها) (صفحه‌ای تازه حاوی «-------------------------------------------------------------------------------- -- Module:Citation -- هسته اصلی سامانه یادکرد ساده -------------------------------------------------------------------------------- local args = require("Module:Arguments") local cfg = require("Module:Citation/Configuration") local U = require("Module:Citation/Utilities") local Citation = {} ----------------------------------...» ایجاد کرد)
(تفاوت) → نسخهٔ قدیمی‌تر | نمایش نسخهٔ فعلی (تفاوت) | نسخهٔ جدیدتر ← (تفاوت)

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

--------------------------------------------------------------------------------
-- Module:Citation
-- هسته اصلی سامانه یادکرد ساده
--------------------------------------------------------------------------------

local args = require("Module:Arguments")
local cfg  = require("Module:Citation/Configuration")
local U    = require("Module:Citation/Utilities")

local Citation = {}

--------------------------------------------------------------------------------
-- گرفتن ورودی‌ها
--------------------------------------------------------------------------------
function Citation.getArgs(frame)
    return args.getArgs(frame, {
        parentOnly = true
    })
end

--------------------------------------------------------------------------------
-- پیدا کردن مقدار با استفاده از alias ها
--------------------------------------------------------------------------------
function Citation.getValue(argTable, key)
    local aliases = cfg.aliases[key]
    if not aliases then
        return nil
    end

    for _, name in ipairs(aliases) do
        if argTable[name] and argTable[name] ~= "" then
            return argTable[name]
        end
    end

    return nil
end

--------------------------------------------------------------------------------
-- نرمال‌سازی پارامترها
--------------------------------------------------------------------------------
function Citation.normalize(args)
    local out = {}

    for key, _ in pairs(cfg.aliases) do
        out[key] = Citation.getValue(args, key)
    end

    return out
end

--------------------------------------------------------------------------------
-- انتخاب layout بر اساس نوع
--------------------------------------------------------------------------------
function Citation.getLayout(citationType)
    return cfg.layouts[citationType] or cfg.layouts.web
end

--------------------------------------------------------------------------------
-- ساخت بخش identifiers (DOI, ISBN, etc.)
--------------------------------------------------------------------------------
function Citation.renderIdentifiers(data)
    local parts = {}

    for key, label in pairs(cfg.identifiers) do
        if data[key] and data[key] ~= "" then
            table.insert(parts, label .. " " .. data[key])
        end
    end

    return table.concat(parts, cfg.separator)
end

--------------------------------------------------------------------------------
-- رندر ساده یک citation item
--------------------------------------------------------------------------------
function Citation.renderItem(label, value)
    if U.isEmpty(value) then
        return ""
    end
    return label .. value
end

--------------------------------------------------------------------------------
-- رندر نهایی citation
--------------------------------------------------------------------------------
function Citation.render(frame, ctype)
    local rawArgs = Citation.getArgs(frame)
    local data    = Citation.normalize(rawArgs)

    local layout  = Citation.getLayout(ctype)

    local parts = {}

    for _, field in ipairs(layout) do

        if field == "identifiers" then
            local ids = Citation.renderIdentifiers(data)
            if not U.isEmpty(ids) then
                table.insert(parts, ids)
            end

        elseif data[field] then
            table.insert(parts, data[field])
        end
    end

    local result = table.concat(parts, cfg.separator)

    return U.cleanEnd(result)
end

--------------------------------------------------------------------------------
-- entry points
--------------------------------------------------------------------------------
function Citation.web(frame)
    return Citation.render(frame, "web")
end

function Citation.book(frame)
    return Citation.render(frame, "book")
end

function Citation.news(frame)
    return Citation.render(frame, "news")
end

function Citation.journal(frame)
    return Citation.render(frame, "journal")
end

--------------------------------------------------------------------------------
return Citation