From 4fe6b441337c63fbc03de7136687ab9afd15dfa3 Mon Sep 17 00:00:00 2001 From: Ian Graham Date: Fri, 30 Jan 2015 21:44:12 -0800 Subject: [PATCH 1/3] Adds ability to configure the open and close shortcode brackets --- lib/shortcode-parser.js | 34 ++++++++++++++++++++++++++++++---- test/unit.js | 19 ++++++++++++++++++- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/shortcode-parser.js b/lib/shortcode-parser.js index 5c6fb2d..ea9be92 100644 --- a/lib/shortcode-parser.js +++ b/lib/shortcode-parser.js @@ -8,12 +8,31 @@ var shortcodes = {}; var SHORTCODE_ATTRS = /(\s+([a-z0-9\-_]+|([a-z0-9\-_]+)\s*=\s*([a-z0-9\-_]+|\d+\.\d+|'[^']*'|"[^"]*")))*/.toString().slice(1,-1); var SHORTCODE_SLASH = /\s*\/?\s*/.toString().slice(1,-1); -var SHORTCODE_OPEN = /\[\s*%s/.toString().slice(1,-1); -var SHORTCODE_RIGHT_BRACKET = '\\]'; -var SHORTCODE_CLOSE = /\[\s*\/\s*%s\s*\]/.toString().slice(1,-1); +var SHORTCODE_OPEN = /\[\[\s*%s/.toString().slice(1,-1); +var SHORTCODE_RIGHT_BRACKET = '\\]\]'; +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,6 +100,13 @@ 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') { 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 From 855b90c6cb60c78d6f602da37d6752b2d30c4a37 Mon Sep 17 00:00:00 2001 From: Ian Graham Date: Fri, 30 Jan 2015 21:48:35 -0800 Subject: [PATCH 2/3] Updated README to include setTag example --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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 From 7b77ad418f302a4f43bedf404ba3e2293c589021 Mon Sep 17 00:00:00 2001 From: Ian Graham Date: Fri, 30 Jan 2015 21:52:37 -0800 Subject: [PATCH 3/3] Reverted change where I used brackets [[ or ]] as the default instead of one [ or ] --- lib/shortcode-parser.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/shortcode-parser.js b/lib/shortcode-parser.js index ea9be92..0ea6e6f 100644 --- a/lib/shortcode-parser.js +++ b/lib/shortcode-parser.js @@ -8,9 +8,9 @@ var shortcodes = {}; var SHORTCODE_ATTRS = /(\s+([a-z0-9\-_]+|([a-z0-9\-_]+)\s*=\s*([a-z0-9\-_]+|\d+\.\d+|'[^']*'|"[^"]*")))*/.toString().slice(1,-1); var SHORTCODE_SLASH = /\s*\/?\s*/.toString().slice(1,-1); -var SHORTCODE_OPEN = /\[\[\s*%s/.toString().slice(1,-1); -var SHORTCODE_RIGHT_BRACKET = '\\]\]'; -var SHORTCODE_CLOSE = /\[\[\s*\/\s*%s\s*\]\]/.toString().slice(1,-1); +var SHORTCODE_OPEN = /\[\s*%s/.toString().slice(1,-1); +var SHORTCODE_RIGHT_BRACKET = '\\]'; +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); @@ -107,7 +107,7 @@ module.exports = { setShortCodeOpen(open); setShortCodeClose(open, close); }, - + add: function (name, callback) { if (typeof name == 'object') { var ob = name;