Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Created October 26, 2014 04:59
Show Gist options
  • Save solidsnack/a41231c519a6c4198161 to your computer and use it in GitHub Desktop.
Save solidsnack/a41231c519a6c4198161 to your computer and use it in GitHub Desktop.
/** Format Dreamboard dreams as Markdown (and copy to clipboard);
*
* To use this file, navigate to https://dreamboard.com/dreams/ and open the
* Chrome or Firefox dev console. Paste in the whole file and your dreams
* will be copied to the clipboard in Markdown format (which can be processed
* any number of ways to make a printable document or presentable email).
*/
function Dream(title, timestamp, text) {
if (title instanceof HTMLElement) {
var $el = $(title);
var header = $el.find(".panel-title a div");
var bodyMarker = $el.find("span.listdreamstitle:contains('NARRATION')");
this.title = Dreams.topLevelText(header);
this.timestamp = header.find("span:first").text().split(",").join(" ");
this.text = Dreams.topLevelText(bodyMarker.parent());
} else {
this.title = title;
this.timestamp = timestamp;
this.text = text;
}
this.markdown = function () {
return [
"# `" + this.timestamp + "` _" + this.title + "_",
this.text // We assume the body text is legit Markdown
].join("\n");
}
}
var Dreams = {
all: function () {
return Dreams.allDivs().map(function (i, e) { return new Dream(e) ;} )
.sort(Dreams.dreamSort);
},
formatted: function (dreams) {
var dreams = typeof(dreams) !== "undefined" ? dreams : Dreams.all();
var text = $(dreams).map(function (i, dream) { return dream.markdown(); });
return $.makeArray(text).join("\n\n\n\n");
},
allDivs: function () {
return $("#listdreams > .panel-group:first > .panel");
},
// Remove all child-nodes and return remaining text (the text directly
// contained in the element; thus no <span/>s, <em/>s, &c.
topLevelText: function(jq) {
return jq.clone().children().remove().end().text();
},
dreamSort: function (a, b) {
if (a.timestamp < b.timestamp) return -1;
if (a.timestamp > b.timestamp) return 1;
return 0;
}
}
copy(Dreams.formatted()); // Undocumented function, available in dev console.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment