From 0eabd2c67ee07b0f04e5b5e4b563641293142273 Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Mon, 4 Jun 2012 10:49:59 +0200 Subject: [PATCH] Add round-trip form and split out rt diffing Change-Id: I3bc8ad7f273937ce6c767b8d7bbccdc86cbd93b4 --- api/ParserService.js | 98 +++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/api/ParserService.js b/api/ParserService.js index 0e69e2f596..a1c8854246 100644 --- a/api/ParserService.js +++ b/api/ParserService.js @@ -42,11 +42,13 @@ app.get('/', function(req, res){ res.write( '

Usage:

'); - res.write('

There are also some tools for experiments:' + - '

'); }); var htmlSpecialChars = function ( s ) { @@ -163,12 +165,46 @@ var refineDiff = function( diff ) { return out; }; +var roundTripDiff = function ( res, src, document ) { + res.write(''); + res.write( '

Wikitext parsed to HTML DOM


' ); + res.write(document.body.innerHTML + '
'); + res.write( '

HTML DOM converted back to Wikitext


' ); + var out = new WikitextSerializer({env: env}).serializeDOM( document.body ); + res.write('
' + htmlSpecialChars( out ) + '

'); + res.write( '

Diff between original Wikitext (green) and round-tripped wikitext (red)


' ); + var patch; + if ( src.length < 4000 ) { + // Use word-based diff for small articles + patch = jsDiff.convertChangesToXML( jsDiff.diffWords( out, src ) ); + } else { + //console.log(JSON.stringify( jsDiff.diffLines( out, src ) )); + //patch = jsDiff.convertChangesToXML( jsDiff.diffLines( out, src ) ); + patch = jsDiff.convertChangesToXML( refineDiff( jsDiff.diffLines( out, src ) ) ); + } + res.end( '
' + patch);
+};
+
+var parse = function ( res, cb, src ) {
+	var parser = parserPipelineFactory.makePipeline( 'text/x-mediawiki/full' );
+	parser.on('document', cb.bind( null, res, src ) );
+	try {
+		res.setHeader('Content-Type', 'text/html; charset=UTF-8');
+		//console.log('starting parsing of ' + req.params[0]);
+		// FIXME: This does not handle includes or templates correctly
+		parser.process( src );
+	} catch (e) {
+		console.log( e );
+		res.end( e );
+	}
+};
+
 /**
  * Round-trip article testing
  */
-app.get(/\/_roundtrip\/(.*)/, function(req, res){
+app.get(/\/_rt\/(.*)/, function(req, res){
 	env.pageName = req.params[0];
-	env.wgScriptPath = '/_roundtrip';
+	env.wgScriptPath = '/_rt';
 
 	if ( env.pageName === 'favicon.ico' ) {
 		res.end( 'no favicon yet..' );
@@ -179,37 +215,25 @@ app.get(/\/_roundtrip\/(.*)/, function(req, res){
 	
 	console.log('retrieving ' + req.params[0]);
 	var tpr = new TemplateRequest( env, target );
-	tpr.once('src', function ( src ) {
-		var parser = parserPipelineFactory.makePipeline( 'text/x-mediawiki/full' );
-		parser.on('document', function ( document ) {
-			res.write('');
-			res.write( '

Wikitext parsed to HTML DOM


' ); - res.write(document.body.innerHTML + '
'); - res.write( '

HTML DOM converted back to Wikitext


' ); - var out = new WikitextSerializer({env: env}).serializeDOM( document.body ); - res.write('
' + htmlSpecialChars( out ) + '

'); - res.write( '

Diff between original Wikitext (green) and round-tripped wikitext (red)


' ); - var patch; - if ( src.length < 4000 ) { - // Use word-based diff for small articles - patch = jsDiff.convertChangesToXML( jsDiff.diffWords( out, src ) ); - } else { - //console.log(JSON.stringify( jsDiff.diffLines( out, src ) )); - //patch = jsDiff.convertChangesToXML( jsDiff.diffLines( out, src ) ); - patch = jsDiff.convertChangesToXML( refineDiff( jsDiff.diffLines( out, src ) ) ); - } - res.end( '
' + patch);
-		});
-		try {
-			res.setHeader('Content-Type', 'text/html; charset=UTF-8');
-			console.log('starting parsing of ' + req.params[0]);
-			// FIXME: This does not handle includes or templates correctly
-			parser.process( src );
-		} catch (e) {
-			console.log( e );
-			res.end( e );
-		}
-	});
+	tpr.once('src', parse.bind( null, res, roundTripDiff ));
+});
+
+/**
+ * Form-based round-tripping for manual testing
+ */
+app.get(/\/_rtform\/(.*)/, function(req, res){
+	env.pageName = req.params[0];
+	res.setHeader('Content-Type', 'text/html; charset=UTF-8');
+	res.write( "Your wikitext:" );
+	textarea( res );
+	res.end('');
+});
+app.post(/\/_rtform\/(.*)/, function(req, res){
+	env.pageName = req.params[0];
+	res.setHeader('Content-Type', 'text/html; charset=UTF-8');
+	var parser = parserPipelineFactory.makePipeline( 'text/x-mediawiki/full' );
+
+	parse( res, roundTripDiff, req.body.content);
 });
 
 /**