Modul:Harvard: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
mNema sažetka uređivanja
mNema sažetka uređivanja
Redak 1: Redak 1:
-- Modul:Harvard
-- Modul:Harvard
-- Ujednačen stil s CS1 modulom (autori, godine, stranice, CITEREF)
--require('strict')
local util = require('Module:Citation/CS1/Utilities')
local util = require('Module:Citation/CS1/Utilities')
local m = {}
local m = {}


Redak 15: Redak 10:


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


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


Redak 28: Redak 24:
local function validate(args)
local function validate(args)
     local errors = {}
     local errors = {}
     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 count = count + 1 end
         if is_set(args['P'..i]) then count = count + 1 end
     end
     end
     if count < cfg.min_positional then
     if count < cfg.min_positional then
         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
     return errors
     return errors
end
end
Redak 43: Redak 36:
local function format_errors(errors)
local function format_errors(errors)
     if #errors == 0 then return '' end
     if #errors == 0 then return '' end
    local out = {}
     return error_tag(table.concat(errors, ' ')) .. cfg.cat_error
    for _, e in ipairs(errors) do
end -- Ovdje je nedostajao ovaj 'end'
        table.insert(out, '• ' .. e)
    end
     return error_tag(table.concat(out, '<br>')) .. cfg.cat_error
end


local function core(args)
local function core(args)
     -- 1) AUTORI I GODINA
     -- 1) AUTORI I GODINA
     local authors = {}
     local authors = {}
     if is_set(args.P1) then table.insert(authors, args.P1) end
     for i = 1, 5 do
    if is_set(args.P2) and not tonumber(args.P2) then table.insert(authors, args.P2) end
        local val = args['P'..i]
     if is_set(args.P3) and not tonumber(args.P3) then table.insert(authors, args.P3) end
        if is_set(val) and not tonumber(val) then
            table.insert(authors, val)
        end
     end


     local year = nil
     local year = nil
Redak 61: Redak 53:
         if tonumber(args['P'..i]) then
         if tonumber(args['P'..i]) then
             year = args['P'..i]
             year = args['P'..i]
            break
         end
         end
     end
     end


     local author_text = util.format_authors(authors)
     local author_text = ""
    if util.format_authors then
        author_text = util.format_authors(authors)
    else
        author_text = table.concat(authors, ', ')
    end
 
     local result = author_text or ''
     local result = author_text or ''
     if year then
     if year then
         result = result .. ' ' .. args.bracket_year_left .. year .. args.bracket_year_right
         result = result .. ' ' .. (args.bracket_year_left or '') .. year .. (args.bracket_year_right or '')
     end
     end


     -- 2) CITEREF logika – SAMO ako imamo barem autora I godinu
     -- 2) CITEREF logika
     if args.ref ~= 'none' then
     if args.ref ~= 'none' then
         local have_key = (#authors > 0) and year ~= nil
         local anchor = ""
 
         if is_set(args.ref) then
         if is_set(args.ref) then
             -- ručno zadani ref uvijek je dozvoljen (ali anchor ne smije biti nil)
             anchor = mw.uri.anchorEncode(args.ref)
            local anchor = mw.uri.anchorEncode(args.ref or '')
             result = '[[' .. '#' .. anchor .. '|' .. result .. ']]'
             result = string.format('[[#%s|%s]]', anchor, result)
         elseif #authors > 0 and year then
         elseif have_key then
             local key = ""
             -- automatski CITEREF ključ, ali samo ako ima autora i godina
             if util.citeref_key then
             local key = util.citeref_key(authors, year) or ''
                key = util.citeref_key(authors, year)
             local anchor = mw.uri.anchorEncode(key)
             else
             result = string.format('[[#CITEREF%s|%s]]', anchor, result)
                key = table.concat(authors) .. year
            end
            anchor = mw.uri.anchorEncode(key)
             result = '[[' .. '#CITEREF' .. anchor .. '|' .. result .. ']]'
         end
         end
     end
     end


     -- 3) stranice
     -- 3) Stranice i lokacija
     if is_set(args.page) then
     if is_set(args.page) then
         result = result .. args.page_sep .. args.page
         result = result .. (args.page_sep or ', str. ') .. args.page
     elseif is_set(args.pages) then
     elseif is_set(args.pages) then
         result = result .. args.pages_sep .. args.pages
         result = result .. (args.pages_sep or ', str. ') .. args.pages
     end
     end


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


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


 
-- Glavna funkcija koju poziva predložak
local function defaults()
function m.sfn(frame)
     return {
    local pframe = frame:getParent()
     local args = {
         bracket_left = '',
         bracket_left = '',
         bracket_right = '',
         bracket_right = '',
        bracket_year_left = '',
        bracket_year_right = '',
        postscript = '',
        page = '',
        pages = '',
        location = '',
         page_sep = ', str.&nbsp;',
         page_sep = ', str.&nbsp;',
         pages_sep = ', str.&nbsp;',
         pages_sep = ', str.&nbsp;',
         ref = '',
         P1 = trim(pframe.args[1]),
         P1 = '',
        P2 = trim(pframe.args[2]),
         P2 = '',
        P3 = trim(pframe.args[3]),
         P3 = '',
        P4 = trim(pframe.args[4]),
         P4 = '',
        P5 = trim(pframe.args[5]),
         P5 = '',
         page = pframe.args.p or pframe.args.page or '',
         pages = pframe.args.pp or pframe.args.pages or '',
         location = pframe.args.loc or '',
         ref = pframe.args.ref or pframe.args.Ref or '',
         postscript = pframe.args.postscript or pframe.args.ps or '.'
     }
     }
