Javascript HEREDOC

Implementing HEREDOC as a post-variable comment. http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript

by orwellophile

HTML

<link rel="stylesheet" href="https://nt4.com/css/fonts/clean.css">
<script src="https://nt4.com/js/useragent.js"></script>
<h3>javascript HEREDOC</h3>
<h4>post-var comment method</h4>
<div class="output"></div>

CSS

body {
    font-family: "clean", "Helvetica Neue", sans-serif;
    margin: 1em;
}

h4 {
    font-size: 1.2em;
}
h3 { 
    font-size: 1.5em;
}

.output {
  white-space: pre-wrap;
  border: 1px solid thistle;
  border-radius: 15px;
  min-height: 200px;
  min-width: 200px;
  display: inline-block;
  margin-top: 1em;
  padding: 0.7em;
}

JavaScript

you = "you";
var HEREDOC = "EOF"; /*!<<<EOF 
   "What the hell choice is there? ${you} cry, ${you} continue to cry, because ${you} don't ever completely come back from where ${you} went with him — a fragment broken off ${you}r pulsing, pumping heart is there still. A nick out of it. 

   A cut that never heals. And if, when it happens to ${you} over and over again in life, too much of ${you}r heart does finally go away, then ${you} can't feel grief any more. And then ${you} ${you}rself are ready to die. 

   ${you}'ll walk up the inclined ladder and someone else will remain behind grieving for ${you}." 

   -- Philip K. Dick, "Flow my Tears, the Policemen Said"
EOF
*/

var currentScript = function() {
    var ref;
    return (ref = document.currentScript) != null 
       ? ref.innerHTML 
       : $('script')
         .map(function(k, f) {
             return f.innerHTML;
         })
         .toArray()
         .join('\n')
       ;
}

var useHereDoc = function() {
  HEREDOC = currentScript().split(HEREDOC)[2]; 
  $('.output').text(HEREDOC.replace(/\$\{([^}]+)\}/g, 
       function(outer, inner, pos) {
           return this[inner];
       }));
  $('.output').css('width', $(window).width() - 64 + "px")
};

useHereDoc();