You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /*******************************
  2. Summarize Docs
  3. *******************************/
  4. var
  5. // node dependencies
  6. console = require('better-console'),
  7. fs = require('fs'),
  8. YAML = require('yamljs')
  9. ;
  10. var data = {};
  11. /**
  12. * Test for prefix in string.
  13. * @param {string} str
  14. * @param {string} prefix
  15. * @return {boolean}
  16. */
  17. function startsWith(str, prefix) {
  18. return str.indexOf(prefix) === 0;
  19. };
  20. function inArray(needle, haystack) {
  21. var length = haystack.length;
  22. for(var i = 0; i < length; i++) {
  23. if(haystack[i] == needle) return true;
  24. }
  25. return false;
  26. }
  27. /**
  28. * Parses a file for metadata and stores result in data object.
  29. * @param {File} file - object provided by map-stream.
  30. * @param {function(?,File)} - callback provided by map-stream to
  31. * reply when done.
  32. */
  33. function parser(file, callback) {
  34. // file exit conditions
  35. if(file.isNull()) {
  36. return callback(null, file); // pass along
  37. }
  38. if(file.isStream()) {
  39. return callback(new Error('Streaming not supported'));
  40. }
  41. try {
  42. var
  43. /** @type {string} */
  44. text = String(file.contents.toString('utf8')),
  45. lines = text.split('\n'),
  46. filename = file.path.substring(0, file.path.length - 4),
  47. key = 'server/documents',
  48. position = filename.indexOf(key)
  49. ;
  50. // exit conditions
  51. if(!lines) {
  52. return;
  53. }
  54. if(position < 0) {
  55. return callback(null, file);
  56. }
  57. filename = filename.substring(position + key.length + 1, filename.length);
  58. var
  59. lineCount = lines.length,
  60. active = false,
  61. yaml = [],
  62. categories = [
  63. 'UI Element',
  64. 'UI Global',
  65. 'UI Collection',
  66. 'UI View',
  67. 'UI Module',
  68. 'UI Behavior'
  69. ],
  70. index,
  71. meta,
  72. line
  73. ;
  74. for(index = 0; index < lineCount; index++) {
  75. line = lines[index];
  76. // Wait for metadata block to begin
  77. if(!active) {
  78. if(startsWith(line, '---')) {
  79. active = true;
  80. }
  81. continue;
  82. }
  83. // End of metadata block, stop parsing.
  84. if(startsWith(line, '---')) {
  85. break;
  86. }
  87. yaml.push(line);
  88. }
  89. // Parse yaml.
  90. meta = YAML.parse(yaml.join('\n'));
  91. if(meta && meta.type && meta.title && inArray(meta.type, categories) ) {
  92. meta.category = meta.type;
  93. meta.filename = filename;
  94. meta.url = '/' + filename;
  95. meta.title = meta.title;
  96. // Primary key will by filepath
  97. data[meta.element] = meta;
  98. }
  99. else {
  100. // skip
  101. // console.log(meta);
  102. }
  103. }
  104. catch(error) {
  105. console.log(error, filename);
  106. }
  107. callback(null, file);
  108. }
  109. /**
  110. * Export function expected by map-stream.
  111. */
  112. module.exports = {
  113. result : data,
  114. parser : parser
  115. };