end
      
 
     if args.postscript == 'none' then args.postscript = '' 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)
     local errors = validate(args)
     return format_errors(errors) .. core(args)
     local content = core(args)
end
   
    -- Generiranje imena fusnote na temelju sadržaja (da se izbjegnu duplikati)
    local footnote_name = "FOOTNOTE" .. mw.uri.anchorEncode(content:gsub('%[%[.-%|(.-)%]%]', '%1'))


function m.harvard_citation_no_bracket(frame)
     return format_errors(errors) .. frame:extensionTag{
     local pframe = frame:getParent()
        name = 'ref',
    local args = defaults()
        args = { name = footnote_name },
 
         content = content
    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
end


function m.sfn(frame)
-- Funkcija za harvardsko citiranje (bez fusnote)
function m.harvard_citation(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local args = defaults()
     local args = {
 
        bracket_left = '(',
    for k, v in pairs(frame.args) do
        bracket_right = ')',
         args[k] = v
        page_sep = ', str.&nbsp;',
    end
        pages_sep = ', str.&nbsp;',
 
        P1 = trim(pframe.args[1]),
    args.postscript = pframe.args.postscript or pframe.args.ps or '.'
         P2 = trim(pframe.args[2]),
    if args.postscript == 'none' then args.postscript = '' end
        P3 = trim(pframe.args[3]),
 
        P4 = trim(pframe.args[4]),
    args.page = pframe.args.p or pframe.args.page or ''
        P5 = trim(pframe.args[5]),
    args.pages = pframe.args.pp or pframe.args.pages or ''
        page = pframe.args.p or pframe.args.page or '',
    args.location = pframe.args.loc or ''
        pages = pframe.args.pp or pframe.args.pages or '',
    args.ref = pframe.args.ref or pframe.args.Ref or ''
        location = pframe.args.loc or '',
 
        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(validate(args)) .. core(args)
     return format_errors(errors) .. ref
end
end
-- Na samom kraju modula:


function m.main(frame)
function m.main(frame)
     return m.sfn(frame)
     return m.sfn(frame)
end
function m.test(frame)
    return "Modul radi!"
end
end


return m
return m

Inačica od 7. travanj 2026. u 12:07

Dokumentacija modula


-- Modul:Harvard
local util = require('Module:Citation/CS1/Utilities')
local m = {}

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

local function trim(s)
    if not s then return '' end
    return mw.text.trim(s):gsub('%.$','')
end

local function is_set(v)
    return v ~= nil and 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
    return error_tag(table.concat(errors, ' • ')) .. cfg.cat_error
end -- Ovdje je nedostajao ovaj 'end'

local function core(args)
    -- 1) AUTORI I GODINA
    local authors = {}
    for i = 1, 5 do
        local val = args['P'..i]
        if is_set(val) and not tonumber(val) then
            table.insert(authors, val)
        end
    end

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

    local author_text = ""
    if util.format_authors then
        author_text = util.format_authors(authors)
    else
        author_text = table.concat(authors, ', ')
    end

    local result = author_text or ''
    if year then
        result = result .. ' ' .. (args.bracket_year_left or '') .. year .. (args.bracket_year_right or '')
    end

    -- 2) CITEREF logika
    if args.ref ~= 'none' then
        local anchor = ""
        if is_set(args.ref) then
            anchor = mw.uri.anchorEncode(args.ref)
            result = '[[' .. '#' .. anchor .. '|' .. result .. ']]'
        elseif #authors > 0 and year then
            local key = ""
            if util.citeref_key then
                key = util.citeref_key(authors, year)
            else
                key = table.concat(authors) .. year
            end
            anchor = mw.uri.anchorEncode(key)
            result = '[[' .. '#CITEREF' .. anchor .. '|' .. result .. ']]'
        end
    end

    -- 3) Stranice i lokacija
    if is_set(args.page) then
        result = result .. (args.page_sep or ', str. ') .. args.page
    elseif is_set(args.pages) then
        result = result .. (args.pages_sep or ', str. ') .. args.pages
    end

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

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

