Pergi ke kandungan

Modul:etymon/text

Daripada Wikikamus
local export = {}

local references_module = "Module:references"

function export.render(dataTree, keywords, make_link_func, usePlusTemplates, maxDepth)
	local children = dataTree.children
	if not children or #children == 0 then
		return ""
	end

	local result = ""
	local depth = 1
	local node = dataTree
	local needsComma = false

	while node and (not maxDepth or depth <= maxDepth) do
		local loop_children = node.children
		if #loop_children == 0 then break end

		local firstChild = loop_children[1]
		if firstChild.derType == "root" or firstChild.derType == "afeq" then break end

		local keywordInfo = keywords[firstChild.derType]
		if not keywordInfo then break end

		local isGroup = keywordInfo.is_group

		local stepText = ""
		if result == "" then
			if firstChild.is_uncertain then
				stepText = "Mungkin " .. keywordInfo.phrase
			else
				stepText = keywordInfo.text
			end
		else
			if needsComma then
				stepText = ", " .. (firstChild.is_uncertain and "mungkin " or "") .. keywordInfo.phrase
			else
				stepText = " " .. (firstChild.is_uncertain and "mungkin " or "") .. keywordInfo.phrase
			end
		end

		local linkParts = {}
		local currentLang = node.lang
		local stepRefs = {}

		for _, child in ipairs(loop_children) do
			if child.derType == "root" or child.derType == "afeq" then break end

			local linkParams = {
				lang = child.lang,
				term = child.title,
				id = child.id,
				gloss = child.t,
				pos = child.pos,
				tr = child.tr,
				ts = child.ts,
				alt = child.alt
			}

			local link = ""
			if child.lang:getCanonicalName() ~= currentLang:getCanonicalName() then
				link = child.lang:makeWikipediaLink() .. " " .. make_link_func(linkParams)
				currentLang = child.lang
			else
				link = make_link_func(linkParams)
			end

			table.insert(linkParts, link)

			if depth == 1 and child.parsed_ref then
				table.insert(stepRefs, require(references_module).format_references(child.parsed_ref))
			end
		end

		stepText = stepText .. " " .. table.concat(linkParts, " + ")
		result = result .. stepText

		local hasMoreSteps = false
		if not isGroup and #loop_children == 1 and firstChild.derType ~= "root" then
			local nextChildren = firstChild.children
			if nextChildren and #nextChildren > 0 and (not maxDepth or depth < maxDepth) then
				hasMoreSteps = true
			end
		end

		if depth == 1 and #stepRefs > 0 then
			if hasMoreSteps then
				result = result .. "," .. table.concat(stepRefs, "")
				needsComma = false
			else
				result = result .. "." .. table.concat(stepRefs, "")
				return result
			end
		else
			needsComma = hasMoreSteps
		end

		if isGroup or #loop_children > 1 or not hasMoreSteps then
			break
		end

		node = firstChild
		depth = depth + 1
	end

	return result .. "."
end

return export