-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (48 loc) · 2.03 KB
/
server.js
File metadata and controls
56 lines (48 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require( 'parsoid/lib/core-upgrade.js' );
var port = 3000;
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.text({limit: '50mb'}));
// wikitext to HTML parsing using Parsoid: code taken from parser.js test program included
// in Parsoid project's source.
var ParserEnv = require('parsoid/lib/mediawiki.parser.environment.js').MWParserEnvironment;
var ParsoidConfig = require('parsoid/lib/mediawiki.ParsoidConfig.js').ParsoidConfig;
var DU = require('parsoid/lib/mediawiki.DOMUtils.js').DOMUtils;
var getParserEnv = Promise.promisify(ParserEnv.getParserEnv, false, ParserEnv);
// test with:
// curl -X POST -v -H"Content-Type:text/plain" \
// --data “stuff that goes in POST body" \
// localhost:3000/parse
//
var prefix = "enwiki";
app.post('/parse', function(req, res) {
// Convert wikitext to HTML and send it as response
var parsoidConfig = new ParsoidConfig(null, null);
//parsoidConfig.fetchTemplates = false;
getParserEnv(parsoidConfig, null, prefix, null, null).then(function(env) {
return new Promise(function(resolve) {
var parser = env.pipelineFactory.getPipeline('text/x-mediawiki/full');
parser.once('document', resolve);
// Kick off the pipeline by feeding the input into the parser pipeline
env.setPageSrcInfo(req.body);
parser.processToplevelDoc(env.page.src);
}).then(function(doc) {
res.send(DU.serializeNode(doc));
}).done();
});
});
app.listen(port);
console.log('Listening on port ' + port);
}