https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112824

Revision: 112824
Author:   gwicke
Date:     2012-03-01 18:07:20 +0000 (Thu, 01 Mar 2012)
Log Message:
-----------
Add productions for image option tokenization, and prepare to call those from
the LinkHandler token stream transformer.

Modified Paths:
--------------
    trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
    trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js
    trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt

Modified: trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js        
2012-03-01 17:57:15 UTC (rev 112823)
+++ trunk/extensions/VisualEditor/modules/parser/ext.core.LinkHandler.js        
2012-03-01 18:07:20 UTC (rev 112824)
@@ -4,26 +4,21 @@
  *
  * @author Gabriel Wicke <[email protected]>
  * 
- * * Collect description/parameter tokens between 'a' tags
- * * Extract image options and add image html if target is media/image
- *   namespace
- * * 
- *
- *
  * TODO: keep round-trip information in meta tag or the like
- *
- *
- *
- * Pro/Contra of single token vs. tags and tokens
- * - Need to collect description tokens between a and /a
- * + noinclude etc handled automatically by having all tokens on content level
  */
 
-var jshashes = require('jshashes');
+var jshashes = require('jshashes'),
+       PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer;
 
 function WikiLinkHandler( manager, isInclude ) {
        this.manager = manager;
        this.manager.addTransform( this.onWikiLink.bind( this ), this.rank, 
'tag', 'wikilink' );
+       // create a new peg parser for image options..
+       if ( !this.imageParser ) {
+               // Actually the regular tokenizer, but we'll call it with the
+               // img_options production only.
+               WikiLinkHandler.prototype.imageParser = new PegTokenizer();
+       }
 }
 
 WikiLinkHandler.prototype.rank = 1.15; // after AttributeExpander
@@ -55,28 +50,40 @@
 
 
 WikiLinkHandler.prototype.renderFile = function ( token, manager, cb, title ) {
+       var env = manager.env;
        // distinguish media types
        // if image: parse options
        // XXX: get /wiki from config!
        var a = new TagTk( 'a', [ new KV( 'href', '/wiki' + title.makeLink() ) 
] );
 
-       var MD5 = new jshashes.MD5,
+       var MD5 = new jshashes.MD5(),
                hash = MD5.hex( title.key ),
+               // XXX: Hackhack..
                path = 'http://example.com/images/' + 
                        [ hash[0], hash.substr(0, 2) ].join('/') + '/' + 
title.key;
        
        
+       // XXX: parse options
+       var options = this.parseImageOptions( env.lookupKV( token.attribs, 
'content' ) );
+       // XXX: check if the file exists, generate thumbnail
+       // XXX: render according to mode (inline, thumb, framed etc)
        var img = new SelfclosingTagTk( 'img', 
                        [ 
+                               // FIXME!
                                new KV( 'height', '220' ),
                                new KV( 'width', '1941' ),
                                new KV( 'src', path ),
-                               new KV( 'alt', title.key ),
+                               new KV( 'alt', title.key )
+                       ] );
 
-                       ] );
        return { tokens: [ a, img, new EndTagTk( 'a' )] };
 };
 
+WikiLinkHandler.prototype.parseImageOptions = function ( tokens ) {
+       var text = this.manager.env.tokensToString( tokens );
+
+};
+
 if (typeof module == "object") {
        module.exports.WikiLinkHandler = WikiLinkHandler;
 }

Modified: 
trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js     
2012-03-01 17:57:15 UTC (rev 112823)
+++ trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js     
2012-03-01 18:07:20 UTC (rev 112824)
@@ -15,8 +15,6 @@
        defines = require('./mediawiki.parser.defines.js');
 
 function PegTokenizer() {
-       var pegSrcPath = path.join( __dirname, 'pegTokenizer.pegjs.txt' );
-       this.src = fs.readFileSync( pegSrcPath, 'utf8' );
 }
 
 
@@ -35,6 +33,8 @@
 PegTokenizer.prototype.process = function( text ) {
        var out, err;
        if ( !this.parser ) {
+               var pegSrcPath = path.join( __dirname, 'pegTokenizer.pegjs.txt' 
);
+               this.src = fs.readFileSync( pegSrcPath, 'utf8' );
                // Only create a single parser, as parse() is a static method.
                var parserSource = PEG.buildParser(this.src).toSource();
                //console.warn( parserSource );

Modified: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt 
2012-03-01 17:57:15 UTC (rev 112823)
+++ trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt 
2012-03-01 18:07:20 UTC (rev 112824)
@@ -803,7 +803,79 @@
 }
 
 
+/**
+ * Image option productions, only called from the LinkHandler token stream
+ * transformer, and only for images.
+ */
+img_options = os:img_option* c:img_caption {
+       var options = {};
+    os = flatten( os );
+       for ( var i = 0, l = options.length; i < l; i++ ) {
+               var o = os[i];
+               options[o.k] = o.v;
+       }
+       options.options = os;
+       options.caption = c;
+       return options;
+}
 
+img_option 
+  = space*
+  (  
+      img_format
+    / img_dimensions
+    / img_halign
+    / img_valign
+    / img_link
+    / img_attribute
+  )
+  ("|" /  eof)
+
+img_format
+  = f:( 'border' / 'frameless' / 'frame' / 'thumb' / 'thumbnail' ) {
+      return new KV( 'format', f );
+  }
+
+img_dimensions 
+  = x:([0-9]+)? y:('x' [0-9]+)? 'px' {
+      if ( x === '' && y ) {
+          return new KV( 'height', y );
+      } else if ( y === '' && x ) {
+          return new KV( 'width', x );
+      } else {
+          return [ new KV( 'width', x ), new KV( 'height', y ) ];
+      }
+  }
+  / 'upright' { return [ new KV( 'width', 'upright' ) ] }
+
+img_halign
+  = a:( 'left' / 'right' / 'center' / 'none' ) {
+      return new KV( 'halign', a );
+  }
+
+img_valign
+  = a:( 'baseline' / 'sub' / 'super' / 'top' / 'text-top' 
+          / 'middle' / 'bottom' / 'text-bottom' ) {
+      return new KV( 'valign', a );
+  }
+
+// External link targets are already parsed as link tokens. If we can make
+// sure that those cleanly convert back to the original text, then we could
+// re-parse them here.
+img_link
+  = 'link=' t:urltext {
+      return new KV( 'link', t );
+  }
+
+img_attribute
+  = k:( 'page' / 'alt' / 'thumb' ) '=' t:preprocessor_text {
+      return new KV( k, t );
+  }
+
+// catch-all
+img_caption = .*
+
+
 /***********************************************************
  * Pre and xmlish tags
  ***********************************************************/


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to