All new
alle new
This commit is contained in:
1 parent
da64472251
commit
5b20adee48
1605 files changed
+396517
-1674
No files matched your search
+159
@@ -0,0 +1,159 @@
|
||||
var _ = require("underscore");
|
||||
|
||||
exports.writer = writer;
|
||||
|
||||
function writer(options) {
|
||||
options = options || {};
|
||||
if (options.prettyPrint) {
|
||||
return prettyWriter();
|
||||
} else {
|
||||
return simpleWriter();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var indentedElements = {
|
||||
div: true,
|
||||
p: true,
|
||||
ul: true,
|
||||
li: true
|
||||
};
|
||||
|
||||
|
||||
function prettyWriter() {
|
||||
var indentationLevel = 0;
|
||||
var indentation = " ";
|
||||
var stack = [];
|
||||
var start = true;
|
||||
var inText = false;
|
||||
|
||||
var writer = simpleWriter();
|
||||
|
||||
function open(tagName, attributes) {
|
||||
if (indentedElements[tagName]) {
|
||||
indent();
|
||||
}
|
||||
stack.push(tagName);
|
||||
writer.open(tagName, attributes);
|
||||
if (indentedElements[tagName]) {
|
||||
indentationLevel++;
|
||||
}
|
||||
start = false;
|
||||
}
|
||||
|
||||
function close(tagName) {
|
||||
if (indentedElements[tagName]) {
|
||||
indentationLevel--;
|
||||
indent();
|
||||
}
|
||||
stack.pop();
|
||||
writer.close(tagName);
|
||||
}
|
||||
|
||||
function text(value) {
|
||||
startText();
|
||||
var text = isInPre() ? value : value.replace("\n", "\n" + indentation);
|
||||
writer.text(text);
|
||||
}
|
||||
|
||||
function selfClosing(tagName, attributes) {
|
||||
indent();
|
||||
writer.selfClosing(tagName, attributes);
|
||||
}
|
||||
|
||||
function insideIndentedElement() {
|
||||
return stack.length === 0 || indentedElements[stack[stack.length - 1]];
|
||||
}
|
||||
|
||||
function startText() {
|
||||
if (!inText) {
|
||||
indent();
|
||||
inText = true;
|
||||
}
|
||||
}
|
||||
|
||||
function indent() {
|
||||
inText = false;
|
||||
if (!start && insideIndentedElement() && !isInPre()) {
|
||||
writer._append("\n");
|
||||
for (var i = 0; i < indentationLevel; i++) {
|
||||
writer._append(indentation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isInPre() {
|
||||
return _.some(stack, function(tagName) {
|
||||
return tagName === "pre";
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
asString: writer.asString,
|
||||
open: open,
|
||||
close: close,
|
||||
text: text,
|
||||
selfClosing: selfClosing
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function simpleWriter() {
|
||||
var fragments = [];
|
||||
|
||||
function open(tagName, attributes) {
|
||||
var attributeString = generateAttributeString(attributes);
|
||||
fragments.push("<" + tagName + attributeString + ">");
|
||||
}
|
||||
|
||||
function close(tagName) {
|
||||
fragments.push("</" + tagName + ">");
|
||||
}
|
||||
|
||||
function selfClosing(tagName, attributes) {
|
||||
var attributeString = generateAttributeString(attributes);
|
||||
fragments.push("<" + tagName + attributeString + " />");
|
||||
}
|
||||
|
||||
function generateAttributeString(attributes) {
|
||||
return _.map(attributes, function(value, key) {
|
||||
return " " + key + '="' + escapeHtmlAttribute(value) + '"';
|
||||
}).join("");
|
||||
}
|
||||
|
||||
function text(value) {
|
||||
fragments.push(escapeHtmlText(value));
|
||||
}
|
||||
|
||||
function append(html) {
|
||||
fragments.push(html);
|
||||
}
|
||||
|
||||
function asString() {
|
||||
return fragments.join("");
|
||||
}
|
||||
|
||||
return {
|
||||
asString: asString,
|
||||
open: open,
|
||||
close: close,
|
||||
text: text,
|
||||
selfClosing: selfClosing,
|
||||
_append: append
|
||||
};
|
||||
}
|
||||
|
||||
function escapeHtmlText(value) {
|
||||
return value
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>');
|
||||
}
|
||||
|
||||
function escapeHtmlAttribute(value) {
|
||||
return value
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>');
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
var htmlWriter = require("./html-writer");
|
||||
var markdownWriter = require("./markdown-writer");
|
||||
|
||||
exports.writer = writer;
|
||||
|
||||
|
||||
function writer(options) {
|
||||
options = options || {};
|
||||
if (options.outputFormat === "markdown") {
|
||||
return markdownWriter.writer();
|
||||
} else {
|
||||
return htmlWriter.writer(options);
|
||||
}
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
var _ = require("underscore");
|
||||
|
||||
var htmlWriter = require("./html-writer");
|
||||
|
||||
function symmetricMarkdownElement(end) {
|
||||
return markdownElement(end, end);
|
||||
}
|
||||
|
||||
function markdownElement(start, end) {
|
||||
return function() {
|
||||
return {start: start, end: end};
|
||||
};
|
||||
}
|
||||
|
||||
function markdownLink(attributes) {
|
||||
var href = attributes.href || "";
|
||||
if (href) {
|
||||
return {
|
||||
start: "[",
|
||||
end: "](" + escapeMarkdown(href) + ")",
|
||||
anchorPosition: "before"
|
||||
};
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function markdownImage(attributes) {
|
||||
var src = attributes.src || "";
|
||||
var altText = attributes.alt || "";
|
||||
if (src || altText) {
|
||||
return {start: " + ")"};
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function markdownList(options) {
|
||||
return function(attributes, list) {
|
||||
return {
|
||||
start: list ? "\n" : "",
|
||||
end: list ? "" : "\n",
|
||||
list: {
|
||||
isOrdered: options.isOrdered,
|
||||
indent: list ? list.indent + 1 : 0,
|
||||
count: 0
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function markdownListItem(attributes, list, listItem) {
|
||||
list = list || {indent: 0, isOrdered: false, count: 0};
|
||||
list.count++;
|
||||
listItem.hasClosed = false;
|
||||
|
||||
var bullet = list.isOrdered ? list.count + "." : "-";
|
||||
var start = repeatString("\t", list.indent) + bullet + " ";
|
||||
|
||||
return {
|
||||
start: start,
|
||||
end: function() {
|
||||
if (!listItem.hasClosed) {
|
||||
listItem.hasClosed = true;
|
||||
return "\n";
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var htmlToMarkdown = {
|
||||
"p": markdownElement("", "\n\n"),
|
||||
"br": markdownElement("", " \n"),
|
||||
"ul": markdownList({isOrdered: false}),
|
||||
"ol": markdownList({isOrdered: true}),
|
||||
"li": markdownListItem,
|
||||
"strong": symmetricMarkdownElement("__"),
|
||||
"em": symmetricMarkdownElement("*"),
|
||||
"a": markdownLink,
|
||||
"img": markdownImage
|
||||
};
|
||||
|
||||
(function() {
|
||||
for (var i = 1; i <= 6; i++) {
|
||||
htmlToMarkdown["h" + i] = markdownElement(repeatString("#", i) + " ", "\n\n");
|
||||
}
|
||||
})();
|
||||
|
||||
function repeatString(value, count) {
|
||||
return new Array(count + 1).join(value);
|
||||
}
|
||||
|
||||
function markdownWriter() {
|
||||
var fragments = [];
|
||||
var elementStack = [];
|
||||
var list = null;
|
||||
var listItem = {};
|
||||
|
||||
function open(tagName, attributes) {
|
||||
attributes = attributes || {};
|
||||
|
||||
var createElement = htmlToMarkdown[tagName] || function() {
|
||||
return {};
|
||||
};
|
||||
var element = createElement(attributes, list, listItem);
|
||||
elementStack.push({end: element.end, list: list});
|
||||
|
||||
if (element.list) {
|
||||
list = element.list;
|
||||
}
|
||||
|
||||
var anchorBeforeStart = element.anchorPosition === "before";
|
||||
if (anchorBeforeStart) {
|
||||
writeAnchor(attributes);
|
||||
}
|
||||
|
||||
fragments.push(element.start || "");
|
||||
if (!anchorBeforeStart) {
|
||||
writeAnchor(attributes);
|
||||
}
|
||||
}
|
||||
|
||||
function writeAnchor(attributes) {
|
||||
if (attributes.id) {
|
||||
var writer = htmlWriter.writer();
|
||||
writer.open("a", {id: attributes.id});
|
||||
writer.close("a");
|
||||
fragments.push(writer.asString());
|
||||
}
|
||||
}
|
||||
|
||||
function close(tagName) {
|
||||
var element = elementStack.pop();
|
||||
list = element.list;
|
||||
var end = _.isFunction(element.end) ? element.end() : element.end;
|
||||
fragments.push(end || "");
|
||||
}
|
||||
|
||||
function selfClosing(tagName, attributes) {
|
||||
open(tagName, attributes);
|
||||
close(tagName);
|
||||
}
|
||||
|
||||
function text(value) {
|
||||
fragments.push(escapeMarkdown(value));
|
||||
}
|
||||
|
||||
function asString() {
|
||||
return fragments.join("");
|
||||
}
|
||||
|
||||
return {
|
||||
asString: asString,
|
||||
open: open,
|
||||
close: close,
|
||||
text: text,
|
||||
selfClosing: selfClosing
|
||||
};
|
||||
}
|
||||
|
||||
exports.writer = markdownWriter;
|
||||
|
||||
function escapeMarkdown(value) {
|
||||
return value
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/([\`\*_\{\}\[\]\(\)\#\+\-\.\!])/g, '\\$1');
|
||||
}
|
||||
Reference in new issue
Block a user