From f6b89d620201447f1b3b109adf469cee69b5630e Mon Sep 17 00:00:00 2001 From: Jay Traband Date: Thu, 17 Sep 2015 17:51:32 -0700 Subject: [PATCH] refactoring --- .../api-builder/angular.io-package/index.js | 2 +- .../rendering/indentForMarkdown.js | 62 +++++++++++++++++++ .../rendering/indentNonMixin.js | 49 --------------- .../templates/class.template.html | 6 +- .../templates/function.template.html | 2 +- .../templates/var.template.html | 2 +- 6 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 public/api-builder/angular.io-package/rendering/indentForMarkdown.js delete mode 100644 public/api-builder/angular.io-package/rendering/indentNonMixin.js diff --git a/public/api-builder/angular.io-package/index.js b/public/api-builder/angular.io-package/index.js index 34e19be75b..2a1a81706b 100644 --- a/public/api-builder/angular.io-package/index.js +++ b/public/api-builder/angular.io-package/index.js @@ -74,7 +74,7 @@ module.exports = new Package('angular.io', [basePackage]) .config(function(templateEngine, getInjectables) { templateEngine.filters = templateEngine.filters.concat(getInjectables([require('./rendering/trimBlankLines')])); - templateEngine.filters = templateEngine.filters.concat(getInjectables([require('./rendering/indentNonMixin')])); + templateEngine.filters = templateEngine.filters.concat(getInjectables([require('./rendering/indentForMarkdown')])); }); diff --git a/public/api-builder/angular.io-package/rendering/indentForMarkdown.js b/public/api-builder/angular.io-package/rendering/indentForMarkdown.js new file mode 100644 index 0000000000..e7d8dea4fe --- /dev/null +++ b/public/api-builder/angular.io-package/rendering/indentForMarkdown.js @@ -0,0 +1,62 @@ +module.exports = function(encodeCodeBlock) { + // var MIXIN_PATTERN = /\S*\+\S*\(.*/; + return { + name: 'indentForMarkdown', + process: function (str, width ) { + if(str == null || str.length === 0) { + return ''; + } + width = width || 4; + + var lines = str.split('\n'); + var newLines = []; + var sp = spaces(width); + var spMixin = spaces(width - 2); + var isAfterMarkdownTag = true; + lines.forEach(function(line) { + // indent lines that match mixin pattern by 2 less than specified width + if (line.indexOf('{@example') >= 0) { + if (isAfterMarkdownTag) { + // happens if example follows example + if (newLines.length > 0) { + newLines.pop(); + } else { + // wierd case - first expression in str is an @example + // in this case the :markdown appear above the str passed in, + // so we need to put 'something' into the markdown tag. + newLines.push(sp + "."); // '.' is a dummy char + } + } + newLines.push(spMixin + line); + // after a mixin line we need to reenter markdown. + newLines.push(spMixin + ':markdown'); + isAfterMarkdownTag = true; + } else { + if ((!isAfterMarkdownTag) || (line.trim().length > 0)) { + newLines.push(sp + line); + isAfterMarkdownTag = false; + } + } + }); + if (isAfterMarkdownTag) { + if (newLines.length > 0) { + // if last line is a markdown tag remove it. + newLines.pop(); + } + } + + var res = newLines.join('\n'); + + return res; + } + }; + + function spaces(n) { + var str = ''; + for(var i=0; i