 
 TemplateWatcher.renderers =
     html: (code, path)->
-        @wrap path, code, code
+        content = code
+            .replace(/^\s+/g, '')
+            .replace(/\r?\n\s*/g, '')
+            .replace(/\\/g, '\\\\')
+            .replace(/'/g, '\\\'')
+        @wrap path, content, code
     jade: (code, path)->
+        stripNewlines: (content)->
+            content.replace(/\r?\n/g, '\\n\' +\n    \'')
+
+        # Normalize backslashes and strip newlines.
+        escapeContent: (content)->
+            stripNewlines(content)
+            .replace(/\\/g, '\\\\')
+            .replace(/'/g, '\\\'')
+
         options = filename: path
         content = require('jade').render(code, options)
-        @wrap path, content, code
+        @wrap path, escapeContent(content), code
 
 module.exports = TemplateWatcher

        content = """
        angular.module('#{module}', [])
        .run(function($templateCache){
            $templateCache.put('#{shortPath}', '#{@escapeContent(content)}');
        });
        """



    getModuleRoot: -> @config.templateModuleRoot

    render: (code, path)->
        extension = path.substr(path.lastIndexOf('.') + 1)
        TemplateWatcher.renderers[extension].call(this, code, path)
