var get = function(data) { var lines = data.split('\n'); var separatorCount = 0; var yamlData = ""; var i, line; for (i = 0; i < lines.length; i += 1) { line = lines[i]; if (/^---/.test(line)) { separatorCount += 1; continue; } if (separatorCount === 1) { yamlData += line + '\n'; } else if (separatorCount === 2) { break; } } return yaml.load(yamlData); };
function parser(str) { var yaml; try { yaml = YAML.load(str); } catch(e) { throw new Error('Invalid YAML'); } var deps = []; var runtimeDeps = yaml.packages || []; Object.keys(runtimeDeps).forEach( (dep) => { deps.push({ name: dep, version: runtimeDeps[dep].version, type: 'runtime' }); }); return deps; }
frontman.parse = function(src, type) { type = type || 'yfm'; if (type == "cson" ) // Todo: coffee? return coffee.eval(src) || {}; if (type == "json") return JSON.parse(src) || {}; if (type == "yml") // Todo: yaml return yaml.load(src) || {}; if (type == "env") return envfile.parseSync(src) || {}; return yfm.parse(src) || { __content: src, __type: type }; }
global.__defineGetter__( 'CONF', function (){ var fs = require( 'fs' ); var utils = require( './utils' ); var path = CONF_DIR + NODE_ENV + '/config.yml'; if( !fs.existsSync( path )){ console.log( utils.$alert( 'error' ) + ' config path does not exist', path ); return process.exit( 1 ); } var source = fs.readFileSync( path, 'utf8' ); var yaml = require( 'js-yaml' ); return yaml.load( source ); });
Plankton.prototype.parsePost = function(text) { var containsFrontMatter = (text.substring(0, 3) === defaults.FRONT_MATTER_DELIMETER); if(containsFrontMatter) { var parts = text.split(defaults.FRONT_MATTER_DELIMETER); var post = yaml.load(parts[1]); post[constants.BODY_PROPERTY] = parts[2]; } else { var post = {}; post[constants.BODY_PROPERTY] = text; } if(post.hasOwnProperty(constants.DATE_PROPERTY)) { post[constants.TIMESTAMP_GENERATED_PROPERTY] = moment(post[constants.DATE_PROPERTY]).valueOf(); } else { post[constants.TIMESTAMP_GENERATED_PROPERTY] = constants.DEFAULT_TIMESTAMP; } return post; }
function (t) { const TEST_PATH = path.resolve(__dirname, './fixture/chapters/exclude-complex-empty-directory'); process.chdir(TEST_PATH); const result = chapters({ dir: 'docs' }); t.equal(result.chapters[0]['a.md'], 'A'); t.equal(result.chapters[1]['images/'], 'images'); t.equal(result.chapters[2]['images/dir3/'], 'dir3'); t.equal(result.chapters[3]['images/dir3/b.md'], 'B'); t.equal(result.chapters.length, 4); t.ok(fs.existsSync(path.join(TEST_PATH, CHAPTERS_FILE))); const content = yaml.load(fs.readFileSync(path.join(TEST_PATH, CHAPTERS_FILE), 'utf8')); t.equal(content[0]['a.md'], 'A'); t.equal(content[1]['images/'], 'images'); t.equal(content[2]['images/dir3/'], 'dir3'); t.equal(content[3]['images/dir3/b.md'], 'B'); t.equal(content.length, 4); t.end(); fs.unlinkSync(path.join(TEST_PATH, CHAPTERS_FILE)); }
readFrontMatter(file) { var end, ret; if (/^---\n/.test(file)) { end = file.search(/\n---\n/); if (end !== -1) { ret = { front: yaml.load(file.slice(4, end + 1)) || {}, main: file.slice(end + 5) }; return ret; } } ret = { front:{}, main:file }; return ret; }
module.exports = function (fromFile, toFile, prettifyJson) { var yaml = require('js-yaml'); var fs = require('fs'); var md5 = require('MD5'); var path = require("path"); var i, j, rdlistCount, recordProcessorsCount; fromFile = path.resolve(fromFile); toFile = path.resolve(toFile); var yamlString = fs.readFileSync(fromFile, "utf8"); var rdlist = yaml.load(yamlString, { filename: fromFile }); var applyGravatarHash = function(item) { // Hash email with md5 // http://en.gravatar.com/site/implement/hash/ // http://en.gravatar.com/site/implement/images/ var email = item.Email; if(email) { item.GravatarHash = md5(email.trim().toLowerCase()); } return item; } var recordProcessors = [ applyGravatarHash ]; rdlist.forEach(function (item) { recordProcessors.forEach(function (processor) { processor(item); }); }); fs.writeFileSync(toFile, JSON.stringify(rdlist, null, prettifyJson ? "\t" : "")); }
entries.forEach(function(value, index) { try { var contents = fs.readFileSync('blog/' + value, 'utf8'); // Extract the date var arr = value.match(/(\d{4}-\d{2}-\d{2})/) || []; if (arr.length == 2) { var date = arr[1]; // Parse the YAML header // Generate frontMatter and actual markdown content var lines = contents.split('\n'); var frontMatter = GetFrontMatter(lines); var newContent = lines.join('\n'); var html = marked(newContent, MarkeOptions); var matter = frontMatter ? yaml.load(frontMatter) : {}; // Add entry, key is the date BlogEntries[date] = { // yaml: frontMatter.trim(), // markdown: newContent.trim(), content: html.trim(), meta: matter }; } else { logger.error('Bad array size: ', arr.length, value); } } catch(e) { logger.error('Error while parsing markdown blog entries'); } });
module.exports = function(str, div) { div = div || /\n\n\n|\r\n\r\n\r\n/; // `str` must be a string if (typeof str != 'string') str = str.toString(); // trim string str = str.trim(); var split; var result = {}; var content; // If a match was found if ((split = str.split(div)).length > 0) try { // JSON if (split[0].charAt(0) == '{') result = JSON.parse(split[0]); // JSML else if (split[0].charAt(0) == '"') result = JSML.parse(split[0]); // YAML else result = YAML.load(split[0]); } catch (e) { return { __content: str }; } else return { __content: str }; delete split[0]; // Join remaining str = split.join('\n\n\n'); str += '\n'; // append a line seperator after content, since some tools (like // pandoc) need it result.__content = str; return result; };
function loadConfig(configPath, overrideOptions) { const _defsPath = path.resolve(path.join(__dirname, 'configuration.default.yml')) const defs = yaml.load(fs.readFileSync(_defsPath, 'utf8')) nconf.use('overrides', { type: 'literal', store: overrideOptions, // logicalSeparator: '.', }) nconf.use('argv') nconf.use('env', { type: 'env', separator: '_', match: /^volebo/i, whitelist: ['NODE_ENV'], // logicalSeparator: '.', }) if (configPath) { nconf.use('yaml-config-file', { type: 'file', file: configPath, format: yamlFormat, // logicalSeparator: '.', }) debug('append config from file', configPath) } nconf.use('defaults', { type: 'literal', store: defs, // logicalSeparator: '.', }) // const cfg = nconf.get() const cfg = new Config(nconf) return cfg }
function (t) { const TEST_PATH = path.resolve( __dirname, './fixture/chapters/complex-docs-directory-structure' ); process.chdir(TEST_PATH); const result = chapters({ dir: 'docs' }); t.equal(result.chapters[0]['a.md'], 'Title A'); t.equal(result.chapters[1]['b/'], 'b'); t.equal(result.chapters[2]['b/c/'], 'c'); t.equal(result.chapters[3]['b/c/d.md'], 'Title D'); t.ok(fs.existsSync(path.join(TEST_PATH, CHAPTERS_FILE))); const content = yaml.load(fs.readFileSync(path.join(TEST_PATH, CHAPTERS_FILE), 'utf8')); t.equal(content[0]['a.md'], 'Title A'); t.equal(content[1]['b/'], 'b'); t.equal(content[2]['b/c/'], 'c'); t.equal(content[3]['b/c/d.md'], 'Title D'); fs.unlinkSync(path.join(TEST_PATH, CHAPTERS_FILE)); t.end(); }
var configs = files.map(function (file) { logger.debug('merge pattern file ' + file) var cfg = {} try { cfg = yaml.load(fs.readFileSync(file, 'utf8')) cfg._fileName = file } catch (e) { cfg.patterns = [] logger.error('ignoring pattern file ' + file + ' ' + e) if (e.reason && e.mark) { logger.error('Error parsing file: ' + file + ' ' + e.reason + ' line:' + e.mark.line + ' column:' + e.mark.columns) } else { // console.log(error.stack) logger.debug('Error parsing file: ' + file + ' ' + e + ' ' + e.stack) } } finally { // console.log(config) return cfg } })
async.forEach(pages, function(file, next) { grunt.log.write('Converting to JSON '+ file.src.cyan); try { file.page = YAML.load(file.page); file.page = JSON.stringify(file.page); grunt.log.success(' OK'); converted += 1; next(); } catch (e) { grunt.log.error(' FAILED'); next(); } });
const readPost = filename => { const buffer = fs.readFileSync(filename), file = buffer.toString('utf8'); try { const parts = file.split(/---\s*[\n^]/), frontmatter = parts[1]; return { name: filename, file: file, metadata: jsyaml.load(frontmatter), content: parts[2] }; } catch (err) { console.log( `\nCould not read metadata, check the syntax of the metadata and front matter in ${filename}` ); } };
function data(filename) { var filedata; if (filename === null) { return []; } try { filedata = (fs.readFileSync(filename, 'utf8')).trim(); } catch (e) { out.warn('File "' + filename + '" could not be found. Ignoring...'); return []; } try { return yaml.load(filedata); } catch (e) { out.warn('Couldn\'t parse "' + filename + '" due to syntax errors:'); out.log(e.message); process.exit(0); } }
/** * Decide whether to skip the test based on flags in demo.details. * @param {String} path The sample path * @return {Boolean} False if we should skip the test */ function handleDetails(path) { // Skip it? if (fs.existsSync(`samples/${path}/demo.details`)) { let details = fs.readFileSync( `samples/${path}/demo.details`, 'utf8' ); details = details && yaml.load(details); if (details && details.skipTest) { // console.log(`- skipTest: ${path}`.gray); return false; } if (details && details.requiresManualTesting) { // console.log(`- requiresManualTesting: ${path}`.gray); return false; } return true; } return true; }
function extractYfm(src) { var re = /^-{3}([\w\W]+?)(-{3})([\w\W]*)*/; var text = grunt.file.read(src); var results = re.exec(text), conf = {}; if(results) { conf = jsYaml.load(results[1]); //Add content if set // if(options.includeContent) // { // conf[options.contentName] = results[3] || ''; // } } return conf; }
function processSample(data) { var yaml = Yaml.load(data); var sample_id = Crypto.createHash('md5').update(yaml.template).digest('hex').substr(0,8); var rootmap = "/code/github.com/" + user + "/" + repo + "/tree/" + branch + "/"; var sample_folder = "/" + folder; if (query.skip) { var parts = sample_folder.substr(1).split("/"); var prefix = parts.splice(0, query.skip); rootmap += prefix.join("/") + "/"; sample_folder = "/" + parts.join("/"); } return { 'sample_id': "sample-"+sample_id, 'title': yaml.title, 'yaml': JSON.stringify(yaml), 'rootmap': rootmap, 'sample_folder': sample_folder }; }
Page.prototype.parseFrontMatter = function() { var frontMatter; var split = this.data.trim().match(/^\-{3}([\w\W]+)\-{3}([\w\W]+)$/); if (!split || split.length !== 3) { return new Error('Could not find front matter block'); } try { frontMatter = yaml.load(split[1]); } catch(err) { return new Error('Could not parse front matter data'); } return { data: frontMatter, template: split[2] }; };
exports.getResource = function(resourcePath, next) { var resx; var jsonString; var error = null; try { // the synchronous code that we want to catch thrown errors on resx = yaml.load(fs.readFileSync(resourcePath)); jsonString = JSON.stringify(resx); } catch (err) { // handle the error safely console.log(err); error = err; } return next(error, jsonString); };
}).then(function(frontMatter) { var separator = yfmSplit.separator; var jsonMode = separator[0] === ';'; var content = ''; var obj; // Parse front-matter if (jsonMode) { obj = JSON.parse('{' + frontMatter + '}'); } else { obj = yaml.load(frontMatter); } // Add data which are not in the front-matter var keys = Object.keys(data); var key = ''; for (var i = 0, len = keys.length; i < len; i++) { key = keys[i]; if (!preservedKeys[key] && obj[key] == null) { obj[key] = data[key]; } } // Prepend the separator if (yfmSplit.prefixSeparator) content += separator + '\n'; content += yfm.stringify(obj, { mode: jsonMode ? 'json' : '' }); // Concat content content += yfmSplit.content; if (data.content) { content += '\n' + data.content; } return content; });
}).then(frontMatter => { const separator = yfmSplit.separator; const jsonMode = separator[0] === ';'; let content = ''; let obj; // Parse front-matter if (jsonMode) { obj = JSON.parse(`{${frontMatter}}`); } else { obj = yaml.load(frontMatter); } // Add data which are not in the front-matter const keys = Object.keys(data); let key = ''; for (let i = 0, len = keys.length; i < len; i++) { key = keys[i]; if (!preservedKeys[key] && obj[key] == null) { obj[key] = data[key]; } } // Prepend the separator if (yfmSplit.prefixSeparator) content += `${separator}\n`; content += yfm.stringify(obj, { mode: jsonMode ? 'json' : '' }); // Concat content content += yfmSplit.content; if (data.content) { content += `\n${data.content}`; } return content; });
exports.parse = function(md, filename, options) { if (typeof filename === 'object') { options = filename; filename = null; } options = options || {}; // Generate frontMatter and actual markdown content var lines = md.split('\n'); var frontMatter = content.getFrontMatter(lines); var newContent = lines.join('\n'); if (options.preCompile) { newContent = options.preCompile(newContent) || newContent; } var html = marked(newContent, options.marked || {}); if (options.postCompile) { html = options.postCompile(html) || html; } var matter = frontMatter ? yaml.load(frontMatter) : {}; // Construct and return a context object var context = { filenameExtension: '.md', yaml: frontMatter.trim(), markdown: newContent.trim(), content: html.trim(), meta: matter }; if (filename) { context.filename = filename; } extend(context.meta, options.context || {}); return context; };
/** * @param {string} content * @param {Object} options * @this {DocsPluginExtractorContext} */ function extractor(content, options) { var isEmpty = content.trim() == ''; if (isEmpty) return Promise.resolve(null); var extractor = this; var source = this.source; var page = new DocPage({source: source}); var data = parseYAMLFrontMatter(content); if (data.meta) { var meta = yaml.load(data.meta); page.attrs = meta; } var md = new Remarkable(); var rendered = md.render(data.body); page.content = rendered; return Promise.resolve(page); }
Server.prototype.loadRoutes = function() { var _this = this; log.info("Loading routes from file %s", conf.routefile); this.routes = Yaml.load(fs.readFileSync(conf.routefile)+ ''); log.info("Registering %d route(s)", this.routes.length); // Create all secure contexts for each route _.each(this.routes, function(route) { var url = URL.parse(route.target); if(route.key) { _this.secure_certs[route.hostname] = crypto.createCredentials({ key: fs.readFileSync(route.key), cert: fs.readFileSync(route.cert) }).context; } else _this.secure_certs[route.hostname || url.host] = _this.secure_certs.default; }); };
fs.readFile(location, 'utf8', function(error, body) { if (!error) { var ext = path.extname(location); if (ext) { ext = ext.toLowerCase(); } try { var data = _.includes(yamlExtensions, ext) ? jsyaml.load(body) : JSON.parse(body); deferred.resolve(data); } catch (err) { deferred.reject(err); } } else { deferred.reject(error); } });
function parseContent(src) { src = src.toString(); var yfm = ""; var yfmClosed = false; var yfmStarted = false; var content = ""; var lines = src.replace(/\r/g, '').trim().split("\n"); while (lines.length) { var line = lines.shift(); if (!yfmStarted && !yfmClosed) { yfmStarted = (line.trim() == "---"); } else if (yfmStarted && !yfmClosed) { if (line.trim() == "---") { yfmClosed = true; } else { yfm += line + "\n"; } } else { content += line + "\n"; } } try { yfm = yaml.load(yfm); } catch (e) { cb(e); return; } cb(null, { from: file.from, yfm: yfm, post: content }); }
fs.readFile(templateFile, 'utf8', function(err, content) { if (err) throw err; const templateYml = yaml.load(content); for (let credentialsKey in credentials) if (credentials.hasOwnProperty(credentialsKey)) replaceValues(templateYml.applications, credentials, credentialsKey); const templatePath = path.dirname(templateFile); const templateBaseName = path.basename(templateFile); const manifestBaseName = templateBaseName.replace(/\.template/g, ''); const manifestFile = path.join(templatePath, manifestBaseName); const manifestContent = yaml.dump(templateYml); fs.writeFile(manifestFile, manifestContent, 'utf8', (err) => { if (err) throw err; }); console.log(' %s', manifestFile); });
TMSource.prototype.init = function(uri, yamlData, callback) { var self = this; try { self.info = yaml.load(yamlData); self.info.id = url.format(uri); self.info = normalize(self.info); } catch (err) { return callback(err); } return toXML(self.info, function(err, xml) { if (err) { return callback(err); } uri.xml = xml; uri.base = uri.pathname; return Bridge.call(self, uri, callback); }); };