پرش به محتوا

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

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
 
(۴ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد)
خط ۱: خط ۱:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Module:Citation
-- Module:Citation
-- Simple Citation System (Wiki Alborz)
-- Wiki Alborz Lightweight Citation System
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local args = require("Module:Arguments")
local args = require("Module:Arguments")
local cfg = require("Module:Citation/Configuration")
local cfg = require("Module:Citation/Configuration")
local U   = require("Module:Citation/Utilities")
local U = require("Module:Citation/Utilities")
local F   = require("Module:Citation/Formatter")
local F = require("Module:Citation/Formatter")
 
local Citation = {}
local Citation = {}


خط ۱۵: خط ۱۳:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Citation.getArgs(frame)
function Citation.getArgs(frame)
     return args.getArgs(frame, {
     return args.getArgs(frame, { parentOnly = true })
        parentOnly = true
    })
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- گرفتن مقدار با alias ها
-- alias resolver
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Citation.getValue(argTable, key)
function Citation.getValue(argTable, key)
     local aliases = cfg.aliases[key]
     local aliases = cfg.aliases[key]
     if not aliases then
     if not aliases then return nil end
        return nil
    end
 
     for _, name in ipairs(aliases) do
     for _, name in ipairs(aliases) do
         if argTable[name] and argTable[name] ~= "" then
         local v = argTable[name]
            return argTable[name]
        if v and v ~= "" then return v end
        end
     end
     end
     return nil
     return nil
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- نرمال‌سازی داده‌ها
-- normalize input
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Citation.normalize(args)
function Citation.normalize(args)
     local out = {}
     local out = {}
     for key, _ in pairs(cfg.aliases) do
     for key, _ in pairs(cfg.aliases) do
         out[key] = Citation.getValue(args, key)
         out[key] = Citation.getValue(args, key)
     end
     end
     return out
     return out
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ساخت شناسه‌ها (DOI / ISBN / ...)
-- identifiers
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Citation.renderIdentifiers(data)
function Citation.renderIdentifiers(data)
     local parts = {}
     local parts = {}
     for key, label in pairs(cfg.identifiers) do
     for key, label in pairs(cfg.identifiers) do
         if data[key] and data[key] ~= "" then
         if data[key] and data[key] ~= "" then
خط ۶۲: خط ۵۰:
         end
         end
     end
     end
     return table.concat(parts, cfg.separator)
     return table.concat(parts, cfg.separator)
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- رندر اصلی
-- build citation text
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Citation.render(frame, ctype)
function Citation.buildText(data)
 
    local rawArgs = Citation.getArgs(frame)
    local data   = Citation.normalize(rawArgs)
    local layout  = cfg.layouts[ctype] or cfg.layouts.web
 
     local parts = {}
     local parts = {}


     for _, field in ipairs(layout) do
     -- author
    if not U.isEmpty(data.author) then
        table.insert(parts, data.author .. ".")
    end


        ----------------------------------------------------------------------
    -- title
        -- identifiers
    if not U.isEmpty(data.title) then
         ----------------------------------------------------------------------
         table.insert(parts, "''" .. data.title .. "''.")
        if field == "identifiers" then
    end


            local ids = Citation.renderIdentifiers(data)
    -- website
            if not U.isEmpty(ids) then
    if not U.isEmpty(data.website) then
                table.insert(parts, ids)
        table.insert(parts, "در: " .. data.website .. ".")
            end
    end


        ----------------------------------------------------------------------
    -- publisher
        -- URL
    if not U.isEmpty(data.publisher) then
         ----------------------------------------------------------------------
         table.insert(parts, data.publisher .. ".")
        elseif field == "url" then
    end


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


        ----------------------------------------------------------------------
    -- URL
         -- title
    if not U.isEmpty(data.url) then
        ----------------------------------------------------------------------
         table.insert(parts, F.formatLink(data.title, data.url))
        elseif field == "title" then
    end


            local title = F.formatTitle(data.title)
    -- identifiers
            if not U.isEmpty(title) then
    local ids = Citation.renderIdentifiers(data)
                table.insert(parts, title)
    if not U.isEmpty(ids) then
            end
         table.insert(parts, ids)
 
    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


    ----------------------------------------------------------------------
    -- Archive (نسخه کوتاه و زیبا)
    ----------------------------------------------------------------------
    if not U.isEmpty(data.archiveurl) then
        local archivedate = data.archivedate or ""
        local archiveText = "نسخهٔ بایگانی"
       
        if archivedate ~= "" then
            archiveText = archiveText .. " (" .. archivedate .. ")"
         end
         end
       
        local archiveLink = F.formatLink(archiveText, data.archiveurl)
        table.insert(parts, archiveLink)
     end
     end


     local result = table.concat(parts, cfg.separator)
     return table.concat(parts, " ")
 
    return U.cleanEnd(result)
