2011-12-28 01:37:06 +00:00
|
|
|
/**
|
|
|
|
* Command line wikidom parse utility.
|
|
|
|
* Read from STDIN, write to STDOUT.
|
2012-01-20 02:36:18 +00:00
|
|
|
*
|
|
|
|
* @author Neil Kandalgaonkar <neilk@wikimedia.org>
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
2011-12-28 01:37:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
( function() {
|
|
|
|
|
2012-01-04 08:39:45 +00:00
|
|
|
var ParserPipeline = require('./mediawiki.parser.js').ParserPipeline,
|
2012-01-10 01:09:50 +00:00
|
|
|
ParserEnv = require('./mediawiki.parser.environment.js').MWParserEnvironment,
|
2012-01-18 23:46:01 +00:00
|
|
|
DOMConverter = require('./mediawiki.DOMConverter.js').DOMConverter,
|
2011-12-28 01:37:06 +00:00
|
|
|
optimist = require('optimist');
|
|
|
|
|
2012-01-20 00:49:27 +00:00
|
|
|
var env = new ParserEnv( {
|
2012-01-20 02:36:18 +00:00
|
|
|
// fetch templates from enwiki by default..
|
|
|
|
wgScriptPath: "http://en.wikipedia.org/w",
|
|
|
|
wgScriptExtension: ".php",
|
2012-01-20 00:49:27 +00:00
|
|
|
fetchTemplates: true,
|
2012-01-20 02:36:18 +00:00
|
|
|
// enable/disable debug output using this switch
|
2012-01-21 20:38:13 +00:00
|
|
|
debug: false
|
2012-01-20 00:49:27 +00:00
|
|
|
} ),
|
2012-01-19 23:43:39 +00:00
|
|
|
parser = new ParserPipeline( env );
|
2011-12-28 01:37:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
process.stdin.resume();
|
|
|
|
process.stdin.setEncoding('utf8');
|
|
|
|
|
|
|
|
var inputChunks = [];
|
|
|
|
process.stdin.on( 'data', function( chunk ) {
|
|
|
|
inputChunks.push( chunk );
|
|
|
|
} );
|
|
|
|
|
2012-01-18 23:46:01 +00:00
|
|
|
|
|
|
|
|
2011-12-28 01:37:06 +00:00
|
|
|
process.stdin.on( 'end', function() {
|
|
|
|
var input = inputChunks.join('');
|
2012-01-18 23:46:01 +00:00
|
|
|
parser.on('document', function ( document ) {
|
|
|
|
var wikiDom = new DOMConverter().HTMLtoWiki( document.body ),
|
|
|
|
// Serialize the WikiDom with indentation
|
|
|
|
output = JSON.stringify( wikiDom, null, 2 );
|
|
|
|
process.stdout.write( output );
|
|
|
|
// add a trailing newline for shell user's benefit
|
|
|
|
process.stdout.write( "\n" );
|
2012-01-19 23:43:39 +00:00
|
|
|
|
|
|
|
if ( env.debug ) {
|
|
|
|
// Also print out the html
|
|
|
|
process.stderr.write( document.body.innerHTML );
|
|
|
|
process.stderr.write( "\n" );
|
|
|
|
}
|
2012-01-18 23:46:01 +00:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
// Kick off the pipeline by feeding the input into the parser pipeline
|
2012-01-04 08:39:45 +00:00
|
|
|
parser.parse( input );
|
2011-12-28 01:37:06 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
} )();
|