“模块:ArrayTable”的版本间的差异

来自科学ADV中文wiki
跳到导航 跳到搜索
第47行: 第47行:
 
escaped = true
 
escaped = true
 
elseif ch == splitter then
 
elseif ch == splitter then
parsedParams.append(cur_item)
+
table.insert(parsedParams, cur_item)
 
cur_item = ''
 
cur_item = ''
 
initial = false
 
initial = false

2020年5月12日 (二) 18:53的版本

此模块的文档可以在模块:ArrayTable/doc创建

local p = {}

function p.render(frame)
	local params = {}
	if frame:getParent() ~= nil then
		frame = frame:getParent()
	end
	local tableParams = parseTableParams(frame)
	if #tableParams == 0 then
		return ''
	end
	local tableClass
	if frame.args['class'] then
		tableClass = frame.args['class']
	else
		tableClass = 'array-table'
	end
	local result = mw.html.create()
	local tbl = html:tag('table'):addClass(tableClass)
	for i = 1, #tableParams do
		tbl:tag('tr'):tag('td'):wikitext(tableParams[i])
	end
	return tostring(result)
end

function parseTableParams(frame)
	local parsedParams = {}
	local index = 1
	local data = frame.args[1];
	if data == nil then
		data = ''
	end
	local splitter = frame.args[2];
	if splitter == nil then
		splitter = ','
	end
	local escape_char = '\\'
	local escaped = false
	local cur_item = '';
	local initial = true
	for i = 1, #data do
		local ch = string.sub(data, i, i);
		if escaped then
			cur_item = cur_item .. ch;
			escaped = false
		elseif ch == escape_char then
			escaped = true
		elseif ch == splitter then
			table.insert(parsedParams, cur_item)
			cur_item = ''
			initial = false
		else
			cur_item = cur_item .. ch;
		end
	end
    if not initial then
    	parsedParams.append(cur_item)
    end
	return parsedParams	
end

return p