Created
June 9, 2014 00:27
-
-
Save gebn/4447a77c35b9e1989e3b to your computer and use it in GitHub Desktop.
Formats a code listing for Peerwise.
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>PeerWise Code Formatter</title> | |
<script type="text/javascript"> | |
(function() { | |
"use strict"; | |
var tagsToReplace = { | |
' ': ' ', | |
'&': '&', | |
'<': '<', | |
'>': '>' | |
}; | |
function htmlentities(string) { | |
return string.replace(/[ &<>]/g, function(tag) { | |
return tagsToReplace[tag] || tag; | |
}); | |
} | |
function format(code) { | |
var lines = code.trim().replace(/\r\n/g, "\n").replace(/\t/g, " ").split("\n"); | |
var output = "<p><span style=\"font-family: 'courier new', courier, monospace;\">"; | |
for(var i = 0; i < lines.length; i++) { | |
output += htmlentities(lines[i]); | |
output += "<br />"; | |
} | |
output += "</span></p>"; | |
return output; | |
} | |
function init() { | |
var form = document.getElementById("form"); | |
var textarea = document.getElementById("code"); | |
form.onsubmit = function() { | |
textarea.value = format(textarea.value); | |
return false; | |
}; | |
} | |
if(window.addEventListener) { | |
window.addEventListener("load", init, false); | |
} | |
else if(window.attachEvent) { | |
window.attachEvent("onload", init); | |
} | |
else { | |
document.addEventListener("load", init, false); | |
} | |
}()); | |
</script> | |
</head> | |
<body> | |
<h2>Instructions</h2> | |
<ol> | |
<li>Paste your code into the text area below.</li> | |
<li>Click <em>Format</em>.</li> | |
<li>Copy the generated HTML.</li> | |
<li>On PeerWise, click the HTML button to bring up the source editor.</li> | |
<li>Paste in the generated HTML.</li> | |
<li>Click <em>Update</em> at the bottom left of the source editor.</li> | |
</ol> | |
<form id="form"> | |
<textarea id="code" cols="100" rows="40"></textarea> | |
<input type="submit" value="Format"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment