Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllThingsSmitty/89a6a1ea5d25fb56fd5d to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/89a6a1ea5d25fb56fd5d to your computer and use it in GitHub Desktop.
Correct title capitalization
// based on titlecapitalization.com
!function () {
function e(e) {
return e;
}
function t(e) {
var t = 0;
if (document.selection) {
e.focus();
var o = document.selection.createRange();
o.moveStart("character", -e.value.length), t = o.text.length
} else (e.selectionStart || "0" == e.selectionStart) && (t = e.selectionStart);
return t;
}
function o(e, t) {
if (e.setSelectionRange) e.focus(), e.setSelectionRange(t, t);
else if (e.createTextRange) {
var o = e.createTextRange();
o.collapse(!0), o.moveEnd("character", t), o.moveStart("character", t), o.select()
}
}
function n() {
var e = document.getElementById("main_input"),
t = document.getElementById("output");
t.style.left = 10.9 - e.scrollLeft + "px", t.style.top = 10.7 - e.scrollTop + "px", s !== e.value && a()
}
function a() {
var e = document.getElementById("main_input"),
n = document.getElementById("output"),
a = document.getElementById("notices");
n.style.position = "absolute", n.style.left = 10.9 - e.scrollLeft + "px", n.style.top = 10.7 - e.scrollTop + "px";
var i = t(e);
s = titleCaps(e.value), s !== e.value && (e.value = s), o(e, i), c = i;
if (u && n.removeChild(u), u = document.createElement("div"), u.style.fontWeight = "bold", "" === s) u.style.color = "rgba(0, 0, 0, 0.3)", u.innerHTML = "Type/Paste Your Title Here";
else {
var p = s;
p = p.replace(/([^a-zA-Z0-9_])/g, ",@@@$1,@@@");
for (var g = p.split(",@@@"), m = [], v = 0; v < g.length; v++) {
var h = g[v],
f = 0 === v || v === g.length - 1 || h.length >= l ? null : r[h.toLowerCase()],
w = document.createElement("div"),
b = document.createTextNode(h);
if (" " === h ? w.innerHTML = "&nbsp;" : w.appendChild(b), w.style.display = "inline-block", f) {
w.style.color = "gray", w.className = "ambiguous";
var y = document.createElement("div");
y.className = "tooltipAfter";
for (var C = [h + " Can Be Used in Multiple Ways:"].concat(["Do Capitalize"]).concat(f.doCapitalize).concat(["Don't Capitalize"]).concat(f.dontCapitalize), x = new RegExp(h, "i"), E = 0; E < C.length; E++) {
var z = document.createElement("div"),
I = C[E].match(x),
T = C[E].split(x);
I && (T[2] = T[1], T[1] = I[0]);
for (var A = 0; A < T.length; A++) {
var S = document.createElement("span");
S.textContent = T[A], 1 === A && (console.log("bold"), S.style.fontWeight = "bold", S.style.textDecoration = "underline", S.style.fontStyle = "italic"), z.appendChild(S)
}
y.appendChild(z)
}
d.push({
span: w,
token: h,
tooltip: y
})
} else w.style.color = "green";
m.push(w)
}
for (var R = 0; R < m.length; R++) u.appendChild(m[R])
}
for (n.appendChild(u); a.firstChild;) a.removeChild(a.firstChild);
var b
}
var i = 1,
l = 5;
window.localStorage && (l = window.localStorage.getItem("preferences") || 5), window.onload = function() {
var e = document.getElementById("preps_" + l);
e.checked = !0
}, window.radioSelect = function(e) {
var t = e.value;
l = parseInt(t, 10), window.localStorage && (window.localStorage.setItem("version", i), window.localStorage.setItem("preferences", l)), a()
};
var r = {
save: {
lowercase: !1,
doCapitalize: ["You Save the Day (Verb)"],
dontCapitalize: ["I Like All Fruits save Oranges (Preposition)"]
},
times: {
lowercase: !1,
doCapitalize: ["I Ran Eight Times around the Track (Noun)", "I Love Times Square (Part of a Proper Noun)", "Jim Times His Runs (Verb)"],
dontCapitalize: ["What Is Seven times Seven? (Preposition)"]
},
minus: {
lowercase: !0,
doCapitalize: ["I Received an A Minus in Math (Adjective)", "Eating Out Can Be a Minus to Your Health (Noun)"],
dontCapitalize: ["One minus One Is Zero (Preposition)"]
},
like: {
lowercase: !1,
doCapitalize: ["I Like You (Verb)"],
dontCapitalize: ["Run like the Wind (Preposition)"]
}
};
! function() {
function t(e) {
return e.toLowerCase()
}
function o(e) {
return e.substr(0, 1).toUpperCase() + e.substr(1)
}
var n = ["a", "abaft", "aboard", "about", "above", "absent", "across", "afore", "after", "against", "along", "alongside", "amid", "amidst", "among", "amongst", "an", "apropos", "apud", "around", "as", "aside", "astride", "at", "athwart", "atop", "barring", "before", "behind", "below", "beneath", "beside", "besides", "between", "beyond", "but", "by", "circa", "concerning", "despite", "down", "during", "except", "excluding", "failing", "following", "for", "from", "given", "in", "including", "inside", "into", "lest", "mid", "midst", "modulo", "near", "next", "notwithstanding", "of", "off", "on", "onto", "opposite", "out", "outside", "over", "pace", "past", "per", "plus", "pro", "qua", "regarding", "sans", "since", "than", "through", "thru", "throughout", "thruout", "till", "to", "toward", "towards", "under", "underneath", "unlike", "until", "unto", "up", "upon", "versus", "vs.", "vs", "v.", "v", "via", "vice", "with", "within", "without", "worth"],
a = ["a", "an", "the"],
i = ["and", "but", "for", "so", "nor", "or", "yet"],
s = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)",
u = n.concat(a).concat(i);
for (var c in r) {
var d = r[c];
d.lowercase && u.push(c)
}
u = "(" + u.join("|") + ")", window.titleCaps = function(n) {
var a = [],
i = /[:.;?!] |(?: |^)["Ò]|-/g,
r = 0;
for (n = n.replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"');;) {
var c = i.exec(n);
if (console.log("prefs", l), a.push(n.substring(r, c ? c.index : n.length).replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(e) {
return /[A-Za-z]\.[A-Za-z]/.test(e) ? e : o(e)
}).replace(RegExp("\\b" + u + "\\b", "ig"), t).replace(RegExp("\\b[A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z]+\\b", "ig"), 5 >= l ? o : e).replace(RegExp("\\b[A-Za-z][A-Za-z][A-Za-z][A-Za-z]+\\b", "ig"), 4 >= l ? o : e).replace(RegExp("^" + s + u + "\\b", "ig"), function(e, t, n) {
return t + o(n)
}).replace(RegExp("\\b" + u + s + "$", "ig"), o)), r = i.lastIndex, !c) break;
a.push(c[0])
}
return a.join("").replace(/ V(s?)\. /gi, " v$1. ").replace(/(['Õ])S\b/gi, "$1s").replace(/\b(AT&T|Q&A)\b/gi, function(e) {
return e.toUpperCase()
})
}
}(), window.expand = function() {
return window.the_rules.className = "animated", window.see_rules.style.display = "none", !1
};
var s = null,
u = null,
c = null,
d = [],
p = null,
g = null;
window.see_rules = document.getElementById("see_rules"), window.main_input = document.getElementById("main_input"), window.output = document.getElementById("output"), window.onmousemove = function(e) {
console.log("mouse move");
for (var t = e && e.x || p, o = e && e.y || g, n = 0; n < d.length; n++) {
var a = d[n];
if (rect = a.span.getBoundingClientRect(), t >= rect.left && t <= rect.right && o >= rect.top && o <= rect.bottom) {
a.span.className = "tooltip ambiguous", "tooltipAfter" !== !a.span.lastChild.className && a.span.appendChild(a.tooltip);
var i = a.tooltip.getBoundingClientRect();
a.span.removeChild(a.tooltip), a.tooltip.style.position = "fixed", a.tooltip.style.left = i.left + "px", a.tooltip.style.top = i.top + "px", a.currTooltip = a.tooltip, document.body.appendChild(a.currTooltip); {
document.createElement("div")
}
} else a.span.className = "ambiguous", a.currTooltip && (a.currTooltip.parentNode.removeChild(a.currTooltip), a.currTooltip = null)
}
}, window.the_rules = document.getElementById("the_rules"), window.result = document.getElementById("result"), window.checkmark = document.getElementById("checkmark"), window.show_image = null, window.last = "", a(), setInterval(n, 20);
var m = function() {};
main_input.onkeyup = m, main_input.onkeydown = n
}();
<input type="text" id="main_input">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment