Loncat ke isi

Modul:references

Deri Wikikamus

Dokumentasi untuk modul ini dapat dibuat di Modul:references/doc

local p = {}

-- helper: wrap with italics
local function italics(txt)
	return "''" .. txt .. "''"
end

-- helper: wrap with bold
local function bold(txt)
	return "'''" .. txt .. "'''"
end

function p.format(frame)
	local args = frame:getParent().args
	local mode = args.mode or "cite" -- "cite" = citation, "quote" = quotation
	local out = {}

	-- === Author / Year ===
	local year = args[2] or args.year
	local origyear = args.origyear

	if mode == "cite" then
		-- Author, then year in parentheses
		if args.author then
			table.insert(out, args.author)
		end
		if year then
			table.insert(out, " (" .. year .. ")")
		end
		if origyear then
			table.insert(out, " [" .. origyear .. "]")
		end
	elseif mode == "quote" then
		-- Year first in bold, then author
		if year then
			table.insert(out, bold(year))
		end
		if args.author then
			if #out > 0 then
				table.insert(out, ", " .. args.author)
			else
				table.insert(out, args.author)
			end
		end
		if origyear then
			table.insert(out, " [" .. origyear .. "]")
		end
	end

	-- === Chapter or entry ===
	local chapter = args.chapter or args.entry
	if chapter then
		local c = '"' .. chapter .. '"'
		if args["trans-chapter"] then
			c = c .. " [" .. args["trans-chapter"] .. "]"
		end
		if #out > 0 then table.insert(out, ", " .. c) else table.insert(out, c) end
	end

	-- === Editor ===
	if args.editor then
		table.insert(out, ", dalem " .. args.editor .. " (prm.)")
	end

	-- === Title + translated title ===
	if args.title then
		local t = italics(args.title)
		if args["trans-title"] then
			t = t .. " [" .. italics(args["trans-title"]) .. "]"
		end
		table.insert(out, ", " .. t)
	end

	-- === Volume / edition ===
	if args.volume then table.insert(out, ", jilid " .. args.volume) end
	if args.edition then table.insert(out, ", " .. args.edition) end

	-- === Publisher + location ===
	if args.publisher then
		local pub = ""
		if args.location then
			pub = args.location .. ": "
		end
		pub = pub .. args.publisher
		table.insert(out, ", " .. pub)
	end

	-- === Identifiers ===
	if args.isbn then table.insert(out, ", ISBN " .. args.isbn) end
	if args.doi then table.insert(out, ", doi:" .. args.doi) end

	-- === Page ===
	if args.page then
		table.insert(out, ", h. " .. args.page)
	end

	-- === Note ===
	if args.note then
		table.insert(out, ", " .. args.note)
	end

	-- === URL ===
	if args.url then
		table.insert(out, string.format(' <sup>[<a href="%s">%s</a>]</sup>', args.url, args.url))
	end

	-- === Passage / text ===
	local text = args.text or args.passage
	if text then
		if mode == "cite" then
			-- in citation, passage usually inline after colon
			table.insert(out, ': "' .. text .. '"')
		elseif mode == "quote" then
			-- in quotation, the passage comes in block formatting
			table.insert(out, ' — "' .. text .. '"')
		end
	end

	return table.concat(out)
end

return p