From 21da8b6424b78466b953df8ea4d66bd4020e0b7a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 29 Mar 2013 21:05:21 +0000 Subject: Move HtmlGenerator to its own module --- lib/html-generator.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/html-generator.js (limited to 'lib/html-generator.js') diff --git a/lib/html-generator.js b/lib/html-generator.js new file mode 100644 index 0000000..247ca09 --- /dev/null +++ b/lib/html-generator.js @@ -0,0 +1,56 @@ +var util = require("util"); + +exports.HtmlGenerator = HtmlGenerator; + + +function HtmlGenerator() { + var stack = []; + var fragments = []; + + function text(value) { + fragments.push(escapeHtml(value)) + } + + function asString() { + popAll(); + return fragments.join(""); + } + + function style(style) { + popAll(); + style.forEach(function(tagName) { + push(tagName); + }); + } + + function push(element) { + stack.push(element); + fragments.push(util.format("<%s>", element)); + } + + function pop() { + var element = stack.pop(); + fragments.push(util.format("", element)); + } + + function popAll() { + while (stack.length > 0) { + pop(); + } + } + + return { + style: style, + text: text, + asString: asString + }; +} + + +function escapeHtml(value) { + return value + .replace(/&/g, '&') + .replace(/"/g, '"') + .replace(//g, '>'); +} -- cgit v1.2.3