2013-09-19 19:48:21 +00:00
|
|
|
define([
|
2014-03-14 14:57:22 +00:00
|
|
|
"underscore",
|
2013-09-19 19:48:21 +00:00
|
|
|
"classes/Extension",
|
2013-10-07 00:17:21 +00:00
|
|
|
"yaml-js",
|
2014-03-14 14:57:22 +00:00
|
|
|
], function(_, Extension, YAML) {
|
2013-09-19 19:48:21 +00:00
|
|
|
|
2013-10-06 14:34:01 +00:00
|
|
|
var yamlFrontMatterParser = new Extension("yamlFrontMatterParser", "YAML front matter");
|
2013-09-19 19:48:21 +00:00
|
|
|
|
2013-11-07 23:10:38 +00:00
|
|
|
var eventMgr;
|
2013-09-19 19:48:21 +00:00
|
|
|
yamlFrontMatterParser.onEventMgrCreated = function(eventMgrParameter) {
|
|
|
|
eventMgr = eventMgrParameter;
|
|
|
|
};
|
|
|
|
|
2013-11-07 23:10:38 +00:00
|
|
|
var fileDesc;
|
2013-10-06 14:34:01 +00:00
|
|
|
yamlFrontMatterParser.onFileSelected = function(fileDescParam) {
|
|
|
|
fileDesc = fileDescParam;
|
|
|
|
};
|
|
|
|
|
2014-03-21 00:36:28 +00:00
|
|
|
var regex = /^(\s*-{3}\s*\n([\w\W]+?)\n\s*-{3}\s*?\n)?([\w\W]*)$/;
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-29 01:22:24 +00:00
|
|
|
function parseFrontMatter(fileDescParam, content) {
|
|
|
|
if(fileDescParam !== fileDesc) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-23 02:33:41 +00:00
|
|
|
var results = regex.exec(content);
|
2014-04-21 19:13:39 +00:00
|
|
|
var frontMatter = results[1];
|
2014-03-17 02:01:46 +00:00
|
|
|
var yaml = results[2];
|
2013-09-19 19:48:21 +00:00
|
|
|
|
2014-03-21 00:36:28 +00:00
|
|
|
if(!yaml) {
|
|
|
|
fileDesc.frontMatter = undefined;
|
|
|
|
}
|
2014-04-21 19:13:39 +00:00
|
|
|
else if(!fileDesc.frontMatter || fileDesc.frontMatter._frontMatter != frontMatter) {
|
2014-03-17 02:01:46 +00:00
|
|
|
fileDesc.frontMatter = undefined;
|
|
|
|
try {
|
|
|
|
fileDesc.frontMatter = YAML.parse(yaml);
|
|
|
|
if(!_.isObject(fileDesc.frontMatter)) {
|
|
|
|
fileDesc.frontMatter = undefined;
|
2014-03-14 14:57:22 +00:00
|
|
|
}
|
2014-03-17 02:01:46 +00:00
|
|
|
fileDesc.frontMatter._yaml = yaml;
|
2014-04-21 19:13:39 +00:00
|
|
|
fileDesc.frontMatter._frontMatter = frontMatter;
|
2014-03-17 02:01:46 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2013-09-19 19:48:21 +00:00
|
|
|
}
|
2014-03-17 02:01:46 +00:00
|
|
|
}
|
2014-03-19 00:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
yamlFrontMatterParser.onFileOpen = parseFrontMatter;
|
|
|
|
yamlFrontMatterParser.onContentChanged = parseFrontMatter;
|
2013-09-19 19:48:21 +00:00
|
|
|
|
|
|
|
return yamlFrontMatterParser;
|
2014-03-19 00:33:57 +00:00
|
|
|
});
|