"इस मॉड्यूल हेतु प्रलेख मॉड्यूल:txg/doc पर बनाया जा सकता है"

local M = {}
local links = require("Module:links")

local find = mw.ustring.find
local format = mw.ustring.format
local len = mw.ustring.len
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub

local function format_Tangut_text(text) return '<span class="Tang" lang="txg">' .. text .. '</span>' end

function M.pron(frame)
	local args = frame:getParent().args
	local title = args["1"] or mw.title.getCurrentTitle().text
	local pronlist = require("Module:txg/data/pronlist")
	
	local text = {'<div class="toccolours zhpron" style="width: max-content; max-width: 100%; padding-right: 1em; font-size:100%; overflow: hidden">'}
	
	local LFW2008 = {}
	local Miyake = {}
	local Kychanov2006 = {}
	local Sofronov1968 = {}
	local Nishida1966 = {}
	for i in gmatch(title, ".") do
		if i ~= "-" then
			table.insert(LFW2008, pronlist.LFW2008[i] or "?")
			table.insert(Miyake, pronlist.Miyake[i] or "?")
			table.insert(Kychanov2006, pronlist.Kychanov2006[i] or "?")
			table.insert(Sofronov1968, pronlist.Sofronov1968[i] or "?")
			table.insert(Nishida1966, pronlist.Nishida1966[i] or "?")
		end
	end
	
	for _, v in ipairs(LFW2008) do
		if v ~= "?" then
			table.insert(text, "\n* <small>(<i>[[w:Li Fanwen|Li Fanwen]] 2008</i>)</small>: ")
			table.insert(text, "*" .. table.concat(LFW2008, " "))
		break end
	end
	
	for _, v in ipairs(Miyake) do
		if v ~= "?" then
			table.insert(text, "\n* <small>(<i>[[w:Marc Miyake|Miyake]] 2019</i>)</small>: ")
			table.insert(text, "*" .. table.concat(Miyake, " "))
		break end
	end
	
	for _, v in ipairs(Kychanov2006) do
		if v ~= "?" then
			table.insert(text, "\n* <small>(<i>[[w:Yevgeny Kychanov|Kychanov]] 2006</i>)</small>: ")
			table.insert(text, "*" .. table.concat(Kychanov2006, " "))
		break end
	end
	
	for _, v in ipairs(Sofronov1968) do
		if v ~= "?" then
			table.insert(text, "\n* <small>(<i>Sofronov 1968</i>)</small>: ")
			table.insert(text, "*" .. table.concat(Sofronov1968, " "))
		break end
	end
	
	for _, v in ipairs(Nishida1966) do
		if v ~= "?" then
			table.insert(text, "\n* <small>(<i>[[w:Tatsuo Nishida|Nishida]] 1966</i>)</small>: ")
			table.insert(text, "*" .. table.concat(Nishida1966, " "))
		break end
	end
	
	table.insert(text, "</div>")
	
	return table.concat(text)
	
end
	

function M.der(frame)
	local params = {
		[1] = { list = true },
		["name"] = {},
		["title"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local pronlist = require("Module:txg/data/pronlist").LFW2008
	local pagename = mw.title.getCurrentTitle().text
	local result = {}
	
	local name = args["name"] or "Derived terms from"
	local title = args["title"] and " (<i>" .. args["title"] .. "</i>)" or ""
	local saurus = (mw.title.getCurrentTitle().nsText == "Thesaurus")
	
	for _, word in ipairs(args[1]) do
		note, word = match(word, ";(.+)") or nil, match(word, "^[^;]+")
		local trtab = {}
		for i in gmatch(word, ".") do
			table.insert(trtab, pronlist[i] or "?")
		end
		if word ~= pagename then  -- synonyms should not be the same as the title
			table.insert(result,
				frame:expandTemplate{ title = "link", args = { "txg", word, ["tr"] = "''*" .. table.concat(trtab, " ") .. "''" } } ..
				(note and " (''" .. note .. "'')" or ""))
		end
	end
	
	return 
	require("Module:columns/old").create_table(
		(len(pagename) > 1 and 2 or 3), -- column number
		result, -- terms
		true, -- alphabetize
		"#F5F5FF", -- background
		(((#result > 72 or fold) and not saurus) and true or false), -- whether to collapse
		"derivedterms", -- class applied to table containing terms
		name .. " " .. format_Tangut_text(pagename) .. title, -- title
		nil, -- column width
		nil, -- line start (??)
		require("Module:languages").getByCode("txg")
	)
end

function M.newDer(frame)
	local title = mw.title.getCurrentTitle().subpageText
	if 101119 > mw.ustring.codepoint(title) and mw.ustring.codepoint(title) >= 100352 then
		idslist = require("Module:txg/data/idslist").list
	else
		wordlist = require("Module:txg/data/wordlist").list
	end
	local args = frame:getParent().args
	
	local result = {}
	
	for _, arg in ipairs(args) do
		table.insert(result, arg)
	end
	
	if wordlist then
		for _, word in ipairs(wordlist) do
			if match(word, title) and word ~= title then
				table.insert(result, word)
			end
		end
	elseif idslist then
		for char, seq in pairs(idslist) do
			if match(seq, title) and seq ~= title then
				table.insert(result, char)
			end
		end
	end
	
	return "{{txg-der|" .. table.concat(result, "|") .. "}}"
end

function M.see(frame)
	local params = {
		[1] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local title = args[1]
	local curr_title = mw.title.getCurrentTitle().subpageText
	local content = mw.title.new(title):getContent()
	local senses = {}
	local sense_id = 0
	local j, pos, s, section
	
	if title == curr_title then
		return error("The soft-directed item is the same as the page title.")
	end
	
	if content then
		if not match(content, "==Tangut==") then
			categories = categories .. "[[Category:Tangut redlinks/txg-see]]"
		elseif not match(content, "txg%-pron") and not match(content, "txg%-see") then
			require("Module:debug").track("txg-see/unidirectional reference to variant")
		elseif not match(content, curr_title) then
			require("Module:debug").track("txg-see/unidirectional reference variant→orthodox")
		end
	end
	
	while true do
		_, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.-)\n==%f[^=]", pos)
		
		if j == nil then
			i, j, language_name, s = content:find("%f[=]==%s*([^=]+)%s*==(\n.+)", pos)
		end
		
		if j == nil then
			break
		else
			pos = j - 1
		end
		
		if language_name == "Tangut" then
			section = s
		end
	end
		
	if not section then
		return ""
	end
	
	section = section:gsub("\n===+Etymology.-(\n==)", "%1")
	section = section:gsub("\n===+Glyph origin.-(\n==)", "%1")
	
	local text = {'<div class="toccolours zhpron" style="width: max-content; padding-left: 1em; padding-right: 1.5em; padding-bottom: 1em; font-size:100%; overflow: hidden">'}
	
	table.insert(text, 'This character is a variant form of <span style="font-size:150%;">' .. frame:expandTemplate{ title = "l", args = { "txg", title} } .. "</span>")
	
	for sense in section:gmatch("\n# ([^\n]+)") do
		if not sense:match("rfdef") and not sense:match("defn") then
			sense_id = sense_id + 1
			table.insert(senses, sense)
		end
	end
	if sense_id == 0 then
		table.insert(text, ".")
	else
		table.insert(text, ":\n# " .. table.concat(senses, "\n# "))
	end
	
	table.insert(text, "</div>")
	
	return frame:preprocess( table.concat(text) )
		
end

return M