Modul:Harvard

Izvor: Hrvatska internetska enciklopedija
Inačica 640285 od 12. siječanj 2026. u 10:15 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

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

-- Modul:Harvard
-- Ujednačen stil s CS1 modulom (autori, godine, stranice, CITEREF)

require('strict')

local util = require('Module:Citation/CS1/Utilities')

local m = {}

local cfg = {
    cat_error = '[[Kategorija:Greške u citiranju]]',
    max_params = 5,
    min_positional = 2,
}

local function trim(s)
    return util.trim and util.trim(s) or (mw.text.trim(s or ''):gsub('%.$',''))
end

local function is_set(v)
    return v ~= nil and trim(v) ~= ''
end

local function error_tag(msg)
    return '<span class="citation-error" style="color:#b00;">' .. msg .. '</span>'
end

local function validate(args)
    local errors = {}

    local count = 0
    for i = 1, cfg.max_params do
        if is_set(args['P'..i]) then count = count + 1 end
    end

    if count < cfg.min_positional then
        table.insert(errors, 'Premalo parametara: potrebno je barem ime autora i godina.')
    end

    return errors
end

local function format_errors(errors)
    if #errors == 0 then return '' end
    local out = {}
    for _, e in ipairs(errors) do
        table.insert(out, '• ' .. e)
    end
    return error_tag(table.concat(out, '<br>')) .. cfg.cat_error
end

local function core(args)
    -- 1) AUTORI I GODINA
    local authors = {}
    if is_set(args.P1) then table.insert(authors, args.P1) end
    if is_set(args.P2) and not tonumber(args.P2) then table.insert(authors, args.P2) end
    if is_set(args.P3) and not tonumber(args.P3) then table.insert(authors, args.P3) end

    local year = nil
    for i = 1, 5 do
        if tonumber(args['P'..i]) then
            year = args['P'..i]
        end
    end

    local author_text = util.format_authors(authors)
    local result = author_text or ''

    if year then
        result = result .. ' ' .. args.bracket_year_left .. year .. args.bracket_year_right
    end

    -- 2) CITEREF logika – SAMO ako imamo barem autora I godinu
    if args.ref ~= 'none' then
        local have_key = (#authors > 0) and year ~= nil

        if is_set(args.ref) then
            -- ručno zadani ref uvijek je dozvoljen (ali anchor ne smije biti nil)
            local anchor = mw.uri.anchorEncode(args.ref or '')
            result = string.format('[[#%s|%s]]', anchor, result)
        elseif have_key then
            -- automatski CITEREF ključ, ali samo ako ima autora i godina
            local key = util.citeref_key(authors, year) or ''
            local anchor = mw.uri.anchorEncode(key)
            result = string.format('[[#CITEREF%s|%s]]', anchor, result)
        end
    end

    -- 3) stranice
    if is_set(args.page) then
        result = result .. args.page_sep .. args.page
    elseif is_set(args.pages) then
        result = result .. args.pages_sep .. args.pages
    end

    -- 4) lokacija
    if is_set(args.location) then
        result = result .. ', ' .. args.location
    end

    return args.bracket_left .. result .. args.bracket_right .. (args.postscript or '')
end


local function defaults()
    return {
        bracket_left = '',
        bracket_right = '',
        bracket_year_left = '',
        bracket_year_right = '',
        postscript = '',
        page = '',
        pages = '',
        location = '',
        page_sep = ', str.&nbsp;',
        pages_sep = ', str.&nbsp;',
        ref = '',
        P1 = '',
        P2 = '',
        P3 = '',
        P4 = '',
        P5 = '',
    }
end

function m.harvard_citation(frame)
    local pframe = frame:getParent()
    local args = defaults()

    args.bracket_left = '('
    args.bracket_right = ')'

    args.page = pframe.args.p or pframe.args.page or ''
    args.pages = pframe.args.pp or pframe.args.pages or ''
    args.location = pframe.args.loc or ''
    args.ref = pframe.args.ref or pframe.args.Ref or ''

    for i = 1, 5 do
        args['P'..i] = trim(pframe.args[i])
    end

    local errors = validate(args)
    return format_errors(errors) .. core(args)
end

function m.harvard_citation_no_bracket(frame)
    local pframe = frame:getParent()
    local args = defaults()

    args.page = pframe.args.p or pframe.args.page or ''
    args.pages = pframe.args.pp or pframe.args.pages or ''
    args.location = pframe.args.loc or ''
    args.ref = pframe.args.ref or pframe.args.Ref or ''

    for i = 1, 5 do
        args['P'..i] = trim(pframe.args[i])
    end

    local errors = validate(args)
    return format_errors(errors) .. core(args)
end

function m.sfn(frame)
    local pframe = frame:getParent()
    local args = defaults()

    for k, v in pairs(frame.args) do
        args[k] = v
    end

    args.postscript = pframe.args.postscript or pframe.args.ps or '.'
    if args.postscript == 'none' then args.postscript = '' end

    args.page = pframe.args.p or pframe.args.page or ''
    args.pages = pframe.args.pp or pframe.args.pages or ''
    args.location = pframe.args.loc or ''
    args.ref = pframe.args.ref or pframe.args.Ref or ''

    for i = 1, 5 do
        args['P'..i] = trim(pframe.args[i])
    end

    local errors = validate(args)
    local base = core(args)

    local authors = {}
    if is_set(args.P1) then table.insert(authors, args.P1) end
    if is_set(args.P2) and not tonumber(args.P2) then table.insert(authors, args.P2) end
    if is_set(args.P3) and not tonumber(args.P3) then table.insert(authors, args.P3) end

    local year = nil
    for i = 1, 5 do
        if tonumber(args['P'..i]) then year = args['P'..i] end
    end

    local key = util.citeref_key(authors, year)

    local ref = frame:extensionTag{
        name = 'ref',
        args = { name = 'FOOTNOTE' .. key .. args.page .. args.pages .. args.location },
        content = base
    }

    return format_errors(errors) .. ref
end

m.main = m.sfn

return m