模組:Zh-glyph

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


local export = {}
local sub = mw.ustring.sub

local eras = {
	{ "[[w:商朝|商]]", { "bronze-shang", "oracle" } },
	{ "[[w:西周|西周]]", { "bronze" } },
	{ "[[w:春秋时期|春秋時期]]", { "bronze-spring" } },
	{ "[[w:战国时期|戰國時期]]", { "bronze-warring", "silk", "slip" } },
	{ "《[[w:说文解字|說文解字]]》<br>(漢·[[w:許慎|許慎]])", { "zhou", "ancient", "odd", "seal", "vulgar" } },
	{ "《汗簡》<br>(宋·[[w:郭忠恕|郭忠恕]])", { "hanjian" } },
	{ "《古文四聲韻》<br>(宋·[[w:夏竦|夏竦]])", { "GWSSY" } },
	{ "《集篆古文韻海》<br>(宋·杜從古)", { "JZGWYH" } },
	{ "《六書通》<br>(明·[[w:閔齊伋|閔齊伋]])", { "bigseal" } },
	{ "《隸辨》<br>(清·顧藹吉)", { "clerical" } },
	{ "[[w:開成石經|開成石經]]", { "kai-Kaicheng" } },
	{ "《[[w:康熙字典|康熙字典]]》<br>(於[[w:清朝|清朝]]編纂)", { "mingti-Kangxi" } }
}

local scripts = {
	["bronze-shang"] = "[[w:金文|金文]]",
	["oracle"] = "[[w:甲骨文|甲骨文]]",
	["oracle-zhouyuan"] = "[[w:甲骨文|周原甲骨文]]",
	["bronze"] = "[[w:金文|金文]]",
	["bronze-spring"] = "[[w:金文|金文]]",
	["bronze-warring"] = "[[w:金文|金文]]",
	["silk"] = "[[w:简牍|楚系簡帛文字]]",
	["slip"] = "[[w:简牍|秦系簡牘文字]]",
	["zhou"] = "[[w:籀文|籀文(大篆)]]",
	["ancient"] = "古文字",
	["odd"] = "[[w:生僻字|生僻字]]",
	["seal"] = "[[w:小篆|小篆]]",
	["vulgar"] = "[[w:俗字|俗字]]",
	["bigseal"] = "傳抄古文字",
	["hanjian"] = "傳抄古文字",
	["GWSSY"] = "傳抄古文字",
	["JZGWYH"] = "傳抄古文字",
	["clerical"] = "[[w:隶书|隸書]]",
	["kai-Kaicheng"] = "[[w:楷书|楷書]]",
	["mingti-Kangxi"] = "[[w:宋体|明體]]",
}

local hide_scripts = { "oracle", "bronze", "seal", "bigseal" }

local script_abbrev = {
	["B"] = "bronze",
	["b"] = "bronze",
	["31"] = "bronze",
	["32"] = "bronze",
	["33"] = "bronze",
	["34"] = "bronze",
	["j"] = "oracle",
	["41"] = "oracle",
	["42"] = "oracle",
	["43"] = "oracle",
	["51"] = "silk",
	["52"] = "silk",
	["53"] = "silk",
	["71"] = "slip",
	["Q"] = "slip",
	["S"] = "silk",
	["s"] = "seal",
	["27"] = "seal",
	["L"] = "bigseal",
}

