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...«.
 
mNema sažetka uređivanja
 
(Nije prikazano 9 međuinačica 2 suradnika)
Redak 1: Redak 1:
-- Modul:Harvard
-- Modul:Harvard
-- Hrvatska verzija harvardskih citata (sfn, harvard, itd.)
local util = require('Module:Citation/CS1/Utilities')
-- s osnovnom validacijom i tracking kategorijama
local m = {}
 
require('strict')
 
local mHarv = {}
 
-------------------------------------------------------------------------------
-- KONFIGURACIJA
-------------------------------------------------------------------------------


local cfg = {
local cfg = {
    -- skrivena kategorija za probleme
     cat_error = '[[Kategorija:Grješ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
     if not s then return '' end
-------------------------------------------------------------------------------
     return mw.text.trim(s):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


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


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
 
local function build_citeref_key(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
end


-------------------------------------------------------------------------------
local function validate(args)
-- 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
     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
    -- 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
end
end


local function format_errors(errors)
local function format_errors(errors)
     if #errors == 0 then
     if #errors == 0 then return '' end
         return ''
    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
     end


     local out = {}
     local year = nil
     for _, e in ipairs(errors) do
     for i = 1, 5 do
         table.insert(out, '' .. e)
         if tonumber(args['P'..i]) then
            year = args['P'..i]
            break
        end
     end
     end


     return error_tag(table.concat(out, '<br>')) .. add_tracking()
     local author_text = ""
end
    if util.format_authors then
 
        author_text = util.format_authors(authors)
-------------------------------------------------------------------------------
    else
-- JEZGRA FORMATIRANJA
        author_text = table.concat(authors, ', ')
-------------------------------------------------------------------------------
    end
 
local function core(args)
    local result


     -- osnovni obrazac: autori + godina
     local result = author_text or ''
    if is_set(args.P5) then
     if year then
        -- P1 + " et al." + P5 (godina)
         result = result .. ' ' .. (args.bracket_year_left or '') .. year .. (args.bracket_year_right or '')
        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
     -- 2) CITEREF logika
     if args.ref ~= 'none' then
     if args.ref ~= 'none' then
        local anchor = ""
         if is_set(args.ref) then
         if is_set(args.ref) then
             result = string.format('[[#%s|%s]]', mw.uri.anchorEncode(args.ref), result)
             anchor = mw.uri.anchorEncode(args.ref)
        else
            result = '[[' .. '#' .. anchor .. '|' .. result .. ']]'
            local key = build_citeref_key(args)
        elseif #authors > 0 and year then
             result = string.format('[[#CITEREF%s|%s]]', mw.uri.anchorEncode(key), result)
            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
     end
     end


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


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


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


-------------------------------------------------------------------------------
function m.sfn(frame)
-- DEFAULT ARGUMENTI
    local pframe = frame:getParent()
-------------------------------------------------------------------------------
    local args = {
        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 function default_args()
    local content = core(args)
     return {
   
         bracket_left = '',
    -- Najjednostavniji mogući ref tag bez kompliciranja s imenima
        bracket_right = '',
     return frame:extensionTag{
        bracket_year_left = '',
         name = 'ref',
        bracket_year_right = '',
         content = content
        postscript = '',
        page = '',
        pages = '',
        location = '',
        page_sep = ', str.&nbsp;',
        pages_sep = ', str.&nbsp;',
        ref = '',
        P1 = '',
        P2 = '',
        P3 = '',
        P4 = '',
         P5 = '',
     }
     }
end
end


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


function mHarv.harvard_citation_no_bracket(frame)
function m.main(frame)
     local pframe = frame:getParent()
     return m.sfn(frame)
    local args = default_args()
 
    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 ''
 
    args.P1 = trim(pframe.args[1])
    args.P2 = trim(pframe.args[2])
    args.P3 = trim(pframe.args[3])
    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
end
end


function mHarv.sfn(frame)
function m.nonexistent(frame)
    local pframe = frame:getParent()
     return ""
    local args = default_args()
 
    -- override s #invoke args
    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 ''
 
    args.P1 = trim(pframe.args[1])
    args.P2 = trim(pframe.args[2])
    args.P3 = trim(pframe.args[3])
    args.P4 = trim(pframe.args[4])
    args.P5 = trim(pframe.args[5])
 
    local errors = validate_args(args)
    local base = core(args)
 
    local name = 'FOOTNOTE' ..
        args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 ..
        args.page .. args.pages .. args.location
 
    local ref = frame:extensionTag{
        name = 'ref',
        args = { name = name },
        content = base
    }
 
     return format_errors(errors) .. ref
end
end


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

Posljednja izmjena od 7. travanj 2026. u 12:10

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

function m.sfn(frame)
    local pframe = frame:getParent()
    local args = {
        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 content = core(args)
    
    -- Najjednostavniji mogući ref tag bez kompliciranja s imenima
    return frame:extensionTag{
        name = 'ref',
        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

function m.nonexistent(frame)
    return ""
end

return m