“模块:ArrayTable”的版本间的差异
跳到导航
跳到搜索
(创建页面,内容为“local p = {} function p.render(frame) local params = {} if frame:getParent() ~= nil then frame = frame:getParent() end local tableParams = parseTableParams(fr…”) |
小 |
||
(未显示同一用户的4个中间版本) | |||
第17行: | 第17行: | ||
end | end | ||
local result = mw.html.create() | local result = mw.html.create() | ||
− | local tbl = | + | local tbl = result:tag('table'):addClass(tableClass) |
for i = 1, #tableParams do | for i = 1, #tableParams do | ||
tbl:tag('tr'):tag('td'):wikitext(tableParams[i]) | tbl:tag('tr'):tag('td'):wikitext(tableParams[i]) | ||
第39行: | 第39行: | ||
local cur_item = ''; | local cur_item = ''; | ||
local initial = true | local initial = true | ||
− | for i = 1, | + | for i = 1, mw.ustring.len(data) do |
− | local ch = | + | local ch = mw.ustring.sub(data, i, i); |
if escaped then | if escaped then | ||
cur_item = cur_item .. ch; | cur_item = cur_item .. ch; | ||
第47行: | 第47行: | ||
escaped = true | escaped = true | ||
elseif ch == splitter then | elseif ch == splitter then | ||
− | + | table.insert(parsedParams, cur_item) | |
cur_item = '' | cur_item = '' | ||
initial = false | initial = false | ||
第55行: | 第55行: | ||
end | end | ||
if not initial then | if not initial then | ||
− | + | table.insert(parsedParams, cur_item) | |
end | end | ||
return parsedParams | return parsedParams |
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