Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 247 lines (214 sloc) 7.06 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Code Highlighter 0.2</title>
<script type="text/javascript" src="CodeHighlighter.js"></script>
<script type="text/javascript" src="javascript.js"></script>
<script type="text/javascript" src="css.js"></script>
<script type="text/javascript" src="html.js"></script>
<style type="text/css">
body {
background : #EEE;
font-family: Arial, Helvetica, sans-serif;
margin : 3em 5em;
line-height : 1.5;
font-size: 80%;
}
pre {
border: 1px solid black;
border-color: #BBB #DDD #DDD #BBB;
margin : 2em;
padding : 1em;
line-height: 1.5;
background: white;
overflow: auto;
}
code {
font-size: 1.2em;
}
h1 span {
font-size : 0.5em;
}
a {
color : gray;
}
/* end of page css - below is for the code highlighter */
.javascript .comment {
color : green;
}
.javascript .string {
color : teal;
}
.javascript .keywords {
color : navy;
}
.javascript .global {
color : blue;
}
.javascript .brackets {
color : navy;
}
.css .comment {
color : gray;
}
.css .properties {
color : navy;
}
.css .selectors {
color : maroon;
font-weight : bold;
}
.css .units {
color :red;
}
.css .urls {
color :green;
}
.html .tag {
color : purple;
}
.html .comment {
color : gray;
font-style: italic;
}
.html .string {
color : navy;
}
.html .doctype {
color : teal;
}
</style>
</head>
<body>
<h1>CodeHighlighter 0.2 <span>by <a href="http://www.danwebb.net">Dan Webb</a></span></h1>
<p>CodeHighlighter is a lightweight, unobstrusive and fully configurable script for displaying code examples highlighted in a way similar to the way many text editors highlight code. It weighs in at just under 4K, allows users to configure their own style sets so that you can highlight any language you like and is deployable simply by attaching it to a page with the script tag and adding class names as hooks. It should also play nicely with any other scripts on your page as it has a tiny footprint on the global namespace.</p>
<p>Many thanks to <a href="http://dean.edwards.name">Dean Edwards</a> who's star-light behaviour inspired this.</p>
<h3>Deploying the script</h3>
<ol>
<li>Add a &lt;script&gt; tag for <a href="CodeHighlighter.js">CodeHighlighter.js</a> and a script tag for each of the code types you want to highlight on your page. At this time there are only very basic style sets for <a href="javascript.js">JavaScript</a>, <a href="css.js">CSS</a> and <a href="html.js">HTML</a>. But it's easy to make your own for any language you like if you know regular expressions. Let me know if you do and I'll link to them.</li>
<li>Add an appropriate class to each &lt;pre&gt; element that contains code. 'javascript', 'css' or 'html' will do the trick.</li>
<li>Define CSS styles for each code element, the script simply parses the code and wraps a &lt;span&gt; tag around each element with the appropriate class name. You just need to write CSS to style the code the way you want. No programming or weirdness required. See the source of this code as an example.</li>
</ol>
<h3>Testing</h3>
<p>Known to work on:</p>
<ul>
<li>IE 5.5+ PC</li>
<li>Firefox 1.0/Mozilla 1.3 + PC</li>
<li>Opera 7.23 + PC</li>
<li>Netscape 7.2 PC</li>
</ul>
<p>Known to degrade well on:</p>
<ul>
<li>IE 5.0 PC</li>
</ul>
<p>Any other feedback for any other browser would be greatly apprieciated. Please email Dan Webb at dan[at]danwebb[dot]net. Have a look through the small examples below.</p>
<h3>Creating your own style sets for other languages</h3>
<p>Have a look at this <a href="stylesetguide.htm">guide to creating style sets</a>.</p>
<p>Cheers,<br /><strong>Dan</strong></p>
<h2>Inline code</h2>
<p>Hopefully, you should be able to put some code inline like this: <code class="javascript">document.write("bong")</code> and hopefully it should work.</p>
<h2>JavaScript Example</h2>
<pre><code class="javascript">/*
This script detects external links in a page
and attaches a behaviour to them so they open
in a external window.
*/
function initialiseLinks() {
if (document.getElementsByTagName) {
var links = document.getElementsByTagName(&quot;A&quot;);
for (var i = 0; i &lt; links.length; i++) {
if (links[i].href.indexOf(&quot;http:&quot;)==0) {
// if the links URL starts with http: then we assume it's an external link
links[i].onclick = function() {
window.open(this.href);
return false; // stop normal link behaviour
}
}
}
}
}
window.onload = initialiseLinks();</code></pre>
<h2>CSS Example</h2>
<pre><code class="css">.javascript .comment {
color : green; /* ffbgffg */
}
.javascript .string {
color : maroon;
}
.javascript .keywords {
font-weight : bold;
}
.javascript .global {
color : blue;
font-weight: bolder;
}
.javascript .brackets {
color : Gray;
}
.javascript .thing {
font-size : 10px;
background : url(ghgfhfg gh f.rtjhf);
}</code></pre>
<h2>HTML Example</h2>
<pre><code class="javascript">
function suckerfish(type, tag, parentId) {
if (window.attachEvent) {
window.attachEvent("onload", function() {
var sfEls = (parentId==null)?document.getElementsByTagName(tag):document.getElementById(parentId).getElementsByTagName(tag);
type(sfEls);
});
}
}
sfHover = function(sfEls) {
for (var i=0; i&lt;sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=" sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
sfFocus = function(sfEls) {
for (var i=0; i&lt;sfEls.length; i++) {
sfEls[i].onfocus=function() {
this.className+=" sffocus";
}
sfEls[i].onblur=function() {
this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
}
}
}
sfActive = function(sfEls) {
for (var i=0; i&lt;sfEls.length; i++) {
sfEls[i].onmousedown=function() {
this.className+=" sfactive";
}
sfEls[i].onmouseup=function() {
this.className=this.className.replace(new RegExp(" sfactive\\b"), "");
}
}
}
sfTarget = function(sfEls) {
var aEls = document.getElementsByTagName("A");
document.lastTarget = null;
for (var i=0; i&lt;sfEls.length; i++) {
if (sfEls[i].id) {
if (location.hash==("#" + sfEls[i].id)) {
sfEls[i].className+=" sftarget";
document.lastTarget=sfEls[i];
}
for (var j=0; j&lt;aEls.length; j++) {
if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i];
aEls[j].onclick = function() {
if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
if (this.targetEl) this.targetEl.className+=" sftarget";
document.lastTarget=this.targetEl;
return true;
}
}
}
}
}</code></pre>
</body>
</html>