Broken/Footnotes: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
Nema sažetka uređivanja
Nema sažetka uređivanja
 
Redak 1: Redak 1:
-- Modul:Harvard
local f = {}
-- Hrvatska verzija harvardskih citata (sfn, harvard, itd.)
-- s osnovnom validacijom i tracking kategorijama


require('strict')
f.args_default = {
 
    bracket_left = "",
local mHarv = {}
    bracket_right = "",
 
    bracket_year_left = "",
-------------------------------------------------------------------------------
    bracket_year_right = "",
-- KONFIGURACIJA
    postscript = "",
-------------------------------------------------------------------------------
    page = "",
 
    pages = "",
local cfg = {
    location = "",
     -- skrivena kategorija za probleme
     page_sep = ", str. ",
     cat_error = '[[Kategorija:Stranice s greškama u harvardskim citatima]]',
     pages_sep = ", str. ",
 
    ref = "",
     -- maksimalan broj pozicijskih parametara (autori + godina)
     P1 = "",
     max_params = 5,
     P2 = "",
 
    P3 = "",
     -- minimalno: autor + godina
     P4 = "",
     min_positional = 2,
     P5 = ""
}
}
-------------------------------------------------------------------------------
-- POMOĆNE FUNKCIJE
-------------------------------------------------------------------------------


local function trim(str)
local function trim(str)
     if not str then return '' end
     if not str then return "" end
     str = mw.text.trim(str)
     str = mw.text.trim(str)
    -- makni završnu točku da ne dupliramo
     return str:gsub("%.$", "")
     return str:gsub('%.$', '')
end
 
local function is_set(v)
    return v ~= nil and trim(v) ~= ''
end
 
