Skip to content

Instantly share code, notes, and snippets.

@kaecy
Last active December 20, 2015 11:39
Show Gist options
  • Save kaecy/6124852 to your computer and use it in GitHub Desktop.
Save kaecy/6124852 to your computer and use it in GitHub Desktop.
Web app for entity conversion
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="description" content="">
<meta name="keywords" content="">
<script src="entity utils.js"></script>
<script src="entity embed-util.js"></script>
</head>
<body>
<script>
function initialize() {
var input = document.getElementById('input');
var output = document.getElementById('output');
input.value = teststring;
}
onload = initialize;
function encode() {
var entity_text = entatise(input.value);
output.value = entity_text;
}
function decode() {
var plain_text = unentatise(input.value);
output.value = plain_text;
}
function translate() {
var plain_text = translate_entaties(input.value);
output.value = plain_text;
}
</script>
<h1>Entity - String conversion</h1>
<p>Currently converts from a character string to entity string only.</p>
<div class="util">
<textarea cols="50" rows="10" id="input"></textarea>
<textarea cols="50" rows="10" id="output"></textarea>
<br>
<input type="button" value="Encode" onclick="encode()">
<input type="button" value="Dncode" onclick="decode()">
<input type="button" value="Translate" onclick="translate()">
</div>
</body>
</html>
function translate_entaties(stream) {
var store = "";
var entity = null;
var entity_begin;
var entity_end
var next_store_position = 0;
while (true)
{
entity_begin = stream.indexOf('&', next_store_position);
entity_end = stream.indexOf(';', entity_begin);
if (entity_begin == -1 || entity_end == -1)
break;
entity_end += 1;
store += stream.slice(next_store_position, entity_begin);
next_store_position = entity_end;
entity = stream.substring(entity_begin+2, entity_end);
store += String.fromCharCode(parseInt(entity));
}
if (next_store_position < stream.length)
store += stream.slice(next_store_position);
return store;
}
var teststring = "I'm bringing my sinking ship back to the shore.\
Start over, bring it back in.";
function unentatise(string) {
var store = "";
var entities = string.split(";");
var entity;
while (entity = entities.shift())
{
entity = entity.slice(2);
store += String.fromCharCode(parseInt(entity));
}
return store;
}
function entatise(string) {
var store = "";
var a = 0;
for (;a < string.length; ++a)
store += "&#" + string.charCodeAt(a).toString() + ";";
return store;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment