Skip to content

Instantly share code, notes, and snippets.

@mikeumus
Created August 7, 2013 21:29
Show Gist options
  • Save mikeumus/6178907 to your computer and use it in GitHub Desktop.
Save mikeumus/6178907 to your computer and use it in GitHub Desktop.
docs.stardog.com -> footnotes.plugin.js (formerly footnotes.plugin.coffee. Translated via http://js2coffee.org/)
var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
module.exports = function(BasePlugin) {
var FootnotesPlugin, cheerio, fs, path, util;
cheerio = require("cheerio");
path = require('path');
fs = require('fs');
util = require('util');
return FootnotesPlugin = (function(_super) {
var FN_ANCHOR_MASK, FN_CLASS, FN_CONTAINER, FN_ID_MASK, FN_SUP, RETURN_MARKER, processDocStructure, processFootnotes;
__extends(FootnotesPlugin, _super);
function FootnotesPlugin() {
return FootnotesPlugin.__super__.constructor.apply(this, arguments);
}
FootnotesPlugin.prototype.name = 'footnotes';
FN_CONTAINER = "#fn";
RETURN_MARKER = "↪";
FN_ID_MASK = "fn-ref-";
FN_ANCHOR_MASK = "footnote-";
FN_SUP = true;
FN_CLASS = "footnote";
processFootnotes = function(content) {
var $;
$ = cheerio.load(content.attributes.contentRendered);
$("fn").each(function(index, elem) {
var marked, sequence_marker1, sequence_marker2, target1, target2, theBigLink, theLink, theNum, theNumber, theReturn, theSup, theText;
theNumber = index + 1;
theLink = $("<a></a>").attr({
id: FN_ID_MASK + theNumber,
href: "#" + FN_ANCHOR_MASK + theNumber,
"class": "skip fn-marker",
"gumby-easing": "easeOutQuad",
"gumby-duration": "400",
"gumby-goto": "#" + FN_ANCHOR_MASK + theNumber
});
theNum = $("<span class='ref-mark'></span>").text(theNumber);
sequence_marker1 = theNumber;
sequence_marker2 = theNumber + ". ";
if (FN_SUP) {
theSup = $("<sup></sup>");
theSup.append(sequence_marker1);
theLink.append(theSup);
} else {
theLink.append(theNum);
}
$(this).replaceWith(theLink);
target1 = FN_ANCHOR_MASK + theNumber;
target2 = "#" + FN_ID_MASK + theNumber;
theReturn = $("<a></a>").attr({
"class": "skip fn-marker",
"gumby-easing": "easeOutQuad",
"gumby-duration": "400",
"gumby-goto": target2
}).html("&nbsp;" + RETURN_MARKER);
marked = require('marked');
theText = cheerio.load(marked($(this).html()))("p");
theText.prepend(sequence_marker2).append(theReturn);
theBigLink = $("<div></div>").attr({
"class": FN_CLASS,
id: target1
}).prepend(theText);
$("#footnote-container").append(theBigLink);
if (index >= 0) {
return $("#footnote-container").attr({
"class": "",
style: "display: block;"
});
}
});
return $.html();
};
processDocStructure = function(document) {
var $, fileOut, m, outputFileName, outputPath, slug, toc;
$ = cheerio.load(document.attributes.contentRendered);
toc = $("<ol></ol>").attr({
id: "",
"class": "chapter-toc"
});
outputPath = docpad.getConfig().srcPath + "/partials/";
outputFileName = document.attributes.name;
fileOut = outputPath + outputFileName;
slug = document.attributes.slug;
$('#mdblock > h2').each(function(index, elem) {
var id, name, theLink, txt;
txt = $(this).text();
id = $(this).attr("id");
name = "h2";
theLink = $("<a>" + txt + "</a>").attr({
href: "/" + slug + "/#" + id
});
return toc.append($("<li></li>").append(theLink));
});
m = "---\ncacheable: false\n---\n\n";
return fs.writeFile(fileOut, m + cheerio.load(toc).html(), function(err) {
if (err) {
return typeof next === "function" ? next(err) : void 0;
}
return docpad.log("debug", "Wrote the " + outputFileName + " partial to: " + outputPath);
});
};
FootnotesPlugin.prototype.writeBefore = function(opts, next) {
var config, defaultConfig, docpad, me, templateData;
me = this;
docpad = this.docpad;
defaultConfig = this.defaultConfig;
config = this.config;
templateData = docpad.getTemplateData();
docpad.getCollection('html').sortCollection({
date: 9
}).forEach(function(document) {
document.attributes.contentRendered = processFootnotes(document);
return processDocStructure(document);
});
return next();
};
return FootnotesPlugin;
})(BasePlugin);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment