Okay, I've been working on the syntax highlighting (or "keyword colouring", more accurately) using the code linked above as a basis.
Here's a sample HTML page complete with the current JavaScript. Since I haven't yet figured out a way to call the function automatically, there's a button to call it:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<title>TED</title>
<script language="JavaScript" type="text/javascript">
function Syntaxify() {
var BodyHtml = document.getElementById("mainbody").innerText;
document.getElementById("mainbody").innerHTML = BodyHtml;
var curKeyw;
var curHex;
var keywords = ["only", "in"];
var keyHexes = ["ffff00", "ff00ff"];
for(i = 0; i <=1; i++)
{
curKeyw = keywords[i];
curHex = keyHexes[i];
var RegText = new RegExp(curKeyw,"gi");
var StyledText = "<font style=color:#"+curHex+">" + curKeyw + "</font>";
BodyHtml = BodyHtml.replace(RegText,StyledText);
}
document.getElementById("mainbody").innerHTML = BodyHtml
}
</SCRIPT>
<STYLE type=text/css>.canexpand { font-family: Courier New; font-weight:bold; text-align:center; color: #007FFF; }
.canexpand A:link { font-family: Courier New; color: #006AEC; }
.canexpand A:visited { font-family: Courier New; color: #006AEC; }
.canexpand A:hover { font-family: Courier New; color: #D8A6FF; }
.cancollapse { font-family: Courier New; font-weight:bold; text-align:center; color: #FFFFFF;}
.cancollapse A:link { font-family: Courier New; color: #FFFFFF; }
.cancollapse A:visited { font-family: Courier New; color: #FFFFFF; }
.cancollapse A:hover { font-family: Courier New; color: #D8A6FF; }
.main { font-family: Courier New; font-size: 15px; color: #FFFFFF; background: #000000 }
.box { font-family: Courier New; font-size:15px; color:#FFFFFF; background: #003652; border: 1px solid #00A8FF; }
.generaltext { font-family: Courier New; font-weight:bold; color: #FFFFFF; background: #000000; border: 1px solid #00527c;}
.generaltext A:link { font-family: Courier New; color: #FFFFFF; }
.generaltext A:visited { font-family: Courier New; color: #FFFFFF; }
.generaltext A:hover { font-family: Courier New; color: #D8A6FF; }
</STYLE>
</HEAD>
<BODY class=main>
<TABLE width="99%" align=center>
<div id="mainbody" contentEditable=true>
<P>Initial thoughts about "t09a"...
<P><!--COL-->
<P align=center><SPAN class=canexpand contentEditable=false>-> <A href="">LINK: Second note</A> <-</SPAN><!--ENDCOL-->
<P>PureBasic is a programming language based on established BASIC rules. The key features of PureBasic are portability (Windows, AmigaOS and Linux are
currently fully supported), the production of very fast and highly optimized executables and, of course, the very simple BASIC syntax.
<P>PureBasic has been created for the beginner and expert alike. We have put a lot of effort into its realization to produce a fast, reliable and system
friendly language.
<P>
</div>
</TABLE>
<input type="button" value="Highlight" onclick="Syntaxify()" />
</BODY></HTML>
There are two problems with the JavaScript as it is:
1. It resets the caret position to the end of the document (I'm working on this)
2. It has to be triggered manually (I'm working on this)
3. It removes any HTML formatting internal to the text. (I can't work on this because I'm totally lost.)
For a start, linebreaks vanish. But so does the hyperlink styling. It seems that anything inside <> tags will be ignored, meaning the revised text has only the formatting applied to it by the JS function. The function takes the text as plain text and works from that.
I can see that this is good in that it prevents the same keywords from being perpetually wrapped in duplicate tags. However, as I say, it also means that all other HTML tagging is lost.
Can anyone think of a way to change the JavaScript such that it would fulfil both requirements (ie retain all HTML tags, except for previous coloring tags because they may be obsolete)?
I can think of one way, which would be to grab the HTML instead of the plain text (if that's possible), and then replace all instances of the color formatting tags with nothing, before doing the scan to reapply the tags where they are now needed. Seems a bit clumsy. I'll give it a go, see if it's possible.
Thanks for reading,
Seymour.