Next round of image roundtripping fixes

* Changed PrefixImageOptions so that thumb and thumbnail are
  distinct key-value pairs.  Without this fix, cannot distinguish
  between thumb=foo.jpg and thumbnail=foo.jpg
* Fixed link handler so whitespace is preserved around prefixed image
  options.
* Fixed figure handler to process the 3 different kind of image options:
  size, simple image options, and prefixed image options.
* There is a hack/fixme for "upright: aspect" prefixed image option
  which needs to be looked into.
* Still need to fix uppercasing of the image resource name.

With these fixes, the following wikitext roundtrips perfectly
(after newline breaks are removed)

[[Image:Foo.jpg|thumbnail = 'baby.jpg'|100x100px|center| alt =bbbbb|
upright=true|bottom|link='http://foo.bar'|
This is a [[Linked Caption]] in the image]]

Change-Id: I6606df56874c2b97f00f08cb6bbeaec9878167d3
This commit is contained in:
Subramanya Sastry 2012-06-28 18:55:47 -05:00
parent 11e7c1031a
commit 88fc91a292
3 changed files with 44 additions and 7 deletions

View file

@ -132,10 +132,16 @@ WikiLinkHandler.prototype.renderFile = function ( token, frame, cb, title ) {
oHash.height = y;
}
} else {
var bits = oText.split( '=', 2 ),
key = WikitextConstants.Image.PrefixOptions[ bits[0].trim().toLowerCase() ];
var bits = oText.split( '=', 2 );
var normalizedBit0 = bits[0].trim().toLowerCase();
var key = WikitextConstants.Image.PrefixOptions[normalizedBit0];
if ( bits[0] && key) {
oHash[key] = bits[1];
// Preserve white space
// FIXME: But this doesn't work for the 'upright' key
if (key === normalizedBit0) {
key = bits[0];
}
options.push( new KV( key, bits[1] ) );
//console.warn('handle prefix ' + bits );
} else {

View file

@ -13,6 +13,7 @@ WikitextSerializer = function( options ) {
require('./core-upgrade.js');
var PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer;
var WikitextConstants = require('./mediawiki.wikitext.constants.js').WikitextConstants;
var WSP = WikitextSerializer.prototype;
@ -784,13 +785,43 @@ WSP.tagHandlers = {
var imgR = argDict.resource.replace(/(^\[:)|(\]$)/g, '');
// Now, build the complete wikitext for the figure
var outBits = [imgR];
var outBits = [imgR];
var figToken = figTokens[0];
var figAttrs = figToken.dataAttribs.optionList;
// SSS FIXME: May not be entirely correct
var simpleImgOptions = WikitextConstants.Image.SimpleOptions;
var prefixImgOptions = WikitextConstants.Image.PrefixOptions;
var sizeOptions = { "width": 1, "height": 1};
var size = {};
for (i = 0, n = figAttrs.length; i < n; i++) {
outBits.push(figAttrs[i].v);
var a = figAttrs[i];
var k = a.k, v = a.v;
if (sizeOptions[k]) {
size[k] = v;
} else {
// Output size first and clear it
var w = size.width;
if (w) {
outBits.push(w + (size.height ? "x" + size.height : '') + "px");
size.width = null;
}
// The values and keys in the parser attributes are a flip
// of how they are in the wikitext constants image hash
// Hence the indexing by 'v' instead of 'k'
if (simpleImgOptions[v] === k) {
outBits.push(v);
} else if (prefixImgOptions[v]) {
outBits.push(prefixImgOptions[k] + "=" + v);
} else if (k === "aspect") {
// SSS: Bad Hack! Need a better solution
// One solution is to search through prefix options hash but seems ugly.
// Another is to flip prefix options hash and use it to search.
outBits.push("upright=" + v);
} else {
console.warn("Unknown image option encountered: " + JSON.stringify(a));
}
}
}
if (caption) {
outBits.push(caption);

View file

@ -37,7 +37,7 @@ WikitextConstants = {
'link': 'link',
'alt': 'alt',
'page': 'page',
'thumbnail': 'thumb',
'thumbnail': 'thumbnail',
'thumb': 'thumb',
'upright': 'aspect'
}