Created
August 12, 2010 00:07
-
-
Save jxson/520055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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