Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function forEach(array, action) {
- for (var i = 0; i < array.length; i++)
- action(array[i]);
- }
- function negate(func) {
- return function() {
- return !func.apply(null, arguments);
- };
- }
- function reduce(combine, base, array) {
- forEach(array, function (element) {
- base = combine(base, element);
- });
- return base;
- }
- function count(test, array) {
- return reduce(function(total, element) {
- return total + (test(element) ? 1 : 0);
- }, 0, array);
- }
- function splitParagraph(text) {
- function indexOrEnd(character) {
- var index = text.indexOf(character);
- return index == -1 ? text.length : index;
- }
- function takeNormal() {
- var end = reduce(Math.min, text.length,
- map(indexOrEnd, ["*", "{"]));
- var part = text.slice(0, end);
- text = text.slice(end);
- return part;
- }
- function takeUpTo(character) {
- var end = text.indexOf(character, 1);
- if (end == -1)
- throw new Error("Missing closing '" + character + "'");
- var part = text.slice(1, end);
- text = text.slice(end + 1);
- return part;
- }
- var fragments = [];
- while (text != "") {
- if (text.charAt(0) == "*")
- fragments.push({type: "emphasised",
- content: takeUpTo("*")});
- else if (text.charAt(0) == "{")
- fragments.push({type: "footnote",
- content: takeUpTo("}")});
- else
- fragments.push({type: "normal",
- content: takeNormal()});
- }
- return fragments;
- }
- function processParagraph(paragraph) {
- var header = 0;
- while (paragraph.charAt(0) == "%") {
- paragraph = paragraph.slice(1);
- header++;
- }
- return {type: (header == 0 ? "p" : "h" + header),
- content: splitParagraph(paragraph)};
- }
- function extractFootnotes(paragraphs) {
- var footnotes = [];
- var currentNote = 0;
- function replaceFootnote(fragment) {
- if (fragment.type == "footnote") {
- currentNote++;
- footnotes.push(fragment);
- fragment.number = currentNote;
- return {type: "reference", number: currentNote};
- }
- else {
- return fragment;
- }
- }
- forEach(paragraphs, function(paragraph) {
- paragraph.content = map(replaceFootnote,
- paragraph.content);
- });
- return footnotes;
- }
- function link(target, text) {
- return tag("a", [text], {href: target});
- }
- function htmlDoc(title, bodyContent) {
- return tag("html", [tag("head", [tag("title", [title])]),
- tag("body", bodyContent)]);
- }
- function escapeHTML(text) {
- var replacements = [[/&/g, "&"], [/"/g, """],
- [/</g, "<"], [/>/g, ">"]];
- forEach(replacements, function(replace) {
- text = text.replace(replace[0], replace[1]);
- });
- return text;
- }
- function renderHTML(element) {
- var pieces = [];
- function renderAttributes(attributes) {
- var result = [];
- if (attributes) {
- for (var name in attributes)
- result.push(" " + name + "="" +
- escapeHTML(attributes[name]) + """);
- }
- return result.join("");
- }
- function render(element) {
- // Text node
- if (typeof element == "string") {
- pieces.push(escapeHTML(element));
- }
- // Empty tag
- else if (!element.content || element.content.length == 0) {
- pieces.push("<" + element.name +
- renderAttributes(element.attributes) + "/>");
- }
- // Tag with content
- else {
- pieces.push("<" + element.name +
- renderAttributes(element.attributes) + ">");
- forEach(element.content, render);
- pieces.push("</" + element.name + ">");
- }
- }
- render(element);
- return pieces.join("");
- }
- function footnote(number) {
- return tag("sup", [link("#footnote" + number,
- String(number))]);
- }
- function renderFragment(fragment) {
- if (fragment.type == "reference")
- return footnote(fragment.number);
- else if (fragment.type == "emphasised")
- return tag("em", [fragment.content]);
- else if (fragment.type == "normal")
- return fragment.content;
- }
- function renderFootnote(footnote) {
- var anchor = tag("a", [], {name: "footnote" + footnote.number});
- var number = "[" + footnote.number + "] ";
- return tag("p", [tag("small", [anchor, number,
- footnote.content])]);
- }
- function tag(name, content, attributes) {
- return {name: name, attributes: attributes, content: content};
- }
- function renderParagraph(paragraph) {
- return tag(paragraph.type, map(renderFragment, paragraph.content));
- }
- function map(func, array) {
- var result = [];
- forEach(array, function (element) {
- result.push(func(element));
- });
- return result;
- }
- function renderFile(file, title) {
- var paragraphs = map(processParagraph, file.split("nn"));
- var footnotes = map(renderFootnote, extractFootnotes(paragraphs));
- var body = map(renderParagraph, paragraphs).concat(footnotes);
- return renderHTML(htmlDoc(title, body));
- }
- viewHTML(renderFile(recluseFile(), "The Book of Programming"));
Add Comment
Please, Sign In to add comment