Module:Text: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(fix several bugs in getPlain)
(factor out delimiter remover)
Line 246: Line 246:
end -- Text.containsCJK()
end -- Text.containsCJK()


Text.removeDelimited = function (s, prefix, suffix)

-- Remove all text in s delimited by prefix and suffix (inclusive)
-- Arguments:
-- s = string to process
-- prefix = initial delimiter
-- suffix = ending delimiter
-- Returns: stripped string
local prefixLen = mw.ustring.len(prefix)
local suffixLen = mw.ustring.len(suffix)
local i = s:find(prefix, 1, true)
local r = s
local j
while i do
j = r:find(suffix, i + prefixLen)
if j then
r = r:sub(1, i - 1)..r:sub(j+suffixLen)
else
r = r:sub(1, i - 1)
end
i = r:find(prefix, 1, true)
end
return r
end


Text.getPlain = function ( adjust )
Text.getPlain = function ( adjust )
Line 253: Line 275:
-- adjust -- string
-- adjust -- string
-- Returns: string
-- Returns: string
local i = adjust:find( "<!--", 1, true )
local r = Text.removeDelimited(adjust,"<!--","-->")
local r = adjust
local j
while i do
j = r:find( "-->", i + 4, true )
if j then
r = r:sub( 1, i - 1) .. r:sub( j + 3 )
else
r = r:sub( 1, i - 1)
end
i = r:find( "<!--", 1, true )
end -- "<!--"
r = r:gsub( "(</?%l[^>]*>)", "" )
r = r:gsub( "(</?%l[^>]*>)", "" )
:gsub( "'''", "" )
:gsub( "'''", "" )