local function error_tag(msg)
    return string.format(
        '<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
-------------------------------------------------------------------------------
-- VALIDACIJA
-------------------------------------------------------------------------------
local function validate_args(args)
    local errors = {}
    -- broj pozicijskih parametara
    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
    -- 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
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>')) .. add_tracking()
end
-------------------------------------------------------------------------------
-- JEZGRA FORMATIRANJA
-------------------------------------------------------------------------------


local function core(args)
local function core(args)
     local result
     local result


    -- osnovni obrazac: autori + godina
     if args.P5 ~= "" then
     if is_set(args.P5) then
         result = args.P1 .. " et al. " .. args.bracket_year_left .. args.P5 .. args.bracket_year_right
        -- P1 + " et al." + P5 (godina)
     elseif args.P4 ~= "" then
         result = args.P1 .. ' et al. ' ..
         result = args.P1 .. ", " .. args.P2 .. " i " .. args.P3 .. " " ..
            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
             args.bracket_year_left .. args.P4 .. args.bracket_year_right
     elseif is_set(args.P3) then
     elseif args.P3 ~= "" then
        -- P1 i P2 + P3 (godina)
         result = args.P1 .. " i " .. args.P2 .. " " ..
         result = args.P1 .. ' i ' .. args.P2 .. ' ' ..
             args.bracket_year_left .. args.P3 .. args.bracket_year_right
             args.bracket_year_left .. args.P3 .. args.bracket_year_right
     else
     else
        -- P1 + P2
         result = trim(args.P1 .. " " .. args.bracket_year_left .. args.P2 .. args.bracket_year_right)
         result = trim(args.P1 .. ' ' .. args.bracket_year_left .. args.P2 .. args.bracket_year_right)
     end
     end


     -- REF / CITEREF logika
     -- REF handling
     if args.ref ~= 'none' then
     if args.ref ~= "none" then
         if is_set(args.ref) then
         if args.ref ~= "" then
             result = string.format('[[#%s|%s]]', mw.uri.anchorEncode(args.ref), result)
             result = "[[#" .. mw.uri.anchorEncode(args.ref) .. "|" .. result .. "]]"
         else
         else
             local key = build_citeref_key(args)
             local anchor = args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5
             result = string.format('[[#CITEREF%s|%s]]', mw.uri.anchorEncode(key), result)
             result = "[[#CITEREF" .. mw.uri.anchorEncode(anchor) .. "|" .. result .. "]]"
         end
         end
     end
     end


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


     -- lokacija
     -- location
     if is_set(args.location) then
     if 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 .. result .. args.bracket_right .. args.postscript
 
    return result
end
end


-------------------------------------------------------------------------------
function f.harvard_core(frame)
-- DEFAULT ARGUMENTI
-------------------------------------------------------------------------------
 
local function default_args()
    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
 
-------------------------------------------------------------------------------
-- JAVNE FUNKCIJE
-------------------------------------------------------------------------------
 
function mHarv.harvard_core(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local args = default_args()
     local args = mw.clone(f.args_default)


     args.bracket_left = pframe.args.BracketLeft or ''
     args.bracket_left = pframe.args.BracketLeft or ""
     args.bracket_right = pframe.args.BracketRight or ''
     args.bracket_right = pframe.args.BracketRight or ""
     args.bracket_year_left = pframe.args.BracketYearLeft or ''
     args.bracket_year_left = pframe.args.BracketYearLeft or ""
     args.bracket_year_right = pframe.args.BracketYearRight or ''
     args.bracket_year_right = pframe.args.BracketYearRight or ""
     args.postscript = pframe.args.Postscript or ''
     args.postscript = (pframe.args.Postscript == "none") and "" or (pframe.args.Postscript or "")
    if args.postscript == 'none' then
        args.postscript = ''
    end


     args.page = pframe.args.Page or ''
     args.page = pframe.args.Page or ""
     args.pages = pframe.args.Pages or ''
     args.pages = pframe.args.Pages or ""
     args.location = pframe.args.Location or ''
     args.location = pframe.args.Location or ""
     args.page_sep = pframe.args.PageSep or args.page_sep
     args.page_sep = pframe.args.PageSep or args.page_sep
     args.pages_sep = pframe.args.PagesSep or args.pages_sep
     args.pages_sep = pframe.args.PagesSep or args.pages_sep
     args.ref = pframe.args.REF or ''
     args.ref = pframe.args.REF or ""


     args.P1 = trim(pframe.args.P1)
     args.P1 = trim(pframe.args.P1)
Redak 208: Redak 89:
     args.P5 = trim(pframe.args.P5)
     args.P5 = trim(pframe.args.P5)


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


function mHarv.harvard_citation(frame)
function f.harvard_citation(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local args = default_args()
     local args = mw.clone(f.args_default)


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


     args.page = pframe.args.p or pframe.args.page or ''
     args.page = pframe.args.p or pframe.args.page or ""
     args.pages = pframe.args.pp or pframe.args.pages or ''
     args.pages = pframe.args.pp or pframe.args.pages or ""
     args.location = pframe.args.loc or ''
     args.location = pframe.args.loc or ""
     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])
     args.P1 = trim(pframe.args[1])
Redak 232: Redak 110:
     args.P5 = trim(pframe.args[5])
     args.P5 = trim(pframe.args[5])


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


function mHarv.harvard_citation_no_bracket(frame)
function f.harvard_citation_no_bracket(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local args = default_args()
     local args = mw.clone(f.args_default)


     args.page = pframe.args.p or pframe.args.page or ''
     args.page = pframe.args.p or pframe.args.page or ""
     args.pages = pframe.args.pp or pframe.args.pages or ''
     args.pages = pframe.args.pp or pframe.args.pages or ""
     args.location = pframe.args.loc or ''
     args.location = pframe.args.loc or ""
     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])
     args.P1 = trim(pframe.args[1])
Redak 253: Redak 128:
     args.P5 = trim(pframe.args[5])
     args.P5 = trim(pframe.args[5])


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


function mHarv.sfn(frame)
function f.sfn(frame)
     local pframe = frame:getParent()
     local pframe = frame:getParent()
     local args = default_args()
     local args = mw.clone(f.args_default)


     -- override s #invoke args
     -- override defaults with #invoke args
     for k, v in pairs(frame.args) do
     for k, v in pairs(frame.args) do
         args[k] = v
         args[k] = v
     end
     end


     args.postscript = pframe.args.postscript or pframe.args.ps or '.'
     args.postscript = (pframe.args.postscript == "none") and "" or (pframe.args.postscript or pframe.args.ps or ".")
    if args.postscript == 'none' then
     args.page = pframe.args.p or pframe.args.page or ""
        args.postscript = ''
     args.pages = pframe.args.pp or pframe.args.pages or ""
    end
     args.location = pframe.args.loc or ""
 
     args.ref = pframe.args.ref or pframe.args.Ref or ""
     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.P1 = trim(pframe.args[1])
Redak 284: Redak 152:
     args.P5 = trim(pframe.args[5])
     args.P5 = trim(pframe.args[5])


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


     local name = 'FOOTNOTE' ..
     local name = "FOOTNOTE" ..
         args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 ..
         args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 ..
         args.page .. args.pages .. args.location
         args.page .. args.pages .. args.location


     local ref = frame:extensionTag{
     return frame:extensionTag{
         name = 'ref',
         name = "ref",
         args = { name = name },
         args = { name = name },
         content = base
         content = result
     }
     }
    return format_errors(errors) .. ref
end
end


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

Posljednja izmjena od 12. siječanj 2026. u 10:07

local f = {}

f.args_default = {

   bracket_left = "",
   bracket_right = "",
   bracket_year_left = "",
   bracket_year_right = "",
   postscript = "",
   page = "",
   pages = "",
   location = "",
   page_sep = ", str. ",
   pages_sep = ", str. ",
   ref = "",
   P1 = "",
   P2 = "",
   P3 = "",
   P4 = "",
   P5 = ""

}

local function trim(str)

   if not str then return "" end
   str = mw.text.trim(str)
   return str:gsub("%.$", "")

end

local function core(args)

   local result
   if args.P5 ~= "" then
       result = args.P1 .. " et al. " .. args.bracket_year_left .. args.P5 .. args.bracket_year_right
   elseif args.P4 ~= "" then
       result = args.P1 .. ", " .. args.P2 .. " i " .. args.P3 .. " " ..
           args.bracket_year_left .. args.P4 .. args.bracket_year_right
   elseif args.P3 ~= "" then
       result = args.P1 .. " i " .. args.P2 .. " " ..
           args.bracket_year_left .. args.P3 .. args.bracket_year_right
   else
       result = trim(args.P1 .. " " .. args.bracket_year_left .. args.P2 .. args.bracket_year_right)
   end
   -- REF handling
   if args.ref ~= "none" then
       if args.ref ~= "" then
           result = "" .. result .. ""
       else
           local anchor = args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5
           result = "" .. result .. ""
       end
   end
   -- pages
   if args.page ~= "" then
       result = result .. args.page_sep .. args.page
   elseif args.pages ~= "" then
       result = result .. args.pages_sep .. args.pages
   end
   -- location
   if args.location ~= "" then
       result = result .. ", " .. args.location
   end
   return args.bracket_left .. result .. args.bracket_right .. args.postscript

end

function f.harvard_core(frame)

   local pframe = frame:getParent()
   local args = mw.clone(f.args_default)
   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 == "none") and "" or (pframe.args.Postscript or "")
   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)
   return core(args)

end

function f.harvard_citation(frame)

   local pframe = frame:getParent()
   local args = mw.clone(f.args_default)
   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 ""
   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])
   return core(args)

end

function f.harvard_citation_no_bracket(frame)

   local pframe = frame:getParent()
   local args = mw.clone(f.args_default)
   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])
   return core(args)

end

function f.sfn(frame)

   local pframe = frame:getParent()
   local args = mw.clone(f.args_default)
   -- override defaults with #invoke args
   for k, v in pairs(frame.args) do
       args[k] = v
   end
   args.postscript = (pframe.args.postscript == "none") and "" or (pframe.args.postscript or pframe.args.ps or ".")
   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 result = core(args)
   local name = "FOOTNOTE" ..
       args.P1 .. args.P2 .. args.P3 .. args.P4 .. args.P5 ..
       args.page .. args.pages .. args.location
   return frame:extensionTag{
       name = "ref",
       args = { name = name },
       content = result
   }

end

return f