Modul:Harvard: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
Stvorena nova stranica sa sadržajem: »-- Modul:Harvard -- Hrvatska verzija harvardskih citata (sfn, harvard, itd.) -- s osnovnom validacijom i tracking kategorijama require('strict') local mHarv = {} ------------------------------------------------------------------------------- -- KONFIGURACIJA ------------------------------------------------------------------------------- local cfg = { -- skrivena kategorija za probleme cat_error = '[[Kategorija:Stranice s greškama u har...«.
 
Nema sažetka uređivanja
Redak 1: Redak 1:
-- Modul:Harvard
-- Modul:Harvard
-- Hrvatska verzija harvardskih citata (sfn, harvard, itd.)
-- Ujednačen stil s CS1 modulom (autori, godine, stranice, CITEREF)
-- s osnovnom validacijom i tracking kategorijama


require('strict')
require('strict')


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


-------------------------------------------------------------------------------
local m = {}
-- KONFIGURACIJA
-------------------------------------------------------------------------------


local cfg = {
local cfg = {
    -- skrivena kategorija za probleme
     cat_error = '[[Kategorija:Greške u citiranju]]',
     cat_error = '[[Kategorija:Stranice s greškama u harvardskim citatima]]',
 
    -- maksimalan broj pozicijskih parametara (autori + godina)
     max_params = 5,
     max_params = 5,
    -- minimalno: autor + godina
     min_positional = 2,
     min_positional = 2,
}
}


-------------------------------------------------------------------------------
local function trim(s)
-- POMOĆNE FUNKCIJE
     return util.trim and util.trim(s) or (mw.text.trim(s or ''):gsub('%.$',''))
-------------------------------------------------------------------------------
 
local function trim(str)
     if not str then return '' end
    str = mw.text.trim(str)
    -- makni završnu točku da ne dupliramo
    return str:gsub('%.$', '')
end
end


Redak 38: Redak 23:


local function error_tag(msg)
local function error_tag(msg)
     return string.format(
     return '<span class="citation-error" style="color:#b00;">' .. msg .. '</span>'
        '<span class="citation-error" style="color:#b00;">%s</span>',
        msg
    )
end
 
local function add_tracking()
    return cfg.cat_error
end
end


local function build_citeref_key(args)
local function validate(args)
    -- ENWIKI koristi sofisticiraniji algoritam; ovdje pojednostavljena verzija:
    -- spajamo autore i godinu bez razmaka
    local parts = {}
 
    if is_set(args.P1) then table.insert(parts, args.P1) end
    if is_set(args.P2) then table.insert(parts, args.P2) end
    if is_set(args.P3) then table.insert(parts, args.P3) end
    if is_set(args.P4) then table.insert(parts, args.P4) end
    if is_set(args.P5) then table.insert(parts, args.P5) end
 
    return table.concat(parts, '')
end
 
-------------------------------------------------------------------------------
-- VALIDACIJA
-------------------------------------------------------------------------------
 
local function validate_args(args)
     local errors = {}
     local errors = {}


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


Redak 80: Redak 37:
         table.insert(errors, 'Premalo parametara: potrebno je barem ime autora i godina.')
         table.insert(errors, 'Premalo parametara: potrebno je barem ime autora i godina.')
     end
     end
    -- očekivanje da je jedan od parametara godina (P2 ili P3 ili P4 ili P5)
    -- (ne strogo, samo upozorenje ako su svi očito imena)
    -- ovo ostavljamo mekano – radije manje lažnih pozitivnih


     return errors
     return errors
Redak 89: Redak 42:


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


-------------------------------------------------------------------------------
local function core(args)
-- JEZGRA FORMATIRANJA
    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 function core(args)
    local year = nil
     local result
    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


    -- osnovni obrazac: autori + godina
     if year then
     if is_set(args.P5) then
         result = result .. ' ' .. args.bracket_year_left .. year .. args.bracket_year_right
        -- P1 + " et al." + P5 (godina)
        result = args.P1 .. ' et al. ' ..
            args.bracket_year_left .. args.P5 .. args.bracket_year_right
    elseif is_set(args.P4) then
        -- P1, P2 i P3 + P4 (godina)
        result = args.P1 .. ', ' .. args.P2 .. ' i ' .. args.P3 .. ' ' ..
            args.bracket_year_left .. args.P4 .. args.bracket_year_right
    elseif is_set(args.P3) then
        -- P1 i P2 + P3 (godina)
         result = args.P1 .. ' i ' .. args.P2 .. ' ' ..
            args.bracket_year_left .. args.P3 .. args.bracket_year_right
    else
        -- P1 + P2
        result = trim(args.P1 .. ' ' .. args.bracket_year_left .. args.P2 .. args.bracket_year_right)
     end
     end


     -- REF / CITEREF logika
     -- CITEREF
     if args.ref ~= 'none' then
     if args.ref ~= 'none' then
        local key = util.citeref_key(authors, year)
         if is_set(args.ref) then
         if is_set(args.ref) then
             result = string.format('[[#%s|%s]]', mw.uri.anchorEncode(args.ref), result)
             result = string.format('[[#%s|%s]]', mw.uri.anchorEncode(args.ref), result)
         else
         else
            local key = build_citeref_key(args)
             result = string.format('[[#CITEREF%s|%s]]', mw.uri.anchorEncode(key), result)
             result = string.format('[[#CITEREF%s|%s]]', mw.uri.anchorEncode(key), result)
         end
         end
Redak 148: Redak 92:
     end
     end


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


-------------------------------------------------------------------------------
local function defaults()
-- DEFAULT ARGUMENTI
-------------------------------------------------------------------------------
 
local function default_args()
     return {
     return {
         bracket_left = '',
         bracket_left = '',
Redak 178: Redak 116:
end
end


-------------------------------------------------------------------------------
function m.harvard_citation(frame)
-- JAVNE FUNKCIJE
-------------------------------------------------------------------------------
 
function mHarv.harvard_core(frame)
    local pframe = frame:getParent()
    local args = default_args()
 
    args.bracket_left = pframe.args.BracketLeft or ''
    args.bracket_right = pframe.args.BracketRight or ''
    args.bracket_year_left = pframe.args.BracketYearLeft or ''
    args.bracket_year_right = pframe.args.BracketYearRight or ''
    args.postscript = pframe.args.Postscript or ''
    if args.postscript == 'none' then
        args.postscript = ''
    end
 
    args.page = pframe.args.Page or ''
    args.pages = pframe.args.Pages or ''
    args.location = pframe.args.Location or ''
    args.page_sep = pframe.args.PageSep or args.page_sep
    args.pages_sep = pframe.args.PagesSep or args.pages_sep
    args.ref = pframe.args.REF or ''
 
    args.P1 = trim(pframe.args.P1)
    args.P2 = trim(pframe.args.P2)
    args.P3 = trim(pframe.args.P3)
    args.P4 = trim(pframe.args.P4)
    args.P5 = trim(pframe.args.P5)
 
    local errors = validate_args(args)
    local base = core(args)
 
    return format_errors(errors) .. base
end
 
function mHarv.harvard_citation(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local args = default_args()
     local args = defaults()


     args.bracket_left = '('
     args.bracket_left = '('
Redak 226: Redak 128:
     args.ref = pframe.args.ref or pframe.args.Ref or ''
     args.ref = pframe.args.ref or pframe.args.Ref or ''


     args.P1 = trim(pframe.args[1])
     for i = 1, 5 do
    args.P2 = trim(pframe.args[2])
        args['P'..i] = trim(pframe.args[i])
    args.P3 = trim(pframe.args[3])
     end
    args.P4 = trim(pframe.args[4])
    args.P5 = trim(pframe.args[5])
 
     local errors = validate_args(args)
    local base = core(args)


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


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


     args.page = pframe.args.p or pframe.args.page or ''
     args.page = pframe.args.p or pframe.args.page or ''
Redak 247: Redak 145:
     args.ref = pframe.args.ref or pframe.args.Ref or ''
     args.ref = pframe.args.ref or pframe.args.Ref or ''


     args.P1 = trim(pframe.args[1])
     for i = 1, 5 do
    args.P2 = trim(pframe.args[2])
        args['P'..i] = trim(pframe.args[i])
    args.P3 = trim(pframe.args[3])
     end
    args.P4 = trim(pframe.args[4])
     args.P5 = trim(pframe.args[5])


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


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


    -- override s #invoke args
     for k, v in pairs(frame.args) do
     for k, v in pairs(frame.args) do
         args[k] = v
         args[k] = v
Redak 269: Redak 162:


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


     args.page = pframe.args.p or pframe.args.page or ''
     args.page = pframe.args.p or pframe.args.page or ''
Redak 278: Redak 169:
     args.ref = pframe.args.ref or pframe.args.Ref or ''
     args.ref = pframe.args.ref or pframe.args.Ref or ''


     args.P1 = trim(pframe.args[1])
     for i = 1, 5 do
    args.P2 = trim(pframe.args[2])
        args['P'..i] = trim(pframe.args[i])
    args.P3 = trim(pframe.args[3])
     end
    args.P4 = trim(pframe.args[4])
     args.P5 = trim(pframe.args[5])


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


     local name = 'FOOTNOTE' ..
     local authors = {}
        args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 ..
    if is_set(args.P1) then table.insert(authors, args.P1) end
         args.page .. args.pages .. args.location
    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{
     local ref = frame:extensionTag{
         name = 'ref',
         name = 'ref',
         args = { name = name },
         args = { name = 'FOOTNOTE' .. key .. args.page .. args.pages .. args.location },
         content = base
         content = base
     }
     }
Redak 300: Redak 197:
end
end


-- alias za main (ako netko pozove |main)
m.main = m.sfn
mHarv.main = mHarv.sfn


return mHarv
return m

Inačica od 12. siječanj 2026. u 10:12

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

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

    -- CITEREF
    if args.ref ~= 'none' then
        local key = util.citeref_key(authors, year)
        if is_set(args.ref) then
            result = string.format('[[#%s|%s]]', mw.uri.anchorEncode(args.ref), result)
        else
            result = string.format('[[#CITEREF%s|%s]]', mw.uri.anchorEncode(key), result)
        end
    end

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

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