Merge "Improve mw.text.nowiki"

This commit is contained in:
jenkins-bot 2013-09-03 23:43:54 +00:00 committed by Gerrit Code Review
commit 5a3175fce8
2 changed files with 49 additions and 9 deletions

View file

@ -2,16 +2,37 @@
class Scribunto_LuaTextLibrary extends Scribunto_LuaLibraryBase {
function register() {
global $wgUrlProtocols;
$lib = array(
'unstrip' => array( $this, 'textUnstrip' ),
'getEntityTable' => array( $this, 'getEntityTable' ),
);
$this->getEngine()->registerInterface( 'mw.text.lua', $lib, array(
$opts = array(
'comma' => wfMessage( 'comma-separator' )->inContentLanguage()->text(),
'and' => wfMessage( 'and' )->inContentLanguage()->text() .
wfMessage( 'word-separator' )->inContentLanguage()->text(),
'ellipsis' => wfMessage( 'ellipsis' )->inContentLanguage()->text(),
) );
'nowiki_protocols' => array(),
);
foreach ( $wgUrlProtocols as $prot ) {
if ( substr( $prot, -1 ) === ':' ) {
// To convert the protocol into a case-insensitive Lua pattern,
// we need to replace letters with a character class like [Xx]
// and insert a '%' before various punctuation.
$prot = preg_replace_callback( '/([a-zA-Z])|([()^$%.\[\]*+?-])/', function ( $m ) {
if ( $m[1] ) {
return '[' . strtoupper( $m[1] ) . strtolower( $m[1] ) . ']';
} else {
return '%' . $m[2];
}
}, substr( $prot, 0, -1 ) );
$opts['nowiki_protocols']["($prot):"] = '%1:';
}
}
$this->getEngine()->registerInterface( 'mw.text.lua', $lib, $opts );
}
function textUnstrip( $s ) {

View file

@ -101,19 +101,38 @@ local nowikiRepl1 = {
}
local nowikiRepl2 = {
["\n#"] = "\n#",
["\n*"] = "\n*",
["\n:"] = "\n:",
["\n;"] = "\n&#59;",
["\n#"] = "\n#", ["\r#"] = "\r#",
["\n*"] = "\n*", ["\r*"] = "\r*",
["\n:"] = "\n:", ["\r:"] = "\r:",
["\n;"] = "\n&#59;", ["\r;"] = "\r&#59;",
["\n "] = "\n ", ["\r "] = "\r ",
}
local nowikiReplMagic = {}
for sp, esc in pairs( {
[' '] = ' ',
['\t'] = '	',
['\r'] = '
',
['\n'] = '
',
['\f'] = '',
} ) do
nowikiReplMagic['ISBN' .. sp] = 'ISBN' .. esc
nowikiReplMagic['RFC' .. sp] = 'RFC' .. esc
nowikiReplMagic['PMID' .. sp] = 'PMID' .. esc
end
function mwtext.nowiki( s )
-- string.gsub is safe here, because we're only caring about ASCII chars
s = string.gsub( s, '["&\'<=>%[%]{|}]', nowikiRepl1 )
s = string.sub( string.gsub( '\n' .. s, '\n[#*:;]', nowikiRepl2 ), 2 )
s = string.sub( string.gsub( '\n' .. s, '[\r\n][#*:; ]', nowikiRepl2 ), 2 )
s = string.gsub( s, '__', '_&#95;' )
s = string.gsub( s, '://', '&#58;//' )
s = string.gsub( s, 'ISBN ', 'ISBN&#32;' )
s = string.gsub( s, 'RFC ', 'RFC&#32;' )
s = string.gsub( s, 'ISBN%s', nowikiReplMagic )
s = string.gsub( s, 'RFC%s', nowikiReplMagic )
s = string.gsub( s, 'PMID%s', nowikiReplMagic )
for k, v in pairs( options.nowiki_protocols ) do
s = string.gsub( s, k, v )
end
return s
end