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

来自科学ADV中文wiki
跳到导航 跳到搜索
 
第39行: 第39行:
 
local cur_item = '';
 
local cur_item = '';
 
local initial = true
 
local initial = true
for i = 1, #data do
+
for i = 1, mw.ustring.len(data) do
local ch = string.sub(data, i, i);
+
local ch = mw.ustring.sub(data, i, i);
 
if escaped then
 
if escaped then
 
cur_item = cur_item .. ch;
 
cur_item = cur_item .. ch;

2020年5月26日 (二) 01:58的最新版本

此模块的文档可以在模块: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 = result: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, mw.ustring.len(data) do
		local ch = mw.ustring.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
    	table.insert(parsedParams, cur_item)
    end
	return parsedParams	
end

return p