Jump to content

Module:headword/util

Wiktionary වෙතින්


local export = {}

local normalize_keys = {
	["g"] = "genders",
	["tr"] = "translit",
	["ts"] = "transcription",
}

local function parse_and_apply_tags(result, tags)
	local last_key = nil
	for tag in mw.ustring.gmatch(tags, "[^,]+") do
		local key, value = mw.ustring.match(tag, "([^:]+):(.*)")
		if not value then
			value = tag
			if last_key then
				result[last_key] = result[last_key] .. "," .. value
			end
		else
			key = normalize_keys[key] or key
			result[key] = value
			last_key = key
		end
	end
end

-- parses <key:value> pairs in inflections. modifies the table passed in.
function export.parse_overrides_in_inflections(inflections)
	for _, inflection in ipairs(inflections) do
		for index, inflected_form in ipairs(inflection) do
			local term = inflected_form
			if type(term) == "table" then
				term = term.term
			end
			
			if type(term) == "string" then
				local tag_start, tag_end = mw.ustring.find(term, "<.+>$")
				if tag_start then
					local result
					local tag = mw.ustring.sub(term, tag_start + 1, tag_end - 1)
					term = mw.ustring.sub(term, 1, tag_start - 1)
		
					if type(inflected_form) == "table" then
						result = inflected_form
					else
						result = { term = term }
					end
					
					parse_and_apply_tags(result, tag)
					
					if type(result.genders) == "string" then
						result.genders = mw.text.split(result.genders, ",")
					end
					if type(result.q) == "string" then
						result.q = mw.text.split(result.q, ",")
					end
					inflection[index] = result
				end
			end
		end
	end
end

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