Skip to content

Instantly share code, notes, and snippets.

@foolip
Last active September 13, 2016 15:29
Show Gist options
  • Save foolip/f60a1614fb5d194aad4dbff89c54ceb3 to your computer and use it in GitHub Desktop.
Save foolip/f60a1614fb5d194aad4dbff89c54ceb3 to your computer and use it in GitHub Desktop.
Tool to convert Fullscreen from Anolis to Bikeshed
<meta charset=utf-8>
<textarea style="width: 100%; height:100%"></textarea>
<script>
function stripIntro(doc) {
while (doc.body.firstChild.id != 'conformance')
doc.body.firstChild.remove();
}
function anolisToBikeshed(doc) {
// remove all internal markup from the IDL block
var idl = doc.body.querySelector('pre.idl');
idl.textContent = idl.textContent;
// loop over and transform all elements/attributes
const elements = [].slice.call(doc.body.querySelectorAll('*'));
for (let e of elements) {
const attributeNames = [].map.call(e.attributes, a => a.name);
if (e.localName == 'span') {
// <span> => <a>
let a = doc.createElement('a');
for (let name of attributeNames)
a.setAttribute(name, e.getAttribute(name));
while (e.firstChild)
a.appendChild(e.firstChild);
e.parentNode.replaceChild(a, e);
e = a;
}
for (let name of attributeNames) {
const value = e.getAttribute(name);
switch (name) {
case 'data-anolis-spec':
// the <pre class=anchors> block replaces data-anolis-spec
e.removeAttribute(name);
break;
case 'data-anolis-ref':
// <span data-anolis-ref>CSS</span> => [[!CSS]]
var text = document.createTextNode('[[!' + e.textContent + ']]');
e.parentNode.replaceChild(text, e);
break;
case 'title':
if (value) {
if (e.localName == 'code') {
// wrap in <a>, Bikeshed doesn't autolink <code>
var a = doc.createElement('a');
e.parentNode.replaceChild(a, e);
a.appendChild(e);
} else {
if (value.startsWith('concept-')) {
// drop and fix with <pre class=link-defaults>
} else {
// title="foo-bar" -> lt="foo bar"
const ltValue = value.replace(/-/g, ' ');
// skip plural forms, Bikeshed understand
if (e.textContent != ltValue + 's')
e.setAttribute('lt', ltValue);
}
}
}
e.removeAttribute(name);
break;
}
}
}
}
function serialize(doc) {
var html = doc.body.innerHTML;
// strip </p> and similar
html = html.replace(/<\/(p|li|dd|dt|th|td|tr|thead|tbody)>/g, '');
// attr="" => attr
html = html.replace(/=""/g, '');
// &gt; => >
html = html.replace(/&gt;/g, '>');
// attr="no-whitespace" => attr=no-whitespace
html = html.replace(/="([^ ]+)"/g, '=$1');
return html;
}
document.querySelector('textarea').addEventListener('paste', event => {
setTimeout(() => {
const input = event.target.value;
var parser = new DOMParser();
var doc = parser.parseFromString(input, "text/html");
stripIntro(doc);
// if anolisToBikeshed() is skipped, there are no interesting differences
anolisToBikeshed(doc);
var output = serialize(doc);
event.target.value = output;
event.target.select();
}, 0);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment