| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<!-- Note this code is pretty fugly, it needs to be rewritten --> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 |
<style> |
| 7 |
body { |
| 8 |
font-family: 'Verdana', sans-serif; |
| 9 |
color: #333; |
| 10 |
background-color: #f4f4f4; |
| 11 |
padding: 20px; |
| 12 |
font-size: 16px; |
| 13 |
width: 42em; |
| 14 |
} |
| 15 |
input[type="submit"] { |
| 16 |
font-size: 1.2rem; |
| 17 |
border-radius: 5px; |
| 18 |
border: 1px solid black; |
| 19 |
margin-top: 1ex; |
| 20 |
padding: 10px; |
| 21 |
} |
| 22 |
textarea { |
| 23 |
width: 100%; |
| 24 |
height: 22rem; |
| 25 |
font-family: monospace; |
| 26 |
} |
| 27 |
|
| 28 |
hr { |
| 29 |
border: 1px solid #999; |
| 30 |
margin: 0; |
| 31 |
} |
| 32 |
</style> |
| 33 |
<script type="module"> |
| 34 |
import ICAL from "https://unpkg.com/ical.js"; |
| 35 |
|
| 36 |
function stringError(e) { |
| 37 |
return "Error: " + e + |
| 38 |
("fileName" in e ? "\nFilename: " + e.fileName : "") + |
| 39 |
("lineNumber" in e ? "\nLine: " + e.lineNumber : "") + |
| 40 |
("stack" in e ? "\nStack: " + e.stack : ""); |
| 41 |
} |
| 42 |
|
| 43 |
window.validate = function validate() { |
| 44 |
let duration = document.getElementById("duration"); |
| 45 |
|
| 46 |
let data = document.getElementById("data"); |
| 47 |
|
| 48 |
let error = document.getElementById("error"); |
| 49 |
let jcalres = document.getElementById("jcal"); |
| 50 |
let icalres = document.getElementById("ical"); |
| 51 |
|
| 52 |
duration.textContent = ''; |
| 53 |
error.textContent = jcalres.textContent = icalres.textContent = ""; |
| 54 |
|
| 55 |
let beginTime = Date.now(); |
| 56 |
|
| 57 |
let jcal, ical, from; |
| 58 |
try { |
| 59 |
jcal = JSON.parse(data.value); |
| 60 |
from = "json"; |
| 61 |
} catch (jsonerr) { |
| 62 |
try { |
| 63 |
jcal = ICAL.parse(data.value); |
| 64 |
from = "ical"; |
| 65 |
} catch (icalerr) { |
| 66 |
error.textContent = "Couldn't parse as iCalendar:\n" + |
| 67 |
stringError(icalerr) + "\n\nNor as jCal:\n" + |
| 68 |
stringError(jsonerr); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if (from == "json") { |
| 73 |
ical = ICAL.stringify(jcal); |
| 74 |
jcal = ICAL.parse(ical); |
| 75 |
} else if (from == "ical") { |
| 76 |
ical = ICAL.stringify(jcal); |
| 77 |
} else { |
| 78 |
ical = jcal = null; |
| 79 |
} |
| 80 |
|
| 81 |
let totalRuntime = Date.now() - beginTime; |
| 82 |
duration.textContent = 'Total Runtime: ' + totalRuntime + ' ms'; |
| 83 |
|
| 84 |
try { |
| 85 |
jcalres.textContent = jcal ? JSON.stringify(jcal, null, " ") : ""; |
| 86 |
icalres.textContent = ical; |
| 87 |
document.getElementById("jcalbox").style.display = ""; |
| 88 |
document.getElementById("icalbox").style.display = ""; |
| 89 |
} catch (e) { |
| 90 |
error.textContent = stringError(e); |
| 91 |
} |
| 92 |
|
| 93 |
return false; |
| 94 |
}; |
| 95 |
</script> |
| 96 |
</head> |
| 97 |
<body> |
| 98 |
<form method="post" action="#" onsubmit="return validate()"> |
| 99 |
<h1>iCalendar / jCal Validator</h1> |
| 100 |
<p id="descr">This validator form takes either iCalendar or jCal data. iCalendar data will be parsed to jCal and re-serialized to iCalendar. Similarly, jCal data will be parsed to iCalendar and re-serialized to jCal.</p> |
| 101 |
<textarea id="data" rows="20" cols="80"></textarea><br/> |
| 102 |
<input type="submit" value="Validate" /><br/> |
| 103 |
<p id="duration"></p> |
| 104 |
<hr> |
| 105 |
<pre id="error"></pre> |
| 106 |
<div id="jcalbox" style="display: none"> |
| 107 |
<h2>jCal Representation</h2> |
| 108 |
<pre id="jcal"></pre> |
| 109 |
<hr> |
| 110 |
</div> |
| 111 |
<div id="icalbox" style="display: none"> |
| 112 |
<h2>iCalendar Representation</h2> |
| 113 |
<pre id="ical"></pre> |
| 114 |
</div> |
| 115 |
</form> |
| 116 |
</body> |
| 117 |
</html> |