Skip to content

Instantly share code, notes, and snippets.

@dergachev
Created December 17, 2012 20:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dergachev/4321959 to your computer and use it in GitHub Desktop.
Save dergachev/4321959 to your computer and use it in GitHub Desktop.
Manual JS prettification of enterprisewiki.co. See http://news.ycombinator.com/item?id=4933049
// https://github.com/EightMedia/hammer.js/blob/master/hammer.js
function toId(t) {
return t.toLowerCase().replace(/[^a-z]/gi, "-").replace(/-+/gi, "-")
}
function toc() {
var t = $("#sidebar").html("");
$("h1,h2,h3").each(function() {
var e = $(this).clone(), n = $(this).offset().top;
"#" != e.html() && (e.is("h3") && e.html("‐ " + e.html()), t.append(e), e.on("click", function() {
$("body").animate({scrollTop: n}, 200)
}))
})
}
// https://github.com/EightMedia/hammer.js/blob/master/jquery.hammer.js
// http://code.google.com/p/rangyinputs/source/browse/trunk/jquery_plugin.js
// https://github.com/jeresig/jquery.hotkeys/blob/master/jquery.hotkeys.js
// ELASTIC
// possibly vaguely inspired by http://code.google.com/p/jquery-elastic/source/browse/trunk/jquery.elastic.source.js
$.fn.elastic = function(t) {
var e = {rows: 6,onHeightChange: function() {
}};
return t && $.extend(e, t), this.each(function() {
var t = $(this);
return t.data("elastic") ? t.data("elastic").update() : t.data("elastic", $.fn.elastic.newInstance(t, e)), t
})
},
$.fn.elastic.newInstance = function(t) {
function e() {
var e = t.val().replace(/&/g, "&amp;").replace(/ /g, "&nbsp;").replace(/<|>/g, "&gt;").replace(/\n/g, "<br />") + "&nbsp;";
if (e != n.html()) {
var i = n.show().width(t.width()).html(e).height();
n.hide();
var r = 1 + Math.ceil(i / a);
r >= s ? t.css("overflow", "auto").attr("rows", s) : o >= r ? t.css("overflow", "hidden").attr("rows", o) : t.css("overflow", "hidden").attr("rows", r)
}
}
if (t.is("textarea")) {
t.parent().find(".elastic-clone").remove();
for (var n =
$('<div class="elastic-clone"></div>')
.insertAfter(t)
.css({position: "absolute",display: "block","word-wrap": "break-word"}),
i = ["padding-top", "padding-right", "padding-bottom", "padding-left", "font-size", "line-height", "font-family", "font-weight"],
r = 0; i.length > r; r++)
n.css("" + i[r], t.css("" + i[r]));
var a = parseInt(t.css("line-height"), 10) || parseInt(t.css("font-size"), 10), o = parseInt(t.attr("rows"), 10), s = parseInt(t.attr("data-max-rows"), 999999999999);
return t.css("overflow", "hidden").on("keyup", e).on("change", e).on("paste", function() {
setTimeout(e, 100)
}), "none" !== t.css("display") && e(), {update: e,destroy: function() {
t.off("keyup"), t.off("paste")
}}
}
};
// END OF ELASTIC
// http://code.google.com/p/webapptools/source/browse/trunk/watools/crawler-ui/chrome/content/prettify/prettify.js?r=380
// -- the above minified links definitely includes http://code.google.com/p/google-code-prettify/source/browse/trunk/js-modules/prettify.js
// https://github.com/coreyti/showdown/blob/master/src/showdown.js
// https://github.com/coreyti/showdown/blob/master/src/extensions/table.js
// https://github.com/coreyti/showdown/blob/master/src/extensions/prettify.js
// https://github.com/coreyti/showdown/blob/master/src/extensions/github.js
// http://sharejs.org/channel/bcsocket.js
// http://sharejs.org/share/share.js
// for bcsocket source, see https://github.com/josephg/node-browserchannel/blob/master/lib/bcsocket.coffee
// for shareJs source, see https://github.com/josephg/ShareJS/blob/master/src/client/
// see http://sharejs.org/pad/pad.html#14b3gfm for a demo using share.js and bcsocket.js
// http://sharejs.org/share/textarea.js
$(function() {
var t = new Showdown.converter({extensions: ["prettify", "github", "table"]}),
e = $("#editor"),
n = $("#doc"),
i = $("#wiki-template").html(),
r = $("#sidebar"),
a = window.location.pathname.split("/")[2], o = function() {
return e.is(":visible") ? (e.hide(), n.css("visibility", "visible")) : (n.css("visibility", "hidden"), e.show()), !1
},
s = "",
c = function(i) {
var r = e.val();
if (!(1 > r.length || r == s)) {
s = r;
var a = t.makeHtml(r);
return n.html(a), toc(), prettyPrint(), e.elastic(), i.preventDefault(), !1
}
};
e.elastic(),
$(document).bind("keydown", "esc", o),
$(document).bind("keydown", "ctrl+e", o),
e.bind("keydown", "esc", o),
e.bind("keydown", "ctrl+e", o),
e.bind("keydown", "tab", function() {
var t = e.getSelection();
if (!t)
return !1;
if (1 > t.length)
e.insertText(" ", t.start, !0);
else {
var n = " " + t.text.replace(/\n/m, "\n ");
e.replaceSelectedText(n)
}
return !1
});
var h = 640 > $(window).width(), l = 1;
$(document.body).hammer(
{swipe: !0,drag: !0,css_hacks: !1,swipe_time: 1e3,swipe_min_distance: 100,transform: !1,tap: !1,tap_double: !1,hold: !1}
).bind("swipe", function(t) {
"left" == t.direction ? 1 == l ? (o(), l = 2) : 0 == l && (r.hide(), l = 1) : "right" == t.direction && (1 == l && h ? (r.show().one("click", function() {
r.hide(), l = 1
}), l = 0) : 2 == l && (o(), l = 1))
}), sharejs.open(a, "text", function(t, n) {
t ? (console.log(t), alert("An error has occurred, try refreshing!")) : (n.attach_textarea(e.get(0)), n.on("change", function() {
setTimeout(c, 100)
}), 1 > n.getText().length && (console.log("starting from template"), n.insert(0, i), e.val(i)), c())
})
});
// see http://enterprisewiki.co/bin/app.min.js