Skip to content

Instantly share code, notes, and snippets.

@gebn
Created June 9, 2014 00:27
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 gebn/4447a77c35b9e1989e3b to your computer and use it in GitHub Desktop.
Save gebn/4447a77c35b9e1989e3b to your computer and use it in GitHub Desktop.
Formats a code listing for Peerwise.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PeerWise Code Formatter</title>
<script type="text/javascript">
(function() {
"use strict";
var tagsToReplace = {
' ': '&nbsp;',
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
};
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