มอดูล:Sandbox/Nullzero/Autonumber
คู่มือการใช้งานมอดูล[สร้าง]
คุณอาจจะต้องการสร้างคู่มือการใช้งานของมอดูลนี้ ผู้เขียนสามารถทำการทดลองได้ที่กระบะทราย (สร้าง | คัดลอก) และชุดทดสอบ (สร้าง) ของมอดูลนี้ โปรดเพิ่มหมวดหมู่ไปที่หน้าย่อย /doc หน้าย่อยของมอดูลนี้ |
p = {};
function p.autonum( frame )
frame = frame:getParent()
local numpat_args = p._getParameters( frame.args, {'numpat'} );
local open_args = p._getParameters( frame.args, {'open'} );
local close_args = p._getParameters( frame.args, {'close'} );
local numpat = numpat_args['numpat'];
local opent = open_args['open'];
local closet = close_args['close'];
local title = mw.title.getCurrentTitle();
local wiki_text = title:getContent();
local lentag = string.len(opent)
local escapetag = p._escape(opent)
local textintp = string.match(wiki_text, "open%s*%=%s*" .. escapetag .. "%s*%|.-%|%s*close%s*%=%s*" .. escapetag)
local text = textintp:gsub("open%s*%=%s*" .. escapetag .. "%s*%|", "")
:gsub("%|%s*close%s*%=%s*" .. escapetag, "")
counter = 0
text = text:gsub(p._escape(numpat), function(x)
counter = counter + 1
return tostring(counter)
end)
return frame:preprocess(text)
end
function p._getParameters( frame_args, arg_list )
local new_args = {};
local index = 1;
local value;
for i,arg in ipairs( arg_list ) do
value = frame_args[arg]
if value == nil then
value = frame_args[index];
index = index + 1;
end
new_args[arg] = value;
end
return new_args;
end
function p._escape(x)
return (x:gsub('%%', '%%%%')
:gsub('%^', '%%%^')
:gsub('%$', '%%%$')
:gsub('%(', '%%%(')
:gsub('%)', '%%%)')
:gsub('%.', '%%%.')
:gsub('%[', '%%%[')
:gsub('%]', '%%%]')
:gsub('%*', '%%%*')
:gsub('%+', '%%%+')
:gsub('%-', '%%%-')
:gsub('%?', '%%%?'))
end
return p;