Switch carto to be synchronous.

browserify
Young Hahn 10 years ago
parent cd18bea7ba
commit c5d8f4510e

@ -205,20 +205,19 @@ The `Renderer` interface is the main API for developers, and it takes an MML fil
// - data (a string containing the MML or an object of MML)
var carto = require('carto');
new carto.Renderer({
try {
var output = new carto.Renderer({
filename: input,
local_data_dir: path.dirname(input),
}).render(data, function(err, output) {
if (err) {
}).render(data);
} catch(err) {
if (Array.isArray(err)) {
err.forEach(function(e) {
carto.writeError(e, options);
});
} else { throw err; }
} else {
sys.puts(output);
}
});
console.log(output);
### Vim

@ -65,26 +65,13 @@ function compileMML(err, data) {
console.error(err);
process.exit(1);
}
try {
var renderer = new carto.Renderer({
filename: input,
benchmark: options.benchmark,
ppi: options.ppi
});
renderer.render(data, function(err, output) {
if (err) {
console.error(err);
throw err;
process.exit(1);
} else {
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
}
});
try {
var output = renderer.render(data);
} catch (e) {
if (e.stack) {
console.error(e.stack);
@ -93,6 +80,12 @@ function compileMML(err, data) {
}
process.exit(1);
}
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
};
function compileMSS(err, data) {
@ -100,26 +93,13 @@ function compileMSS(err, data) {
console.error(err);
process.exit(1);
}
try {
var renderer = new carto.Renderer({
filename: path.basename(input),
benchmark: options.benchmark,
ppi: options.ppi
});
renderer.renderMSS(data, function(err, output) {
if (err) {
console.error(err);
throw err;
process.exit(1);
} else {
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
}
});
try {
var output = renderer.renderMSS(data);
} catch (e) {
if (e.stack) {
console.error(e.stack);
@ -128,6 +108,12 @@ function compileMSS(err, data) {
}
process.exit(1);
}
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
};
try {

@ -223,7 +223,7 @@ carto.Parser = function Parser(env) {
// Start with the primary rule.
// The whole syntax tree is held under a Ruleset node,
// with the `root` property set to true, so no `{}` are
// output. The callback is called when the input is parsed.
// output.
root = new tree.Ruleset([], $(this.parsers.primary));
root.root = true;

@ -12,9 +12,8 @@ carto.Renderer = function Renderer(env, options) {
* XML Style fragment (mostly useful for debugging)
*
* @param {String} data the mss contents as a string.
* @param {Object} callback renderer environment options.
*/
carto.Renderer.prototype.renderMSS = function render(data, callback) {
carto.Renderer.prototype.renderMSS = function render(data) {
// effects is a container for side-effects, which currently
// are limited to FontSets.
var env = _(this.env).defaults({
@ -24,7 +23,7 @@ carto.Renderer.prototype.renderMSS = function render(data, callback) {
});
if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) {
return callback(new Error("Could not set mapnik version to " + this.options.mapnik_version), null);
throw new Error("Could not set mapnik version to " + this.options.mapnik_version);
}
var output = [];
@ -58,8 +57,8 @@ carto.Renderer.prototype.renderMSS = function render(data, callback) {
if (env.benchmark) console.timeEnd(bench_name);
}
if (env.benchmark) console.timeEnd('Total Style generation');
if (env.errors) return callback(env.errors);
return callback(null, output.join('\n'));
if (env.errors) throw env.errors;
return output.join('\n');
};
/**
@ -67,9 +66,8 @@ carto.Renderer.prototype.renderMSS = function render(data, callback) {
* fully-localized XML file ready for Mapnik2 consumption
*
* @param {String} m - the JSON file as a string.
* @param {Object} callback - renderer environment options.
*/
carto.Renderer.prototype.render = function render(m, callback) {
carto.Renderer.prototype.render = function render(m) {
// effects is a container for side-effects, which currently
// are limited to FontSets.
var env = _(this.env).defaults({
@ -80,7 +78,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
});
if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) {
return callback(new Error("Could not set mapnik version to " + this.options.mapnik_version), null);
throw new Error("Could not set mapnik version to " + this.options.mapnik_version);
}
var output = [];
@ -89,13 +87,12 @@ carto.Renderer.prototype.render = function render(m, callback) {
var definitions = _(m.Stylesheet).chain()
.map(function(s) {
if (typeof s == 'string') {
callback(new Error("Stylesheet object is expected not a string: '" + s + "'"));
throw new Error("Stylesheet object is expected not a string: '" + s + "'");
}
// Passing the environment from stylesheet to stylesheet,
// allows frames and effects to be maintained.
env = _(env).extend({filename:s.id});
// @TODO try/catch?
var time = +new Date(),
root = (carto.Parser(env)).parse(s.data);
if (env.benchmark)
@ -149,16 +146,10 @@ carto.Renderer.prototype.render = function render(m, callback) {
return e.toXML(env);
}).join('\n'));
var map_properties;
try {
map_properties = getMapProperties(m, definitions, env);
} catch (err) {
env.error(err);
return callback(err);
}
var map_properties = getMapProperties(m, definitions, env);
// Exit on errors.
if (env.errors) return callback(env.errors);
if (env.errors) throw env.errors;
// Pass TileJSON and other custom parameters through to Mapnik XML.
var parameters = _(m).reduce(function(memo, v, k) {
@ -222,7 +213,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
'<!DOCTYPE Map[]>\n' +
'<Map' + properties +'>\n');
output.push('</Map>');
return callback(null, output.join('\n'));
return output.join('\n');
};
/**

@ -11,34 +11,30 @@ var helper = require('./support/helper');
describe('Rendering mss', function() {
helper.files('rendering-mss', 'mss', function(file) {
it('should render mss ' + path.basename(file) + ' correctly', function(done) {
it('should render mss ' + path.basename(file) + ' correctly', function() {
var completed = false;
var renderResult;
var mss = helper.mss(file);
new carto.Renderer({
try {
var output = new carto.Renderer({
paths: [ path.dirname(file) ],
data_dir: path.join(__dirname, '../data'),
local_data_dir: path.join(__dirname, 'rendering'),
filename: file
}).renderMSS(mss, function (err, output) {
if (err) {
}).renderMSS(mss);
} catch(err) {
if (Array.isArray(err)){
err.forEach(carto.writeError);
done();
} else {
throw err;
done();
}
} else {
}
var expected = file.replace(path.extname(file),'')+'.xml';
if (!existsSync(expected)) {
fs.writeFileSync(expected,output);
}
var expected_data = fs.readFileSync(expected).toString();
assert.equal(output,expected_data);
done();
}
});
});
});
});

@ -12,19 +12,21 @@ helper.files('rendering', 'mml', function(file) {
var completed = false;
var renderResult;
var mml = helper.mml(file);
new carto.Renderer({
try {
var output = new carto.Renderer({
paths: [ path.dirname(file) ],
data_dir: path.join(__dirname, '../data'),
local_data_dir: path.join(__dirname, 'rendering'),
filename: file
}).render(mml, function (err, output) {
if (err) {
}).render(mml);
} catch(err) {
if (Array.isArray(err)){
err.forEach(carto.writeError);
return done();
} else {
throw err;
return done(err);
}
}
} else {
var result = helper.resultFile(file);
renderResult = output;
helper.compareToXMLFile(result, output, function(err,expected_json,actual_json) {
@ -56,8 +58,6 @@ helper.files('rendering', 'mml', function(file) {
helper.removeAbsoluteImages,
helper.removeAbsoluteDatasources
]);
}
});
// beforeExit(function() {
// if (!completed && renderResult) {

Loading…
Cancel
Save