index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var walk = require('pug-walk');
  5. var assign = require('object-assign');
  6. module.exports = load;
  7. function load(ast, options) {
  8. options = getOptions(options);
  9. // clone the ast
  10. ast = JSON.parse(JSON.stringify(ast));
  11. return walk(ast, function(node) {
  12. if (node.str === undefined) {
  13. if (
  14. node.type === 'Include' ||
  15. node.type === 'RawInclude' ||
  16. node.type === 'Extends'
  17. ) {
  18. var file = node.file;
  19. if (file.type !== 'FileReference') {
  20. throw new Error('Expected file.type to be "FileReference"');
  21. }
  22. var path, str, raw;
  23. try {
  24. path = options.resolve(file.path, file.filename, options);
  25. file.fullPath = path;
  26. raw = options.read(path, options);
  27. str = raw.toString('utf8');
  28. } catch (ex) {
  29. ex.message += '\n at ' + node.filename + ' line ' + node.line;
  30. throw ex;
  31. }
  32. file.str = str;
  33. file.raw = raw;
  34. if (node.type === 'Extends' || node.type === 'Include') {
  35. file.ast = load.string(
  36. str,
  37. assign({}, options, {
  38. filename: path,
  39. })
  40. );
  41. }
  42. }
  43. }
  44. });
  45. }
  46. load.string = function loadString(src, options) {
  47. options = assign(getOptions(options), {
  48. src: src,
  49. });
  50. var tokens = options.lex(src, options);
  51. var ast = options.parse(tokens, options);
  52. return load(ast, options);
  53. };
  54. load.file = function loadFile(filename, options) {
  55. options = assign(getOptions(options), {
  56. filename: filename,
  57. });
  58. var str = options.read(filename).toString('utf8');
  59. return load.string(str, options);
  60. };
  61. load.resolve = function resolve(filename, source, options) {
  62. filename = filename.trim();
  63. if (filename[0] !== '/' && !source)
  64. throw new Error(
  65. 'the "filename" option is required to use includes and extends with "relative" paths'
  66. );
  67. if (filename[0] === '/' && !options.basedir)
  68. throw new Error(
  69. 'the "basedir" option is required to use includes and extends with "absolute" paths'
  70. );
  71. filename = path.join(
  72. filename[0] === '/' ? options.basedir : path.dirname(source.trim()),
  73. filename
  74. );
  75. return filename;
  76. };
  77. load.read = function read(filename, options) {
  78. return fs.readFileSync(filename);
  79. };
  80. load.validateOptions = function validateOptions(options) {
  81. /* istanbul ignore if */
  82. if (typeof options !== 'object') {
  83. throw new TypeError('options must be an object');
  84. }
  85. /* istanbul ignore if */
  86. if (typeof options.lex !== 'function') {
  87. throw new TypeError('options.lex must be a function');
  88. }
  89. /* istanbul ignore if */
  90. if (typeof options.parse !== 'function') {
  91. throw new TypeError('options.parse must be a function');
  92. }
  93. /* istanbul ignore if */
  94. if (options.resolve && typeof options.resolve !== 'function') {
  95. throw new TypeError('options.resolve must be a function');
  96. }
  97. /* istanbul ignore if */
  98. if (options.read && typeof options.read !== 'function') {
  99. throw new TypeError('options.read must be a function');
  100. }
  101. };
  102. function getOptions(options) {
  103. load.validateOptions(options);
  104. return assign(
  105. {
  106. resolve: load.resolve,
  107. read: load.read,
  108. },
  109. options
  110. );
  111. }