diff options
-rw-r--r-- | lib/templating/__tests__/conrefsLoader.js | 6 | ||||
-rw-r--r-- | lib/templating/conrefsLoader.js | 23 | ||||
-rw-r--r-- | lib/templating/listShortcuts.js | 14 | ||||
-rw-r--r-- | lib/templating/renderFile.js | 14 | ||||
-rw-r--r-- | lib/templating/replaceShortcuts.js | 26 | ||||
-rw-r--r-- | lib/templating/themesLoader.js | 50 |
6 files changed, 70 insertions, 63 deletions
diff --git a/lib/templating/__tests__/conrefsLoader.js b/lib/templating/__tests__/conrefsLoader.js index bcbee05..196b513 100644 --- a/lib/templating/__tests__/conrefsLoader.js +++ b/lib/templating/__tests__/conrefsLoader.js @@ -75,6 +75,12 @@ describe('ConrefsLoader', function() { describe('transform', function() { function transform(filePath, source) { + expect(filePath).toBeA('string'); + expect(source).toBeA('string'); + + expect(filePath).toBe(path.resolve(__dirname, 'include.md')); + expect(source).toBe('Hello World'); + return 'test-' + source + '-endtest'; } var engine = TemplateEngine({ diff --git a/lib/templating/conrefsLoader.js b/lib/templating/conrefsLoader.js index 475aa92..b3cdb3f 100644 --- a/lib/templating/conrefsLoader.js +++ b/lib/templating/conrefsLoader.js @@ -8,16 +8,15 @@ var PathUtils = require('../utils/path'); /** - Template loader resolving both: - - relative url ("./test.md") - - absolute url ("/test.md") - - git url ("") - - - @param {String} rootFolder - @param {Function(filePath, source)} transformFn (optional) - @param {Logger} logger (optional) -*/ + * Template loader resolving both: + * - relative url ("./test.md") + * - absolute url ("/test.md") + * - git url ("") + * + * @param {String} rootFolder + * @param {Function(filePath, source)} transformFn (optional) + * @param {Logger} logger (optional) + */ var ConrefsLoader = nunjucks.Loader.extend({ async: true, @@ -43,6 +42,8 @@ var ConrefsLoader = nunjucks.Loader.extend({ // Read file from absolute path return fs.readFile(filepath) .then(function(source) { + source = source.toString('utf8'); + if (that.transformFn) { return that.transformFn(filepath, source); } @@ -51,7 +52,7 @@ var ConrefsLoader = nunjucks.Loader.extend({ }) .then(function(source) { return { - src: source.toString('utf8'), + src: source, path: filepath }; }); diff --git a/lib/templating/listShortcuts.js b/lib/templating/listShortcuts.js index b5fe793..8d0a64a 100644 --- a/lib/templating/listShortcuts.js +++ b/lib/templating/listShortcuts.js @@ -2,13 +2,13 @@ var Immutable = require('immutable'); var parsers = require('../parsers'); /** - Return a list of all shortcuts that can apply - to a file for a TemplatEngine - - @param {List<TemplateBlock>} engine - @param {String} filePath - @return {List<TemplateShortcut>} -*/ + * Return a list of all shortcuts that can apply + * to a file for a TemplatEngine + * + * @param {List<TemplateBlock>} engine + * @param {String} filePath + * @return {List<TemplateShortcut>} + */ function listShortcuts(blocks, filePath) { var parser = parsers.getForFile(filePath); diff --git a/lib/templating/renderFile.js b/lib/templating/renderFile.js index 9204090..8672e8b 100644 --- a/lib/templating/renderFile.js +++ b/lib/templating/renderFile.js @@ -3,13 +3,13 @@ var error = require('../utils/error'); var render = require('./render'); /** - Render a template - - @param {TemplateEngine} engine - @param {String} filePath - @param {Object} context - @return {Promise<TemplateOutput>} -*/ + * Render a template + * + * @param {TemplateEngine} engine + * @param {String} filePath + * @param {Object} context + * @return {Promise<TemplateOutput>} + */ function renderTemplateFile(engine, filePath, context) { var loader = engine.getLoader(); diff --git a/lib/templating/replaceShortcuts.js b/lib/templating/replaceShortcuts.js index acdd1b9..1cfdbf0 100644 --- a/lib/templating/replaceShortcuts.js +++ b/lib/templating/replaceShortcuts.js @@ -1,12 +1,12 @@ var escapeStringRegexp = require('escape-string-regexp'); var listShortcuts = require('./listShortcuts'); -/* - Apply a shortcut of block to a template - @param {String} content - @param {Shortcut} shortcut - @return {String} -*/ +/** + * Apply a shortcut of block to a template + * @param {String} content + * @param {Shortcut} shortcut + * @return {String} + */ function applyShortcut(content, shortcut) { var start = shortcut.getStart(); var end = shortcut.getEnd(); @@ -24,13 +24,13 @@ function applyShortcut(content, shortcut) { } /** - Replace shortcuts from blocks in a string - - @param {List<TemplateBlock>} engine - @param {String} filePath - @param {String} content - @return {String} -*/ + * Replace shortcuts from blocks in a string + * + * @param {List<TemplateBlock>} engine + * @param {String} filePath + * @param {String} content + * @return {String} + */ function replaceShortcuts(blocks, filePath, content) { var shortcuts = listShortcuts(blocks, filePath); return shortcuts.reduce(applyShortcut, content); diff --git a/lib/templating/themesLoader.js b/lib/templating/themesLoader.js index 69c3879..bae4c12 100644 --- a/lib/templating/themesLoader.js +++ b/lib/templating/themesLoader.js @@ -12,11 +12,11 @@ var ThemesLoader = nunjucks.Loader.extend({ .map(path.normalize); }, - /* - Read source of a resolved filepath - @param {String} - @return {Object} - */ + /** + * Read source of a resolved filepath + * @param {String} + * @return {Object} + */ getSource: function(fullpath) { if (!fullpath) return null; @@ -40,19 +40,19 @@ var ThemesLoader = nunjucks.Loader.extend({ }; }, - /* - Nunjucks calls "isRelative" to determine when to call "resolve". - We handle absolute paths ourselves in ".resolve" so we always return true - */ + /** + * Nunjucks calls "isRelative" to determine when to call "resolve". + * We handle absolute paths ourselves in ".resolve" so we always return true + */ isRelative: function() { return true; }, - /* - Get original search path containing a template - @param {String} filepath - @return {String} searchPath - */ + /** + * Get original search path containing a template + * @param {String} filepath + * @return {String} searchPath + */ getSearchPath: function(filepath) { return this.searchPaths .sortBy(function(s) { @@ -63,22 +63,22 @@ var ThemesLoader = nunjucks.Loader.extend({ }); }, - /* - Get template name from a filepath - @param {String} filepath - @return {String} name - */ + /** + * Get template name from a filepath + * @param {String} filepath + * @return {String} name + */ getTemplateName: function(filepath) { var originalSearchPath = this.getSearchPath(filepath); return originalSearchPath? path.relative(originalSearchPath, filepath) : null; }, - /* - Resolve a template from a current template - @param {String|null} from - @param {String} to - @return {String|null} - */ + /** + * Resolve a template from a current template + * @param {String|null} from + * @param {String} to + * @return {String|null} + */ resolve: function(from, to) { var searchPaths = this.searchPaths; |