Module:zh-hanzi-box

ពីWiktionary

Documentation for this module may be created at Module:zh-hanzi-box/doc

-- This module is currently used by Template:zh-hanzi-box, which is meant to be a replacement for both zh-hanzi and Hani-forms
local export = {}

local gsub = mw.ustring.gsub
local match = mw.ustring.match
local len = mw.ustring.len

local function create_combined_box(title)
	local combined_box = [=[{| class="zh-hanzi-box"
! [[Traditional Chinese|trad.]] and [[Simplified Chinese|simpl.]]
|- 
| lang="zh" class="Hani" | ]=] .. add_glosses(title) .. [=[ 
|}]=]
	return combined_box
end

local function create_separate_box(simtitle, tratitle)
	local separate_box = [=[{| class="zh-hanzi-box"
! [[Traditional Chinese|trad.]] 
| lang="zh-Hant" class="Hani" | ]=] .. add_glosses(tratitle) .. [=[ 
|-
! [[Simplified Chinese|simpl.]] 
| lang="zh-Hans" class="Hani" | ]=] .. add_glosses(simtitle) .. [=[ 
|}]=]
   return separate_box
end

local function check_box(sim,tra)
	local title = mw.title.getCurrentTitle()
	if title.nsText == '' and title.exists then
		local text = sim .. '/' .. tra
		text = gsub(text,'%[%[[^%]|]+|([^%]|]+)%]%]','%1')
		text = gsub(text,'%[%[File:[^%]]+|link=([^%]]+)%]%]','%1')
		text = gsub(text,'or','/')
		text = gsub(text,' ','/')
		text = gsub(text,'[%[%]]','')
		text = gsub(text,'/+','/')
		for item in mw.text.gsplit(text,"/",true) do
			if item ~= '' and not (mw.title.new(item) or {}).exists then
				return '[[Category:Chinese terms with uncreated forms]]<small class="attentionseeking">(At least one of the forms in the hanzi box is uncreated. Detected: "[[' .. item .. ']]".)</small>'
			end
		end
	end
	return ''
end

function add_glosses(text)
	local link, character = '', ''
	local m_zh, m_st_data, m_glosses_data = require("Module:zh"), mw.loadData("Module:zh/data/st"), mw.loadData("Module:zh/data/glosses")
	if len(mw.title.getCurrentTitle().text) == 1 or match(text, '%|') then
		return text
	end
	for link in mw.ustring.gmatch(text, '%[%[[^%[%]]+%]%]') do
		link = gsub(link, '[%[%]]', '')
		local original_link = link
		for character in mw.ustring.gmatch(link, '[一-鿿㐀-䶿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩]') do
			local original_character = character
			if m_glosses_data.glosses[character] or m_glosses_data.glosses[m_st_data.st[character]] then
				character = '<span class="explain" title="' .. (m_glosses_data.glosses[character] or m_glosses_data.glosses[m_st_data.st[character]]) .. '">' .. character .. '</span>'
			end
			link = gsub(link, original_character, character)
		end
		link = original_link .. '|' .. link
		text = gsub(text, '%[%['..original_link..'%]%]', '[['..link..']]')
	end
	return text
end

-- This function should be publicly invoked by a template
-- It takes two arguments, the first of which is required and second optional. 
-- The first parameter represents the simplified form of an entry. If the characters are the same in both scripts, then only the first parameter is used.
-- The second parameter optionally represents the traditional form of an entry, if applicable.
function export.init(frame)
	local arguments = frame:getParent().args
	--local sim = arguments[1] or error("First parameter is required!")
	local sim = arguments[1] or ""
	local tra = arguments[2] or ""

	if tra == "" then
		return create_combined_box(sim) .. check_box(sim,'')
		.. require("Module:TemplateStyles")("Module:zh-hanzi-box/style.css")
	else
		return create_separate_box(sim, tra) .. check_box(sim,tra)
		.. require("Module:TemplateStyles")("Module:zh-hanzi-box/style.css")
	end
end

return export