Skip to content

Instantly share code, notes, and snippets.

@rlugge
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save rlugge/9311614 to your computer and use it in GitHub Desktop.

Select an option

Save rlugge/9311614 to your computer and use it in GitHub Desktop.
Javascript designed to transform Words 'save as HTML' into useable HTML; per request includes several pre-transform tags that can be used to create various resulting HTML outputs
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Transmogrifier</title>
<style type='text/css'>
textarea{
width:500px;
height:250px;
}
</style>
<script type='text/javascript'>
function convert(){
var input=document.getElementById('input');
var output=document.getElementById('output');
var html=input.value;
html=/<body.*?>([^]*?)<\/body.*?>/i.exec(html)[1];
html=html.replace(/<p.*?>/gi,'<p>');
html=html.replace(/<font.*?>/gi,'');
html=html.replace(/<\/font.*?>/gi,'');
html=html.replace(/<span.*?>/gi,'');
html=html.replace(/<\/span.*?>/gi,'');
html=html.replace(/\n/gi,' ');
html=html.replace(/\r/gi,' ');
html=html.replace(/\t/gi,' ');
html=html.replace(/<BR>/gi,'</p><p>');
html=html.replace(/<p>\s*<\/p>/gi,'');
html=html.replace(/<\/I><I>/gi,'');
html=html.replace(/<p>\s*<BR>\s*<\/P>/gi,'');
for (var i = 0; i<10; i++) {
html=html.replace(/[\s]+/gi,' ');
};
html=html.replace(/<p>\s*&lt;hr\/*&gt;\s*<\/p>/gi,'<hr>');
//Experimental transmogrifier syntax additions
html=html.replace(/<p>\s*::([^:]+?.*?)<\/p>/gi,'<p class="subsecttitle">$1</p>');
html=html.replace(/<p>\s*:::([^:]+?.*?)<\/p>/gi,'<p class="subsecttitle">$1</p>');
html=html.replace(/<p>\s*##([^]*?)<\/p>/gi,'');
html=html.replace(/<p>\s*\$START FLASHBACK\$\s*<\/p>([^]*?)<p>\s*\$END FLASHBACK\$\s*<\/p>/gi,"<div class='flashback'>\n\n$1</div>\n\n");
html=html.replace(/<p>\s*\$START LETTER\$\s*<\/p>([^]*?)<p>\s*\$END LETTER\$\s*<\/p>/gi,"<blockquote>$1</blockquote>");
html=html.replace(/<p>\s*\$START MUSIC\$\s*<\/p>([^]*?)<p>\s*\$END MUSIC\$\s*<\/p>/gi,"<div class='music'>$1</div>");
html=html.replace(/<p>\s*\$START NEWSPAPER\$\s*<\/p>\s*<p>([^]*?)<\/p>([^]*?)<p>\s*\$END NEWSPAPER\$\s*<\/p>/gi,"<blockquote class='newspaper'>\n\n<h1>$1</h1>$2</blockquote>\n\n");
html=html.replace(/<p>\s*\$START NOTE\$\s*<\/p>([^]*?)<p>\s*\$END NOTE\$\s*<\/p>/gi,"<div class='note'>$1</div>\n\n");
html=html.replace(/<p>\s*\$START RECORDING\$\s*<\/p>([^]*?)<p>\s*\$END RECORDING\$\s*<\/p>/gi,"<div class='recording'>$1</div>\n\n");
//Human readability transformations
html=html.replace(/<\/p>\s*/gi,'</p>\n\n');
output.value=html;
}
</script>
</head>
<body>
<h1>Input</h1>
<p>Use 'save as HTML' on a word doc, open the resulting file in a plain text editor, and then copy and paste the contents of the file into the area below.</p>
<textarea id="input"></textarea><br>
<h2>Output</h2>
<p>Hit the convert button to automatically clean up the HTML and run some basic transmogrifier conversions. The resulting text should be safe to paste into Wordpress's text editor (DO NOT USE VISUAL EDITOR). Please remember to eyeball the results before calling it final, as additional changes may be needed.</p>
<button onclick='convert()'>Convert</button><br>
<textarea id="output"></textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment