mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-24 16:25:00 +00:00
Merge "Improve mw.text.nowiki"
This commit is contained in:
commit
5a3175fce8
|
@ -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 ) {
|
||||
|
|
|
@ -101,19 +101,38 @@ local nowikiRepl1 = {
|
|||
}
|
||||
|
||||
local nowikiRepl2 = {
|
||||
["\n#"] = "\n#",
|
||||
["\n*"] = "\n*",
|
||||
["\n:"] = "\n:",
|
||||
["\n;"] = "\n;",
|
||||
["\n#"] = "\n#", ["\r#"] = "\r#",
|
||||
["\n*"] = "\n*", ["\r*"] = "\r*",
|
||||
["\n:"] = "\n:", ["\r:"] = "\r:",
|
||||
["\n;"] = "\n;", ["\r;"] = "\r;",
|
||||
["\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, '__', '__' )
|
||||
s = string.gsub( s, '://', '://' )
|
||||
s = string.gsub( s, 'ISBN ', 'ISBN ' )
|
||||
s = string.gsub( s, 'RFC ', 'RFC ' )
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue