跳去內容

模組:zh-see

來自維基辭典

呢個模組嘅解說可以喺模組:zh-see/doc度開

local export = {}
local m_zh = require("Module:zh")
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local match = mw.ustring.match

local function link(text, make_link)
	return '<span lang="zh" class="Hani">' .. (make_link and ('[[' .. text .. '#官話|' .. text .. ']]') or text) .. '</span>'
end

local function get_occurrence(content)
	local _, occurrence = content:gsub("zh%-pron", "zh-see"):gsub("zh%-see", "")
	return occurrence
end

function export.show(frame)
	local non_lemma_type = {
		['l'] = '借字',
		['s'] = '簡化字', ['simp'] = '簡化字', ['simplified'] = '簡化字',
		['fs'] = '一簡字(1964-1986)',
		['ss'] = '二簡字',
		['ssv'] = '二簡字同異體字',
		['sgs'] = '新加坡簡體字(1969–1976)',
		['sgsv'] = '新加坡簡體字(1969–1976)/異體字',
		['sgss'] = '新加坡簡體字(1969–1976)/沿用中國大陸簡體字',
		['sgssv'] = '新加坡簡體字(1969–1976)/沿用中國大陸簡體字/異體字',
		['ns'] = '非標準簡化字',
		['a'] = '古字', ['ancient'] = '古字',
		['o'] = '淘汰舊字', ['obsolete'] = '淘汰舊字',
		['v'] = '異體字', ['var'] = '異體字', ['variant'] = '異體字',
		['vt'] = '傳統異體字',
		['av'] = '古代異體字',
		['sv'] = '簡化字同異體字',
		['svt'] = '簡化字/傳統異體字',
		['hv'] = '有辭書收錄過嘅異體字',
		['hdv'] = '有辭書收錄過嘅異體字',
		['ha'] = '有辭書收錄過嘅古體字',
		['hda'] = '有辭書收錄過嘅古體字',
		['poj'] = '白話字',

		['all'] = '簡化字/淘汰舊字/異體字',
	}

	local args = frame:getParent().args
	local title = args[1]
	local curr_title = mw.title.getCurrentTitle().subpageText
	local content = mw.title.new(title):getContent()
	local title_exists = content ~= nil
	local simp = args["simp"] or false
	local non_lemma_abbrev = args[2] or ""
	
	if title == curr_title then
		return error("軟彈目標版嘅標題同呢版嘅一樣")
	end
	
	if content then
		if not match(content, "{{lang-yue}}") then
			
			if not match(content, "{{langue|zh}}") or not match(content, "{{lang-zh}}") then
				require('Module:debug').track('zh-see/no Chinese section found')
			end
		--elseif not match(content, "zh%-pron") and not match(content, "zh%-see") then
			--require('Module:debug').track('zh-see/unidirectional reference to variant')
		elseif not match(content, curr_title) then
			require('Module:debug').track('zh-see/unidirectional reference variant→orthodox')
		end
		
		occurrence = get_occurrence(content)
		if match(content, "zh%-see") and occurrence == 1 then
			new_title = match(content, "zh%-see%|([^%|%}]+)")
			if non_lemma_abbrev == "" and occurrence == 1 and match(content, "zh%-see%|([^%|%}]+)%|v") then
				chain = true
			end
			content = mw.title.new(new_title):getContent()
		end
		
		content = gsub(content, "zh%-forms", "Ѭ")
		content = gsub(content, "zh%-pron", "Ꙁ")
		if non_lemma_abbrev == "" then
			template = mw.ustring.match(content, "{{Ѭ[^}]*}}") or false
			if template then
				for forms_template in mw.ustring.gmatch(content, "{{Ѭ[^}]*}}") do
					if match(forms_template, curr_title) then
						if match(forms_template, "|s[0-9]*=" .. curr_title) then
							non_lemma_abbrev = non_lemma_abbrev .. "s"
						end
						if match(forms_template, "|t[0-9]*=" .. curr_title) then
							non_lemma_abbrev = non_lemma_abbrev .. "vt"
						end
						if match(forms_template, "|alt=[^|]*" .. curr_title) then
							non_lemma_abbrev = non_lemma_abbrev .. "v"
						end
						break
					end
				end
			end
		end
	end
	
	local non_lemma_cat = non_lemma_type[non_lemma_abbrev ~= "" and non_lemma_abbrev or 's'] or '簡化字'
	local note = (content or mw.title.getCurrentTitle().nsText ~= "") and "" or '[[Category:Chinese terms with uncreated forms]]'
	
	local gloss_text = args[3] or (content and m_zh.extract_gloss(gsub(content, "Ѭ", "zh%-forms"), true) or "")
	local box =
		'{| class="wikitable' .. (match(non_lemma_cat, 'simplified') and ' mw-collapsible mw-collapsed' or '') ..
			'" style="border:1px solid #797979; margin-left: 1px; text-align:left; min-width:' .. (chain and 80 or 70) .. '%"' ..
		
			'\n|-\n| style="background-color: #eeeeee; padding-left: 0.5em; font-size:110%;" | \'\'\'想睇\'\'\'' .. (non_lemma_abbrev=="poj" and curr_title or link(gsub(curr_title, '(.)', '[[%1|%1]]'), false)) .. 
			'\'\'\'嘅讀音同含義嘅話,請去<span>' .. link(chain and new_title or title, true) .. '</span>' .. 
			(gloss_text ~= "" and ('(「' .. gloss_text .. '」)') or '') .. '。\'\'\'' ..
			
			'<br>(呢個' .. (mw.ustring.len(title) == 1 and '' or '詞' ) ..  link(curr_title) .. (mw.ustring.len(title) == 1 and '字' or '' ) ..
			'係' .. link(title, chain and true or false) .. '嘅' .. non_lemma_cat .. '寫法' .. '' ..
			
			(simp and
				('<small>:&nbsp; ' .. link(sub(simp, 1, 1), true) .. ' → ' .. link(sub(simp, 2, 2), true) .. '</small>')
			or
				'') ..
			
			(chain and ',亦即係<span style="font-size:120%">' .. link(new_title, true) .. '</span>嘅\'\'遞個寫法\'\'' or '') .. '。)' .. 
			
			(match(non_lemma_cat, 'simplified') and [=[

|-
| class="mw-collapsible-content" style="background-color: #F5DEB3; font-size: smaller" | <b>Notes:</b>
* [[w:yue:簡化字|簡化字]]主要喺中國大陸同星加坡度用。
* [[w:yue:傳統字|傳統字]]主要喺香港、澳門同臺灣度用。]=] or '') ..

			'\n|}' .. note
	
	local cat = { "variant", "simplified", "obsolete" }
	local m_cat = require("Module:zh-cat")
	
	categories = ''
	for _, word in ipairs(cat) do
		if match(non_lemma_cat, word) then
			categories = categories .. m_cat.categorize(word)
		end
	end
	
	if content then
		local part_a, part_b, part_c = '{{Ѭ[^%}]+' .. curr_title .. '[^%}]*}}', '[^ѬꙀ]+', '({{Ꙁ.+\n%|cat%=[^\n]*\n?}})'
		match_group = mw.ustring.len(gsub(content, "[^Ѭ]", "")) == 1
			and mw.ustring.gmatch(content, '{{Ꙁ[^Ꙁ]+%|cat%=[^\n]*\n?}}')
			or (match(content, part_a)
				and (match(content, part_a .. part_b .. part_c)
					and mw.ustring.gmatch(content, part_a .. part_b .. part_c)
					or mw.ustring.gmatch(content, part_c .. part_b .. part_a))
				or mw.ustring.gmatch(content, part_c))
		
		for match_result in match_group do
			local function find_pron(variety)
				return match(match_result, '|' .. variety .. '=([^|}\n]*)') or ''
			end
			if non_lemma_abbrev == "poj" then
				categories = categories .. frame:expandTemplate{
					title = "Template:zh-pron",
					args = {
						['mn'] = find_pron('mn'),
						['cat'] = find_pron('cat'),
						['only_cat'] = 'yes',
						}
					}
			else
				categories = categories .. frame:expandTemplate{
					title = "Template:zh-pron",
					args = {
						['m'] = find_pron('m'),
						['c'] = find_pron('c'),
						['c-t'] = find_pron('c%-t'),
						['dg'] = find_pron('dg'),
						['g'] = find_pron('g'),
						['h'] = find_pron('h'),
						['j'] = find_pron('j'),
						['mb'] = find_pron('mb'),
						['md'] = find_pron('md'),
						['mn'] = find_pron('mn'),
						['mn-t'] = find_pron('mn%-t'),
						['w'] = find_pron('w'),
						['x'] = find_pron('x'),
						['cat'] = find_pron('cat'),
						['only_cat'] = 'yes',
						}
					}
			end
		end
		
		if non_lemma_abbrev ~= "poj" then
			match_cat = mw.ustring.match(content, "{{zh%-cat%|([^}]+)}}") or nil
			if match_cat then
				match_cat_group = mw.text.split(match_cat, "|")
				categories = categories .. frame:expandTemplate{
					title = "Template:zh-cat",
					args = match_cat_group
				}
			end
			
			match_label = mw.ustring.gmatch(content, "{{lb%|zh%|([^}]+)}}") or nil
			if match_label then
				local lang = require("Module:languages").getByCode("zh")
				for match_label_item in match_label do
					match_label_group = mw.text.split(match_label_item, "|")
					local label_text = require("Module:labels").show_labels(match_label_group, lang)
					categories = categories .. (mw.ustring.match(label_text, "%[%[Category:[^%]]+%]%]") or "")
				end
			end
		else
			categories = categories .. "[[Category:閩南話白話字]]"
		end
	end

	return box .. categories
end

return export