function export.main(frame)
	local args = frame:getParent().args
	local pagename = mw.title.getCurrentTitle().subpageText
	local target_page = args[1] or pagename
	local showEras, showScripts, showImgs, hideText = {}, {}, {}, {}
	local success, data_module = pcall(mw.loadData, "Module:zh/data/glyph-data/" .. target_page)

	local char_data = {
		["bronze"] = {},
		["oracle"] = {},
		["silk"] = {},
		["slip"] = {},
		["seal"] = {},
		["bigseal"] = {},
	}

	count = 0
	if not frame:getParent().args["no_img"] then
		for _, era in ipairs(eras) do
			local count_era = 0
			for _, script in ipairs(era[2]) do
				local img_link = target_page .. "-" .. script .. ".svg"
				local img_file = mw.title.new("Media:" .. img_link)
				if img_file.exists and not img_file.isRedirect then
					count = count + 1
					count_era = count_era + 1
					table.insert(showScripts, "\n!" .. scripts[script])
					table.insert(showImgs, "\n| style=\"padding: 8px;\"|[[File:" .. img_link .. "|60px]]")
				end
			end
			if count_era > 0 then
				table.insert(showEras, "\n! colspan=\"" .. count_era .. "\"|" .. era[1])
			end
		end
	end
	if success then
		local img_count = 0
		for _, img in ipairs(data_module[1]) do
			img_count = img_count + 1
		end
		success = success and mw.title.new("Media:ACC-" .. data_module[1][img_count] .. ".svg").exists
	end
	if success then
		local capacity = 8
		for _, img in ipairs(data_module[1]) do
			table.insert(char_data[script_abbrev[sub(img, 1, 1)]], '<td><div class="gallerybox"' ..
				'>\n<div style="text-align:center; padding:8px" class="thumb">[[File:ACC-' .. img ..
				'.svg|60px]]\n<div class="gallerytext">\n<p><small>' .. img .. '</small></p>\n</div>\n</div>\n</div>\n</td>')
		end
		for _, script in ipairs(hide_scripts) do
			if char_data[script][1] then
				local script_concat = { "\n----\n<table class=\"gallery\">\n<caption><b>", scripts[script], "</b></caption>\n" }
				script_data = char_data[script]
				for i = 0, math.ceil(#script_data / capacity) - 1 do
					table.insert(script_concat, "<tr>")
					for j = 1, capacity do
						table.insert(script_concat, script_data[i*capacity+j] or nil)
					end
					table.insert(script_concat, "</tr>")
				end
				table.insert(script_concat, "\n</table>")
				table.insert(hideText, table.concat(script_concat))
			end
		end
		table.insert(hideText, '\n|- class="vsHide"\n| width="100%" align="left" colspan="' .. (count == 0 and 1 or count) ..
			'|\n----<div class="toccolours mw-collapsible mw-collapsed"><b>參考</b>:' ..
			'<div class="mw-collapsible-content">\n主要來自[[w:理查德·西尔斯 (学者)|理查德·西爾斯]]的' ..
			"[http://hanziyuan.net/#" ..
			target_page .. " 漢字字源網]([[c:Commons:Ancient Chinese characters/Richard Sears Agreement" ..
			"|允許使用說明]]),<br>收集來自不同形式的古文字的繪圖資料" ..
			",包括:\n* 《說文解字》(小篆)\n* 《金文編》(金文)" ..
			"\n* 《六書通》(傳抄古文字)\n* 《殷墟甲骨文編》(甲骨文)</div></div>")
	end

	if ( success or count > 0 ) and not frame:getParent().args["no_img"] then

		glyph_forms = [=[{| class="wikitable zh-glyph]=] .. (success and ' vsSwitcher" data-toggle-category="forms' or "") ..
			[=[" cellpadding=7 style="border-spacing: 1px; border: 1px solid darkgray; text-align:center"
			! class="vsToggleElement" colspan="]=] .. (count == 0 and 1 or count) ..
			[=[" |古代字體(<span lang="zh-Hant" class="Hani">-{]=] .. target_page .. [=[}-</span>)]=] ..

			(count > 0
				and "\n|-" .. table.concat(showEras) .. "\n|-" .. table.concat(showScripts) .. "\n|-" .. table.concat(showImgs)
				or "") ..

			(success
				and "\n|- class=\"vsHide\"\n| align=\"center\" colspan=\"" .. (count == 0 and 1 or count) ..
					"|" .. table.concat(hideText, "")
				or "") ..

			"\n|}"

	else
		glyph_forms = ""
	end

	local phonText = {}
	local list = mw.loadData("Module:zh-glyph/phonetic/list")
	phonComp = list[target_page] or nil

	if phonComp and not frame:getParent().args["no_phon"] then
		table.insert(phonText,
			'\n' ..
			'<div class="NavFrame" style="border:0px; max-width: 37em; text-align:center;" cellpadding="6">' ..
			'<div class="NavHead" style="font-size:105%; border:1px solid #aaaaaa; margin-left:-1px; background-color:#CCCCFF; text-align:center;" cellpadding="3">' ..
			'同聲符字(' ..
			'<span class="Hani">-{[[' .. phonComp .. ']]}-</span>' ..
			'<small>([[w:郑张尚芳|鄭張尚芳 (2003)]])</small>&nbsp;)' ..
			'</div>' ..
			'<div class="NavContent">' ..
			'\n{| class="wikitable zh-glyph" style="margin-top: 0; width: 100%;"' ..
			'\n|-\n! align="center"|\n!上古漢語'
		)

		local phonData = mw.loadData("Module:zh-glyph/phonetic")
		local m_och = require("Module:och-pron").retrieve_pron

		for item in mw.text.gsplit(phonData[phonComp], "") do
			table.insert(phonText, '\n|-\n| align="center" |<span class="Hani">-{[[' .. item .. '#漢語|' .. item .. ']]}-</span>')
			local IPA = m_och(item, nil, true)
			if IPA then
				table.insert(phonText, '\n|<small><span class="IPA">' .. IPA .. '</span></small>')
			end
		end
		table.insert(phonText, '\n|}\n</div></div>')
	end

	return glyph_forms .. table.concat(phonText)
end

return export