function matter(str, options) {
if (typeof str !== 'string') {
throw new Error('gray-matter expects a string');
}
// default results to build up
var res = {orig: str, data: {}, content: str};
if (str === '') {
return res;
}
// delimiters
var delims = arrayify((options && options.delims) || '---');
var a = delims[0];
// strip byte order marks
str = stripBom(str);
// if the first delim isn't the first thing, return
if (!isFirst(str, a)) {
return res;
}
var b = '\n' + (delims[1] || delims[0]);
var alen = a.length;
// if the next character after the first delim
// is a character in the first delim, then just
// return the default object. it's either a bad
// delim or not a delimiter at all.
if (a.indexOf(str.charAt(alen + 1)) !== -1) {
return res;
}
var len = str.length;
// find the index of the next delimiter before
// going any further. If not found, return.
var end = str.indexOf(b, alen + 1);
if (end === -1) {
end = len;
}
// detect a language, if defined
var lang = str.slice(alen, str.indexOf('\n'));
// measure the lang before trimming whitespace
var start = alen + lang.length;
var opts = options || {};
opts.lang = opts.lang || 'yaml';
lang = (lang && lang.trim()) || opts.lang;
// get the front matter (data) string
var data = str.slice(start, end).trim();
if (data) {
// if data exists, see if we have a matching parser
var fn = opts.parser || parsers[lang];
if (typeof fn === 'function') {
// run the parser on the data string
res.data = fn(data, opts);
} else {
throw new Error('gray-matter cannot find a parser for: ' + str);
}
}
// grab the content from the string, stripping
// an optional new line after the second delim
var con = str.substr(end + b.length);
if (con.charAt(0) === '\n') {
con = con.substr(1);
} else if (con.charAt(0) === '\r' && con.charAt(1) === '\n') {
con = con.substr(2);
}
res.content = con;
return res;
}n/a
read = function (fp, options) {
var str = fs.readFileSync(fp, 'utf8');
var obj = matter(str, options);
return extend(obj, {
path: fp
});
}n/a
stringify = function (str, data, options) {
var delims = arrayify(options && options.delims || '---');
var res = '';
res += delims[0] + '\n';
res += YAML.safeDump(data, options);
res += (delims[1] || delims[0]) + '\n';
res += str + '\n';
return res;
}n/a
test = function (str, options) {
var delims = arrayify(options && options.delims || '---');
return isFirst(str, delims[0]);
}n/a
coffee = function (str, options) {
var opts = extend({eval: false, strict: false}, options);
if (opts.eval) {
try {
var coffee = parser.requires.coffee || (parser.requires.coffee = require('coffee-script'));
return coffee['eval'](str, options);
} catch (err) {
throw new SyntaxError(msg('coffee-script', err));
}
} else {
// if `eval` isn't set
if (opts.strict) {
throw new Error(evalError('coffee'));
} else {
console.error(evalError('coffee', true));
}
}
}n/a
cson = function (str, options) {
var opts = extend({eval: false, strict: false}, options);
if (opts.eval) {
try {
var coffee = parser.requires.coffee || (parser.requires.coffee = require('coffee-script'));
return coffee['eval'](str, options);
} catch (err) {
throw new SyntaxError(msg('coffee-script', err));
}
} else {
// if `eval` isn't set
if (opts.strict) {
throw new Error(evalError('coffee'));
} else {
console.error(evalError('coffee', true));
}
}
}n/a
javascript = function (str, options) {
var opts = extend({wrapped: true, eval: false, strict: false}, options);
if (opts.eval) {
if (opts.wrapped) {
str = 'function data() {return {' + str + '}; } data();';
}
try {
return eval(str);
} catch (err) {
throw new SyntaxError(msg('javascript', err));
}
return {};
} else {
// if `eval` isn't set
if (opts.strict) {
throw new Error(evalError('javascript'));
} else {
console.error(evalError('javascript', true));
}
}
}n/a
js = function (str, options) {
var opts = extend({wrapped: true, eval: false, strict: false}, options);
if (opts.eval) {
if (opts.wrapped) {
str = 'function data() {return {' + str + '}; } data();';
}
try {
return eval(str);
} catch (err) {
throw new SyntaxError(msg('javascript', err));
}
return {};
} else {
// if `eval` isn't set
if (opts.strict) {
throw new Error(evalError('javascript'));
} else {
console.error(evalError('javascript', true));
}
}
}n/a
json = function (str, options) {
var opts = extend({strict: false}, options);
try {
return JSON.parse(str);
} catch (err) {
if (opts.strict) {
throw new SyntaxError(msg('JSON', err));
} else {
return {};
}
}
}n/a
toml = function (str, opts) {
try {
var toml = parser.requires.toml || (parser.requires.toml = require('toml'));
return toml.parse(str);
} catch (err) {
if (opts.strict) {
throw new SyntaxError(msg('TOML', err));
} else {
return {};
}
}
}n/a
yaml = function (str, options) {
var opts = extend({strict: false, safeLoad: false}, options);
try {
var YAML = parser.requires.yaml || (parser.requires.yaml = require('js-yaml'));
return opts.safeLoad ? YAML.safeLoad(str, options) : YAML.load(str, options);
} catch (err) {
if (opts.strict) {
throw new SyntaxError(msg('js-yaml', err));
} else {
return {};
}
}
}n/a