Modul:Citiranje

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje

Script error: The function "nonexistent" does not exist.

local p = {}

--------------------------------------------------------------------
-- Utility
--------------------------------------------------------------------

local function safe(v)
    if not v or v == "" then return nil end
    return mw.text.trim(v)
end

local function join(list)
    return table.concat(list, "")
end

--------------------------------------------------------------------
-- Autor formatiranje (više autora)
--------------------------------------------------------------------

local function formatAuthors(args)
    local authors = {}

    -- Ako postoji "author", koristi ga
    if safe(args.author) then
        table.insert(authors, args.author)
    end

    -- Ako postoji last/first
    if safe(args.last) then
        local name = args.last
        if safe(args.first) then
            name = name .. ", " .. args.first
        end
        table.insert(authors, name)
    end

    -- Author1, Author2, Author3...
    local i = 1
    while args["author" .. i] do
        local a = safe(args["author" .. i])
        if a then table.insert(authors, a) end
        i = i + 1
    end

    -- Ako postoji authorlink
    if safe(args.authorlink) and authors[1] then
        authors[1] = string.format("[[%s|%s]]", args.authorlink, authors[1])
    end

    if #authors == 0 then return nil end

    return table.concat(authors, "; ")
end

--------------------------------------------------------------------
-- Identifikatori (DOI, ISSN, OCLC, PMID)
--------------------------------------------------------------------

local function formatIDs(args)
    local out = {}

    if safe(args.doi) then
        table.insert(out, " DOI: " .. args.doi)
    end
    if safe(args.issn) then
        table.insert(out, " ISSN " .. args.issn)
    end
    if safe(args.oclc) then
        table.insert(out, " OCLC " .. args.oclc)
    end
    if safe(args.pmid) then
        table.insert(out, " PMID " .. args.pmid)
    end

    if #out == 0 then return nil end
    return table.concat(out, ";")
end

--------------------------------------------------------------------
-- Automatsko generiranje ID-a
--------------------------------------------------------------------

local function slugify(text)
    if not text then return nil end
    text = mw.ustring.lower(text)

    -- uklanjanje dijakritike
    text = mw.ustring.gsub(text, "č", "c")
    text = mw.ustring.gsub(text, "ć", "c")
    text = mw.ustring.gsub(text, "š", "s")
    text = mw.ustring.gsub(text, "ž", "z")
    text = mw.ustring.gsub(text, "đ", "dj")

    -- zamjena razmaka i nedozvoljenih znakova
    text = mw.ustring.gsub(text, "[^a-z0-9]+", "_")

    -- uklanjanje viška podvlaka
    text = mw.ustring.gsub(text, "_+", "_")
    text = mw.ustring.gsub(text, "^_", "")
    text = mw.ustring.gsub(text, "_$", "")

    return text
end

local function autoID(args)
    -- ako korisnik već ima ID → koristi njega
    if safe(args.id) or safe(args.doi) or safe(args.issn)
        or safe(args.oclc) or safe(args.pmid) then
        return nil
    end

    local last = safe(args.last) or safe(args.author)
    local year = safe(args.year)
    local title = safe(args.title) or safe(args.Title)

    if not last and not title then return nil end

    local parts = {}

    if last then table.insert(parts, slugify(last)) end
    if year then table.insert(parts, year) end
    if title then table.insert(parts, slugify(title)) end

    if #parts == 0 then return nil end

    return " ID: " .. table.concat(parts, "-")
end


--------------------------------------------------------------------
-- Datum
--------------------------------------------------------------------

local function formatDate(args)
    if safe(args.date) then
        return " (" .. args.date .. ")"
    end
    if safe(args.year) then
        if safe(args.month) then
            return " (" .. args.month .. " " .. args.year .. ")"
        end
        return " (" .. args.year .. ")"
    end
    return nil
end

--------------------------------------------------------------------
-- Naslov (s URL-om)
--------------------------------------------------------------------

local function formatTitle(args)
    local title = safe(args.title) or safe(args.Title)
    if not title then return nil end

    if safe(args.url) then
        return ". ''[" .. args.url .. " " .. title .. "]''"
    end

    return ". ''" .. title .. "''"
