Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/mw.text.lua

raw symbols SCIP
1 local mwtext = {}
2 local php
3 local options
4
5 local util = require 'libraryUtil'
6 local checkType = util.checkType
7 local checkTypeForNamedArg = util.checkTypeForNamedArg
8
9 function mwtext.setupInterface( opts )
10 -- Boilerplate
11 mwtext.setupInterface = nil
12 php = mw_interface
13 mw_interface = nil
14 options = opts
15
16 -- Register this library in the "mw" global
17 mw = mw or {}
18 mw.text = mwtext
19
20 package.loaded['mw.text'] = mwtext
21 end
22
23 function mwtext.trim( s, charset )
24 if not charset then
25 return string.match( s, '^()%s*$' ) and '' or string.match( s, '^%s*(.*%S)' )
26 else
27 return ( mw.ustring.gsub( s, '^[' .. charset .. ']*(.-)[' .. charset .. ']*$', '%1' ) )
28 end
29 end
30
31 local htmlencode_map = {
32 ['>'] = '>',
33 ['<'] = '&lt;',
34 ['&'] = '&amp;',
35 ['"'] = '&quot;',
36 ["'"] = '&#039;',
37 ['\194\160'] = '&nbsp;',
38 }
39 local htmldecode_map = {}
40 for k, v in pairs( htmlencode_map ) do
41 htmldecode_map[v] = k
42 end
43 local decode_named_entities = nil
44
45 function mwtext.encode( s, charset )
46 charset = charset or '<>&"\'\194\160'
47 s = mw.ustring.gsub( s, '[' .. charset .. ']', function ( m )
48 if not htmlencode_map[m] then
49 local e = string.format( '&#%d;', mw.ustring.codepoint( m ) )
50 htmlencode_map[m] = e
51 htmldecode_map[e] = m
52 end
53 return htmlencode_map[m]
54 end )
55 return s
56 end
57
58 function mwtext.decode( s, decodeNamedEntities )
59 local dec
60 if decodeNamedEntities then
61 if decode_named_entities == nil then
62 decode_named_entities = php.getEntityTable()
63 setmetatable( decode_named_entities, { __index = htmldecode_map } )
64 end
65 dec = decode_named_entities
66 else
67 dec = htmldecode_map
68 end
69 -- string.gsub is safe here, because only ASCII chars are in the pattern
70 s = string.gsub( s, '(&(#?x?)([a-zA-Z0-9]+);)', function ( m, flg, name )
71 if not dec[m] then
72 local n = nil
73 if flg == '#' then
74 n = tonumber( name, 10 )
75 elseif flg == '#x' then
76 n = tonumber( name, 16 )
77 end
78 if n and n <= 0x10ffff then
79 n = mw.ustring.char( n )
80 if n then
81 htmldecode_map[m] = n
82 htmlencode_map[n] = m
83 end
84 end
85 end
86 return dec[m]
87 end )
88 return s
89 end
90
91 local nowikiRepl1 = {
92 ['"'] = '&#34;',
93 ['&'] = '&#38;',
94 ["'"] = '&#39;',
95 ['<'] = '&#60;',
96 ['='] = '&#61;',
97 ['>'] = '&#62;',
98 ['['] = '&#91;',
99 [']'] = '&#93;',
100 ['{'] = '&#123;',
101 ['|'] = '&#124;',
102 ['}'] = '&#125;',
103 [';'] = '&#59;',
104 }
105
106 local nowikiRepl2 = {
107 ["\n!"] = "\n&#33;", ["\r!"] = "\r&#33;",
108 ["\n#"] = "\n&#35;", ["\r#"] = "\r&#35;",
109 ["\n*"] = "\n&#42;", ["\r*"] = "\r&#42;",
110 ["\n:"] = "\n&#58;", ["\r:"] = "\r&#58;",
111 ["\n "] = "\n&#32;", ["\r "] = "\r&#32;",
112 ["\n\n"] = "\n&#10;", ["\r\n"] = "&#13;\n",
113 ["\n\r"] = "\n&#13;", ["\r\r"] = "\r&#13;",
114 ["\n\t"] = "\n&#9;", ["\r\t"] = "\r&#9;",
115 }
116
117 local nowikiRepl3 = {
118 ['+'] = '&#43;',
119 ['-'] = '&#45;',
120 ['_'] = '&#95;',
121 ['~'] = '&#126;',
122 ["\n"] = "&#10;",
123 ["\r"] = "&#13;",
124 ["\t"] = "&#9;",
125 }
126
127 local nowikiRepl4 = nowikiRepl3
128
129 local nowikiReplMagic = {}
130 for sp, esc in pairs( {
131 [' '] = '&#32;',
132 ['\t'] = '&#9;',
133 ['\r'] = '&#13;',
134 ['\n'] = '&#10;',
135 ['\f'] = '&#12;',
136 } ) do
137 nowikiReplMagic['ISBN' .. sp] = 'ISBN' .. esc
138 nowikiReplMagic['RFC' .. sp] = 'RFC' .. esc
139 nowikiReplMagic['PMID' .. sp] = 'PMID' .. esc
140 end
141
142 function mwtext.nowiki( s )
143 -- string.gsub is safe here, because we're only caring about ASCII chars
144 s = string.gsub( s, '["&\'<=>%[%]{|};]', nowikiRepl1 )
145 s = '\n' .. s
146 s = string.gsub( s, '[\r\n][!#*: \n\r\t]', nowikiRepl2 )
147 s = string.gsub( s, '([\r\n])%-%-%-%-', '%1&#45;---' )
148 s = string.sub( s, 2 )
149 s = string.gsub( s, '!!', '&#33;!' )
150 s = string.gsub( s, '__', '_&#95;' )
151 s = string.gsub( s, '://', '&#58;//' )
152 s = string.gsub( s, '~~~', '~~&#126;' )
153 -- protect first and last character
154 s = string.gsub( s, '^[%-+_~]', nowikiRepl3 )
155 s = string.gsub( s, '[_~\r\n\t]$', nowikiRepl4 )
156 -- technically, should only do these if $wgEnableMagicLinks, but
157 -- it doesn't hurt to be safe
158 s = string.gsub( s, 'ISBN%s', nowikiReplMagic )
159 s = string.gsub( s, 'RFC%s', nowikiReplMagic )
160 s = string.gsub( s, 'PMID%s', nowikiReplMagic )
161 for k, v in pairs( options.nowiki_protocols ) do
162 s = string.gsub( s, k, v )
163 end
164
165 return s
166 end
167
168 function mwtext.tag( name, attrs, content )
169 local named = false
170 if type( name ) == 'table' then
171 named = true
172 name, attrs, content = name.name, name.attrs, name.content
173 checkTypeForNamedArg( 'tag', 'name', name, 'string' )
174 checkTypeForNamedArg( 'tag', 'attrs', attrs, 'table', true )
175 else
176 checkType( 'tag', 1, name, 'string' )
177 checkType( 'tag', 2, attrs, 'table', true )
178 end
179
180 local ret = { '<' .. name }
181 for k, v in pairs( attrs or {} ) do
182 if type( k ) ~= 'string' then
183 error( "bad named argument attrs to 'tag' (keys must be strings, found " .. type( k ) .. ")", 2 )
184 end
185 if string.match( k, '[\t\r\n\f /<>"\'=]' ) then
186 error( "bad named argument attrs to 'tag' (invalid key '" .. k .. "')", 2 )
187 end
188 local tp = type( v )
189 if tp == 'boolean' then
190 if v then
191 ret[#ret+1] = ' ' .. k
192 end
193 elseif tp == 'string' or tp == 'number' then
194 ret[#ret+1] = string.format( ' %s="%s"', k, mwtext.encode( tostring( v ) ) )
195 else
196 error( "bad named argument attrs to 'tag' (value for key '" .. k .. "' may not be " .. tp .. ")", 2 )
197 end
198 end
199
200 local tp = type( content )
201 if content == nil then
202 ret[#ret+1] = '>'
203 elseif content == false then
204 ret[#ret+1] = ' />'
205 elseif tp == 'string' or tp == 'number' then
206 ret[#ret+1] = '>'
207 ret[#ret+1] = content
208 ret[#ret+1] = '</' .. name .. '>'
209 else
210 if named then
211 checkTypeForNamedArg( 'tag', 'content', content, 'string, number, nil, or false' )
212 else
213 checkType( 'tag', 3, content, 'string, number, nil, or false' )
214 end
215 end
216
217 return table.concat( ret )
218 end
219
220 function mwtext.unstrip( s )
221 return php.unstrip( s )
222 end
223
224 -- getOrigTextWhenPreprocessing defaults to false since Lua modules
225 -- expect to use the nowiki contents (innerXML) rather than the
226 -- full tag source (outerXML). This flag is a workaround to support
227 -- Parsoid and other clients that process preprocessed wikitext.
228 function mwtext.unstripNoWiki( s, getOrigTextWhenPreprocessing )
229 return php.unstripNoWiki( s, getOrigTextWhenPreprocessing or false )
230 end
231
232 function mwtext.killMarkers( s )
233 return php.killMarkers( s )
234 end
235
236 function mwtext.split( text, pattern, plain )
237 local ret = {}
238 for m in mwtext.gsplit( text, pattern, plain ) do
239 ret[#ret+1] = m
240 end
241 return ret
242 end
243
244 function mwtext.gsplit( text, pattern, plain )
245 local s, l = 1, mw.ustring.len( text )
246 return function ()
247 if s then
248 local e, n = mw.ustring.find( text, pattern, s, plain )
249 local ret
250 if not e then
251 ret = mw.ustring.sub( text, s )
252 s = nil
253 elseif n < e then
254 -- Empty separator!
255 ret = mw.ustring.sub( text, s, e )
256 if e < l then
257 s = e + 1
258 else
259 s = nil
260 end
261 else
262 ret = e > s and mw.ustring.sub( text, s, e - 1 ) or ''
263 s = n + 1
264 end
265 return ret
266 end
267 end, nil, nil
268 end
269
270 function mwtext.listToText( list, separator, conjunction )
271 separator = separator or options.comma
272 conjunction = conjunction or options['and']
273 local n = #list
274
275 local ret
276 if n > 1 then
277 ret = table.concat( list, separator, 1, n - 1 ) .. conjunction .. list[n]
278 else
279 ret = tostring( list[1] or '' )
280 end
281
282 return ret
283 end
284
285 function mwtext.truncate( text, length, ellipsis, adjustLength )
286 local l = mw.ustring.len( text )
287 if l <= math.abs( length ) then
288 return text
289 end
290
291 ellipsis = ellipsis or options.ellipsis
292 local elen = 0
293 if adjustLength then
294 elen = mw.ustring.len( ellipsis )
295 end
296
297 local ret
298 if math.abs( length ) <= elen then
299 ret = ellipsis
300 elseif length > 0 then
301 ret = mw.ustring.sub( text, 1, length - elen ) .. ellipsis
302 else
303 ret = ellipsis .. mw.ustring.sub( text, length + elen )
304 end
305
306 if mw.ustring.len( ret ) < l then
307 return ret
308 else
309 return text
310 end
311 end
312
313 -- Check for stuff that can't even be passed to PHP properly and other stuff
314 -- that gives different error messages in different versions of PHP
315 local function checkForJsonEncode( t, seen, lvl )
316 local tp = type( t )
317 if tp == 'table' then
318 if seen[t] then
319 error( "mw.text.jsonEncode: Cannot use recursive tables", lvl )
320 end
321 seen[t] = 1
322 for k, v in pairs( t ) do
323 if type( k ) == 'number' then
324 if k >= math.huge or k <= -math.huge then
325 error( string.format( "mw.text.jsonEncode: Cannot use 'inf' as a table key", type( k ) ), lvl )
326 end
327 elseif type( k ) ~= 'string' then
328 error( string.format( "mw.text.jsonEncode: Cannot use type '%s' as a table key", type( k ) ), lvl )
329 end
330 checkForJsonEncode( v, seen, lvl + 1 )
331 end
332 seen[t] = nil
333 elseif tp == 'number' then
334 if t ~= t or t >= math.huge or t <= -math.huge then
335 error( "mw.text.jsonEncode: Cannot encode non-finite numbers", lvl )
336 end
337 elseif tp ~= 'boolean' and tp ~= 'string' and tp ~= 'nil' then
338 error( string.format( "mw.text.jsonEncode: Cannot encode type '%s'", tp ), lvl )
339 end
340 end
341
342 function mwtext.jsonEncode( value, flags )
343 checkForJsonEncode( value, {}, 3 )
344 return php.jsonEncode( value, flags )
345 end
346
347 function mwtext.jsonDecode( json, flags )
348 return php.jsonDecode( json, flags )
349 end
350
351 -- Matches PHP MediaWiki\Extension\Scribunto\Engines\LuaCommon\TextLibrary constants
352 mwtext.JSON_PRESERVE_KEYS = 1
353 mwtext.JSON_TRY_FIXING = 2
354 mwtext.JSON_PRETTY = 4
355
356 return mwtext