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 || {} );
|
|
|
|
};
|
|
|
|
|
2012-06-19 13:24:13 +00:00
|
|
|
require('./core-upgrade.js');
|
|
|
|
var PegTokenizer = require('./mediawiki.tokenizer.peg.js').PegTokenizer;
|
|
|
|
|
2012-05-16 14:53:18 +00:00
|
|
|
var WSP = WikitextSerializer.prototype;
|
|
|
|
|
|
|
|
WSP.defaultOptions = {
|
2012-06-19 13:24:13 +00:00
|
|
|
onNewline: true, // actually following start of file or a real newline
|
|
|
|
onStartOfLine : true, // in start-of-line context, not necessarily after newline
|
|
|
|
listStack: [],
|
|
|
|
lastHandler: null,
|
|
|
|
availableNewlineCount: 0, // collected (and stripped) newlines from the input
|
|
|
|
singleLineMode: 0 // single-line syntactical context: list items, headings etc
|
|
|
|
};
|
|
|
|
|
|
|
|
WSP.escapeWikiText = function ( state, text ) {
|
|
|
|
// tokenize the text
|
|
|
|
var p = new PegTokenizer( state.env ),
|
|
|
|
tokens = [];
|
|
|
|
p.on('chunk', function ( chunk ) {
|
|
|
|
//console.warn( JSON.stringify(chunk));
|
|
|
|
tokens.push.apply( tokens, chunk );
|
|
|
|
});
|
|
|
|
p.on('end', function(){
|
|
|
|
//console.warn( JSON.stringify('end'));
|
|
|
|
});
|
|
|
|
// this is synchronous for now, will still need sync version later, or
|
|
|
|
// alternatively make text processing in the serializer async
|
2012-06-20 17:28:34 +00:00
|
|
|
var prefixedText = text;
|
2012-06-19 13:24:13 +00:00
|
|
|
if ( ! state.onNewline ) {
|
|
|
|
// Prefix '_' so that no start-of-line wiki syntax matches. Strip it from
|
|
|
|
// the result.
|
2012-06-20 17:28:34 +00:00
|
|
|
prefixedText = '_' + text;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( state.inIndentPre ) {
|
|
|
|
prefixedText = prefixedText.replace(/(\r?\n)/g, '$1_');
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: parse using
|
|
|
|
p.process( prefixedText );
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! state.onNewline ) {
|
2012-06-19 13:24:13 +00:00
|
|
|
// now strip the leading underscore.
|
|
|
|
if ( tokens[0] === '_' ) {
|
|
|
|
tokens.shift();
|
|
|
|
} else {
|
|
|
|
tokens[0] = tokens[0].substr(1);
|
|
|
|
}
|
|
|
|
}
|
2012-06-20 17:28:34 +00:00
|
|
|
|
|
|
|
// state.inIndentPre is handled on the complete output
|
|
|
|
|
2012-06-19 13:24:13 +00:00
|
|
|
//
|
|
|
|
// wrap any run of non-text tokens into <nowiki> tags using the source
|
|
|
|
// offsets of top-level productions
|
|
|
|
// return the updated text
|
|
|
|
var outTexts = [],
|
|
|
|
nonTextTokenAccum = [],
|
|
|
|
cursor = 0;
|
|
|
|
function wrapNonTextTokens () {
|
|
|
|
if ( nonTextTokenAccum.length ) {
|
|
|
|
var missingRangeEnd = false;
|
|
|
|
// TODO: make sure the source positions are always set!
|
|
|
|
// The start range
|
|
|
|
var startRange = nonTextTokenAccum[0].dataAttribs.tsr,
|
|
|
|
rangeStart, rangeEnd;
|
|
|
|
if ( ! startRange ) {
|
|
|
|
console.warn( 'No tsr on ' + nonTextTokenAccum[0] );
|
|
|
|
rangeStart = cursor;
|
|
|
|
} else {
|
|
|
|
rangeStart = startRange[0];
|
|
|
|
if ( ! state.onNewline ) {
|
|
|
|
// compensate for underscore.
|
|
|
|
rangeStart--;
|
|
|
|
}
|
|
|
|
cursor = rangeStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
var endRange = nonTextTokenAccum.last().dataAttribs.tsr;
|
|
|
|
if ( ! endRange ) {
|
|
|
|
// FIXME: improve this!
|
|
|
|
//rangeEnd = state.env.tokensToString( tokens ).length;
|
|
|
|
// Be conservative and extend the range to the end for now.
|
|
|
|
// Alternatives: only extend it to the next token with range
|
|
|
|
// info on it.
|
|
|
|
missingRangeEnd = true;
|
|
|
|
rangeEnd = text.length;
|
|
|
|
} else {
|
|
|
|
rangeEnd = endRange[1];
|
|
|
|
if ( ! state.onNewline ) {
|
|
|
|
// compensate for underscore.
|
|
|
|
rangeEnd--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var escapedSource = text.substr( rangeStart, rangeEnd - rangeStart )
|
|
|
|
.replace( /<(\/?nowiki)>/g, '<$1>' );
|
|
|
|
outTexts.push( '<nowiki>' );
|
|
|
|
outTexts.push( escapedSource );
|
|
|
|
outTexts.push( '</nowiki>' );
|
|
|
|
cursor += 17 + escapedSource.length;
|
|
|
|
if ( missingRangeEnd ) {
|
|
|
|
throw 'No tsr on end token: ' + nonTextTokenAccum.last();
|
|
|
|
}
|
|
|
|
nonTextTokenAccum = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
for ( var i = 0, l = tokens.length; i < l; i++ ) {
|
|
|
|
var token = tokens[i];
|
|
|
|
switch ( token.constructor ) {
|
|
|
|
case String:
|
|
|
|
wrapNonTextTokens();
|
|
|
|
outTexts.push( token );
|
|
|
|
cursor += token.length;
|
|
|
|
break;
|
|
|
|
case NlTk:
|
|
|
|
wrapNonTextTokens();
|
|
|
|
outTexts.push( '\n' );
|
|
|
|
cursor++;
|
|
|
|
break;
|
|
|
|
case EOFTk:
|
|
|
|
wrapNonTextTokens();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//console.warn('pushing ' + token);
|
|
|
|
nonTextTokenAccum.push(token);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( e ) {
|
|
|
|
console.warn( e );
|
|
|
|
}
|
|
|
|
//console.warn( 'escaped wikiText: ' + outTexts.join('') );
|
2012-06-20 17:28:34 +00:00
|
|
|
var res = outTexts.join('');
|
|
|
|
if ( state.inIndentPre ) {
|
|
|
|
return res.replace(/\n_/g, '\n');
|
|
|
|
} else {
|
|
|
|
return res;
|
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
};
|
|
|
|
|
2012-06-14 19:59:18 +00:00
|
|
|
var id = function(v) {
|
2012-06-02 14:39:33 +00:00
|
|
|
return function( state ) {
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-05-16 14:53:18 +00:00
|
|
|
WSP._listHandler = function( bullet, state, token ) {
|
2012-06-14 22:01:52 +00:00
|
|
|
function isListItem(token) {
|
|
|
|
if (token.constructor !== TagTk) return false;
|
|
|
|
|
|
|
|
var tokenName = token.name;
|
|
|
|
return (tokenName === 'li' || tokenName === 'dt' || tokenName === 'dd');
|
|
|
|
}
|
2012-06-19 23:48:52 +00:00
|
|
|
if ( state.singleLineMode ) {
|
|
|
|
state.singleLineMode--;
|
|
|
|
}
|
2012-06-14 22:01:52 +00:00
|
|
|
|
2012-05-17 15:51:18 +00:00
|
|
|
var bullets, res;
|
|
|
|
var stack = state.listStack;
|
|
|
|
if (stack.length === 0) {
|
2012-05-29 06:04:19 +00:00
|
|
|
bullets = bullet;
|
2012-05-17 15:51:18 +00:00
|
|
|
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-06-14 22:01:52 +00:00
|
|
|
// A nested list, not directly after a list item
|
|
|
|
(curList.itemCount > 1 && !isListItem(state.prevToken))) {
|
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-06-14 05:10:51 +00:00
|
|
|
return res;
|
2012-05-16 14:53:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WSP._listEndHandler = function( state, token ) {
|
|
|
|
state.listStack.pop();
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2012-05-25 11:59:55 +00:00
|
|
|
WSP._listItemHandler = function ( bullet, state, token ) {
|
2012-06-14 05:10:51 +00:00
|
|
|
var stack = state.listStack;
|
|
|
|
var curList = stack[stack.length - 1];
|
|
|
|
curList.itemCount++;
|
|
|
|
return ((curList.itemCount > 1 ) ? curList.bullets + bullet : bullet);
|
2012-05-16 14:53:18 +00:00
|
|
|
};
|
|
|
|
|
2012-06-16 15:09:06 +00:00
|
|
|
WSP._serializeTableTag = function ( symbol, optionEndSymbol, state, token ) {
|
2012-05-16 14:53:18 +00:00
|
|
|
if ( token.attribs.length ) {
|
2012-06-14 05:10:51 +00:00
|
|
|
return symbol + ' ' + 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-22 11:36:06 +00:00
|
|
|
WSP._emptyTags = { br: true, meta: true };
|
|
|
|
|
|
|
|
WSP._serializeHTMLTag = function ( state, token ) {
|
|
|
|
var close = '';
|
|
|
|
if ( WSP._emptyTags[ token.name ] ) {
|
|
|
|
close = '/';
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-20 17:28:34 +00:00
|
|
|
if ( token.name === 'pre' ) {
|
|
|
|
// html-syntax pre is very similar to nowiki
|
|
|
|
state.inHTMLPre = true;
|
|
|
|
}
|
|
|
|
|
2012-06-16 15:09:06 +00:00
|
|
|
// Swallow required newline from previous token on encountering a HTML tag
|
2012-06-18 15:18:20 +00:00
|
|
|
//state.emitNewlineOnNextToken = false;
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-05-22 11:36:06 +00:00
|
|
|
if ( token.attribs.length ) {
|
|
|
|
return '<' + token.name + ' ' +
|
|
|
|
WSP._serializeAttributes( token.attribs ) + close + '>';
|
|
|
|
} else {
|
|
|
|
return '<' + token.name + close + '>';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WSP._serializeHTMLEndTag = function ( state, token ) {
|
2012-06-20 17:28:34 +00:00
|
|
|
if ( token.name === 'pre' ) {
|
|
|
|
state.inHTMLPre = false;
|
|
|
|
}
|
2012-05-22 11:36:06 +00:00
|
|
|
if ( ! WSP._emptyTags[ token.name ] ) {
|
|
|
|
return '</' + token.name + '>';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-17 12:32:56 +00:00
|
|
|
WSP._linkHandler = function( state, token ) {
|
2012-05-22 11:36:06 +00:00
|
|
|
//return '[[';
|
2012-05-17 12:32:56 +00:00
|
|
|
// 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.
|
2012-05-22 11:36:06 +00:00
|
|
|
|
2012-06-13 18:47:26 +00:00
|
|
|
var env = state.env;
|
|
|
|
var attribDict = env.KVtoHash( token.attribs );
|
2012-05-22 11:36:06 +00:00
|
|
|
if ( attribDict.rel && attribDict.href !== undefined ) {
|
2012-06-13 18:47:26 +00:00
|
|
|
var tokenData = token.dataAttribs;
|
2012-05-22 11:36:06 +00:00
|
|
|
if ( attribDict.rel === 'mw:wikiLink' ) {
|
2012-06-14 23:23:49 +00:00
|
|
|
var base = env.wgScriptPath;
|
|
|
|
var href = attribDict.href;
|
|
|
|
var prefix = href.substr(0, base.length);
|
|
|
|
var target = (prefix === base) ? href.substr(base.length) : href;
|
|
|
|
target = decodeURIComponent(target);
|
|
|
|
|
|
|
|
var tail = tokenData.tail;
|
2012-06-05 15:26:27 +00:00
|
|
|
if ( tail && tail.length ) {
|
|
|
|
state.dropTail = tail;
|
2012-06-19 22:34:57 +00:00
|
|
|
target = tokenData.gc ? tokenData.sHref : target.replace( /_/g, ' ' );
|
2012-06-05 15:26:27 +00:00
|
|
|
} else {
|
2012-06-19 22:34:57 +00:00
|
|
|
var origLinkTgt = tokenData.sHref;
|
2012-06-14 23:23:49 +00:00
|
|
|
if (origLinkTgt) {
|
2012-06-19 22:34:57 +00:00
|
|
|
// Normalize the source target so that we can compare it
|
|
|
|
// with href.
|
|
|
|
var normalizedOrigLinkTgt = env.normalizeTitle( env.tokensToString(origLinkTgt) );
|
2012-06-14 23:23:49 +00:00
|
|
|
if ( normalizedOrigLinkTgt === target ) {
|
2012-06-05 15:26:27 +00:00
|
|
|
// Non-standard capitalization
|
2012-06-14 23:23:49 +00:00
|
|
|
target = origLinkTgt;
|
2012-06-05 15:26:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target = target.replace( /_/g, ' ' );
|
|
|
|
}
|
2012-06-02 04:29:39 +00:00
|
|
|
}
|
|
|
|
|
2012-06-05 15:26:27 +00:00
|
|
|
// FIXME: Properly handle something like [[{{Foo}}]]s
|
2012-06-13 18:47:26 +00:00
|
|
|
target = env.tokensToString( target );
|
2012-06-05 15:26:27 +00:00
|
|
|
|
2012-06-13 18:47:26 +00:00
|
|
|
if ( tokenData.gc ) {
|
2012-06-05 15:26:27 +00:00
|
|
|
state.dropContent = true;
|
|
|
|
return '[[' + target;
|
2012-05-22 11:36:06 +00:00
|
|
|
} else {
|
|
|
|
return '[[' + target + '|';
|
|
|
|
}
|
|
|
|
} else if ( attribDict.rel === 'mw:extLink' ) {
|
2012-06-13 18:47:26 +00:00
|
|
|
if ( tokenData.stx === 'urllink' ) {
|
2012-05-22 11:36:06 +00:00
|
|
|
state.dropContent = true;
|
|
|
|
return attribDict.href;
|
2012-06-13 18:47:26 +00:00
|
|
|
} else if ( tokenData.gc ) {
|
2012-05-22 11:36:06 +00:00
|
|
|
state.dropContent = true;
|
|
|
|
return '[' + attribDict.href;
|
|
|
|
} else {
|
|
|
|
return '[' + attribDict.href + ' ';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return WSP._serializeHTMLTag( state, token );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return WSP._serializeHTMLTag( state, token );
|
|
|
|
}
|
|
|
|
|
2012-05-17 12:32:56 +00:00
|
|
|
//if ( rtinfo.type === 'wikilink' ) {
|
|
|
|
// return '[[' + rtinfo.target + ']]';
|
|
|
|
//} else {
|
|
|
|
// // external link
|
|
|
|
// return '[' + rtinfo.
|
|
|
|
};
|
2012-06-14 20:18:48 +00:00
|
|
|
WSP._linkEndHandler = function( state, token ) {
|
2012-05-22 11:36:06 +00:00
|
|
|
var attribDict = state.env.KVtoHash( token.attribs );
|
|
|
|
if ( attribDict.rel && attribDict.href !== undefined ) {
|
|
|
|
if ( attribDict.rel === 'mw:wikiLink' ) {
|
|
|
|
state.dropContent = false;
|
2012-06-14 20:18:48 +00:00
|
|
|
state.dropTail = false;
|
|
|
|
return "]]" + (token.dataAttribs.tail ? token.dataAttribs.tail : "");
|
2012-05-22 11:36:06 +00:00
|
|
|
} else if ( attribDict.rel === 'mw:extLink' ) {
|
2012-06-14 20:18:48 +00:00
|
|
|
state.dropContent = false;
|
|
|
|
return (token.dataAttribs.stx === 'urllink') ? '' : ']';
|
2012-05-22 11:36:06 +00:00
|
|
|
} else {
|
|
|
|
return WSP._serializeHTMLEndTag( state, token );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return WSP._serializeHTMLEndTag( state, token );
|
|
|
|
}
|
2012-05-17 12:32:56 +00:00
|
|
|
};
|
|
|
|
|
2012-06-16 15:09:06 +00:00
|
|
|
WSP.tagHandlers = {
|
|
|
|
body: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: function(state, token) {
|
|
|
|
// swallow trailing new line
|
|
|
|
state.emitNewlineOnNextToken = false;
|
|
|
|
return '';
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
}
|
|
|
|
},
|
2012-05-16 14:53:18 +00:00
|
|
|
ul: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
startsNewline : true,
|
|
|
|
handle: WSP._listHandler.bind( null, '*' ),
|
|
|
|
pairSepNLCount: 2,
|
2012-06-18 20:56:41 +00:00
|
|
|
newlineTransparent: true
|
2012-06-18 15:18:20 +00:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
endsLine: true,
|
|
|
|
handle: WSP._listEndHandler
|
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
},
|
|
|
|
ol: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
startsNewline : true,
|
|
|
|
handle: WSP._listHandler.bind( null, '#' ),
|
|
|
|
pairSepNLCount: 2,
|
2012-06-18 20:56:41 +00:00
|
|
|
newlineTransparent: true
|
2012-06-18 15:18:20 +00:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
endsLine : true,
|
|
|
|
handle: WSP._listEndHandler
|
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
},
|
|
|
|
dl: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
startsNewline : true,
|
|
|
|
handle: WSP._listHandler.bind( null, ''),
|
2012-06-20 11:50:05 +00:00
|
|
|
pairSepNLCount: 2
|
2012-06-18 15:18:20 +00:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
endsLine: true,
|
|
|
|
handle: WSP._listEndHandler
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
},
|
|
|
|
li: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: WSP._listItemHandler.bind( null, '' ),
|
|
|
|
singleLine: 1,
|
|
|
|
pairSepNLCount: 1
|
2012-06-16 15:09:06 +00:00
|
|
|
},
|
2012-06-18 15:18:20 +00:00
|
|
|
end: {
|
|
|
|
singleLine: -1
|
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
},
|
|
|
|
// XXX: handle single-line vs. multi-line dls etc
|
2012-06-16 15:09:06 +00:00
|
|
|
dt: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
singleLine: 1,
|
2012-06-20 11:50:05 +00:00
|
|
|
handle: WSP._listItemHandler.bind( null, ';' ),
|
|
|
|
pairSepNLCount: 1,
|
|
|
|
newlineTransparent: true
|
2012-06-18 15:18:20 +00:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
singleLine: -1
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
},
|
|
|
|
dd: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
singleLine: 1,
|
2012-06-20 11:50:05 +00:00
|
|
|
handle: WSP._listItemHandler.bind( null, ":" ),
|
|
|
|
pairSepNLCount: 1,
|
|
|
|
newlineTransparent: true
|
2012-06-18 15:18:20 +00:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
endsLine: true,
|
|
|
|
singleLine: -1
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
},
|
2012-05-16 14:53:18 +00:00
|
|
|
// XXX: handle options
|
|
|
|
table: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: WSP._serializeTableTag.bind(null, "{|", '')
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
handle: function(state, token) {
|
|
|
|
if ( state.prevTagToken && state.prevTagToken.name === 'tr' ) {
|
|
|
|
this.startsNewline = true;
|
|
|
|
} else {
|
|
|
|
this.startsNewline = false;
|
|
|
|
}
|
|
|
|
return "|}";
|
|
|
|
}
|
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
},
|
2012-06-18 15:18:20 +00:00
|
|
|
tbody: { start: { ignore: true }, end: { ignore: true } },
|
2012-05-21 12:46:07 +00:00
|
|
|
th: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: function ( state, token ) {
|
|
|
|
if ( token.dataAttribs.stx_v === 'row' ) {
|
|
|
|
this.startsNewline = false;
|
|
|
|
return WSP._serializeTableTag("!!", ' |', state, token);
|
|
|
|
} else {
|
|
|
|
this.startsNewline = true;
|
|
|
|
return WSP._serializeTableTag( "!", ' |', state, token);
|
|
|
|
}
|
2012-05-21 12:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tr: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: function ( state, token ) {
|
|
|
|
if ( state.prevToken.constructor === TagTk && state.prevToken.name === 'tbody' ) {
|
|
|
|
// Omit for first row in a table. XXX: support optional trs
|
|
|
|
// for first line (in source wikitext) too using some flag in
|
|
|
|
// data-mw (stx: 'wikitext' ?)
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return WSP._serializeTableTag("|-", '', state, token );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
startsNewline: true
|
2012-05-21 12:46:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
td: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: function ( state, token ) {
|
|
|
|
if ( token.dataAttribs.stx_v === 'row' ) {
|
|
|
|
this.startsNewline = false;
|
|
|
|
return WSP._serializeTableTag("||", ' |', state, token);
|
|
|
|
} else {
|
|
|
|
this.startsNewline = true;
|
|
|
|
return WSP._serializeTableTag("|", ' |', state, token);
|
|
|
|
}
|
2012-05-21 12:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-06-16 15:09:06 +00:00
|
|
|
caption: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
startsNewline: true,
|
|
|
|
handle: WSP._serializeTableTag.bind(null, "|+", ' |')
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
},
|
2012-05-16 14:53:18 +00:00
|
|
|
p: {
|
2012-06-18 15:18:20 +00:00
|
|
|
make: function(state, token) {
|
2012-06-16 15:09:06 +00:00
|
|
|
// Special case handling in a list context
|
|
|
|
// VE embeds list content in paragraph tags
|
2012-06-18 15:18:20 +00:00
|
|
|
return state.singleLineMode ? WSP.defaultHTMLTagHandler : this;
|
2012-06-16 15:09:06 +00:00
|
|
|
},
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
startsNewline : true,
|
|
|
|
pairSepNLCount: 2
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
endsLine: true
|
|
|
|
}
|
2012-05-16 14:53:18 +00:00
|
|
|
},
|
2012-05-25 16:55:38 +00:00
|
|
|
// XXX: support indent variant instead by registering a newline handler?
|
|
|
|
pre: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
startsNewline: true,
|
|
|
|
handle: function( state, token ) {
|
2012-06-20 17:28:34 +00:00
|
|
|
state.inIndentPre = true;
|
2012-06-19 13:24:13 +00:00
|
|
|
state.textHandler = function( t ) {
|
|
|
|
return t.replace(/\n/g, '\n ' );
|
|
|
|
};
|
2012-06-18 15:18:20 +00:00
|
|
|
return ' ';
|
|
|
|
}
|
2012-05-25 16:55:38 +00:00
|
|
|
},
|
2012-06-18 15:18:20 +00:00
|
|
|
end: {
|
|
|
|
endsLine: true,
|
2012-06-20 17:28:34 +00:00
|
|
|
handle: function( state, token) {
|
|
|
|
state.inIndentPre = false;
|
|
|
|
state.textHandler = null;
|
|
|
|
return '';
|
|
|
|
}
|
2012-06-18 15:18:20 +00:00
|
|
|
}
|
2012-05-25 16:55:38 +00:00
|
|
|
},
|
2012-05-22 11:36:06 +00:00
|
|
|
meta: {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: {
|
|
|
|
handle: function ( state, token ) {
|
|
|
|
var argDict = state.env.KVtoHash( token.attribs );
|
|
|
|
if ( argDict['typeof'] === 'mw:tag' ) {
|
2012-06-18 20:56:41 +00:00
|
|
|
// we use this currently for nowiki and noinclude & co
|
|
|
|
this.newlineTransparent = true;
|
2012-06-19 13:24:13 +00:00
|
|
|
if ( argDict.content === 'nowiki' ) {
|
|
|
|
state.inNoWiki = true;
|
|
|
|
} else if ( argDict.content === '/nowiki' ) {
|
|
|
|
state.inNoWiki = false;
|
2012-06-20 11:50:05 +00:00
|
|
|
} else {
|
|
|
|
console.warn( JSON.stringify( argDict ) );
|
2012-06-19 13:24:13 +00:00
|
|
|
}
|
2012-06-18 15:18:20 +00:00
|
|
|
return '<' + argDict.content + '>';
|
|
|
|
} else {
|
2012-06-18 20:56:41 +00:00
|
|
|
this.newlineTransparent = false;
|
2012-06-18 15:18:20 +00:00
|
|
|
return WSP._serializeHTMLTag( state, token );
|
|
|
|
}
|
2012-05-22 11:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-06-18 15:18:20 +00:00
|
|
|
hr: {
|
|
|
|
start: { startsNewline: true, handle: id("----") },
|
|
|
|
end: { endsLine: true }
|
|
|
|
},
|
|
|
|
h1: {
|
|
|
|
start: { startsNewline: true, handle: id("=") },
|
|
|
|
end: { endsLine: true, handle: id("=") }
|
|
|
|
},
|
|
|
|
h2: {
|
|
|
|
start: { startsNewline: true, handle: id("==") },
|
|
|
|
end: { endsLine: true, handle: id("==") }
|
|
|
|
},
|
|
|
|
h3: {
|
|
|
|
start: { startsNewline: true, handle: id("===") },
|
|
|
|
end: { endsLine: true, handle: id("===") }
|
|
|
|
},
|
|
|
|
h4: {
|
|
|
|
start: { startsNewline: true, handle: id("====") },
|
|
|
|
end: { endsLine: true, handle: id("====") }
|
|
|
|
},
|
|
|
|
h5: {
|
|
|
|
start: { startsNewline: true, handle: id("=====") },
|
|
|
|
end: { endsLine: true, handle: id("=====") }
|
|
|
|
},
|
|
|
|
h6: {
|
|
|
|
start: { startsNewline: true, handle: id("======") },
|
|
|
|
end: { endsLine: true, handle: id("======") }
|
|
|
|
},
|
|
|
|
br: {
|
|
|
|
start: { startsNewline: true, handle: id("") },
|
|
|
|
end: { endsLine: true }
|
|
|
|
},
|
|
|
|
b: {
|
|
|
|
start: { handle: id("'''") },
|
|
|
|
end: { handle: id("'''") }
|
|
|
|
},
|
|
|
|
i: {
|
|
|
|
start: { handle: id("''") },
|
|
|
|
end: { handle: id("''") }
|
|
|
|
},
|
|
|
|
a: {
|
|
|
|
start: { handle: WSP._linkHandler },
|
|
|
|
end: { handle: WSP._linkEndHandler }
|
|
|
|
}
|
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
|
|
|
|
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-16 14:53:18 +00:00
|
|
|
if ( chunkCB === undefined ) {
|
|
|
|
var out = [];
|
2012-06-14 05:10:51 +00:00
|
|
|
state.chunkCB = 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-06-14 05:10:51 +00:00
|
|
|
state.chunkCB = 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-06-11 22:55:41 +00:00
|
|
|
WSP.defaultHTMLTagHandler = {
|
2012-06-18 15:18:20 +00:00
|
|
|
start: { handle: WSP._serializeHTMLTag },
|
|
|
|
end : { handle: WSP._serializeHTMLEndTag }
|
2012-06-11 22:55:41 +00:00
|
|
|
};
|
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
WSP._getTokenHandler = function(state, token) {
|
|
|
|
var handler;
|
|
|
|
if (token.dataAttribs.stx === 'html') {
|
|
|
|
handler = this.defaultHTMLTagHandler;
|
|
|
|
} else {
|
|
|
|
var tname = token.name;
|
|
|
|
handler = this.tagHandlers[tname];
|
|
|
|
if ( handler && handler.make ) {
|
|
|
|
handler = handler.make(state, token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! handler ) {
|
|
|
|
handler = this.defaultHTMLTagHandler;
|
|
|
|
}
|
|
|
|
if ( token.constructor === TagTk || token.constructor === SelfclosingTagTk ) {
|
|
|
|
return handler.start || {};
|
2012-06-11 22:55:41 +00:00
|
|
|
} else {
|
2012-06-18 15:18:20 +00:00
|
|
|
return handler.end || {};
|
2012-06-11 22:55:41 +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 ) {
|
2012-06-18 15:18:20 +00:00
|
|
|
var handler = {},
|
|
|
|
res = '',
|
|
|
|
dropContent = state.dropContent;
|
2012-06-14 05:10:51 +00:00
|
|
|
|
2012-05-21 12:46:07 +00:00
|
|
|
state.prevToken = state.curToken;
|
2012-06-14 05:10:51 +00:00
|
|
|
state.curToken = token;
|
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
|
2012-05-17 11:20:39 +00:00
|
|
|
switch( token.constructor ) {
|
|
|
|
case TagTk:
|
|
|
|
case SelfclosingTagTk:
|
2012-06-18 15:18:20 +00:00
|
|
|
handler = WSP._getTokenHandler( state, token );
|
|
|
|
if ( ! handler.ignore ) {
|
|
|
|
state.prevTagToken = state.currTagToken;
|
|
|
|
state.currTagToken = token;
|
|
|
|
res = handler.handle ? handler.handle( state, token ) : '';
|
|
|
|
}
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case EndTagTk:
|
2012-06-18 15:18:20 +00:00
|
|
|
handler = WSP._getTokenHandler( state, token );
|
|
|
|
if ( ! handler.ignore ) {
|
|
|
|
state.prevTagToken = state.currTagToken;
|
|
|
|
state.currTagToken = token;
|
2012-06-19 23:48:52 +00:00
|
|
|
if ( handler.singleLine < 0 && state.singleLineMode ) {
|
2012-06-18 15:18:20 +00:00
|
|
|
state.singleLineMode--;
|
|
|
|
}
|
|
|
|
res = handler.handle ? handler.handle( state, token ) : '';
|
|
|
|
}
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case String:
|
2012-06-20 17:28:34 +00:00
|
|
|
res = ( state.inNoWiki || state.inHTMLPre ) ? token
|
|
|
|
: this.escapeWikiText( state, token );
|
2012-06-19 13:24:13 +00:00
|
|
|
res = state.textHandler ? state.textHandler( res ) : res;
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case CommentTk:
|
2012-05-22 11:36:06 +00:00
|
|
|
res = '<!--' + token.value + '-->';
|
2012-06-19 13:24:13 +00:00
|
|
|
// don't consider comments for changes of the onStartOfLine status
|
2012-06-18 20:56:41 +00:00
|
|
|
// XXX: convert all non-tag handlers to a similar handler
|
|
|
|
// structure as tags?
|
|
|
|
handler = { newlineTransparent: true };
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case NlTk:
|
2012-05-22 11:36:06 +00:00
|
|
|
res = '\n';
|
2012-06-18 15:18:20 +00:00
|
|
|
res = state.textHandler ? state.textHandler( res ) : res;
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
case EOFTk:
|
2012-06-13 16:53:05 +00:00
|
|
|
res = '';
|
2012-06-18 15:18:20 +00:00
|
|
|
for ( var i = 0, l = state.availableNewlineCount; i < l; i++ ) {
|
|
|
|
res += '\n';
|
|
|
|
}
|
|
|
|
state.chunkCB(res);
|
2012-05-17 11:20:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-06-13 16:53:05 +00:00
|
|
|
res = '';
|
2012-05-17 11:20:39 +00:00
|
|
|
console.warn( 'Unhandled token type ' + JSON.stringify( token ) );
|
|
|
|
break;
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
2012-06-14 05:10:51 +00:00
|
|
|
|
2012-06-20 11:50:05 +00:00
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
if (! dropContent || ! state.dropContent ) {
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-20 14:31:39 +00:00
|
|
|
var newNLCount = 0;
|
2012-06-18 15:18:20 +00:00
|
|
|
if (res !== '') {
|
2012-06-20 14:31:39 +00:00
|
|
|
// Strip leading or trailing newlines from the returned string
|
2012-06-18 15:18:20 +00:00
|
|
|
var match = res.match( /^((?:\r?\n)*)((?:.*?|[\r\n]+[^\r\n])*?)((?:\r?\n)*)$/ ),
|
|
|
|
leadingNLs = match[1],
|
|
|
|
trailingNLs = match[3];
|
|
|
|
|
|
|
|
if (leadingNLs === res) {
|
2012-06-14 05:10:51 +00:00
|
|
|
// all newlines, accumulate count, and clear output
|
2012-06-18 15:18:20 +00:00
|
|
|
state.availableNewlineCount += leadingNLs.replace(/\r\n/g, '\n').length;
|
2012-06-14 05:10:51 +00:00
|
|
|
res = "";
|
2012-05-29 06:04:19 +00:00
|
|
|
} else {
|
2012-06-20 14:31:39 +00:00
|
|
|
newNLCount = trailingNLs.replace(/\r\n/g, '\n').length;
|
2012-06-18 15:18:20 +00:00
|
|
|
if ( leadingNLs !== '' ) {
|
2012-06-20 14:31:39 +00:00
|
|
|
state.availableNewlineCount += leadingNLs.replace(/\r\n/g, '\n').length;
|
2012-06-18 15:18:20 +00:00
|
|
|
}
|
|
|
|
// strip newlines
|
|
|
|
res = match[2];
|
2012-06-14 05:10:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
// Check if we have a pair of identical tag tokens </p><p>; </ul><ul>; etc.
|
|
|
|
// that have to be separated by extra newlines and add those in.
|
|
|
|
if (handler.pairSepNLCount && state.prevTagToken &&
|
|
|
|
state.prevTagToken.constructor === EndTagTk &&
|
2012-06-19 23:48:52 +00:00
|
|
|
state.prevTagToken.name == token.name )
|
|
|
|
{
|
2012-06-20 14:31:39 +00:00
|
|
|
if ( state.availableNewlineCount < handler.pairSepNLCount) {
|
|
|
|
state.availableNewlineCount = handler.pairSepNLCount;
|
2012-06-19 23:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
if ( state.env.debug ) {
|
2012-06-20 11:50:05 +00:00
|
|
|
console.warn(token + " -> " + res + ", onnl: " + state.onNewline + ", #nls " +
|
2012-06-20 14:31:39 +00:00
|
|
|
state.availableNewlineCount + ', new ' + newNLCount);
|
2012-06-16 15:09:06 +00:00
|
|
|
}
|
2012-06-20 11:50:05 +00:00
|
|
|
if (res !== '' ) {
|
2012-06-18 15:18:20 +00:00
|
|
|
var out = '';
|
|
|
|
// Prev token's new line token
|
|
|
|
if ( !state.singleLineMode &&
|
|
|
|
( ( !res.match(/^\s*$/) && state.emitNewlineOnNextToken ) ||
|
2012-06-19 13:24:13 +00:00
|
|
|
( handler.startsNewline && !state.onStartOfLine ) ) )
|
2012-06-18 15:18:20 +00:00
|
|
|
{
|
|
|
|
// Emit new line, if necessary
|
2012-06-20 14:31:39 +00:00
|
|
|
if ( ! state.availableNewlineCount ) {
|
|
|
|
state.availableNewlineCount++;
|
2012-06-18 15:18:20 +00:00
|
|
|
}
|
|
|
|
state.emitNewlineOnNextToken = false;
|
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-20 14:31:39 +00:00
|
|
|
if ( state.availableNewlineCount ) {
|
2012-06-18 15:18:20 +00:00
|
|
|
state.onNewline = true;
|
2012-06-19 13:24:13 +00:00
|
|
|
state.onStartOfLine = true;
|
2012-06-18 15:18:20 +00:00
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-20 14:31:39 +00:00
|
|
|
// Add required # of new lines in the beginning
|
|
|
|
for (; state.availableNewlineCount; state.availableNewlineCount--) {
|
|
|
|
out += '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
state.availableNewlineCount = newNLCount;
|
|
|
|
|
2012-06-05 15:26:27 +00:00
|
|
|
// FIXME: This might modify not just the last content token in a
|
|
|
|
// link, which would be wrong. We'll likely have to collect tokens
|
|
|
|
// between a tags instead, and strip only the last content token.
|
|
|
|
if (state.dropTail && res.substr(- state.dropTail.length) === state.dropTail) {
|
|
|
|
res = res.substr(0, res.length - state.dropTail.length);
|
2012-06-03 22:00:54 +00:00
|
|
|
}
|
2012-06-14 05:10:51 +00:00
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
if ( state.singleLineMode ) {
|
|
|
|
res = res.replace(/\n/g, ' ');
|
|
|
|
}
|
|
|
|
out += res;
|
2012-06-19 13:24:13 +00:00
|
|
|
if ( res !== '' ) {
|
2012-06-18 15:18:20 +00:00
|
|
|
state.onNewline = false;
|
2012-06-19 13:24:13 +00:00
|
|
|
if ( !handler.newlineTransparent ) {
|
|
|
|
state.onStartOfLine = false;
|
|
|
|
}
|
2012-06-18 15:18:20 +00:00
|
|
|
}
|
2012-06-20 11:50:05 +00:00
|
|
|
state.env.dp(' =>', out);
|
2012-06-18 15:18:20 +00:00
|
|
|
state.chunkCB( out );
|
2012-06-20 11:50:05 +00:00
|
|
|
} else {
|
2012-06-20 14:31:39 +00:00
|
|
|
state.availableNewlineCount += newNLCount;
|
2012-06-20 11:50:05 +00:00
|
|
|
if ( handler.startsNewline && ! state.onStartOfLine ) {
|
|
|
|
state.emitNewlineOnNextToken = true;
|
|
|
|
}
|
2012-05-29 06:04:19 +00:00
|
|
|
}
|
2012-06-18 15:18:20 +00:00
|
|
|
/* else {
|
|
|
|
console.warn("SILENT: tok: " + token + ", res: <" + res + ">" + ", onnl: " + state.onNewline + ", # nls: " + state.availableNewlineCount);
|
|
|
|
}
|
|
|
|
*/
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
if (handler.endsLine) {
|
|
|
|
// Record end of line
|
|
|
|
state.emitNewlineOnNextToken = true;
|
|
|
|
}
|
|
|
|
if ( handler.singleLine > 0 ) {
|
|
|
|
state.singleLineMode += handler.singleLine;
|
|
|
|
}
|
|
|
|
}
|
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-29 06:04:19 +00:00
|
|
|
//console.warn( node.innerHTML );
|
2012-05-16 14:53:18 +00:00
|
|
|
if ( ! chunkCB ) {
|
|
|
|
var out = [];
|
2012-06-14 05:10:51 +00:00
|
|
|
state.chunkCB = out.push.bind( out );
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeDOM( node, state );
|
2012-06-18 15:18:20 +00:00
|
|
|
this._serializeToken( state, new EOFTk() );
|
2012-05-16 14:53:18 +00:00
|
|
|
return out.join('');
|
|
|
|
} else {
|
2012-06-14 05:10:51 +00:00
|
|
|
state.chunkCB = chunkCB;
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeDOM( node, state );
|
2012-06-18 15:18:20 +00:00
|
|
|
this._serializeToken( state, new EOFTk() );
|
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(),
|
2012-05-23 15:50:35 +00:00
|
|
|
tkAttribs = this._getDOMAttribs(node.attributes),
|
|
|
|
tkRTInfo = this._getDOMRTInfo(node.attributes);
|
2012-05-17 11:20:39 +00:00
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
// Serialize the start token
|
|
|
|
this._serializeToken(state, new TagTk(name, tkAttribs, tkRTInfo));
|
2012-06-16 15:09:06 +00:00
|
|
|
|
|
|
|
// then children
|
2012-05-23 15:50:35 +00:00
|
|
|
for ( var i = 0, l = children.length; i < l; i++ ) {
|
|
|
|
this._serializeDOM( children[i], state );
|
2012-05-16 14:53:18 +00:00
|
|
|
}
|
2012-06-16 15:09:06 +00:00
|
|
|
|
2012-06-18 15:18:20 +00:00
|
|
|
// then the end token
|
|
|
|
this._serializeToken(state, new EndTagTk(name, tkAttribs, tkRTInfo));
|
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-06-18 15:18:20 +00:00
|
|
|
// delay the newline creation until after the comment
|
|
|
|
var savedEmitNewlineOnNextToken = state.emitNewlineOnNextToken;
|
2012-06-16 15:09:06 +00:00
|
|
|
state.emitNewlineOnNextToken = false;
|
2012-05-21 12:46:07 +00:00
|
|
|
this._serializeToken( state, new CommentTk( node.data ) );
|
2012-06-18 15:18:20 +00:00
|
|
|
state.emitNewlineOnNextToken = savedEmitNewlineOnNextToken;
|
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 {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-19 13:24:13 +00:00
|
|
|
|
2012-05-16 14:53:18 +00:00
|
|
|
// 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;
|
|
|
|
}
|