Jump to content

Module:bibliography/utils

Wiktionary වෙතින්


local export = {}

-- checks whether an ID is an abbreviation or not
function export.is_abbrev(sid, is_author)
	if type(is_author) == "boolean" then
		return not is_author
	else
		return not sid:match(":%d+[a-z]?$") or sid:match("^[A-Z]+:%d+[a-z]?$")
	end
end

-- returns the formatted short citation
function export.format(sid, is_author)
	if export.is_abbrev(sid, is_author) then
		if not sid:match(":%d+[a-z]?$") then
			local volume_number, preceding_author = sid:match(" %d+$"), sid:match("^.+%f[:]")
			if volume_number then sid = sid:gsub(volume_number.."$", "") end
			if preceding_author then sid = sid:gsub("^"..preceding_author..":", "") end
			sid = "''"..sid:gsub("%^(.+)", "<sup>%1</sup>").."''"
			if volume_number then sid = sid..volume_number end
			if preceding_author then sid = preceding_author.." "..sid end
			return sid
		else
			-- TODO: clarify situation, hacky solution
			return sid:gsub("^(.+):(%d+[a-z]?)$", "''%1'' (%2)")
		end
	else
		return sid:gsub("^(.+):(%d+[a-z]?)$", "%1 (%2)"):gsub("et al.", "''et al.''")
	end
end

-- Returns the abbreviation without formatting characters to be used in links and parameters.
function export.strip_markup(sid)
	if export.is_abbrev(sid) then
		return sid:gsub("%^", "")
	end
	return sid
end

-- Returns the tracking path for sources.
function export.tracking_path(lang, sid)
	return "bibliography/"..lang.."/"..sid
end

-- Container for requiring submodules, in case the path changes.
function export.require_submodule(lang)
	return require("Module:bibliography/data/"..lang)
end

-- Applies default_value tables to content tables.
local function apply_default(starting, default)
	for index, default_value in pairs(default) do
		if type(default_value) == "table" then
			if starting[index] ~= nil then
				if type(starting[index]) ~= "table" then
					-- Assume it is formattable text.
					starting[index] = { text = starting[index] }
				end
				apply_default(starting[index], default_value)
			end
		elseif starting[index] == nil then
			starting[index] = default_value
		end
	end
end

function export.handle_alias(node, input, first_check, err_msg)
	-- Simply return the source if easily accessible. It should ideally be like this in the
	-- majority of cases, as the alternative takes time, which is not optimal for a templates
	-- which is to be repeated many times in a single page.
	if node[input] then
		return input, node[input]
	end
	-- Skim the whole node for compatible children.
	for id, child in pairs(node) do
		if first_check and first_check(id, child) then
			return id, child
		end
		-- Check each alias.
		local alias = child.alias
		if alias then
			-- Deal with both single aliases and lists thereof.
			if type(alias) == "string" then
				if alias == input then
					return id, child
				end
			elseif type(alias) == "table" then
				for _, v in ipairs(alias) do
					if v == input then
						return id, child
					end
				end
			end
		end
	end
	-- No node found, print error message if present.
	if err_msg then
		error(err_msg)
	end
end

-- Load sources data from submodule.
function export.extract_source(submodule_data, tracking_lang, input_sid)
	-- Find the source in among data.
	local sid, source = export.handle_alias(
		-- Node to search into.
		submodule_data.sources,
		-- Value being searched.
		input_sid,
		-- Before looking for aliases, try stripping markup.
		function (id, child)
			return export.is_abbrev(id) and (export.strip_markup(id) == input_sid)
		end,
		-- Error message if everything fails.
		"Could not find the source with the ID "..input_sid.." in the database [[Module:bibliography/data/"..tracking_lang.."]]"
	)
	-- Handle importing.
	if source.import_from then
		local source_header = source.header
		source, tracking_lang, sid = export.extract_source(
			export.require_submodule(source.import_from),
			source.import_from, sid
		)
		source.header = source_header
	-- Deals with default values.
	elseif submodule_data.default_values then
		apply_default(source, submodule_data.default_values)
	end
	return source, tracking_lang, sid
end

-- Adds the ordinal suffix to cardinal number.
function export.ordinal_suffix(i)
	local j = i % 10
	local k = i % 100
	if j == 1 and k ~= 11 then
		return i.."st"
	elseif j == 2 and k ~= 12 then
		return i.."nd"
	elseif j == 3 and k ~= 13 then
		return i.."rd"
	end
	return i.."th"
end

return export
"https://si.wiktionary.org/w/index.php?title=Module:bibliography/utils&oldid=235832" වෙතින් සම්ප්‍රවේශනය කෙරිණි