end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- entry points
-- polish output
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function Citation.web(frame)
function Citation.polish(text)
     return Citation.render(frame, "web")
     if not text then return "" end
    text = mw.text.trim(text)
    text = text:gsub("%s+", " ")
    text = text:gsub("%s+%.", ".")
    text = text:gsub("%.+", ".")
    text = text:gsub("%s+%.?$", "")
    return text
end
end


function Citation.book(frame)
--------------------------------------------------------------------------------
     return Citation.render(frame, "book")
-- render main
--------------------------------------------------------------------------------
function Citation.render(frame, ctype)
    local rawArgs = Citation.getArgs(frame)
    local data = Citation.normalize(rawArgs)
    local result = Citation.buildText(data)
     return Citation.polish(result)
end
end


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


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

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

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

--------------------------------------------------------------------------------
-- Module:Citation
-- Wiki Alborz Lightweight Citation System
--------------------------------------------------------------------------------
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 resolver
--------------------------------------------------------------------------------
function Citation.getValue(argTable, key)
    local aliases = cfg.aliases[key]
    if not aliases then return nil end
    for _, name in ipairs(aliases) do
        local v = argTable[name]
        if v and v ~= "" then return v end
    end
    return nil
end

--------------------------------------------------------------------------------
-- normalize input
--------------------------------------------------------------------------------
function Citation.normalize(args)
    local out = {}
    for key, _ in pairs(cfg.aliases) do
        out[key] = Citation.getValue(args, key)
    end
    return out
end

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

--------------------------------------------------------------------------------
-- build citation text
--------------------------------------------------------------------------------
function Citation.buildText(data)
    local parts = {}

    -- author
    if not U.isEmpty(data.author) then
        table.insert(parts, data.author .. ".")
    end

    -- title
    if not U.isEmpty(data.title) then
        table.insert(parts, "''" .. data.title .. "''.")
    end

    -- website
    if not U.isEmpty(data.website) then
        table.insert(parts, "در: " .. data.website .. ".")
    end

    -- publisher
    if not U.isEmpty(data.publisher) then
        table.insert(parts, data.publisher .. ".")
    end

    -- date
    if not U.isEmpty(data.date) then
        table.insert(parts, data.date .. ".")
    end

    -- URL
    if not U.isEmpty(data.url) then
        table.insert(parts, F.formatLink(data.title, data.url))
    end

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

    ----------------------------------------------------------------------
    -- Archive (نسخه کوتاه و زیبا)
    ----------------------------------------------------------------------
    if not U.isEmpty(data.archiveurl) then
        local archivedate = data.archivedate or ""
        local archiveText = "نسخهٔ بایگانی"
        
        if archivedate ~= "" then
            archiveText = archiveText .. " (" .. archivedate .. ")"
        end
        
        local archiveLink = F.formatLink(archiveText, data.archiveurl)
        table.insert(parts, archiveLink)
    end

    return table.concat(parts, " ")
end

--------------------------------------------------------------------------------
-- polish output
--------------------------------------------------------------------------------
function Citation.polish(text)
    if not text then return "" end
    text = mw.text.trim(text)
    text = text:gsub("%s+", " ")
    text = text:gsub("%s+%.", ".")
    text = text:gsub("%.+", ".")
    text = text:gsub("%s+%.?$", "")
    return text
end

--------------------------------------------------------------------------------
-- render main
--------------------------------------------------------------------------------
function Citation.render(frame, ctype)
    local rawArgs = Citation.getArgs(frame)
    local data = Citation.normalize(rawArgs)
    local result = Citation.buildText(data)
    return Citation.polish(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