Modul:Citation/CS1

Izvor: Hrvatska internetska enciklopedija
Inačica 640272 od 12. siječanj 2026. u 09:03 koju je unio WikiSysop (razgovor | doprinosi)
(razl) ←Starija inačica | vidi trenutačnu inačicu (razl) | Novija inačica→ (razl)
Prijeđi na navigaciju Prijeđi na pretraživanje
Dokumentacija modula
Ovaj modul i njegovi pod-moduli podržavaju CS1 i CS2 stil citiranja. Ovaj modul nije namijenjen za direktno pozivanje, već ga je potrebno pozvati iz predloška koji ga koristi.

<section begin=module_components_table /> Pod-moduli navedeni niže osiguravaju funkcionalnost predložaka za for CS1/2 stil citiranja:

CS1 / CS2 moduli
Pod-modul Opis
Gold padlock Modul:Citation/CS1 Odgovoran za prikazivanje i druge podržane funkcije
Modul:Citation/CS1/Configuration Tablice prijevoda; Rukovanje pogreškama i identifikatorima
Modul:Citation/CS1/Whitelist Popis aktivnih i zastarjelih parametara za citiranje
Modul:Citation/CS1/Date validation Rukovanje oblicima datuma i validacija njihove točnosti
Modul:Citation/CS1/Identifiers Funkcije za rukovanje imenovanim identifikatorima (ISBN, DOI, PMID, i sl.)
Modul:Citation/CS1/Utilities Često korištene tablice i funkcije
Modul:Citation/CS1/COinS Funkcije koje stvaraju meta-podatke o citiranom CS1/2 izvoru
Modul:Citation/CS1/styles.css CSS stilovi primijenjeni na predloške za citiranje
Pogreška pri izradbi sličice: Modul:Citation/CS1/Suggestions Popias koji mapira česte netočno navedene parametre i dodjeljuje im ispravna imena
<section end=module_components_table />


-- Modul:Citation/CS1
-- Hrvatska, stabilna i pojednostavljena verzija CS1 sustava

require('strict')

local p = {}

local cfg = require('Module:Citation/CS1/Configuration')
local util = require('Module:Citation/CS1/Utilities')
local ids  = require('Module:Citation/CS1/Identifiers')
local whitelist = require('Module:Citation/CS1/Whitelist')
local dateval = require('Module:Citation/CS1/Date_validation')

--------------------------------------------------------------------------------
-- POMOĆNE FUNKCIJE
--------------------------------------------------------------------------------

local function getArgs(frame)
    return require('Module:Arguments').getArgs(frame)
end

local function clean(s)
    if not s then return nil end
    return mw.text.trim(s):gsub('[%.,;:]+$', '')
end

local function italic(s)
    if not util.is_set(s) then return nil end
    return "''" .. clean(s) .. "''"
end

local function quoted(s)
    if not util.is_set(s) then return nil end
    return '„' .. clean(s) .. '“'
end

local function sentence(parts)
    local line = util.join(parts, ' ')
    if not line or line == '' then return nil end
    if not line:match('[%.%!%?]$') then
        line = line .. '.'
    end
    return line
end

local function normalizeClass(args, frame)
    local cls = args.CitationClass or args.type
    if cls then
        cls = mw.text.trim(cls):lower()
    else
        local title = frame:getParent() and frame:getParent():getTitle() or frame:getTitle()
        title = mw.ustring.lower(title or '')
        if title:find('cite web', 1, true) then cls = 'web'
        elseif title:find('cite book', 1, true) then cls = 'book'
        elseif title:find('cite journal', 1, true) then cls = 'journal'
        else cls = 'web'
        end
    end
    if cls ~= 'web' and cls ~= 'book' and cls ~= 'journal' then
        cls = 'web'
    end
    return cls
end

--------------------------------------------------------------------------------
-- VALIDACIJA PARAMETARA (CS1-like)
--------------------------------------------------------------------------------

local function validateParams(args, cls)
    local errors = {}

    -- Nepoznati parametri
    for k, v in pairs(args) do
        if type(k) == 'string' and not whitelist.params[k] then
            table.insert(errors, "Nepoznat parametar: <code>" .. k .. "</code>")
        end
    end

    -- Obavezni parametri po klasi
    for _, req in ipairs(cfg.required_params[cls] or {}) do
        if not util.is_set(args[req]) then
            table.insert(errors, "Nedostaje obavezni parametar: <code>" .. req .. "</code>")
        end
    end

    -- Parametri koji ne pripadaju klasi
    local allowed = {}
    for _, p in ipairs(cfg.class_params[cls] or {}) do
        allowed[p] = true
    end

    for k, v in pairs(args) do
        if type(k) == 'string' and whitelist.params[k] and not allowed[k] then
            table.insert(errors, "Parametar <code>" .. k .. "</code> nije dopušten u klasi <code>" .. cls .. "</code>")
        end
    end

    -- Bare URL
    if util.is_set(args.url) and not util.is_set(args.title) then
        table.insert(errors, "Bare URL: nedostaje parametar <code>title</code>")
    end

    -- Prazni parametri
    for k, v in pairs(args) do
        if type(k) == 'string' and type(v) == 'string' and mw.text.trim(v) == '' then
            table.insert(errors, "Prazan parametar: <code>" .. k .. "</code>")
        end
    end

    return errors