-- Glavna funkcija koju poziva predložak
function m.sfn(frame)
    local pframe = frame:getParent()
    local args = {
        bracket_left = '',
        bracket_right = '',
        page_sep = ', str.&nbsp;',
        pages_sep = ', str.&nbsp;',
        P1 = trim(pframe.args[1]),
        P2 = trim(pframe.args[2]),
        P3 = trim(pframe.args[3]),
        P4 = trim(pframe.args[4]),
        P5 = trim(pframe.args[5]),
        page = pframe.args.p or pframe.args.page or '',
        pages = pframe.args.pp or pframe.args.pages or '',
        location = pframe.args.loc or '',
        ref = pframe.args.ref or pframe.args.Ref or '',
        postscript = pframe.args.postscript or pframe.args.ps or '.'
    }
    
    if args.postscript == 'none' then args.postscript = '' end

    local errors = validate(args)
    local content = core(args)
    
    -- Generiranje imena fusnote na temelju sadržaja (da se izbjegnu duplikati)
    local footnote_name = "FOOTNOTE" .. mw.uri.anchorEncode(content:gsub('%[%[.-%|(.-)%]%]', '%1'))

    return format_errors(errors) .. frame:extensionTag{
        name = 'ref',
        args = { name = footnote_name },
        content = content
    }
end

-- Funkcija za harvardsko citiranje (bez fusnote)
function m.harvard_citation(frame)
    local pframe = frame:getParent()
    local args = {
        bracket_left = '(',
        bracket_right = ')',
        page_sep = ', str.&nbsp;',
        pages_sep = ', str.&nbsp;',
        P1 = trim(pframe.args[1]),
        P2 = trim(pframe.args[2]),
        P3 = trim(pframe.args[3]),
        P4 = trim(pframe.args[4]),
        P5 = trim(pframe.args[5]),
        page = pframe.args.p or pframe.args.page or '',
        pages = pframe.args.pp or pframe.args.pages or '',
        location = pframe.args.loc or '',
        ref = pframe.args.ref or pframe.args.Ref or ''
    }
    return format_errors(validate(args)) .. core(args)
end

function m.main(frame)
    return m.sfn(frame)
end

return m