Created
May 10, 2012 14:46
-
-
Save makotom/2653634 to your computer and use it in GitHub Desktop.
JS to format academic papers - aiming to substitute TeX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener("DOMContentLoaded", function(){ | |
"use strict"; | |
var f, e, p, s, i = 0, n = 0; | |
f = document.createElement("style"); | |
f.appendChild(document.createTextNode("\n")); | |
e = document.getElementsByTagName("section"); | |
for(i = 0; i < e.length; i += 1){ | |
p = e[i].parentNode; | |
s = e[i].previousElementSibling; | |
while(s !== null && s.tagName.toLowerCase() !== "section"){ s = s.previousElementSibling; } | |
if(s === null){ | |
while(!p.tagName.match(/^(section|body)$/i)){ p = p.parentNode; } | |
if(p.tagName.toLowerCase() === "body"){ | |
e[i].id = "section-1"; | |
}else{ | |
e[i].id = p.id + ".1"; | |
} | |
}else{ | |
e[i].id = s.id.replace(/\d+$/, "") + (parseInt(s.id.match(/\d+$/)[0], 10) + 1).toString(); | |
} | |
f.appendChild(document.createTextNode("#" + e[i].id.replace(/\./g, "\\.") + " > *:first-child:before{ content: \"" + e[i].id.replace(/^section-/, "") + " \"; }\n")); | |
} | |
e = document.getElementsByTagName("figure"); | |
for(i = 0; i < e.length; i += 1){ | |
e[i].id = "figure-" + (i + 1).toString(); | |
f.appendChild(document.createTextNode("#" + e[i].id + " > figcaption:before{ content: \"Figure " + e[i].id.replace(/^figure-/, "") + ": \"; }\n")); | |
} | |
e = document.getElementsByTagName("math"); | |
for(i = 0; i < e.length; i += 1){ | |
if(e[i].parentNode.className.match(/(?:^| )formula-skip(?: |$)/)){ | |
continue; | |
} | |
n += 1; | |
e[i].parentNode.id = "formula-" + n.toString(); | |
f.appendChild(document.createTextNode("#" + e[i].parentNode.id + ":after{ float: right; line-height: " + getComputedStyle(e[i].parentNode, null).height + "; content: \" (" + e[i].parentNode.id.replace(/^formula-/, "") + ")\"; }\n")); | |
} | |
e = document.getElementsByTagName("table"); | |
for(i = 0; i < e.length; i += 1){ | |
e[i].id = "table-" + (i + 1).toString(); | |
f.appendChild(document.createTextNode("#" + e[i].id + " > caption:before{ content: \"Table " + e[i].id.replace(/^table-/, "") + ": \"; }\n")); | |
} | |
document.documentElement.appendChild(f); | |
f = document.createElement("style"); | |
f.appendChild(document.createTextNode("\n" + | |
"body{ width: 720px; margin-left: auto; margin-right: auto; }\n" + | |
"h1, #author{ font-weight: bold; text-align: center; }\n" + | |
"section p{ text-indent: 1em; }\n" + | |
"ol, ul{ margin: 0; }\n" + | |
"figure, .formula-skip, [id^=\"formula-\"]{ text-align: center; }\n" + | |
"table{ margin: auto; border: solid black; border-collapse: collapse; }\n" + | |
"th, td{ border: solid black; }\n" | |
)); | |
document.documentElement.appendChild(f); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment