aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-03-29 21:05:21 +0000
committerMichael Williamson <mike@zwobble.org>2013-03-29 21:05:21 +0000
commit21da8b6424b78466b953df8ea4d66bd4020e0b7a (patch)
treed35b3cdce3705fa343a4932f9afda54e5f5638b4 /lib
parent0f90a6de310fd86472c29e945f06fe94c666fa2c (diff)
Move HtmlGenerator to its own module
Diffstat (limited to 'lib')
-rw-r--r--lib/documents.js54
-rw-r--r--lib/html-generator.js56
2 files changed, 57 insertions, 53 deletions
diff --git a/lib/documents.js b/lib/documents.js
index 8d9b665..a9e53e3 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -1,8 +1,7 @@
-var util = require("util");
-
var q = require("q");
var styles = require("./styles");
+var HtmlGenerator = require("./html-generator").HtmlGenerator;
exports.DocumentConverter = DocumentConverter;
exports.Document = Document;
@@ -63,49 +62,6 @@ function DocumentConverter(options) {
}
}
-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("</%s>", element));
- }
-
- function popAll() {
- while (stack.length > 0) {
- pop();
- }
- }
-
- return {
- style: style,
- text: text,
- asString: asString
- };
-}
-
function Document(children) {
return {
type: "document",
@@ -134,11 +90,3 @@ function Text(value) {
value: value
};
}
-
-function escapeHtml(value) {
- return value
- .replace(/&/g, '&amp;')
- .replace(/"/g, '&quot;')
- .replace(/</g, '&lt;')
- .replace(/>/g, '&gt;');
-}
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("</%s>", element));
+ }
+
+ function popAll() {
+ while (stack.length > 0) {
+ pop();
+ }
+ }
+
+ return {
+ style: style,
+ text: text,
+ asString: asString
+ };
+}
+
+
+function escapeHtml(value) {
+ return value
+ .replace(/&/g, '&amp;')
+ .replace(/"/g, '&quot;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;');
+}