end

--------------------------------------------------------------------
-- Glavni format citata
--------------------------------------------------------------------

local function buildCitation(parts)
    local out = {}

    for _, v in ipairs(parts) do
        if v then table.insert(out, v) end
    end

    table.insert(out, ".") -- završna točka

    return '<cite class="citation" style="font-style:normal">' ..
        table.concat(out, "") ..
        "</cite>"
end

--------------------------------------------------------------------
-- FUNKCIJE ZA POJEDINE VRSTE IZVORA
--------------------------------------------------------------------

-------------------------
-- KNJIGA
-------------------------
function p.knjiga(frame)
    local args = frame:getParent().args
    local out = {}

    table.insert(out, formatAuthors(args))
    table.insert(out, formatDate(args))
    table.insert(out, formatTitle(args))

    if safe(args.edition) then
        table.insert(out, ", " .. args.edition .. ". izd.")
    end

    if safe(args.publisher) then
        if safe(args.location) then
            table.insert(out, ", " .. args.location .. ": " .. args.publisher)
        else
            table.insert(out, ", " .. args.publisher)
        end
    end

    if safe(args.pages) then
        table.insert(out, ", str. " .. args.pages)
    end

    table.insert(out, formatIDs(args))
    table.insert(out, autoID(args))


    return buildCitation(out)
end

-------------------------
-- WEB
-------------------------
function p.web(frame)
    local args = frame:getParent().args
    local out = {}

    table.insert(out, formatAuthors(args))
    table.insert(out, formatDate(args))
    table.insert(out, formatTitle(args))

    if safe(args.website) then
        table.insert(out, ", " .. args.website)
    end

    if safe(args["access-date"]) then
        table.insert(out, " (pristupljeno " .. args["access-date"] .. ")")
    end

    table.insert(out, formatIDs(args))
    table.insert(out, autoID(args))


    return buildCitation(out)
end

-------------------------
-- ČASOPIS
-------------------------
function p.casopis(frame)
    local args = frame:getParent().args
    local out = {}

    table.insert(out, formatAuthors(args))
    table.insert(out, formatDate(args))
    table.insert(out, formatTitle(args))

    if safe(args.journal) then
        table.insert(out, ", ''" .. args.journal .. "''")
    end

    if safe(args.volume) then
        table.insert(out, ", " .. args.volume)
    end

    if safe(args.issue) then
        table.insert(out, "(" .. args.issue .. ")")
    end

    if safe(args.pages) then
        table.insert(out, ", " .. args.pages)
    end

    table.insert(out, formatIDs(args))
    table.insert(out, autoID(args))


    return buildCitation(out)
end

-------------------------
-- NOVINE
-------------------------
function p.novine(frame)
    local args = frame:getParent().args
    local out = {}

    table.insert(out, formatAuthors(args))
    table.insert(out, formatDate(args))
    table.insert(out, formatTitle(args))

    if safe(args.newspaper) then
        table.insert(out, ", " .. args.newspaper)
    end

    table.insert(out, formatIDs(args))
    table.insert(out, autoID(args))


    return buildCitation(out)
end

-------------------------
-- ENCIKLOPEDIJA
-------------------------
function p.enciklopedija(frame)
    local args = frame:getParent().args
    local out = {}

    table.insert(out, formatTitle(args))

    if safe(args.encyclopedia) then
        table.insert(out, ", " .. args.encyclopedia)
    end

    if safe(args["access-date"]) then
        table.insert(out, " (pristupljeno " .. args["access-date"] .. ")")
    end

    table.insert(out, formatIDs(args))
    table.insert(out, autoID(args))


    return buildCitation(out)
end

-------------------------
-- OPĆI IZVOR
-------------------------
function p.izvor(frame)
    local args = frame:getParent().args
    local out = {}

    table.insert(out, formatAuthors(args))
    table.insert(out, formatDate(args))
    table.insert(out, formatTitle(args))
    table.insert(out, formatIDs(args))
    table.insert(out, autoID(args))


    return buildCitation(out)
end

return p