Module:zh-hanzi-box

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
The following documentation is located at Module:zh-hanzi-box/documentation. [edit]

-- This module is currently used by Template:zh-hanzi-box, which is meant to be a replacement for both zh-hanzi and Hani-forms
-- CSS is in MediaWiki:Common.css (search for zh-hanzi-box)
local export = {}
 
local function create_combined_box(title)
	local combined_box = [=[{| class="zh-hanzi-box"
! colspan="4" | [[Simplified Chinese|simpl.]] and [[Traditional Chinese|trad.]]
|- 
| lang="zh" class="Hani" | ]=] .. add_glosses(title) .. [=[</span>
|}]=]
	return combined_box
end
 
local function create_separate_box(simtitle, tratitle)
	local separate_box = [=[{| class="zh-hanzi-box"
! colspan="2" | [[Simplified Chinese|simpl.]] 
| lang="zh" class="Hani" | ]=] .. add_glosses(simtitle) .. [=[ </span>
|- 
! colspan="2" | [[Traditional Chinese|trad.]] 
| lang="zh" class="Hani" | ]=] .. add_glosses(tratitle) .. [=[ </span>
|}]=]
   return separate_box
end
 
local function check_box(sim,tra)
	if mw.title.getCurrentTitle().nsText == '' then
		for item in mw.text.gsplit((sim .. '/' .. tra):gsub('or','/'):gsub(' ','/'):gsub('[ %[%]]',''):gsub('/+','/'):gsub('/','/'),"/",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_zh_data, m_glosses_data = require("Module:zh"), require("Module:zh/data"), require("Module:zh/data/glosses")
	if mw.ustring.len(mw.title.getCurrentTitle().text) == 1 or mw.ustring.match(text, '%|') then
		return text
	end
	for link in mw.ustring.gmatch(text, '%[%[[^%[%]]+%]%]') do
		link = mw.ustring.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_zh_data.st[character]] then
				character = '<abbr title="' .. (m_glosses_data.glosses[character] or m_glosses_data.glosses[m_zh_data.st[character]]) .. '">' .. character .. '</abbr>'
			end
			link = mw.ustring.gsub(link, original_character, character)
		end
		link = original_link .. '|' .. link
		text = mw.ustring.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,'')
	else
		return create_separate_box(sim, tra) .. check_box(sim,tra)
	end
end
 
return export