Skip to content

Commit

Permalink
Fix support for groovy
Browse files Browse the repository at this point in the history
* Add support for calling `Prism.highlight` from inside grammars
* Add support for passing a grammar instead of a name to
  `refract.highlight`

Closes GH-13.
Closes GH-14.
  • Loading branch information
peterwilliams authored and wooorm committed Aug 5, 2018
1 parent 216afb0 commit 2a0a7af
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
21 changes: 15 additions & 6 deletions core.js
Expand Up @@ -63,20 +63,29 @@ function register(grammar) {

function highlight(value, name) {
var sup = Prism.highlight
var grammar

if (typeof value !== 'string') {
throw new Error('Expected `string` for `value`, got `' + value + '`')
}

if (typeof name !== 'string') {
throw new Error('Expected `string` for `name`, got `' + name + '`')
}
/* `name` is a grammar object */
if (refract.util.type(name) === 'Object') {
grammar = name
name = null
} else {
if (typeof name !== 'string') {
throw new Error('Expected `string` for `name`, got `' + name + '`')
}

if (!own.call(refract.languages, name)) {
throw new Error('Unknown language: `' + name + '` is not registered')
if (own.call(refract.languages, name)) {
grammar = refract.languages[name]
} else {
throw new Error('Unknown language: `' + name + '` is not registered')
}
}

return sup.call(this, value, refract.languages[name], name)
return sup.call(this, value, grammar, name)
}

function registered(language) {
Expand Down
2 changes: 1 addition & 1 deletion lang/groovy.js
Expand Up @@ -52,7 +52,7 @@ function groovy(Prism) {
env.content.value = env.content.value
.replace(/&lt;/g, '<')
.replace(/&amp;/g, '&')
env.content.value = Prism.highlight(env.content.value, {
env.content = Prism.highlight(env.content.value, {
expression: {
pattern: pattern,
lookbehind: true,
Expand Down
21 changes: 20 additions & 1 deletion script/languages.js
Expand Up @@ -120,6 +120,7 @@ function fixWrapHook() {

return {
visitor: {
// We only perform the later changes inside `wrap` hooks, just to be safe.
CallExpression: {
enter: function(path) {
if (isWrapHook(path)) {
Expand All @@ -132,9 +133,27 @@ function fixWrapHook() {
}
}
},
// If a syntax is assigning `Prism.highlight` to `env.content`, we should
// add the result to `env.content` instead of `env.content.value`.
AssignmentExpression: {
enter: function(path) {
if (
path.get('right.callee').matchesPattern('Prism.highlight') &&
path.get('left').matchesPattern('env.content')
) {
path.get('left').node.ignoreValueSuffix = true
}
}
},
// If a syntax is using `env.content`, it probably expects a string value,
// those are stored at `env.content.value`.
MemberExpression: {
enter: function(path) {
if (this.inWrapHook && path.matchesPattern('env.content')) {
if (
this.inWrapHook &&
!path.node.ignoreValueSuffix &&
path.matchesPattern('env.content')
) {
path.node.object = t.memberExpression(
path.node.object,
t.identifier('content')
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/groovy-string-interpolation/input.txt
@@ -0,0 +1 @@
def sum = "The sum of 2 and 3 equals ${2 + 3}"
1 change: 1 addition & 0 deletions test/fixtures/groovy-string-interpolation/output.html
@@ -0,0 +1 @@
<span class="token keyword">def</span> sum <span class="token operator">=</span> <span class="token string gstring">"The sum of 2 and 3 equals <span class="token expression"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token number">2</span> <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">}</span></span>"</span>

0 comments on commit 2a0a7af

Please sign in to comment.