diff --git a/lib/array.js b/lib/array.js index 81db4134..118f7693 100644 --- a/lib/array.js +++ b/lib/array.js @@ -383,8 +383,6 @@ helpers.some = function(arr, cb, options) { if (arr == null) { return options.inverse(this); } - - var result = false; var len = arr.length, i = -1; while (++i < len) { if (cb(arr[i], i, arr)) { diff --git a/lib/comparison.js b/lib/comparison.js index 13b89d31..2dcf3186 100644 --- a/lib/comparison.js +++ b/lib/comparison.js @@ -2,7 +2,6 @@ var isOdd = require('is-odd'); var isEven = require('is-even'); -var typeOf = require('kind-of'); var contains = require('./utils/contains'); var isObject = require('./object').isObject; var isString = require('./string').isString; diff --git a/lib/html.js b/lib/html.js index 17042b57..90b4d151 100644 --- a/lib/html.js +++ b/lib/html.js @@ -3,7 +3,6 @@ var path = require('path'); var tag = require('html-tag'); var typeOf = require('kind-of'); -var merge = require('mixin-deep'); var html = require('./utils/html'); var utils = require('./utils'); var parseAttr = html.parseAttributes; @@ -51,6 +50,30 @@ helpers.css = function(array, options) { }).join('\n'); }; +/** + * Truncates a string to the specified `length`, and appends + * it with an elipsis, `…`. + * + * ```js + * {{ellipsis "foo bar baz", 7}} + * //=> 'foo bar…' + * ``` + * @name .ellipsis + * @param {String} `str` + * @param {Number} `length` The desired length of the returned string. + * @return {String} The truncated string. + * @api public + */ + +helpers.ellipsis = function(str, limit) { + if (str && typeof str === "string") { + if (str.length <= limit) { + return str; + } + return helpers.truncate(str, limit) + '…'; + } +}; + /** * Generate one or more `` tags with paths/urls to * javascript or coffeescript files. @@ -82,6 +105,52 @@ helpers.js = function(context) { }).join('\n'); }; +/** + * Strip HTML tags from a string, so that only the text nodes + * are preserved. + * + * ```js + * {{sanitize "foo"}} + * //=> 'foo' + * ``` + * + * @param {String} `str` The string of HTML to sanitize. + * @return {String} + * @api public + */ + +helpers.sanitize = function(str) { + return html.sanitize(str); +}; + +/** + * Truncate a string by removing all HTML tags and limiting the result + * to the specified `length`. Aslo see [ellipsis][]. + * + * ```js + * truncate("foo bar baz", 7); + * //=> 'foo bar' + * ``` + * + * @name .truncate + * @param {String} `str` + * @param {Number} `limit` The desired length of the returned string. + * @param {String} `suffix` Optionally supply a string to use as a suffix to + * denote when the string has been truncated. + * @return {String} The truncated string. + * @api public + */ + +helpers.truncate = function(str, limit, suffix) { + if (str && typeof str === "string") { + var ch = typeof suffix === 'string' ? suffix : ''; + if (str.length > limit) { + return html.sanitize(str).slice(0, limit - ch.length) + ch; + } + return str; + } +}; + /** * Block helper for creating unordered lists (``) * diff --git a/lib/path.js b/lib/path.js index 79b8c5d3..d4692aa1 100644 --- a/lib/path.js +++ b/lib/path.js @@ -2,6 +2,7 @@ var path = require('path'); var relative = require('relative'); +var normalize = require('normalize-path'); /** * Expose `helpers` @@ -73,3 +74,27 @@ helpers.extname = function(filepath) { helpers.relative = function(a, b) { return relative(a, b); }; + +/** + * Get specific (joined) segments of a file path by passing a + * range of array indices. + * + * ```js + * {{segments "a/b/c/d" "2" "3"}} + * //=> 'c/d' + * + * {{segments "a/b/c/d" "1" "3"}} + * //=> 'b/c/d' + * + * {{segments "a/b/c/d" "1" "2"}} + * //=> 'b/c' + * ``` + * + * @param {String} `filepath` The file path to split into segments. + * @return {String} Returns a single, joined file path. + * @api public + */ + +helpers.segments = function(fp, a, b) { + return normalize(fp).split('/').slice(a, b).join('/'); +}; diff --git a/lib/string.js b/lib/string.js index 6bb247b4..2a6e067a 100644 --- a/lib/string.js +++ b/lib/string.js @@ -1,6 +1,5 @@ 'use strict'; -var html = require('./utils/html'); var utils = require('./utils'); /** diff --git a/lib/utils/html.js b/lib/utils/html.js index 6859fa7d..56149954 100644 --- a/lib/utils/html.js +++ b/lib/utils/html.js @@ -1,12 +1,14 @@ 'use strict'; +var striptags = require('striptags'); var typeOf = require('kind-of'); +var utils = require('./'); /** * Expose `utils` */ -var utils = module.exports; +var html = module.exports; /** * Remove extra newlines from HTML, respect indentation. @@ -16,7 +18,7 @@ var utils = module.exports; * @api public */ -utils.condense = function(str) { +html.condense = function(str) { return str.replace(/(\n|\r){2,}/g, '\n'); }; @@ -28,7 +30,7 @@ utils.condense = function(str) { * @api public */ -utils.padcomments = function(str) { +html.padcomments = function(str) { return str.replace(/(\s*