Skip to content

Instantly share code, notes, and snippets.

@jxson
Created August 12, 2010 00:07
Show Gist options
  • Save jxson/520055 to your computer and use it in GitHub Desktop.
Save jxson/520055 to your computer and use it in GitHub Desktop.
# borrowed from rails actionpack/lib/action_view/helpers/javascript_helper.rb
JS_ESCAPE_MAP = {
'\\' => '\\\\',
'</' => '<\/',
"\r\n" => '\n',
"\n" => '\n',
"\r" => '\n',
'"' => '\\"',
"'" => "\\'" }
# Escape carrier returns and single and double quotes for JavaScript segments.
def escape_javascript(javascript)
if javascript
javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
else
''
end
end
content = File.new('vendor/client-core/templates/login.mustache').read
File.open('test.js', 'w') {|f| f.write('var = "#{ escape_javascript(content)) }";' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment