Skip to content

Commit

Permalink
Support for ruby's squiggly heredoc (#2049)
Browse files Browse the repository at this point in the history
* Support for ruby's squiggly heredoc

Ruby has the squiggly heredoc notation to insert indented multiline
strings. This should be supported in addition to the existing heredoc
notation.

+ Improved heredoc regexp so that the closing tag must match the open tag
  • Loading branch information
skyfmmf authored and egor-rogov committed Aug 11, 2019
1 parent 4789259 commit 0b6c4ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/languages/ruby.js
Expand Up @@ -65,8 +65,16 @@ function(hljs) {
// is the last character of a preceding identifier, as in: `func?4`
begin: /\B\?(\\\d{1,3}|\\x[A-Fa-f0-9]{1,2}|\\u[A-Fa-f0-9]{4}|\\?\S)\b/
},
{
begin: /<<(-?)\w+$/, end: /^\s*\w+$/,
{ // heredocs
begin: /<<[-~]?'?(\w+)(?:.|\n)*?\n\s*\1\b/,
returnBegin: true,
contains: [
{ begin: /<<[-~]?'?/ },
{ begin: /\w+/,
endSameAsBegin: true,
contains: [hljs.BACKSLASH_ESCAPE, SUBST],
}
]
}
]
};
Expand Down
7 changes: 7 additions & 0 deletions test/markup/ruby/heredoc.expect.txt
Expand Up @@ -4,5 +4,12 @@
&lt;h4&gt;<span class="hljs-subst">#{bar}</span>&lt;/h4&gt;
&lt;/div&gt;
HTML</span>
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">baz</span><span class="hljs-params">()</span></span>
msg = <span class="hljs-string">&lt;&lt;~FOO
&lt;div&gt;
&lt;h4&gt;<span class="hljs-subst">#{bar}</span>&lt;/h4&gt;
&lt;/div&gt;
FOO</span>
<span class="hljs-keyword">end</span>
7 changes: 7 additions & 0 deletions test/markup/ruby/heredoc.txt
Expand Up @@ -4,5 +4,12 @@ def foo()
<h4>#{bar}</h4>
</div>
HTML
end

def baz()
msg = <<~FOO
<div>
<h4>#{bar}</h4>
</div>
FOO
end

0 comments on commit 0b6c4ba

Please sign in to comment.