gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.31k stars 156 forks source link

hereDoc syntax tweak #1081

Open determin1st opened 5 years ago

determin1st commented 5 years ago

What do you think about some utilizer of:

hereDoc = (f) ->
  f = f.toString!
  a = f.indexOf '/*'
  b = f.lastIndexOf '*/'
  return f.substring a + 2, b - 1 .trim!

sample = hereDoc !->
  /*
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
    <circle style="stroke:black; fill:none;" cx="50" cy="50" r="49"/>
    <circle style="stroke:none; fill:none;" cx="50" cy="50" r="46"/>
    <path style="stroke:none; fill:gray;" d="M64 17c0-1-1-2-2-2H38c-1 0-2 1-2 2v19H17c-1 0-2 1-2 2v24c0 1 1 2 2 2h19v19c0 1 1 2 2 2h24c1 0 2-1 2-2V64h19c1 0 2-1 2-2V38c0-1-1-2-2-2H64V17z"/>
    <path style="stroke:none; fill:none;" d="M81 60H62c-1 0-2 1-2 2v19H40V62c0-1-1-2-2-2H19V40h19c1 0 2-1 2-2V19h20v19c0 1 1 2 2 2h19v20z"/>
  </svg>
  */

imo, it's a better version of heredoc, because you dont need to refine/escape any special character combinations.

rhendric commented 5 years ago

Well, that's clever, and if it works for you, great. But bear in mind that you're losing the ability to express the character combination */ at all, since escape sequences won't work. Triple-quoted strings also handle indented contents more gracefully than this approach will.