aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-02-04 21:35:53 +0000
committerMichael Williamson <mike@zwobble.org>2015-02-04 21:35:53 +0000
commit48b0924ce891aed19299d03a8f67ab0f82bd3224 (patch)
tree4deab3ae15a95a72b6e747e7705872678df9160d /lib
parent1657942a57e6ef5c13d9b9155ec736961693fae5 (diff)
Remove state from HTML to Markdown mappers
Diffstat (limited to 'lib')
-rw-r--r--lib/writers/markdown-writer.js159
1 files changed, 60 insertions, 99 deletions
diff --git a/lib/writers/markdown-writer.js b/lib/writers/markdown-writer.js
index 1866be6..cdbfc78 100644
--- a/lib/writers/markdown-writer.js
+++ b/lib/writers/markdown-writer.js
@@ -1,81 +1,44 @@
-var _ = require("underscore");
-
-
function markdownElement(start, end) {
if (end === undefined) {
end = start;
}
-
- function open(attrs) {
- return start;
- }
-
- function close() {
- return end;
- }
- return {open: open, close: close};
+ return function() {
+ return {start: start, end: end};
+ };
}
-function markdownLink() {
- var href;
-
- function open(attributes) {
- href = attributes.href;
- return "[";
- }
-
- function close() {
- return "](" + (href || "") + ")";
- }
-
- return {open: open, close: close, anchorPosition: "before"};
+function markdownLink(attributes) {
+ var href = attributes.href || "";
+ return {
+ start: "[",
+ end: "](" + href + ")",
+ anchorPosition: "before"
+ };
}
-function markdownImage() {
- function open(attributes) {
- var src = attributes.src || "";
- var altText = attributes.alt || "";
- if (src || altText) {
- return "![" + altText + "](" + src + ")";
- }
- }
-
- function close() {
- return "";
+function markdownImage(attributes) {
+ var src = attributes.src || "";
+ var altText = attributes.alt || "";
+ if (src || altText) {
+ return {start: "![" + altText + "](" + src + ")"};
+ } else {
+ return {};
}
-
- return {open: open, close: close};
}
-function markdownList() {
- function open(attrs) {
- return attrs.indent > -1 ? "\n" : "";
- }
-
- function close(attrs) {
- return attrs.indent > -1 ? "" : "\n";
- }
-
- return {open: open, close: close};
+function markdownList(attributes) {
+ return {
+ start: attributes.indent > -1 ? "\n" : "",
+ end: attributes.indent > -1 ? "" : "\n"
+ };
}
-function markdownListItem() {
- function open(attrs) {
- var output = "";
-
- for(var i = 0; i < attrs.indent; ++i) {
- output += "\t";
- }
-
- return output + (attrs.type === "ul" ? "-" : attrs.count + ".") + " ";
- }
-
- function close() {
- return "\n";
- }
-
- return {open: open, close: close};
+function markdownListItem(attributes) {
+ var start = repeatString("\t", Math.max(attributes.indent, 0)) +
+ (attributes.type === "ul" ? "-" : attributes.count + ".") + " ";
+
+ return {start: start, end: "\n"};
}
// map HTML tags to Markdown syntax
@@ -85,18 +48,18 @@ var htmlmarkdown = {
// while still allowing them to be collapsed when multiple empty lines are removed
"p": markdownElement("", "\n\n\n"),
"br": markdownElement("", " \n"),
- "ul": markdownList(),
- "ol": markdownList(),
- "li": markdownListItem(),
+ "ul": markdownList,
+ "ol": markdownList,
+ "li": markdownListItem,
"strong": markdownElement("**"),
"em": markdownElement("*"),
- "a": markdownLink(),
- "img": markdownImage(),
+ "a": markdownLink,
+ "img": markdownImage,
};
(function() {
for (var i = 1; i <= 6; i++) {
- htmlmarkdown["h" + i] = new markdownElement(repeatString("#", i) + " ", "\n\n\n");
+ htmlmarkdown["h" + i] = markdownElement(repeatString("#", i) + " ", "\n\n\n");
}
})();
@@ -106,7 +69,7 @@ function repeatString(value, count) {
function markdownWriter() {
var fragments = [];
- var listStack = [];
+ var elementStack = [];
var list = {
indent: -1,
type: "ul",
@@ -120,31 +83,33 @@ function markdownWriter() {
function open(tagName, attributes) {
attributes = attributes || {};
+ if (tagName === "li") {
+ attributes.indent = list.indent;
+ attributes.type = list.type;
+ attributes.count = ++list.count;
+ } else if (isList(tagName)) {
+ attributes.indent = list.indent;
+ }
+
var idHasBeenWritten = false;
- if (htmlmarkdown[tagName]) {
- if (htmlmarkdown[tagName].anchorPosition === "before") {
- writeAnchor(attributes);
- idHasBeenWritten = true;
- }
-
- if (tagName === "li") {
- attributes.indent = list.indent;
- attributes.type = list.type;
- attributes.count = ++list.count;
- } else if (isList(tagName)) {
- attributes.indent = list.indent;
- }
-
- fragments.push(htmlmarkdown[tagName].open(attributes));
+ var createElement = htmlmarkdown[tagName] || function() { return {}; };
+ var element = createElement(attributes);
+ elementStack.push({end: element.end, list: list});
+
+ if (element.anchorPosition === "before") {
+ writeAnchor(attributes);
+ idHasBeenWritten = true;
+ }
- if (isList(tagName)) {
- listStack.push(_.clone(list));
+ fragments.push(element.start || "");
- list.type = tagName;
- list.indent++;
- list.count = 0;
- }
+ if (isList(tagName)) {
+ list = {
+ type: tagName,
+ indent: list.indent + 1,
+ count: 0
+ };
}
if (!idHasBeenWritten) {
writeAnchor(attributes);
@@ -158,13 +123,9 @@ function markdownWriter() {
}
function close(tagName) {
- if (htmlmarkdown[tagName]) {
- if (isList(tagName)) {
- list = listStack.pop();
- }
-
- fragments.push(htmlmarkdown[tagName].close(list));
- }
+ var element = elementStack.pop();
+ list = element.list;
+ fragments.push(element.end || "");
}
function selfClosing(tagName, attributes) {