var Q = require("q"); // TODO negotiate text/html vs text/html+fragment (or other mime type) /** * @param {Request} request * @param {String} path * @param {String} contentType * @returns {Response} */ exports.HandleHtmlFragmentResponses = function (app, handleHtmlFragmentResponse) { handleHtmlFragmentResponse = handleHtmlFragmentResponse || exports.handleHtmlFragmentResponse; return function (request) { request.handleHtmlFragmentResponse = handleHtmlFragmentResponse; return Q.fcall(app, request) .then(function (response) { if (response.htmlFragment) { return Q.fcall(handleHtmlFragmentResponse, response); } else { return response; } }); }; }; exports.handleHtmlFragmentResponse = function (response) { var htmlFragment = response.htmlFragment; delete response.htmlFragment; response.headers["content-type"] = "text/html; charset=utf-8"; response.body = { forEach: function (write) { write("\n"); write("\n"); write(" \n"); if (response.htmlTitle !== void 0) { write(" " + escapeHtml(response.htmlTitle) + "\n"); } write(" \n"); write(" \n"); htmlFragment.forEach(function (line) { write(" " + line); }); write(" \n"); write("\n"); } }; return response; }; exports.escapeHtml = escapeHtml; function escapeHtml(text) { return String(text) .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) }