Skip to content

Instantly share code, notes, and snippets.

@xles
Created August 19, 2012 23:09
Show Gist options
  • Save xles/3398421 to your computer and use it in GitHub Desktop.
Save xles/3398421 to your computer and use it in GitHub Desktop.
Text-file prettifier for my dropbox.
<!doctype html>
<html>
<head>
<title>Text prettifier</title>
<style media="all">
p {
font-size: 12pt;
text-indent: 2em;
line-height: 1.5em;
text-align: justify;
}
li p {
text-indent: 0;
}
h1 {
text-align: center;
text-transform: capitalize;
}
hr {
background-color: #EEE8D8;
width:75%;
margin-top: 1.5em;
margin-bottom: 1.5em;
}
blockquote {
margin:0;
padding:0 2em 0 4em;
border-left: 5px solid #EEE8D8;
}
</style>
<style media="screen">
html {
background-color: #EEE8D8;
height:100%;
}
a {
color: #999;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
body {
width: 640px;
padding: 40px;
padding-top: 10%;
padding-bottom: 25%;
margin: auto;
background-color: #FFF;
color: #333;
font-family: Georgia, Serif;
}
.copyright {
margin-top: 2em;
border-top: 1px solid #EEE8D8;
padding: 1em;
font-size:0.8em;
text-align: center;
}
</style>
<style media="print">
html, body {
padding:0;
margin:0;
width: 100%;
}
@page {
margin-left: 1in;
margin-right: 1in;
margin-top: 1.25in;
margin-bottom: 1.25in;
/* padding-top: 1in;
padding-bottom: 1in;
*/ color: #000;
background-color: #000;
font-family: Georgia, Serif;
}
a, .copyright {
display: none;
}
h2 {
page-break-after: avoid;
}
</style>
</head>
<body>
<p style="text-align:center">
<script>
document.write('Loading, please wait...');
</script>
<noscript>
Turn on JavaScript you schmuck.
</noscript>
</p>
<script src="https://raw.github.com/coreyti/showdown/master/compressed/showdown.js"></script>
<script>
function readFile(md)
{
md = typeof md !== 'undefined' ? md : true;
file = location.search.substring(1);
if (!file) {
document.body.innerHTML = 'No file specified.';
copyBlurb();
return false;
}
filename = file + '.txt';
xhr = new XMLHttpRequest();
xhr.open("GET", filename, true);
xhr.onreadystatechange = function() {
document.body.innerHTML = '';
if (xhr.readyState == 4) {
if (xhr.status == 404) {
err = '"' + filename + '" no such file.';
document.body.innerHTML = err;
} else {
if(md == true)
text = markdown(xhr.responseText);
else
text = nltop(xhr.responseText);
formatText(file, text);
}
}
copyBlurb();
}
xhr.send(null);
}
function formatText(file, text)
{
rawLink = document.createElement('a');
rawLink.href = file + '.txt';
rawLink.innerHTML = "View raw file.";
document.body.appendChild(rawLink);
document.body.appendChild(text);
article = document.body.getElementsByTagName('div')[0];
leFirstChild = article.firstChild;
while (leFirstChild.nodeType != 1)
leFirstChild = leFirstChild.nextSibling;
if (leFirstChild.nodeName != 'H1') {
header = document.createElement('h1');
header.innerHTML = file.replace('-', ' ');
document.title = header.innerHTML;
article.insertBefore(header, leFirstChild);
} else {
document.title = leFirstChild.innerHTML;
}
}
function markdown(str)
{
var converter = new Showdown.converter();
article = document.createElement('div');
article.innerHTML = converter.makeHtml(str);
return article;
}
function nltop(str)
{
str = str.replace(/\r\n/g,"\n");
str = str.replace(/\n\r/g,"\n");
str = str.replace(/\r/g, "\n");
str = str.split("\n\n");
article = document.createElement('div');
for (line in str) {
if (str[line]){
paragraph = document.createElement('p');
paragraph.innerHTML = str[line];
article.appendChild(paragraph);
}
}
return article;
}
function copyBlurb()
{
copy = document.createElement('div');
copy.className = 'copyright';
copy.innerHTML = 'Text prettifier &copy; xles 2012<br>'
+ 'Showdown markdown parser by John Fraser';
document.body.appendChild(copy);
}
readFile();
</script>
</body>
</html>
@xles
Copy link
Author

xles commented Aug 19, 2012

Showdown Markdown parser here: https://github.com/coreyti/showdown

Don't want to run markdown?
Call readFile(false); at the bottom rather than readFile(); and it will call my crap vanilla function rather than Showdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment