Modul:Harvard
Script error: The function "nonexistent" does not exist.
-- 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 harvardskim citatima]]',
-- maksimalan broj pozicijskih parametara (autori + godina)
max_params = 5,
-- minimalno: autor + godina
min_positional = 2,
}
-------------------------------------------------------------------------------
-- POMOĆNE FUNKCIJE
-------------------------------------------------------------------------------
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
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
-------------------------------------------------------------------------------
-- 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 result
-- osnovni obrazac: autori + godina
if is_set(args.P5) then
-- 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
-- REF / CITEREF logika
if args.ref ~= 'none' then
if is_set(args.ref) then
result = string.format('[[#%s|%s]]', mw.uri.anchorEncode(args.ref), result)
else
local key = build_citeref_key(args)
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
result = args.bracket_left .. result .. args.bracket_right .. (args.postscript or '')
return result
end
-------------------------------------------------------------------------------
-- DEFAULT ARGUMENTI
-------------------------------------------------------------------------------
local function default_args()
return {
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 = '',
}
end
-------------------------------------------------------------------------------
-- 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 args = default_args()
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])
local errors = validate_args(args)
local base = core(args)
return format_errors(errors) .. base
end
function mHarv.harvard_citation_no_bracket(frame)
local pframe = frame:getParent()
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
function mHarv.sfn(frame)
local pframe = frame:getParent()
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
-- alias za main (ako netko pozove |main)
mHarv.main = mHarv.sfn
return mHarv