diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-01 16:57:41 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-01 16:57:41 +0100 |
| commit | 25109ca7e318e188229cac92552ba0dcd7e897a1 (patch) | |
| tree | 324cecb9ddc3cd5ad85c45143cbe1513aa3e2edc /lib | |
| parent | 376094a18d76eef4335e374455f0b2869e53120b (diff) | |
Add conversion of hyperlink elements to <a> tags
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/document-to-html.js | 5 | ||||
| -rw-r--r-- | lib/documents.js | 39 | ||||
| -rw-r--r-- | lib/html-generator.js | 4 | ||||
| -rw-r--r-- | lib/styles.js | 3 |
4 files changed, 33 insertions, 18 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index caccb6f..1224878 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -72,6 +72,11 @@ function DocumentConverter(options) { "run": convertRun, "text": function(element, html) { html.text(element.value); + }, + "hyperlink": function(element, html) { + html.push(styles.element("a", {href: element.href})); + convertElements(element.children, html); + html.pop(); } }; return { diff --git a/lib/documents.js b/lib/documents.js index 27190aa..8fc3df7 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -1,9 +1,3 @@ -exports.Document = Document; -exports.Paragraph = Paragraph; -exports.Run = Run; -exports.Text = Text; - - function Document(children) { return { type: "document", @@ -11,25 +5,36 @@ function Document(children) { }; } -function Paragraph(children, properties) { +Paragraph = ElementWithChildren("paragraph"); +Run = ElementWithChildren("run"); + +function Text(value) { return { - type: "paragraph", - children: children, - properties: properties || {} + type: "text", + value: value }; } -function Run(children, properties) { +function Hyperlink(children, options) { return { - type: "run", + type: "hyperlink", children: children, - properties: properties || {} + href: options.href }; } -function Text(value) { - return { - type: "text", - value: value +function ElementWithChildren(type) { + return function(children, properties) { + return { + type: type, + children: children, + properties: properties || {} + }; }; } + +exports.Document = Document; +exports.Paragraph = Paragraph; +exports.Run = Run; +exports.Text = Text; +exports.Hyperlink = Hyperlink; diff --git a/lib/html-generator.js b/lib/html-generator.js index 339b481..065d1d2 100644 --- a/lib/html-generator.js +++ b/lib/html-generator.js @@ -84,6 +84,9 @@ function HtmlGenerator() { } else { var attributeString = ""; } + if (element.href) { + attributeString += util.format(' href="%s"', escapeHtml(element.href)); + } fragments.push(util.format("<%s%s>", element.tagName, attributeString)); } @@ -94,6 +97,7 @@ function HtmlGenerator() { asString: asString, append: append, push: push, + pop: pop, _fragments: fragments }; } diff --git a/lib/styles.js b/lib/styles.js index 8120a76..cbe36d4 100644 --- a/lib/styles.js +++ b/lib/styles.js @@ -23,6 +23,7 @@ function element(tagName, options) { return { tagName: tagName, fresh: options.fresh, - "class": options["class"] + "class": options["class"], + href: options.href }; } |
