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(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"');//Strip out curly quotes
html=html.replace(/\u2026/gi,'&hellip;');//Replace ellipses
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,'');
var subhtml;
var count=0;
while(subhtml=/<p>\s*\$START FLASHBACK\$\s*<\/p>([^]*?)<p>\s*\$END FLASHBACK\$\s*<\/p>/i.exec(html)){
if(subhtml){
subhtml=subhtml[1];
subhtml=subhtml.replace(/<p>([^]*?)<\/p>/gi,"<p class=\"flashback\">$1</p>");
html=html.replace(/<p>\s*\$START FLASHBACK\$\s*<\/p>([^]*?)<p>\s*\$END FLASHBACK\$\s*<\/p>/i,subhtml);
count++;
if(count>1000){
alert("Possible infinite loop detected, script aborted. Contact Rilbur for assistance.");
return;
}
}
}
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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=windows-1252">
<TITLE></TITLE>
<META NAME="GENERATOR" CONTENT="LibreOffice 4.1.5.3 (Windows)">
<META NAME="AUTHOR" CONTENT="Ronald Lugge">
<META NAME="CREATED" CONTENT="20140227;190848198000000">
<META NAME="CHANGEDBY" CONTENT="Ronald Lugge">
<META NAME="CHANGED" CONTENT="20140227;194914683000000">
<STYLE TYPE="text/css">
<!--
@page { margin: 0.79in }
P { margin-bottom: 0.08in }
A:link { so-language: zxx }
-->
</STYLE>
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<P STYLE="margin-bottom: 0in">:: This is a transmogrifier test
document</P>
<P STYLE="margin-bottom: 0in">:::Produced by Rilbur, for the use of
the RU</P>
<P STYLE="margin-bottom: 0in">##Not for distribution</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">I am the very model of a major brittish
general</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$START NEWSPAPER$</P>
<P STYLE="margin-bottom: 0in">Extra, extra! Zacky's Transmog being
replaces!</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">Rilbur Skryler, programmer
extroardinaire, has stepped up to try and fix what has gone wrong
with the current situation.</P>
<P STYLE="margin-bottom: 0in">Zacky's disappearance, while
potentially disasterous, can still be dealt with.</P>
<P STYLE="margin-bottom: 0in">$END NEWSPAPER$</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$START FLASHBACK$</P>
<P STYLE="margin-bottom: 0in">It all began many years ago...,</P>
<P STYLE="margin-bottom: 0in">$END FLASHBACK$</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$START LETTER$</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">Dear Roland,</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">I've done the best I can, but this is
all I can do.</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">Rilbur</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$END LETTER$</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$START MUSIC$</P>
<P STYLE="margin-bottom: 0in">Oh fuck,</P>
<P STYLE="margin-bottom: 0in">Now I'm stuck,</P>
<P STYLE="margin-bottom: 0in">Doing Zacky's Job,</P>
<P STYLE="margin-bottom: 0in">Of fixing others not learning,</P>
<P STYLE="margin-bottom: 0in">How the hell to do theirs.</P>
<P STYLE="margin-bottom: 0in">$END MUSIC$</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$START NOTE$<BR>No clue what I'm doing
here, people!</P>
<P STYLE="margin-bottom: 0in">$END NOTE$</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P STYLE="margin-bottom: 0in">$START RECORDING$</P>
<P STYLE="margin-bottom: 0in">Rilbur: Done?</P>
<P STYLE="margin-bottom: 0in">Roland: Maybe.</P>
<P STYLE="margin-bottom: 0in">Rilbur: Hurry it up... we need to get
this transmogrifier out fast!</P>
<P STYLE="margin-bottom: 0in">$END RECORDING$</P>
</BODY>
</HTML>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment