aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorStoo Goff <stoo.goff@beatsystems.com>2015-01-30 15:49:22 +0000
committerMichael Williamson <mike@zwobble.org>2015-02-04 18:27:51 +0000
commitbfe6bd131845138fe09a97e7471c4b68ef42a763 (patch)
tree20c69194503ca5af2aa7650e3b4b06b07140864b /lib
parenta7657db08f295fa619090eb6fe3a925f3ba6fb06 (diff)
Added markdown-writer which will write directly from docx to Markdown.
Diffstat (limited to 'lib')
-rw-r--r--lib/document-to-html.js8
-rw-r--r--lib/html-writer.js10
-rw-r--r--lib/markdown-writer.js130
3 files changed, 142 insertions, 6 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index b012ea9..695b34c 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -43,7 +43,7 @@ function DocumentConversion(options) {
function convertToHtml(document) {
var messages = [];
- var html = new HtmlGenerator({prettyPrint: options.prettyPrint});
+ var html = new HtmlGenerator({prettyPrint: options.prettyPrint, markdown: options.markdown});
return promises.nfcall(elementToHtml, document, html, messages).then(function() {
html.closeAll();
@@ -88,7 +88,7 @@ function DocumentConversion(options) {
}
function convertRun(run, html, messages, callback) {
- var runHtml = new HtmlGenerator();
+ var runHtml = new HtmlGenerator({ markdown: options.markdown });
if (run.styleId) {
var style = findStyle(run);
if (style) {
@@ -160,7 +160,7 @@ function DocumentConversion(options) {
html.open(htmlPaths.element("td"), {alwaysWrite: true});
async.map(element.children, function(child, callback) {
- var cellHtml = new HtmlGenerator();
+ var cellHtml = new HtmlGenerator({ markdown: options.markdown });
elementToHtml(child, cellHtml, messages, function(error) {
if (error) {
callback(error);
@@ -251,7 +251,7 @@ function DocumentConversion(options) {
},
"note": function(element, html, messages, callback) {
html.open(htmlPaths.element("li", {id: noteHtmlId(element)}));
- var footnoteHtml = new HtmlGenerator();
+ var footnoteHtml = new HtmlGenerator({ markdown: options.markdown });
convertElements(element.body, footnoteHtml, messages, function(err) {
if (err) {
callback(err);
diff --git a/lib/html-writer.js b/lib/html-writer.js
index f1761c9..33441cb 100644
--- a/lib/html-writer.js
+++ b/lib/html-writer.js
@@ -5,9 +5,15 @@ exports.writer = writer;
function writer(options) {
options = options || {};
- if (options.prettyPrint) {
+ if(options.prettyPrint) {
return prettyWriter();
- } else {
+ }
+ else if(options.markdown) {
+ var markdown = require("./markdown-writer");
+
+ return markdown.writer();
+ }
+ else {
return simpleWriter();
}
}
diff --git a/lib/markdown-writer.js b/lib/markdown-writer.js
new file mode 100644
index 0000000..1bfa942
--- /dev/null
+++ b/lib/markdown-writer.js
@@ -0,0 +1,130 @@
+
+function mdElement(start, end) {
+ if (end === undefined)
+ end = start;
+
+ this.open = function(attributes) {
+ return start;
+ };
+
+ this.close = function() {
+ return end;
+ };
+}
+function mdLink(link, text) {
+ var attrs = {};
+
+ this.open = function(attributes) {
+ attrs = attributes;
+ return "[";
+ };
+
+ this.close = function() {
+ return (attrs[text] || "") + "](" + (attrs[link] || "") + ")";
+ };
+}
+function mdListItem() {
+ this.open = function(attributes) {
+ var output = "";
+
+ for(var i = 0; i < attributes["indent"]; ++i)
+ output += "\t";
+
+ return output + (attributes["type"] === "ul" ? "-" : attributes["count"] + ".") + " ";
+ };
+
+ this.close = function() {
+ return "\n";
+ };
+}
+
+// map HTML tags to Markdown syntax
+var htmlmd = {
+ "h1": new mdElement("# ", "\n\n"),
+ "h2": new mdElement("## ", "\n\n"),
+ "h3": new mdElement("### ", "\n\n"),
+ "h4": new mdElement("#### ", "\n\n"),
+ "h5": new mdElement("##### ", "\n\n"),
+ "h6": new mdElement("###### ", "\n\n"),
+ "p": new mdElement("", "\n\n"),
+ "br": new mdElement("", " \n"),
+ "ul": new mdElement("\n", ""),
+ "ol": new mdElement("\n", ""),
+ "li": new mdListItem(),
+ "strong": new mdElement("**"),
+ "em": new mdElement("*"),
+ "a": new mdLink("href"),
+ "img": new mdLink("src", "alt"),
+};
+
+function mdWriter() {
+ var fragments = [];
+ var list = {
+ indent: -1,
+ type: "ul",
+ count: 0
+ };
+
+ function isList(tagName) {
+ return tagName == "ul" || tagName == "ol";
+ }
+
+ function open(tagName, attributes) {
+ if(htmlmd[tagName]) {
+ if(tagName == "li") {
+ attributes = attributes || {};
+ attributes["indent"] = list.indent;
+ attributes["type"] = list.type;
+ attributes["count"] = ++list.count;
+ }
+
+ fragments.push(htmlmd[tagName].open(attributes));
+
+ if(isList(tagName)) {
+ list.type = tagName;
+ list.indent++;
+ list.count = 0;
+ }
+ }
+ }
+
+ function close(tagName) {
+ if(htmlmd[tagName]) {
+ fragments.push(htmlmd[tagName].close());
+
+ if(isList(tagName)) {
+ list.indent--;
+ }
+ }
+ }
+
+ function selfClosing(tagName, attributes) {
+ if(htmlmd[tagName]) {
+ fragments.push(htmlmd[tagName].open(attributes));
+ fragments.push(htmlmd[tagName].close());
+ }
+ }
+
+ function text(value) {
+ fragments.push(value);
+ }
+
+ function append(html) {
+ fragments.push(html);
+ }
+
+ function asString() {
+ return fragments.join("").replace(/\n{2,}/, "\n\n");
+ }
+
+ return {
+ asString: asString,
+ open: open,
+ close: close,
+ text: text,
+ selfClosing: selfClosing,
+ append: append
+ };
+}
+
+exports.writer = mdWriter; \ No newline at end of file