Skip to content

Commit

Permalink
Check for possible pre-existing marker strings in Smarty
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitgeist87 committed May 13, 2017
1 parent 7a1a404 commit 5df26e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
20 changes: 15 additions & 5 deletions components/prism-smarty.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
var smarty_litteral_start = '{literal}';
var smarty_litteral_end = '{/literal}';
var smarty_litteral_mode = false;

Prism.languages.smarty = Prism.languages.extend('markup', {
'smarty': {
pattern: smarty_pattern,
Expand Down Expand Up @@ -93,9 +93,16 @@
if(match === smarty_litteral_start) {
smarty_litteral_mode = true;
}
env.tokenStack.push(match);

return '___SMARTY' + env.tokenStack.length + '___';
var i = env.tokenStack.length;
// Check for existing strings
while (env.backupCode.indexOf('___SMARTY' + i + '___') !== -1)
++i;

// Create a sparse array
env.tokenStack[i] = match;

return '___SMARTY' + i + '___';
}
return match;
});
Expand All @@ -116,9 +123,12 @@
return;
}

for (var i = 0, t; t = env.tokenStack[i]; i++) {
for (var i = 0, keys = Object.keys(env.tokenStack); i < keys.length; ++i) {
var k = keys[i];
var t = env.tokenStack[k];

// The replace prevents $$, $&, $`, $', $n, $nn from being interpreted as special patterns
env.highlightedCode = env.highlightedCode.replace('___SMARTY' + (i + 1) + '___', Prism.highlight(t, env.grammar, 'smarty').replace(/\$/g, '$$$$'));
env.highlightedCode = env.highlightedCode.replace('___SMARTY' + k + '___', Prism.highlight(t, env.grammar, 'smarty').replace(/\$/g, '$$$$'));
}

env.element.innerHTML = env.highlightedCode;
Expand Down
2 changes: 1 addition & 1 deletion components/prism-smarty.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/languages/smarty/smarty_in_markup_feature.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5df26e2

Please sign in to comment.