2 * Experimental advanced wikitext parser-emitter.
3 * See: http://www.mediawiki.org/wiki/Extension:UploadWizard/MessageParser for docs
5 * @author neilk@wikimedia.org
8 var slice
= Array
.prototype.slice
,
11 'SITENAME' : mw
.config
.get( 'wgSiteName' )
13 messages
: mw
.messages
,
14 language
: mw
.language
18 * Given parser options, return a function that parses a key and replacements, returning jQuery object
19 * @param {Object} parser options
20 * @return {Function} accepting ( String message key, String replacement1, String replacement2 ... ) and returning {jQuery}
22 function getFailableParserFn( options
) {
23 var parser
= new mw
.jqueryMsg
.parser( options
);
25 * Try to parse a key and optional replacements, returning a jQuery object that may be a tree of jQuery nodes.
26 * If there was an error parsing, return the key and the error message (wrapped in jQuery). This should put the error right into
27 * the interface, without causing the page to halt script execution, and it hopefully should be clearer how to fix it.
29 * @param {Array} first element is the key, replacements may be in array in 2nd element, or remaining elements.
32 return function ( args
) {
34 var argsArray
= $.isArray( args
[1] ) ? args
[1] : slice
.call( args
, 1 );
36 return parser
.parse( key
, argsArray
);
38 return $( '<span>' ).append( key
+ ': ' + e
.message
);
47 * Returns a function suitable for use as a global, to construct strings from the message key (and optional replacements).
49 * window.gM = mediaWiki.parser.getMessageFunction( options );
50 * $( 'p#headline' ).html( gM( 'hello-user', username ) );
52 * Like the old gM() function this returns only strings, so it destroys any bindings. If you want to preserve bindings use the
53 * jQuery plugin version instead. This is only included for backwards compatibility with gM().
55 * @param {Array} parser options
56 * @return {Function} function suitable for assigning to window.gM
58 mw
.jqueryMsg
.getMessageFunction = function ( options
) {
59 var failableParserFn
= getFailableParserFn( options
);
61 * N.B. replacements are variadic arguments or an array in second parameter. In other words:
62 * somefunction(a, b, c, d)
64 * somefunction(a, [b, c, d])
66 * @param {String} message key
67 * @param {Array} optional replacements (can also specify variadically)
68 * @return {String} rendered HTML as string
70 return function ( /* key, replacements */ ) {
71 return failableParserFn( arguments
).html();
77 * Returns a jQuery plugin which parses the message in the message key, doing replacements optionally, and appends the nodes to
78 * the current selector. Bindings to passed-in jquery elements are preserved. Functions become click handlers for [$1 linktext] links.
80 * $.fn.msg = mediaWiki.parser.getJqueryPlugin( options );
81 * var userlink = $( '<a>' ).click( function () { alert( "hello!!") } );
82 * $( 'p#headline' ).msg( 'hello-user', userlink );
84 * @param {Array} parser options
85 * @return {Function} function suitable for assigning to jQuery plugin, such as $.fn.msg
87 mw
.jqueryMsg
.getPlugin = function ( options
) {
88 var failableParserFn
= getFailableParserFn( options
);
90 * N.B. replacements are variadic arguments or an array in second parameter. In other words:
91 * somefunction(a, b, c, d)
93 * somefunction(a, [b, c, d])
95 * We append to 'this', which in a jQuery plugin context will be the selected elements.
96 * @param {String} message key
97 * @param {Array} optional replacements (can also specify variadically)
98 * @return {jQuery} this
100 return function ( /* key, replacements */ ) {
101 var $target
= this.empty();
102 $.each( failableParserFn( arguments
).contents(), function ( i
, node
) {
103 $target
.append( node
);
111 * Describes an object, whose primary duty is to .parse() message keys.
112 * @param {Array} options
114 mw
.jqueryMsg
.parser = function ( options
) {
115 this.settings
= $.extend( {}, parserDefaults
, options
);
116 this.emitter
= new mw
.jqueryMsg
.htmlEmitter( this.settings
.language
, this.settings
.magic
);
119 mw
.jqueryMsg
.parser
.prototype = {
120 // cache, map of mediaWiki message key to the AST of the message. In most cases, the message is a string so this is identical.
121 // (This is why we would like to move this functionality server-side).
125 * Where the magic happens.
126 * Parses a message from the key, and swaps in replacements as necessary, wraps in jQuery
127 * If an error is thrown, returns original key, and logs the error
128 * @param {String} message key
129 * @param {Array} replacements for $1, $2... $n
132 parse: function ( key
, replacements
) {
133 return this.emitter
.emit( this.getAst( key
), replacements
);
136 * Fetch the message string associated with a key, return parsed structure. Memoized.
137 * Note that we pass '[' + key + ']' back for a missing message here.
138 * @param {String} key
139 * @return {String|Array} string of '[key]' if message missing, simple string if possible, array of arrays if needs parsing
141 getAst: function ( key
) {
142 if ( this.astCache
[ key
] === undefined ) {
143 var wikiText
= this.settings
.messages
.get( key
);
144 if ( typeof wikiText
!== 'string' ) {
145 wikiText
= "\\[" + key
+ "\\]";
147 this.astCache
[ key
] = this.wikiTextToAst( wikiText
);
149 return this.astCache
[ key
];
152 * Parses the input wikiText into an abstract syntax tree, essentially an s-expression.
154 * CAVEAT: This does not parse all wikitext. It could be more efficient, but it's pretty good already.
155 * n.b. We want to move this functionality to the server. Nothing here is required to be on the client.
157 * @param {String} message string wikitext
159 * @return {Mixed} abstract syntax tree
161 wikiTextToAst: function ( input
) {
163 // Indicates current position in input as we parse through it.
164 // Shared among all parsing functions below.
166 // =========================================================
167 // parsing combinators - could be a library on its own
168 // =========================================================
169 // Try parsers until one works, if none work return null
170 function choice( ps
) {
172 for ( var i
= 0; i
< ps
.length
; i
++ ) {
173 var result
= ps
[i
]();
174 if ( result
!== null ) {
181 // try several ps in a row, all must succeed or return null
182 // this is the only eager one
183 function sequence( ps
) {
184 var originalPos
= pos
;
186 for ( var i
= 0; i
< ps
.length
; i
++ ) {
188 if ( res
=== null ) {
196 // run the same parser over and over until it fails.
197 // must succeed a minimum of n times or return null
198 function nOrMore( n
, p
) {
200 var originalPos
= pos
;
203 while ( parsed
!== null ) {
204 result
.push( parsed
);
207 if ( result
.length
< n
) {
214 // There is a general pattern -- parse a thing, if that worked, apply transform, otherwise return null.
215 // But using this as a combinator seems to cause problems when combined with nOrMore().
216 // May be some scoping issue
217 function transform( p
, fn
) {
220 return result
=== null ? null : fn( result
);
223 // Helpers -- just make ps out of simpler JS builtin types
224 function makeStringParser( s
) {
228 if ( input
.substr( pos
, len
) === s
) {
235 function makeRegexParser( regex
) {
237 var matches
= input
.substr( pos
).match( regex
);
238 if ( matches
=== null ) {
241 pos
+= matches
[0].length
;
247 * ===================================================================
248 * General patterns above this line -- wikitext specific parsers below
249 * ===================================================================
251 // Parsing functions follow. All parsing functions work like this:
252 // They don't accept any arguments.
253 // Instead, they just operate non destructively on the string 'input'
254 // As they can consume parts of the string, they advance the shared variable pos,
255 // and return tokens (or whatever else they want to return).
256 // some things are defined as closures and other things as ordinary functions
257 // converting everything to a closure makes it a lot harder to debug... errors pop up
258 // but some debuggers can't tell you exactly where they come from. Also the mutually
259 // recursive functions seem not to work in all browsers then. (Tested IE6-7, Opera, Safari, FF)
260 // This may be because, to save code, memoization was removed
261 var regularLiteral
= makeRegexParser( /^[^{}\[\]$\\]/ );
262 var regularLiteralWithoutBar
= makeRegexParser(/^[^{}\[\]$\\|]/);
263 var regularLiteralWithoutSpace
= makeRegexParser(/^[^{}\[\]$\s]/);
264 var backslash
= makeStringParser( "\\" );
265 var anyCharacter
= makeRegexParser( /^./ );
266 function escapedLiteral() {
267 var result
= sequence( [
271 return result
=== null ? null : result
[1];
273 var escapedOrLiteralWithoutSpace
= choice( [
275 regularLiteralWithoutSpace
277 var escapedOrLiteralWithoutBar
= choice( [
279 regularLiteralWithoutBar
281 var escapedOrRegularLiteral
= choice( [
285 // Used to define "literals" without spaces, in space-delimited situations
286 function literalWithoutSpace() {
287 var result
= nOrMore( 1, escapedOrLiteralWithoutSpace
)();
288 return result
=== null ? null : result
.join('');
290 // Used to define "literals" within template parameters. The pipe character is the parameter delimeter, so by default
291 // it is not a literal in the parameter
292 function literalWithoutBar() {
293 var result
= nOrMore( 1, escapedOrLiteralWithoutBar
)();
294 return result
=== null ? null : result
.join('');
297 var result
= nOrMore( 1, escapedOrRegularLiteral
)();
298 return result
=== null ? null : result
.join('');
300 var whitespace
= makeRegexParser( /^\s+/ );
301 var dollar
= makeStringParser( '$' );
302 var digits
= makeRegexParser( /^\d+/ );
304 function replacement() {
305 var result
= sequence( [
309 if ( result
=== null ) {
312 return [ 'REPLACE', parseInt( result
[1], 10 ) - 1 ];
314 var openExtlink
= makeStringParser( '[' );
315 var closeExtlink
= makeStringParser( ']' );
316 // this extlink MUST have inner text, e.g. [foo] not allowed; [foo bar] is allowed
319 var parsedResult
= sequence( [
321 nonWhitespaceExpression
,
326 if ( parsedResult
!== null ) {
327 result
= [ 'LINK', parsedResult
[1], parsedResult
[3] ];
331 // this is the same as the above extlink, except that the url is being passed on as a parameter
332 function extLinkParam() {
333 var result
= sequence( [
341 if ( result
=== null ) {
344 return [ 'LINKPARAM', parseInt( result
[2], 10 ) - 1, result
[4] ];
346 var openLink
= makeStringParser( '[[' );
347 var closeLink
= makeStringParser( ']]' );
350 var parsedResult
= sequence( [
355 if ( parsedResult
!== null ) {
356 result
= [ 'WLINK', parsedResult
[1] ];
360 var templateName
= transform(
361 // see $wgLegalTitleChars
362 // not allowing : due to the need to catch "PLURAL:$1"
363 makeRegexParser( /^[ !"$&'()*,.\/0-9;=?@A-Z\^_`a-z~\x80-\xFF+\-]+/ ),
364 function ( result
) { return result
.toString(); }
366 function templateParam() {
367 var result
= sequence( [
369 nOrMore( 0, paramExpression
)
371 if ( result
=== null ) {
374 var expr
= result
[1];
375 // use a "CONCAT" operator if there are multiple nodes, otherwise return the first node, raw.
376 return expr
.length
> 1 ? [ "CONCAT" ].concat( expr
) : expr
[0];
378 var pipe
= makeStringParser( '|' );
379 function templateWithReplacement() {
380 var result
= sequence( [
385 return result
=== null ? null : [ result
[0], result
[2] ];
387 function templateWithOutReplacement() {
388 var result
= sequence( [
393 return result
=== null ? null : [ result
[0], result
[2] ];
395 var colon
= makeStringParser(':');
396 var templateContents
= choice( [
398 var res
= sequence( [
399 // templates can have placeholders for dynamic replacement eg: {{PLURAL:$1|one car|$1 cars}}
400 // or no placeholders eg: {{GRAMMAR:genitive|{{SITENAME}}}
401 choice( [ templateWithReplacement
, templateWithOutReplacement
] ),
402 nOrMore( 0, templateParam
)
404 return res
=== null ? null : res
[0].concat( res
[1] );
407 var res
= sequence( [
409 nOrMore( 0, templateParam
)
411 if ( res
=== null ) {
414 return [ res
[0] ].concat( res
[1] );
417 var openTemplate
= makeStringParser('{{');
418 var closeTemplate
= makeStringParser('}}');
419 function template() {
420 var result
= sequence( [
425 return result
=== null ? null : result
[1];
427 var nonWhitespaceExpression
= choice( [
435 var paramExpression
= choice( [
443 var expression
= choice( [
452 var result
= nOrMore( 0, expression
)();
453 if ( result
=== null ) {
456 return [ "CONCAT" ].concat( result
);
458 // everything above this point is supposed to be stateless/static, but
459 // I am deferring the work of turning it into prototypes & objects. It's quite fast enough
460 // finally let's do some actual work...
461 var result
= start();
464 * For success, the p must have gotten to the end of the input
465 * and returned a non-null.
466 * n.b. This is part of language infrastructure, so we do not throw an internationalizable message.
468 if (result
=== null || pos
!== input
.length
) {
469 throw new Error( "Parse error at position " + pos
.toString() + " in input: " + input
);
476 * htmlEmitter - object which primarily exists to emit HTML from parser ASTs
478 mw
.jqueryMsg
.htmlEmitter = function ( language
, magic
) {
479 this.language
= language
;
481 $.each( magic
, function ( key
, val
) {
482 jmsg
[ key
.toLowerCase() ] = function () {
487 * (We put this method definition here, and not in prototype, to make sure it's not overwritten by any magic.)
488 * Walk entire node structure, applying replacements and template functions when appropriate
489 * @param {Mixed} abstract syntax tree (top node or subnode)
490 * @param {Array} replacements for $1, $2, ... $n
491 * @return {Mixed} single-string node or array of nodes suitable for jQuery appending
493 this.emit = function ( node
, replacements
) {
496 switch ( typeof node
) {
501 case 'object': // node is an array of nodes
502 var subnodes
= $.map( node
.slice( 1 ), function ( n
) {
503 return jmsg
.emit( n
, replacements
);
505 var operation
= node
[0].toLowerCase();
506 if ( typeof jmsg
[operation
] === 'function' ) {
507 ret
= jmsg
[ operation
]( subnodes
, replacements
);
509 throw new Error( 'Unknown operation "' + operation
+ '"' );
513 // Parsing the empty string (as an entire expression, or as a paramExpression in a template) results in undefined
514 // Perhaps a more clever parser can detect this, and return the empty string? Or is that useful information?
515 // The logical thing is probably to return the empty string here when we encounter undefined.
519 throw new Error( 'Unexpected type in AST: ' + typeof node
);
524 // For everything in input that follows double-open-curly braces, there should be an equivalent parser
525 // function. For instance {{PLURAL ... }} will be processed by 'plural'.
526 // If you have 'magic words' then configure the parser to have them upon creation.
528 // An emitter method takes the parent node, the array of subnodes and the array of replacements (the values that $1, $2... should translate to).
529 // Note: all such functions must be pure, with the exception of referring to other pure functions via this.language (convertPlural and so on)
530 mw
.jqueryMsg
.htmlEmitter
.prototype = {
532 * Parsing has been applied depth-first we can assume that all nodes here are single nodes
533 * Must return a single node to parents -- a jQuery with synthetic span
534 * However, unwrap any other synthetic spans in our children and pass them upwards
535 * @param {Array} nodes - mixed, some single nodes, some arrays of nodes
538 concat: function ( nodes
) {
539 var $span
= $( '<span>' ).addClass( 'mediaWiki_htmlEmitter' );
540 $.each( nodes
, function ( i
, node
) {
541 if ( node
instanceof jQuery
&& node
.hasClass( 'mediaWiki_htmlEmitter' ) ) {
542 $.each( node
.contents(), function ( j
, childNode
) {
543 $span
.append( childNode
);
546 // strings, integers, anything else
547 $span
.append( node
);
554 * Return escaped replacement of correct index, or string if unavailable.
555 * Note that we expect the parsed parameter to be zero-based. i.e. $1 should have become [ 0 ].
556 * if the specified parameter is not found return the same string
557 * (e.g. "$99" -> parameter 98 -> not found -> return "$99" )
558 * TODO throw error if nodes.length > 1 ?
559 * @param {Array} of one element, integer, n >= 0
560 * @return {String} replacement
562 replace: function ( nodes
, replacements
) {
563 var index
= parseInt( nodes
[0], 10 );
565 if ( index
< replacements
.length
) {
566 if ( typeof arg
=== 'string' ) {
567 // replacement is a string, escape it
568 return mw
.html
.escape( replacements
[index
] );
570 // replacement is no string, don't touch!
571 return replacements
[index
];
574 // index not found, fallback to displaying variable
575 return '$' + ( index
+ 1 );
580 * Transform wiki-link
583 wlink: function ( nodes
) {
584 return 'unimplemented';
588 * Transform parsed structure into external link
589 * If the href is a jQuery object, treat it as "enclosing" the link text.
590 * ... function, treat it as the click handler
591 * ... string, treat it as a URI
592 * TODO: throw an error if nodes.length > 2 ?
593 * @param {Array} of two elements, {jQuery|Function|String} and {String}
596 link: function ( nodes
) {
598 var contents
= nodes
[1];
600 if ( arg
instanceof jQuery
) {
604 if ( typeof arg
=== 'function' ) {
605 $el
.click( arg
).attr( 'href', '#' );
607 $el
.attr( 'href', arg
.toString() );
610 $el
.append( contents
);
615 * This is basically use a combination of replace + link (link with parameter
616 * as url), but we don't want to run the regular replace here-on: inserting a
617 * url as href-attribute of a link will automatically escape it already, so
618 * we don't want replace to (manually) escape it as well.
619 * TODO throw error if nodes.length > 1 ?
620 * @param {Array} of one element, integer, n >= 0
621 * @return {String} replacement
623 linkparam: function ( nodes
, replacements
) {
625 index
= parseInt( nodes
[0], 10 );
626 if ( index
< replacements
.length
) {
627 replacement
= replacements
[index
];
629 replacement
= '$' + ( index
+ 1 );
631 return this.link( [ replacement
, nodes
[1] ] );
635 * Transform parsed structure into pluralization
636 * n.b. The first node may be a non-integer (for instance, a string representing an Arabic number).
637 * So convert it back with the current language's convertNumber.
638 * @param {Array} of nodes, [ {String|Number}, {String}, {String} ... ]
639 * @return {String} selected pluralized form according to current language
641 plural: function ( nodes
) {
642 var count
= parseFloat( this.language
.convertNumber( nodes
[0], true ) );
643 var forms
= nodes
.slice(1);
644 return forms
.length
? this.language
.convertPlural( count
, forms
) : '';
648 * Transform parsed structure into gender
649 * Usage {{gender:[gender| mw.user object ] | masculine|feminine|neutral}}.
650 * @param {Array} of nodes, [ {String|mw.User}, {String}, {String} , {String} ]
651 * @return {String} selected gender form according to current language
653 gender: function ( nodes
) {
655 if ( nodes
[0] && nodes
[0].options
instanceof mw
.Map
){
656 gender
= nodes
[0].options
.get( 'gender' );
660 var forms
= nodes
.slice(1);
661 return this.language
.gender( gender
, forms
);
665 * Transform parsed structure into grammar conversion.
666 * Invoked by putting {{grammar:form|word}} in a message
667 * @param {Array} of nodes [{Grammar case eg: genitive}, {String word}]
668 * @return {String} selected grammatical form according to current language
670 grammar: function ( nodes
) {
673 return word
&& form
&& this.language
.convertGrammar( word
, form
);
676 // Deprecated! don't rely on gM existing.
677 // The window.gM ought not to be required - or if required, not required here.
678 // But moving it to extensions breaks it (?!)
679 // Need to fix plugin so it could do attributes as well, then will be okay to remove this.
680 window
.gM
= mw
.jqueryMsg
.getMessageFunction();
681 $.fn
.msg
= mw
.jqueryMsg
.getPlugin();
683 // Replace the default message parser with jqueryMsg
684 var oldParser
= mw
.Message
.prototype.parser
;
685 mw
.Message
.prototype.parser = function () {
686 // TODO: should we cache the message function so we don't create a new one every time? Benchmark this maybe?
687 // Caching is somewhat problematic, because we do need different message functions for different maps, so
688 // we'd have to cache the parser as a member of this.map, which sounds a bit ugly.
689 // Do not use mw.jqueryMsg unless required
690 if ( this.map
.get( this.key
).indexOf( '{{' ) < 0 ) {
691 // Fall back to mw.msg's simple parser
692 return oldParser
.apply( this );
694 var messageFunction
= mw
.jqueryMsg
.getMessageFunction( { 'messages': this.map
} );
695 return messageFunction( this.key
, this.parameters
);
698 }( mediaWiki
, jQuery
) );