Pergi ke kandungan

Modul:zh-hanzi-box

Daripada Wikikamus
-- 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" | [[Tulisan Cina Ringkas|ringkas]] dan [[Tulisan Cina Tradisional|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" | [[Tulisan Cina Ringkas|ringkas]] 
| lang="zh" class="Hani" | ]=] .. add_glosses(simtitle) .. [=[ </span>
|- 
! colspan="2" | [[Tulisan Cina Tradisional|trad.]] 
| lang="zh" class="Hani" | ]=] .. add_glosses(tratitle) .. [=[ </span>
|}]=]
   return separate_box
end
 
local function check_box(sim,tra)
	local title = mw.title.getCurrentTitle()
	if title.nsText == '' and title.exists 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:Istilah Cina dengan bentuk yang tidak dicipta]]<small class="attentionseeking">(Sekurang-kurangnya salah satu bentuk dalam kotak hanzi adalah tidak dicipta. Dikesan: "[[' .. 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("Parameter pertama diperlukan!")
	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