Module:HSK list

From Wiktionary, the free dictionary
Jump to navigation Jump to search
local u = require("Module:string/char")

local export = {}

local function cmn_link(word, script)
	return '<span class="' .. script .. '" lang="cmn">[[' .. word
		.. '#Chinese|' .. word .. ']]</span>'
end

local function cmn_tr(word)
	return '<span lang="cmn-Latn" class="tr-Latn tr">' .. word .. '</span>'
end

local open_paren = '<span class="mention-gloss-paren annotation-paren">(</span>'
local close_paren = '<span class="mention-gloss-paren annotation-paren">)</span>'

function export.link(frame)
	local text = frame.args[1] or frame:getParent().args[1]
	
	-- Matches Latin and doesn't match Han characters; that's all that's needed.
	local Latin = "[" .. u(0x0061) .. "-" .. u(0x0370) .. "]"
	
	local zh_link = require "Module:zh/link".link
	
	text = text:gsub(
		'%f[^\n]%* *([^\n]+)',
		function (line)
			local items = mw.text.split(line, "%s*,%s*")
			
			-- Allow multiple pinyin to be input, separated by commas.
			-- Assume that if an item is pinyin, everything after it is pinyin,
			-- and that if there is a fourth item, it's pinyin.
			if items[4] then
				items[3] = table.concat(items, ", ", 3)
				items[4] = nil
			elseif items[3] and mw.ustring.find(items[2], Latin) then
				items[2] = table.concat(items, ", ", 2)
				items[3] = nil
			end
			
			if items[2] then
				line = zh_link(nil, nil, items)
			else
				mw.log(line)
				return ""
			end
			return "* " .. line
		end)
	
	return text
end

return export