Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge 805fb2d into 3f545ab
Browse files Browse the repository at this point in the history
  • Loading branch information
theel0ja committed May 13, 2018
2 parents 3f545ab + 805fb2d commit 424257b
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions README.md
Expand Up @@ -61,42 +61,44 @@ Available parameters:
Alternatively, Carto can be used from JavaScript.
The `Renderer` interface is the main API for developers, and it takes an MML file as a string as input.

// defined variables:
// - input (the name or identifier of the file being parsed)
var carto = require('carto');

try {
var data = fs.readFileSync(input, 'utf-8');
var mml = new carto.MML({});
mml.load(path.dirname(input), data, function (err, data) {
var output = {};

if (!err) {
output = new carto.Renderer({
filename: input
}).render(data);
}

if (output.msg) {
output.msg.forEach(function (v) {
if (v.type === 'error') {
console.error(carto.Util.getMessageToPrint(v));
}
else if (v.type === 'warning') {
console.warn(carto.Util.getMessageToPrint(v));
}
});
}

// output content (if no errors)
if (output.data) {
console.log(output.data);
}
});
} catch (err) {
// program failures
...
}
```javascript
// defined variables:
// - input (the name or identifier of the file being parsed)
var carto = require('carto');

try {
var data = fs.readFileSync(input, 'utf-8');
var mml = new carto.MML({});
mml.load(path.dirname(input), data, function (err, data) {
var output = {};

if (!err) {
output = new carto.Renderer({
filename: input
}).render(data);
}

if (output.msg) {
output.msg.forEach(function (v) {
if (v.type === 'error') {
console.error(carto.Util.getMessageToPrint(v));
}
else if (v.type === 'warning') {
console.warn(carto.Util.getMessageToPrint(v));
}
});
}

// output content (if no errors)
if (output.data) {
console.log(output.data);
}
});
} catch (err) {
// program failures
...
}
```

If you want to use CartoCSS within the browser you should not use MML loading via `carto.MML.load`.
Instead you should supply the JSON of the MML including the Stylesheet strings directly to `carto.Renderer.render`.
Expand Down

0 comments on commit 424257b

Please sign in to comment.