跳转到内容

模組:沙盒/Cookai1205

维基百科,自由的百科全书
local p = {}

-- @param frame.args.1 SVG source code.
function p.svgToDataUri( frame )
	local svg = mw.text.trim( frame.args[ 1 ] )
	if svg == '' then
		return ''
	end
	svg = string.gsub( svg, '>%s*<', '><' )
	local out = 'data:image/svg+xml;utf8,' .. mw.uri.encode( svg, 'PATH' )
	return out
end

function pojInit( ji )
	local out = ''
	local initList = {
		'chh',
		'ch',
		'g',
		'h',
		'j',
		'kh',
		'k',
		'l',
		'm',
		'ng',
		'n',
		'p',
		'ph',
		'b',
		's',
		'tsh',
		'ts',
		'th',
		't'
	}
	local initTranList = {
		chh = 'chh',
		ch = 'ch',
		g = 'g',
		h = 'h',
		j = 'j',
		kh = 'kh',
		k = 'k',
		l = 'l',
		m = 'm',
		ng = 'ng',
		n = 'n',
		p = 'p',
		ph = 'ph',
		b = 'b',
		s = 's',
		tsh = 'chh',
		ts = 'ch',
		th = 'th',
		t = 't'
	}
	for i, init in ipairs( initList ) do
		if mw.ustring.find( ji, init, 1, true ) == 1 then
			out = initTranList[init]
			ji = mw.ustring.gsub( ji, init, '', 1 )
			break
		end
	end
	return out, ji
end

function pojWord( word )
	if not word then return end
	local case = ''
	if word == mw.ustring.upper( word ) then
		case = 'fu'  -- full upper
	elseif word[ 1 ] == mw.ustring.upper( word )[ 1 ] then
		case = 'u'  -- initial upper
	else
		case = 'l'  -- full lower
	end
	local ji = mw.ustring.lower( word )
	local init, out = ''
	init, ji = pojInit( ji )
	out = init .. ji
	return out
end

function p.poj( frame )
	local input = '^' .. mw.text.trim( frame.args[ 1 ] )
	if not input then return end
	local wordPattern = '([%a' ..
		mw.ustring.char( 0x300, 0x301, 0x302, 0x304, 0x306, 0x30b, 0x30c, 0x30d, 0x358 )
		.. ']+)(%d?)'
	local wordList = {}
	for word in mw.ustring.gmatch( input, wordPattern ) do
		table.insert( wordList, word )
	end
	local notWordList = mw.text.split( input, wordPattern, false )
	
	for i = 1, #wordList do
		wordList[i] = pojWord( wordList[i] )
	end
	
	local out = notWordList
	for i = 1, #wordList do
		table.insert( out, i*2, wordList[i] )
	end
	return mw.ustring.sub( table.concat( out ), 2 )
end

return p