2011-12-28 01:37:06 +00:00
|
|
|
/**
|
2012-03-07 18:42:26 +00:00
|
|
|
* Command line parse utility.
|
2011-12-28 01:37:06 +00:00
|
|
|
* Read from STDIN, write to STDOUT.
|
2012-01-31 00:02:48 +00:00
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
var ParserPipelineFactory = require('./mediawiki.parser.js').ParserPipelineFactory,
|
2012-01-21 22:42:54 +00:00
|
|
|
ParserEnv = require('./mediawiki.parser.environment.js').MWParserEnvironment,
|
|
|
|
optimist = require('optimist');
|
2011-12-28 01:37:06 +00:00
|
|
|
|
2012-04-06 01:30:18 +00:00
|
|
|
( function() {
|
2012-03-26 19:40:56 +00:00
|
|
|
var opts = optimist.usage( 'Usage: echo wikitext | $0', {
|
|
|
|
'help': {
|
|
|
|
description: 'Show this message',
|
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
|
|
|
},
|
|
|
|
'linearmodel': {
|
|
|
|
description: 'Output linear model data instead of HTML',
|
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
|
|
|
},
|
2012-04-06 01:30:18 +00:00
|
|
|
'wikidom': {
|
|
|
|
description: 'Output WikiDOM instead of HTML',
|
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
|
|
|
},
|
2012-01-21 22:42:54 +00:00
|
|
|
'debug': {
|
|
|
|
description: 'Debug mode',
|
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
2012-01-22 19:32:28 +00:00
|
|
|
},
|
2012-01-31 16:50:16 +00:00
|
|
|
'trace': {
|
|
|
|
description: 'Trace mode (light debugging), implied by --debug',
|
|
|
|
'boolean': true,
|
|
|
|
'default': false
|
|
|
|
},
|
2012-01-22 19:32:28 +00:00
|
|
|
'maxdepth': {
|
|
|
|
description: 'Maximum expansion depth',
|
|
|
|
'boolean': false,
|
|
|
|
'default': 40
|
2012-01-31 00:02:48 +00:00
|
|
|
},
|
2012-04-09 20:58:55 +00:00
|
|
|
'wgScript': {
|
2012-01-31 00:02:48 +00:00
|
|
|
description: 'http path to remote API, e.g. http://wiki.sample.com/w',
|
|
|
|
'boolean': false,
|
|
|
|
'default': 'http://en.wikipedia.org/w'
|
|
|
|
},
|
2012-04-09 20:58:55 +00:00
|
|
|
'wgScriptPath': {
|
|
|
|
description: 'http path to remote web interface, e.g. http://wiki.sample.com/wiki',
|
|
|
|
'boolean': false,
|
|
|
|
'default': 'http://en.wikipedia.org/wiki'
|
|
|
|
},
|
2012-01-31 00:02:48 +00:00
|
|
|
'wgScriptExtension': {
|
|
|
|
description: 'Extension for PHP files on remote API server, if any. Include the period, e.g. ".php"',
|
|
|
|
'boolean': false,
|
|
|
|
'default': '.php'
|
|
|
|
},
|
|
|
|
'fetchTemplates': {
|
|
|
|
description: 'Whether to fetch included templates recursively',
|
|
|
|
'boolean': true,
|
|
|
|
'default': true
|
2012-04-11 14:34:27 +00:00
|
|
|
},
|
|
|
|
'pagename': {
|
|
|
|
description: 'The page name, returned for {{PAGENAME}}.',
|
|
|
|
'boolean': false,
|
|
|
|
'default': 'Main page'
|
2012-01-21 22:42:54 +00:00
|
|
|
}
|
2012-03-26 19:40:56 +00:00
|
|
|
});
|
|
|
|
var argv = opts.argv;
|
|
|
|
|
|
|
|
if ( argv.help ) {
|
|
|
|
optimist.showHelp();
|
|
|
|
return;
|
|
|
|
}
|
2011-12-28 01:37:06 +00:00
|
|
|
|
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..
|
2012-04-09 20:58:55 +00:00
|
|
|
wgScript: argv.wgScript,
|
2012-01-31 00:02:48 +00:00
|
|
|
wgScriptPath: argv.wgScriptPath,
|
|
|
|
wgScriptExtension: argv.wgScriptExtension,
|
2012-03-06 18:02:35 +00:00
|
|
|
// XXX: add options for this!
|
2012-03-08 11:44:37 +00:00
|
|
|
wgUploadPath: 'http://upload.wikimedia.org/wikipedia/commons',
|
2012-01-31 00:02:48 +00:00
|
|
|
fetchTemplates: argv.fetchTemplates,
|
2012-01-20 02:36:18 +00:00
|
|
|
// enable/disable debug output using this switch
|
2012-01-22 19:32:28 +00:00
|
|
|
debug: argv.debug,
|
2012-01-31 16:50:16 +00:00
|
|
|
trace: argv.trace,
|
2012-04-11 14:34:27 +00:00
|
|
|
maxDepth: argv.maxdepth,
|
|
|
|
pageName: argv.pagename
|
2012-04-25 14:35:59 +00:00
|
|
|
} );
|
|
|
|
var parserPipelineFactory = new ParserPipelineFactory( env );
|
2012-04-25 18:19:43 +00:00
|
|
|
parser = parserPipelineFactory.makePipeline( 'text/x-mediawiki/full' );
|
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 ) {
|
2012-02-11 17:59:17 +00:00
|
|
|
// Print out the html
|
2012-03-26 19:40:56 +00:00
|
|
|
if ( argv.linearmodel ) {
|
|
|
|
process.stdout.write( parser.getLinearModel( document ) );
|
2012-04-06 01:30:18 +00:00
|
|
|
} else if ( argv.wikidom ) {
|
|
|
|
process.stdout.write( parser.getWikiDom( document ) );
|
2012-03-26 19:40:56 +00:00
|
|
|
} else {
|
|
|
|
process.stdout.write( document.body.innerHTML );
|
|
|
|
}
|
2012-01-18 23:46:01 +00:00
|
|
|
// add a trailing newline for shell user's benefit
|
|
|
|
process.stdout.write( "\n" );
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
// Kick off the pipeline by feeding the input into the parser pipeline
|
2012-04-25 14:35:59 +00:00
|
|
|
parser.process( input );
|
2011-12-28 01:37:06 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
} )();
|