end

--------------------------------------------------------------------------------
-- FORMATIRANJE IDENTIFIKATORA
--------------------------------------------------------------------------------

local function formatIds(args)
    local out = {}
    for id, _ in pairs(cfg.id_handlers or {}) do
        local val = args[id:lower()] or args[id]
        if util.is_set(val) then
            table.insert(out, ids.format(id, val))
        end
    end
    return util.join(out, ' · ')
end

--------------------------------------------------------------------------------
-- FORMATIRANJE AUTORA
--------------------------------------------------------------------------------

local function formatAuthors(args)
    local list = {}

    if util.is_set(args.author) then
        table.insert(list, args.author)
    end

    for i = 1, 9 do
        local a = args['author' .. i]
        if util.is_set(a) then
            table.insert(list, a)
        end
    end

    if #list == 0 then return nil end
    if #list == 1 then return list[1] end
    if #list == 2 then return list[1] .. ' i ' .. list[2] end

    local last = table.remove(list)
    return table.concat(list, ', ') .. ' i ' .. last
end

--------------------------------------------------------------------------------
-- CITATION: WEB
--------------------------------------------------------------------------------

local function citeWeb(args)
    local parts = {}

    table.insert(parts, sentence({
        formatAuthors(args),
        args.date and '(' .. args.date .. ')' or nil,
        italic(args.title)
    }))

    table.insert(parts, sentence({
        italic(args.work),
        args.publisher,
        args.language and ('Na jeziku: ' .. args.language) or nil,
        formatIds(args)
    }))

    if util.is_set(args.url) then
        local url = '[' .. args.url .. ' ' .. args.url .. ']'
        if util.is_set(args['access-date']) then
            url = url .. ' (pristupljeno ' .. args['access-date'] .. ')'
        end
        table.insert(parts, sentence({url}))
    end

    if util.is_set(args.quote) then
        table.insert(parts, sentence({quoted(args.quote)}))
    end

    return util.join(parts, ' ')
end

--------------------------------------------------------------------------------
-- CITATION: BOOK
--------------------------------------------------------------------------------

local function citeBook(args)
    local parts = {}

    table.insert(parts, sentence({
        formatAuthors(args),
        args.year and '(' .. args.year .. ')' or nil,
        italic(args.title)
    }))

    table.insert(parts, sentence({
        args.editor and ('Uredio: ' .. args.editor) or nil,
        args.edition and (args.edition .. ' izd.') or nil,
        util.join({args.location, args.publisher}, ': '),
        args.language and ('Na jeziku: ' .. args.language) or nil,
        formatIds(args)
    }))

    if util.is_set(args.url) then
        local url = '[' .. args.url .. ' ' .. args.url .. ']'
        if util.is_set(args['access-date']) then
            url = url .. ' (pristupljeno ' .. args['access-date'] .. ')'
        end
        table.insert(parts, sentence({url}))
    end

    if util.is_set(args.quote) then
        table.insert(parts, sentence({quoted(args.quote)}))
    end

    return util.join(parts, ' ')
end

--------------------------------------------------------------------------------
-- CITATION: JOURNAL
--------------------------------------------------------------------------------

local function citeJournal(args)
    local parts = {}

    table.insert(parts, sentence({
        formatAuthors(args),
        args.year and '(' .. args.year .. ')' or nil,
        quoted(args.title)
    }))

    table.insert(parts, sentence({
        italic(args.journal),
        util.join({args.volume, args.issue and '(' .. args.issue .. ')' or nil}, ' '),
        args.pages and ('str. ' .. args.pages) or nil,
        args.language and ('Na jeziku: ' .. args.language) or nil,
        formatIds(args)
    }))

    if util.is_set(args.url) then
        local url = '[' .. args.url .. ' ' .. args.url .. ']'
        if util.is_set(args['access-date']) then
            url = url .. ' (pristupljeno ' .. args['access-date'] .. ')'
        end
        table.insert(parts, sentence({url}))
    end

    if util.is_set(args.quote) then
        table.insert(parts, sentence({quoted(args.quote)}))
    end

    return util.join(parts, ' ')
end

--------------------------------------------------------------------------------
-- GLAVNA FUNKCIJA
--------------------------------------------------------------------------------

function p.citation(frame)
    local args = getArgs(frame)
    local cls = normalizeClass(args, frame)

    -- Validacija
    local errors = validateParams(args, cls)
    if #errors > 0 then
        local out = '<div class="citation-errors" style="color:#b00; font-size:90%;">'
        for _, e in ipairs(errors) do
            out = out .. '• ' .. e .. '<br>'
        end
        out = out .. '</div>'
        return out
    end

    if cls == 'book' then
        return citeBook(args)
    elseif cls == 'journal' then
        return citeJournal(args)
    else
        return citeWeb(args)
    end
end

-- Alias za #invoke: ... |main
p.main = p.citation

return p