2012-05-16 14:53:18 +00:00
|
|
|
/**
|
|
|
|
* Serializes a chunk of tokens or an HTML DOM to MediaWiki's wikitext flavor.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param options {Object} List of options for serialization
|
|
|
|
*/
|
|
|
|
WikitextSerializer = function( options ) {
|
|
|
|
this.options = $.extend( {
|
|
|
|
// defaults
|
|
|
|
}, options || {} );
|
|
|
|
};
|
|
|
|
|
|
|
|
var WSP = WikitextSerializer.prototype;
|
|
|
|
|
|
|
|
WSP.defaultOptions = {
|
|
|
|
needParagraphLines: false,
|
|
|
|
listStack: [],
|
|
|
|
lastHandler: null
|
|
|
|
};
|
|
|
|
|
|
|
|
var id = function( v ) { return function() { return v; }; };
|
|
|
|
|
|
|
|
WSP._listHandler = function( bullet, state, token ) {
|
2012-05-17 15:51:18 +00:00
|
|
|
var bullets, res;
|
|
|
|
var stack = state.listStack;
|
|
|
|
if (stack.length === 0) {
|
|
|
|
bullets = "\n" + bullet;
|
|
|
|
res = bullets;
|
2012-05-16 14:53:18 +00:00
|
|
|
} else {
|
2012-05-17 15:51:18 +00:00
|
|
|
var curList = stack[stack.length - 1];
|
|
|
|
bullets = curList.bullets + bullet;
|
2012-05-17 17:32:46 +00:00
|
|
|
curList.itemCount++;
|
2012-05-16 14:53:18 +00:00
|
|
|
if ( // deeply nested list
|
2012-05-17 17:32:46 +00:00
|
|
|
curList.itemCount > 2 ||
|
2012-05-17 11:20:39 +00:00
|
|
|
// A nested list, not directly after the li
|
2012-05-17 17:32:46 +00:00
|
|
|
( curList.itemCount > 1 &&
|
2012-05-21 12:46:07 +00:00
|
|
|
! ( state.prevToken.constructor === TagTk &&
|
|
|
|
state.prevToken.name === 'li') )) {
|
2012-05-17 15:51:18 +00:00
|
|
|
res = bullets;
|
|
|
|
} else {
|
|
|
|
res = bullet;
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-17 17:32:46 +00:00
|
|
|
stack.push({ itemCount: 0, bullets: bullets});
|
2012-05-16 14:53:18 +00:00
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
WSP._listEndHandler = function( state, token ) {
|
|
|
|
state.listStack.pop();
|
|
|
|
// FIXME: insert a newline after a list block is closed (the next token is
|
|
|
|
// no list token).
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
WSP._listItemHandler = function ( state, token ) {
|
|
|
|
//console.warn( JSON.stringify( state.listStack ) );
|
2012-05-17 15:51:18 +00:00
|
|
|
var stack = state.listStack;
|
2012-05-16 14:53:18 +00:00
|
|
|
state.needParagraphLines = true;
|
2012-05-17 15:51:18 +00:00
|
|
|
if (stack.length === 0) {
|
2012-05-16 14:53:18 +00:00
|
|
|
return '';
|
|
|
|
} else {
|
2012-05-17 15:51:18 +00:00
|
|
|
var curList = stack[stack.length - 1];
|
2012-05-17 17:32:46 +00:00
|
|
|
curList.itemCount++;
|
2012-05-17 15:51:18 +00:00
|
|
|
// > 1 ==> consecutive list items
|
2012-05-17 17:32:46 +00:00
|
|
|
return ( curList.itemCount > 1 ) ? curList.bullets : '';
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-18 08:46:43 +00:00
|
|
|
WSP._serializeTableTag = function ( symbol, optionEndSymbol, state, token ) {
|
2012-05-16 14:53:18 +00:00
|
|
|
if ( token.attribs.length ) {
|
2012-05-21 12:46:07 +00:00
|
|
|
return symbol + ' ' +
|
2012-05-18 08:46:43 +00:00
|
|
|
WSP._serializeAttributes( token.attribs ) + optionEndSymbol;
|
2012-05-16 14:53:18 +00:00
|
|
|
} else {
|
2012-05-21 12:46:07 +00:00
|
|
|
return symbol;
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-17 12:32:56 +00:00
|
|
|
WSP._linkHandler = function( state, token ) {
|
|
|
|
return '[[';
|
|
|
|
// TODO: handle internal/external links etc using RDFa and dataAttribs
|
|
|
|
// Also convert unannotated html links to external wiki links for html
|
|
|
|
// import. Might want to consider converting relative links without path
|
|
|
|
// component and file extension to wiki links.
|
|
|
|
//if ( rtinfo.type === 'wikilink' ) {
|
|
|
|
// return '[[' + rtinfo.target + ']]';
|
|
|
|
//} else {
|
|
|
|
// // external link
|
|
|
|
// return '[' + rtinfo.
|
|
|
|
};
|
|
|
|
WSP._linkEndHandler = function( state, token ) {
|
|
|
|
return ']]';
|
|
|
|
};
|
|
|
|
|
2012-05-16 14:53:18 +00:00
|
|
|
WSP.tagToWikitext = {
|
|
|
|
body: {},
|
|
|
|
b: { start: id("'''"), end: id("'''") },
|
2012-05-18 07:54:40 +00:00
|
|
|
i: { start: id("''"), end: id("''") },
|
2012-05-16 14:53:18 +00:00
|
|
|
ul: {
|
|
|
|
start: WSP._listHandler.bind( null, '*' ),
|
|
|
|
end: WSP._listEndHandler
|
|
|
|
},
|
|
|
|
ol: {
|
|
|
|
start: WSP._listHandler.bind( null, '#' ),
|
|
|
|
end: WSP._listEndHandler
|
|
|
|
},
|
|
|
|
dl: {
|
|
|
|
start: WSP._listHandler.bind( null, '' ),
|
|
|
|
end: WSP._listEndHandler
|
|
|
|
},
|
|
|
|
li: { start: WSP._listItemHandler },
|
|
|
|
// XXX: handle single-line vs. multi-line dls etc
|
|
|
|
dt: { start: id(";") },
|
|
|
|
dd: { start: id(":") },
|
|
|
|
// XXX: handle options
|
|
|
|
table: {
|
2012-05-21 12:46:07 +00:00
|
|
|
start: WSP._serializeTableTag.bind(null, "\n{|", ''),
|
2012-05-16 14:53:18 +00:00
|
|
|
end: id("\n|}")
|
|
|
|
},
|
|
|
|
tbody: {},
|
2012-05-21 12:46:07 +00:00
|
|
|
th: {
|
|
|
|
start: function ( state, token ) {
|
|
|
|
if ( token.dataAttribs.t_stx === 'row' ) {
|
|
|
|
return WSP._serializeTableTag("!!", ' |', state, token);
|
|
|
|
} else {
|
|
|
|
return WSP._serializeTableTag("\n!", ' |', state, token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-05-16 14:53:18 +00:00
|
|
|
// XXX: omit for first row in table.
|
2012-05-21 12:46:07 +00:00
|
|
|
tr: {
|
|
|
|
start: function ( state, token ) {
|
|
|
|
if ( state.prevToken.constructor === TagTk && state.prevToken.name === 'tbody' ) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return WSP._serializeTableTag("\n|-", ' |', state, token );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
td: {
|
|
|
|
start: function ( state, token ) {
|
|
|
|
if ( token.dataAttribs.t_stx === 'row' ) {
|
|
|
|
return WSP._serializeTableTag("||", ' |', state, token);
|
|
|
|
} else {
|
|
|
|
return WSP._serializeTableTag("\n|", ' |', state, token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
caption: { start: WSP._serializeTableTag.bind(null, "\n|+", ' |') },
|
2012-05-16 14:53:18 +00:00
|
|
|
p: {
|
|
|
|
start: function( state, token ) {
|
|
|
|
if (state.needParagraphLines) {
|
|
|
|
return "\n\n";
|
|
|
|
} else {
|
|
|
|
state.needParagraphLines = true;
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hr: { start: id("\n----"), end: id("\n") },
|
|
|
|
h1: { start: id("\n="), end: id("=\n") },
|
|
|
|
h2: { start: id("\n=="), end: id("==\n") },
|
|
|
|
h3: { start: id("\n==="), end: id("===\n") },
|
|
|
|
h4: { start: id("\n===="), end: id("====\n") },
|
|
|
|
h5: { start: id("\n====="), end: id("=====\n") },
|
|
|
|
h6: { start: id("\n======"), end: id("======\n") },
|
2012-05-17 12:32:56 +00:00
|
|
|
pre: { start: id("<pre>"), end: id("</pre>") },
|
2012-05-21 16:06:09 +00:00
|
|
|
a: { start: WSP._linkHandler, end: WSP._linkEndHandler },
|
|
|
|
nowiki: { start: id("<nowiki>"), end: id("</nowiki>") }
|
2012-05-16 14:53:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
WSP._serializeAttributes = function ( attribs ) {
|
|
|
|
var out = [];
|
|
|
|
for ( var i = 0, l = attribs.length; i < l; i++ ) {
|
|
|
|
var kv = attribs[i];
|
|
|
|
if (kv.k.length) {
|
|
|
|
if ( kv.v.length ) {
|
|
|
|
out.push( kv.k + '=' +
|
|
|
|
'"' + kv.v.replace( '"', '"' ) + '"');
|
|
|
|
} else {
|
|
|
|
out.push( kv.k );
|
|
|
|
}
|
|
|
|
} else if ( kv.v.length ) {
|
|
|
|
// not very likely..
|
|
|
|
out.push( kv.v );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// XXX: round-trip optional whitespace / line breaks etc
|
|
|
|
return out.join(' ');
|
|
|
|
};
|
2012-05-21 12:46:07 +00:00
|
|
|
|
|
|
|
WSP._eatFirstNewLine = function ( state, chunk ) {
|
|
|
|
while ( chunk[0] === '\n' ) {
|
|
|
|
chunk = chunk.substr(1);
|
|
|
|
}
|
|
|
|
state.realChunkCB( chunk );
|
|
|
|
state.chunkCB = state.realChunkCB;
|
|
|
|
};
|
|
|
|
|
2012-05-16 14:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a chunk of tokens
|
|
|
|
*/
|
|
|
|
WSP.serializeTokens = function( tokens, chunkCB ) {
|
2012-05-17 11:20:39 +00:00
|
|
|
var state = $.extend({}, this.defaultOptions, this.options),
|
|
|
|
i, l;
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB = WSP._eatFirstNewLine.bind( this, state );
|
2012-05-16 14:53:18 +00:00
|
|
|
if ( chunkCB === undefined ) {
|
|
|
|
var out = [];
|
2012-05-21 12:46:07 +00:00
|
|
|
state.realChunkCB = out.push.bind(out);
|
2012-05-17 11:20:39 +00:00
|
|
|
for ( i = 0, l = tokens.length; i < l; i++ ) {
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state, tokens[i] );
|
2012-05-17 11:20:39 +00:00
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
return out;
|
|
|
|
} else {
|
2012-05-21 12:46:07 +00:00
|
|
|
state.realChunkCB = chunkCB;
|
2012-05-17 11:20:39 +00:00
|
|
|
for ( i = 0, l = tokens.length; i < l; i++ ) {
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state, tokens[i] );
|
2012-05-17 11:20:39 +00:00
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-17 11:20:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a token.
|
|
|
|
*/
|
2012-05-21 12:46:07 +00:00
|
|
|
WSP._serializeToken = function ( state, token ) {
|
|
|
|
state.prevToken = state.curToken;
|
2012-05-17 11:20:39 +00:00
|
|
|
state.curToken = token;
|
|
|
|
var handler;
|
|
|
|
switch( token.constructor ) {
|
|
|
|
case TagTk:
|
|
|
|
case SelfclosingTagTk:
|
|
|
|
handler = this.tagToWikitext[token.name];
|
|
|
|
if ( handler && handler.start ) {
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB( handler.start( state, token ) );
|
2012-05-17 11:20:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EndTagTk:
|
|
|
|
handler = this.tagToWikitext[token.name];
|
|
|
|
if ( handler && handler.end ) {
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB( handler.end( state, token ) );
|
2012-05-17 11:20:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case String:
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB( token );
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case CommentTk:
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB( '<!--' + token.value + '-->' );
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case NlTk:
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB( '\n' );
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case EOFTk:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn( 'Unhandled token type ' + JSON.stringify( token ) );
|
|
|
|
break;
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize an HTML DOM document.
|
|
|
|
*/
|
|
|
|
WSP.serializeDOM = function( node, chunkCB ) {
|
|
|
|
var state = $.extend({}, this.defaultOptions, this.options);
|
2012-05-21 12:46:07 +00:00
|
|
|
state.chunkCB = this._eatFirstNewLine.bind( this, state );
|
2012-05-16 14:53:18 +00:00
|
|
|
if ( ! chunkCB ) {
|
|
|
|
var out = [];
|
2012-05-21 12:46:07 +00:00
|
|
|
state.realChunkCB = out.push.bind( out );
|
|
|
|
this._serializeDOM( node, state );
|
2012-05-16 14:53:18 +00:00
|
|
|
return out.join('');
|
|
|
|
} else {
|
2012-05-21 12:46:07 +00:00
|
|
|
state.realChunkCB = chunkCB;
|
|
|
|
this._serializeDOM( node, state );
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-17 11:20:39 +00:00
|
|
|
/**
|
|
|
|
* Internal worker. Recursively serialize a DOM subtree by creating tokens and
|
|
|
|
* calling _serializeToken on each of these.
|
|
|
|
*/
|
2012-05-21 12:46:07 +00:00
|
|
|
WSP._serializeDOM = function( node, state ) {
|
2012-05-16 14:53:18 +00:00
|
|
|
// serialize this node
|
|
|
|
switch( node.nodeType ) {
|
|
|
|
case Node.ELEMENT_NODE:
|
|
|
|
//console.warn( node.nodeName.toLowerCase() );
|
|
|
|
var children = node.childNodes,
|
|
|
|
name = node.nodeName.toLowerCase(),
|
|
|
|
handler = this.tagToWikitext[name];
|
|
|
|
if ( handler ) {
|
2012-05-17 11:20:39 +00:00
|
|
|
var tkAttribs = this._getDOMAttribs(node.attributes),
|
|
|
|
tkRTInfo = this._getDOMRTInfo(node.attributes);
|
|
|
|
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state,
|
2012-05-17 11:20:39 +00:00
|
|
|
new TagTk( name, tkAttribs, tkRTInfo ) );
|
2012-05-16 14:53:18 +00:00
|
|
|
for ( var i = 0, l = children.length; i < l; i++ ) {
|
|
|
|
// serialize all children
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeDOM( children[i], state );
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state,
|
2012-05-17 11:20:39 +00:00
|
|
|
new EndTagTk( name, tkAttribs, tkRTInfo ) );
|
2012-05-16 14:53:18 +00:00
|
|
|
} else {
|
2012-05-18 08:03:16 +00:00
|
|
|
console.warn( 'Unhandled element: ' + node.outerHTML );
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Node.TEXT_NODE:
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state, node.data );
|
2012-05-16 14:53:18 +00:00
|
|
|
break;
|
|
|
|
case Node.COMMENT_NODE:
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state, new CommentTk( node.data ) );
|
2012-05-16 14:53:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn( "Unhandled node type: " +
|
2012-05-18 08:03:16 +00:00
|
|
|
node.outerHTML );
|
2012-05-16 14:53:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WSP._getDOMAttribs = function( attribs ) {
|
|
|
|
// convert to list fo key-value pairs
|
|
|
|
var out = [];
|
|
|
|
for ( var i = 0, l = attribs.length; i < l; i++ ) {
|
|
|
|
var attrib = attribs.item(i);
|
|
|
|
if ( attrib.name !== 'data-mw' ) {
|
|
|
|
out.push( { k: attrib.name, v: attrib.value } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
WSP._getDOMRTInfo = function( attribs ) {
|
|
|
|
if ( attribs['data-mw'] ) {
|
|
|
|
return JSON.parse( attribs['data-mw'].value || '{}' );
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Quick HACK: define Node constants locally
|
|
|
|
// https://developer.mozilla.org/en/nodeType
|
|
|
|
var Node = {
|
|
|
|
ELEMENT_NODE: 1,
|
|
|
|
ATTRIBUTE_NODE: 2,
|
|
|
|
TEXT_NODE: 3,
|
|
|
|
CDATA_SECTION_NODE: 4,
|
|
|
|
ENTITY_REFERENCE_NODE: 5,
|
|
|
|
ENTITY_NODE: 6,
|
|
|
|
PROCESSING_INSTRUCTION_NODE: 7,
|
|
|
|
COMMENT_NODE: 8,
|
|
|
|
DOCUMENT_NODE: 9,
|
|
|
|
DOCUMENT_TYPE_NODE: 10,
|
|
|
|
DOCUMENT_FRAGMENT_NODE: 11,
|
|
|
|
NOTATION_NODE: 12
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.WikitextSerializer = WikitextSerializer;
|
|
|
|
}
|