پرش به محتوا

پودمان:Citation

از ویکی البرز

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

--------------------------------------------------------------------------------
-- Module:Citation
-- Simple Citation System (Wiki Alborz)
--------------------------------------------------------------------------------

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

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

--------------------------------------------------------------------------------
-- ساخت شناسه‌ها (DOI / ISBN / ...)
--------------------------------------------------------------------------------
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

--------------------------------------------------------------------------------
-- رندر اصلی
--------------------------------------------------------------------------------
function Citation.render(frame, ctype)

    local rawArgs = Citation.getArgs(frame)
    local data    = Citation.normalize(rawArgs)
    local layout  = cfg.layouts[ctype] or cfg.layouts.web

    local parts = {}

    for _, field in ipairs(layout) do

        ----------------------------------------------------------------------
        -- identifiers
        ----------------------------------------------------------------------
        if field == "identifiers" then

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

        ----------------------------------------------------------------------
        -- URL
        ----------------------------------------------------------------------
        elseif field == "url" then

            local url = F.formatUrl(data.url)
            if not U.isEmpty(url) then
                table.insert(parts, url)
            end

        ----------------------------------------------------------------------
        -- title
        ----------------------------------------------------------------------
        elseif field == "title" then

            local title = F.formatTitle(data.title)
            if not U.isEmpty(title) then
                table.insert(parts, title)
            end

        ----------------------------------------------------------------------
        -- archive
        ----------------------------------------------------------------------
        elseif field == "archive" then

            local arch = F.formatArchive(data.archiveurl, data.archivedate)
            if not U.isEmpty(arch) then
                table.insert(parts, arch)
            end

        ----------------------------------------------------------------------
        -- سایر فیلدها
        ----------------------------------------------------------------------
        else

            local value = data[field]

            if not U.isEmpty(value) then
                table.insert(parts, value)
            end

        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