Module:Row numbers: Difference between revisions
en>Trappist the monk strip marker id is hex; |
en>Trappist the monk +_row_count_hold |
||
| Line 1: | Line 1: | ||
require('Module:No globals'); | require('Module:No globals'); | ||
local p={} | local p={} | ||
local count = 0; -- initial values | |||
local hcount = count; | |||
--[[--------------------------< G E T _ C O U N T >------------------------------------------------------------ | |||
returns a counter value according to the keyword extracted from the table; maintains count and hcount | |||
The keywords have the meanings: | |||
_row_count: use row counter value (count); the hold counter (hcount) is same as count | |||
_row_count_hold: use the value currently assigned to hcount; bump count but do not bump hcount | |||
]] | |||
local function get_count (keyword) | |||
count = count + 1; -- always bump the count | |||
if '_row_count' == keyword then -- bump hcount, return new count value | |||
hcount = count; | |||
return count; | |||
elseif '_row_count_hold' == keyword then -- current hcount value without increment | |||
return hcount; | |||
end | |||
end | |||
--[[--------------------------< R O W _ C O U N T E R >-------------------------------------------------------- | |||
replaces keywords _row_count and _row_count_hold from the table in frame.args[1] | |||
]] | |||
function p.row_counter (frame) | function p.row_counter (frame) | ||
| Line 6: | Line 37: | ||
return '<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: missing nowiki tags</span>'; | return '<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: missing nowiki tags</span>'; | ||
end | end | ||
local tbl_str = mw.text.unstripNoWiki (frame.args[1]); -- get an already rendered table from whereever <nowiki>...</nowiki> put it | local tbl_str = mw.text.unstripNoWiki (frame.args[1]); -- get an already rendered table from whereever <nowiki>...</nowiki> put it | ||
| Line 13: | Line 43: | ||
tbl_str = tbl_str:gsub ('>', '>'); -- mw.text.decode (tbl_str); is too aggressive | tbl_str = tbl_str:gsub ('>', '>'); -- mw.text.decode (tbl_str); is too aggressive | ||
while (tbl_str:find ('_row_count')) do | while (tbl_str:find ('_row_count[^%s|]*')) do -- if there is at least one of our special reserved words | ||
tbl_str = tbl_str:gsub ('_row_count', | tbl_str = tbl_str:gsub ('_row_count[^%s|]*', get_count, 1); -- replace it with a count | ||
end | end | ||
return frame:preprocess (tbl_str); -- done | return frame:preprocess (tbl_str); -- done | ||
Revision as of 13:50, 4 May 2018
Implements {{Row numbers}}
require('Module:No globals');
local p={}
local count = 0; -- initial values
local hcount = count;
--[[--------------------------< G E T _ C O U N T >------------------------------------------------------------
returns a counter value according to the keyword extracted from the table; maintains count and hcount
The keywords have the meanings:
_row_count: use row counter value (count); the hold counter (hcount) is same as count
_row_count_hold: use the value currently assigned to hcount; bump count but do not bump hcount
]]
local function get_count (keyword)
count = count + 1; -- always bump the count
if '_row_count' == keyword then -- bump hcount, return new count value
hcount = count;
return count;
elseif '_row_count_hold' == keyword then -- current hcount value without increment
return hcount;
end
end
--[[--------------------------< R O W _ C O U N T E R >--------------------------------------------------------
replaces keywords _row_count and _row_count_hold from the table in frame.args[1]
]]
function p.row_counter (frame)
if not frame.args[1]:match ('^%s*\127[^\127]*UNIQ%-%-nowiki%-%x%x%x%x%x%x%x%x%-QINU[^\127]*\127%s*$') then -- make sure that what we get for input has been wrapped in <nowiki>...</nowiki> tags
return '<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: missing nowiki tags</span>';
end
local tbl_str = mw.text.unstripNoWiki (frame.args[1]); -- get an already rendered table from whereever <nowiki>...</nowiki> put it
tbl_str = tbl_str:gsub ('<', '<'); -- replace these character entities with their actual characters
tbl_str = tbl_str:gsub ('>', '>'); -- mw.text.decode (tbl_str); is too aggressive
while (tbl_str:find ('_row_count[^%s|]*')) do -- if there is at least one of our special reserved words
tbl_str = tbl_str:gsub ('_row_count[^%s|]*', get_count, 1); -- replace it with a count
end
return frame:preprocess (tbl_str); -- done
end
return p;