模块:FlattenMarks

来自Minecraft Wiki
跳转到导航 跳转到搜索
[ 创建 | 刷新 ]文档页面
此模块没有文档页面。如果你知道此模块的使用方法,请帮助为其创建文档页面。
local p = {}

local langs = { 'zh-cn', 'zh-tw', 'zh-hk' }

function p.parse( str, lang )
	local function parse (s)
		local m = s:match('^%-{zh%-%l%l|(.*)}%-$')
		if m then return m end
		local cn, tw, hk = s:match('^%-{zh%-cn:(.*);zh%-tw:(.*);zh%-hk:(.*);}%-$')
		m = ({
			['zh-cn'] = cn,
			['zh-tw'] = tw,
			['zh-hk'] = hk,
		})[lang]
		return m or s:match('^%-{(.*)}%-$')
	end
	while str:match('-{') do
		local result = {}
		local start = 0
		for i = 1, #str, 1 do
			if str:sub(i - 1, i) == '}-' and start > 0 then
				table.insert(result, parse(str:sub(start, i)))
				start = i + 1
				break
			elseif str:sub(i, i + 1) == '-{' then
				if i > start then table.insert(result, str:sub(start, i - 1)) end
				start = i
			end
		end
		str = table.concat(result) .. str:sub(start, #str)
	end
	return str
end

function p.conv( text, map )
	map = map or function (...) return ... end
	local result = '-{'
	for i, lang in ipairs( langs ) do
		result = result .. lang .. ':-{' .. lang .. '|' .. map( p.parse( text, lang ) ) .. '}-;'
	end
	return result .. '}-'
end

function p.main( f )
	return p.conv( f:getParent().args[1] )
end

return p