js-yaml-front.js 945 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var jsYaml = require('js-yaml')
  2. , path = require('path')
  3. , fs = require('fs')
  4. jsYaml.parse = function (text, name) {
  5. name = name || '__content';
  6. var re = /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?([\w\W]*)*/
  7. , results = re.exec(text)
  8. , conf = {}
  9. , yamlOrJson;
  10. if((yamlOrJson = results[2])) {
  11. if(yamlOrJson.charAt(0) === '{') {
  12. conf = JSON.parse(yamlOrJson);
  13. } else {
  14. conf = jsYaml.load(yamlOrJson);
  15. }
  16. }
  17. conf[name] = results[3] ? results[3] : '';
  18. return conf;
  19. };
  20. jsYaml.loadFront = function (context, name) {
  21. var contents;
  22. if(fs.existsSync(context)) {
  23. contents = fs.readFileSync(context, 'utf8');
  24. if (contents instanceof Error) return contents;
  25. return jsYaml.parse(contents, name);
  26. } else if (Buffer.isBuffer(context)) {
  27. return jsYaml.parse(context.toString(), name);
  28. } else {
  29. return jsYaml.parse(context, name);
  30. }
  31. return false;
  32. };
  33. module.exports = jsYaml;