跳转到内容

模組:Txg

維基詞典,自由的多語言詞典


local export = {}

local len = mw.ustring.len
local loadData = mw.loadData
local m_zh = require"Module:zh"

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

function export.pron(frame)
	local args = frame:getParent().args
	local title = args["1"] or mw.title.getCurrentTitle().text
	local prons = {
		loadData("Module:txg/data/LFW2008"),
		loadData("Module:txg/data/Miyake"),
		loadData("Module:txg/data/Kychanov2006"),
		loadData("Module:txg/data/Sofronov1968"),
		loadData("Module:txg/data/Nishida1966")
	}
	
	local pron = {{}, {}, {}, {}, {}}
	
	local sources = {
		"[[w:李范文|李范文]] 2008",
		"[[w:三宅英雄|三宅英雄]] 2019",
		"[[w:叶夫根尼·克恰诺夫|克恰诺夫]] 2006",
		"Sofronov 1968",
		"[[w:西田龙雄|西田龙雄]] 1966"
	}
	
	local text = {'<div class="toccolours zhpron" style="width: max-content; max-width: 100%; padding-right: 1em; font-size:100%; overflow: hidden">'}
	
	for char in title:gmatch("[%z\1-\127\194-\244][\128-\191]*") do
		if char ~= "-" then
			for i = 1, 5 do
				table.insert(pron[i], prons[i][char] or "?")
			end
		end
	end
	
	for i = 1, 5 do
		for _, v in ipairs(pron[i]) do
			if v ~= "?" then
				table.insert(text, "\n* <small>(<i>" .. sources[i] .. "</i>)</small>: ")
				table.insert(text, "*" .. table.concat(pron[i], " "))
			break end
		end
	end
	
	table.insert(text, "</div>")
	
	return table.concat(text)
	
end
	

function export.der(frame)
	local params = {
		[1] = { list = true },
		["name"] = {},
		["title"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local LFW2008 = loadData("Module:txg/data/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 = word:match(";(.+)") or nil, word:match("^[^;]+")
		local trtab = {}
		for i in word:gmatch("[%z\1-\127\194-\244][\128-\191]*") do
			table.insert(trtab, LFW2008[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 export.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 word:match(title) and word ~= title then
				table.insert(result, word)
			end
		end
	elseif idslist then
		for char, seq in pairs(idslist) do
			if seq:match(title) and seq ~= title then
				table.insert(result, char)
			end
		end
	end
	
	return "{{col-auto|txg|" .. table.concat(result, "|") .. "}}"
end

function export.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 content:match("==西夏語==") and not content:match("==西夏语==") then
			categories = categories .. "[[Category:Tangut redlinks/txg-see]]"
		elseif not content:match("txg%-pron") and not content:match("txg%-see") then
			require("Module:debug").track("txg-see/unidirectional reference to variant")
		elseif not content:match(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 m_zh.st(language_name) == "西夏語" then
			section = s
		end
	end
		
	if not section then
		return ""
	end
	
	section = section:gsub("\n===+詞源.-(\n==)", "%1")
	section = section:gsub("\n===+词源.-(\n==)", "%1")
	section = section:gsub("\n===+字源.-(\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, '該字符是<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 export