This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor MML loading, support absolute paths in MML stylesheet file r…
…eferences
- Loading branch information
Showing
10 changed files
with
217 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
var path = require('path'), | ||
fs = require('fs'), | ||
_ = require('lodash'), | ||
yaml = require('js-yaml'); | ||
|
||
var carto = require('./index'); | ||
|
||
carto.MML = function MML(options) { | ||
this.options = options || {}; | ||
}; | ||
|
||
/** | ||
* Load a MML document. | ||
* | ||
* @param {String} basedir base directory of MML document. | ||
* @param {String} data the MML document. | ||
* @param {Callback} callback function to be called when finished loading. | ||
*/ | ||
carto.MML.prototype.load = function load(basedir, data, callback) { | ||
var mml = '', | ||
that = this; | ||
|
||
try { | ||
mml = yaml.safeLoad(data); | ||
} catch (err) { | ||
return callback("carto: " + err.message.replace(/^[A-Z]+, /, ''), null); | ||
} | ||
|
||
if (this.options.localize) { | ||
var millstone; | ||
try { | ||
require.resolve('millstone'); | ||
millstone = require('millstone'); | ||
} catch (err) { | ||
return callback('carto: Millstone not found, required if localizing stylesheet resources. ' + err.message.replace(/^[A-Z]+, /, ''), null); | ||
} | ||
millstone.resolve({ | ||
mml: mml, | ||
base: basedir, | ||
cache: path.join(basedir, 'cache'), | ||
nosymlink: this.options.nosymlink | ||
}, function (err, data) { | ||
// force drain the millstone download pool now | ||
// to ensure we can exit without waiting | ||
if (that.options.localize && millstone.drainPool) { | ||
millstone.drainPool(function() {}); | ||
} | ||
return callback(err, data); | ||
}); | ||
} else { | ||
if (_.has(mml, 'Stylesheet') && !_.isNil(mml.Stylesheet)) { | ||
mml.Stylesheet = _.castArray(mml.Stylesheet); | ||
for (var i = 0; i < mml.Stylesheet.length; i++) { | ||
var stylesheet = mml.Stylesheet[i]; | ||
if (typeof stylesheet !== 'string') { | ||
mml.Stylesheet[i] = stylesheet; | ||
continue; | ||
} | ||
var mss, | ||
file = path.resolve(basedir, stylesheet); | ||
try { | ||
mss = fs.readFileSync(file, 'utf-8'); | ||
} catch (err) { | ||
return callback('Failed to load file ' + file + ".\n", null); | ||
} | ||
mml.Stylesheet[i] = { id: stylesheet, data: mss }; | ||
} | ||
return callback(null, mml); | ||
} | ||
else { | ||
return callback("Expecting a Stylesheet property containing an (array of) stylesheet object(s) of the form { id: 'x', 'data': 'y' }.\n", null); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = carto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"Stylesheet": [ | ||
"/home/test/test.mss" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Failed to load file /home/test/test.mss. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Failed to load file C:\home\test\test.mss. |
Oops, something went wrong.