Module:Chart: Difference between revisions

Content deleted Content added
experiment
mw.text module was incorporated - let's try to remove local hack
Line 1:
--[[
next 100 lines or so are copied from mw.text package, which is not yet available.
once this library is added to wmf distributions, the whole chunk should be removed
]]
u = require( "libraryUtil" )
mw = mw or {}
mw.text = mw.text or {}
local htmlencode_map = {
['>'] = '>',
['<'] = '&lt;',
['&'] = '&amp;',
['"'] = '&quot;',
["'"] = '&#039;',
['\194\160'] = '&#nbsp;',
}
local htmldecode_map = {}
for k, v in pairs( htmlencode_map ) do
htmldecode_map[v] = k
end
local decode_named_entities = nil
 
function mw.text.encode( s, charset )
charset = charset or '<>&"\'\194\160'
s = mw.ustring.gsub( s, '[' .. charset .. ']', function ( m )
if not htmlencode_map[m] then
local e = string.format( '&#%d;', mw.ustring.codepoint( m ) )
htmlencode_map[m] = e
htmldecode_map[e] = m
end
return htmlencode_map[m]
end )
return s
end
function mw.text.split( text, pattern, plain )
local ret = {}
for m in gsplit( text, pattern, plain ) do
ret[#ret+1] = m
end
return ret
end
 
function mw.text.gsplit( text, pattern, plain )
local s, l = 1, mw.ustring.len( text )
return function ()
if s then
local e, n = mw.ustring.find( text, pattern, s, plain )
local ret
if not e then
ret = mw.ustring.sub( text, s )
s = nil
elseif n < e then
-- Empty separator!
ret = mw.ustring.sub( text, s, e )
if e < l then
s = e + 1
else
s = nil
end
else
ret = e > s and mw.ustring.sub( text, s, e - 1 ) or ''
s = n + 1
end
return ret
end
end, nil, nil
end
 
function mw.text.tag( name, attrs, content )
local named = false
if type( name ) == 'table' then
named = true
name, attrs, content = name.name, name.attrs, name.content
u.checkTypeForNamedArg( 'tag', 'name', name, 'string' )
u.checkTypeForNamedArg( 'tag', 'attrs', attrs, 'table', true )
else
u.checkType( 'tag', 1, name, 'string' )
u.checkType( 'tag', 2, attrs, 'table', true )
end
 
local ret = { '<' .. name }
for k, v in pairs( attrs or {} ) do
if type( k ) ~= 'string' then
error( "bad named argument attrs to 'tag' (keys must be strings, found " .. type( k ) .. ")",
2 )
end
if string.match( k, '[\t\r\n\f /<>"\'=]' ) then
error( "bad named argument attrs to 'tag' (invalid key '" .. k .. "')", 2 )
end
local tp = type( v )
if tp == 'boolean' then
if v then
ret[#ret+1] = ' ' .. k
end
elseif tp == 'string' or tp == 'number' then
ret[#ret+1] = string.format( ' %s="%s"', k, mw.text.encode( tostring( v ) ) )
else
error( "bad named argument attrs to 'tag' (value for key '" .. k .. "' may not be " .. tp .. ")", 2 )
end
end
 
local tp = type( content )
if content == nil then
ret[#ret+1] = '>'
elseif content == false then
ret[#ret+1] = ' />'
elseif tp == 'string' or tp == 'number' then
ret[#ret+1] = '>'
ret[#ret+1] = content
ret[#ret+1] = '</' .. name .. '>'
else
if named then
u.checkTypeForNamedArg( 'tag', 'content', content, 'string, number, nil, or false' )
else
u.checkType( 'tag', 3, content, 'string, number, nil, or false' )
end
end
 
return table.concat( ret )
end
 
function mw.text.trim( s )
return mw.ustring.match(s, "^%s*(.-)%s*$")
end
 
-- everything up to here should be removed once mw.text becomes avaulable.
 
function barChart( frame )
local res = {}