diff --git a/README.md b/README.md index e655e9b..a390dc1 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,18 @@ var out = shortcode.parse(str, { }); ``` +Configuring shortcode tags: + +```javascript +shortcode.setTags('{{', '}}'); +``` ## API +### shortcode.setTags(open, close) + +Changes the shortcode open and close tags to the values passed in. + ### shortcode.add(name, callback) Adds a handler to the shortcode `name`. The handler receives `(str, params, data)`. When using an enclosing diff --git a/lib/shortcode-parser.js b/lib/shortcode-parser.js index 5c6fb2d..0ea6e6f 100644 --- a/lib/shortcode-parser.js +++ b/lib/shortcode-parser.js @@ -14,6 +14,25 @@ var SHORTCODE_CLOSE = /\[\s*\/\s*%s\s*\]/.toString().slice(1,-1); var SHORTCODE_CONTENT = /(.|\n|)*?/.toString().slice(1,-1); var SHORTCODE_SPACE = /\s*/.toString().slice(1,-1); + + +function escapeShortCodeTag(tag){ + return tag.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); +}; + +function setShortCodeOpen(tag){ + tag = escapeShortCodeTag(tag); + SHORTCODE_OPEN = new RegExp(tag+'\s*%s').toString().slice(1,-1); +} + +function setShortCodeClose(open, close){ + var escOpen = escapeShortCodeTag(open); + var escClose = escapeShortCodeTag(close); + + SHORTCODE_CLOSE = new RegExp(escOpen+'\s*\/\s*%s\s*'+escClose).toString().slice(1,-1); + SHORTCODE_RIGHT_BRACKET = '\\'+close; +} + function typecast(val) { val = val.trim().replace(/(^['"]|['"]$)/g, ''); if (/^\d+$/.test(val)) { @@ -32,7 +51,7 @@ function typecast(val) { } function closeTagString(name) { - return /^[^a-z0-9]/.test(name) ? util.format('[%s]?%s', name[0].replace('$', '\\$'), name.slice(1)) : name; + return (/^[^a-z0-9]/).test(name) ? util.format('[%s]?%s', name[0].replace('$', '\\$'), name.slice(1)) : name; } function parseShortcode(name, buf, inline) { @@ -81,7 +100,14 @@ function parseShortcode(name, buf, inline) { module.exports = { _shortcodes: shortcodes, - + + setTags: function(open, close){ + open = open.toString(); + close = close.toString(); + setShortCodeOpen(open); + setShortCodeClose(open, close); + }, + add: function (name, callback) { if (typeof name == 'object') { var ob = name; diff --git a/test/unit.js b/test/unit.js index 85fdcdb..7190eeb 100644 --- a/test/unit.js +++ b/test/unit.js @@ -119,7 +119,24 @@ vows.describe("Shortcode Parser").addBatch({ }); assert.strictEqual(out, 'This is [bold size=2em font="Helvetica"]Some Text[/bold] and SOME MORE TEXT and Something...'); }, - + 'Able to change shortcode tags': function() { + // Set short code tags to {{ and }} + shortcode.setTags('{{', '}}'); + + var out, str = '... {{#data_test}}{{/data_test}} ...'; + var context = { + '#data_test' : function(buf, params, data) { + return ''; + } + } + out = shortcode.parseInContext(str, context, {user: 'john', blah: true}); + + assert.equal(out, '... ...'); + + out = shortcode.parseInContext(str,context); + + assert.equal(out, '... ...'); // Ensure data is provided as object; + }, 'Provides data object to handlers': function() { var out, str = '... [#data_test][/data_test] ...'; // NOTE: Testing ability to skip first char of shortcode tag