Code Syntax

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1743 Views - Last Post: 06 December 2008 - 09:55 AM

#16 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Code Syntax

Posted 04 December 2008 - 08:47 AM

Yes. i would get headaches reading old code tag code. :^:

It's like being stuck in a eternal notepad hell.

This post has been edited by KYA: 04 December 2008 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#17 red_4900   User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: Code Syntax

Posted 04 December 2008 - 08:57 AM

Quote

but if a user posts just a portion of their code, the line numbers in their errors, don't line up with the line numbers in the [code] tags


At least it gave us rough idea where it went wrong. Especially useful in 100+ line codes. ;)

Quote

I know the line numbers were helpful, but does the benefit of having all code highlighted outweigh not having line numbers for right now?


As for me, I'm more into 'no'. I think cross-over between the new and the old one is best. The new code enables us to copy/paste directly without clicking the 'View plain' button, I love that. But I also want the line numbers feature. I want both. I know I'm selfish. Pardon me. :P
Was This Post Helpful? 0
  • +
  • -

#18 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: Code Syntax

Posted 04 December 2008 - 09:01 AM

I'll see what I can do about line numbers, but I think for the time being, we're going to stick with this solution.
Was This Post Helpful? 0
  • +
  • -

#19 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: Code Syntax

Posted 04 December 2008 - 07:28 PM

I have found one interesting issue with the script. It seems to only highlight the last code block in each post and leave the other ones in the post standard black.

Here is a link to one example of where I am seeing it (IE 7):
http://www.dreaminco...h...st&p=481649

As for the syntax highlighter I did write one a while back (it is still in beta, and I haven't tested it on anything nearly as large as DIC, but it seems to work pretty good, plus you can add extra languages to it and only need to give special regex for keywords that are found in the html tags (so it may prove to be hard to get HTML syntax highlighting to work with it)

Here it is if you want to use it, I don't care all tha tmuch:
<style>
.keyword{
  color: #66f;
}
.comment{
  color: #bbb;
}
.str{
  color: #0c0;
}
.digit{
  color: #f00;
}
.lineNums{
  float: left;
  background: #eee;
  padding: 0px 5px 0px 5px;
  border-top: 1px solid #99f;
  border-right: 1px solid #99f;
  margin: 0px;
}
.codeLines{
  display: inline;
  margin-left: -3px;
  width: 100%;
  overflow-x: scroll;
  overflow-y: visible;
  padding-left: 5px;
  border-top: 1px solid #99f;
  white-space: nowrap;
}
#sourceCode{
  border: 1px solid #99f;
  width: 500px;
}
</style>

<script>
var keywords = {
  none: {
	name: "Code",
	words: [],
	comments: {
	  find: [/(?!<)(\'|\")(.*?)(\'|\")/g],
	  rep: ["$1<span class='str'>[str]$2[str]</span>$3"]
	}
  },
  php: {
	name: "PHP",
	words: [
	  'and', 'xor', 'or', '_FILE_', 'exception', '_LINE_', 'array', 'as', 
	  'break', 'case', 'class', 'const', 'continue', 'declare', 'default', 
	  'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'eval', 
	  'exit', 'extends', 'foreach', 'for', 'function', 'global', 'if', 
	  'include', 'include_once', 'isset', 'list', 'new', 'print', 'require', 
	  'require_once', 'return', 'static', 'switch', 'unset', 'use', 'var', 
	  'while', '_FUNCTION_', '_CLASS_', '_METHOD_', 'final', 'php_user_filter', 
	  'interface', 'implements', 'instanceof', 'public', 'private', 'protected',
	  'abstract', 'clone', 'try', 'catch', 'throw', 'cfunction', 'old_function',
	  'this', 'final', '_NAMESPACE_', 'namespace', 'goto', '_DIR_'
	],
	comments: {
	  find: [
		/(?!<)(\'|\")(.*?)(\'|\")/g,
		/(\/\/)+(.*?)(?=<br>)/gi,
		/(\/\*)(.*?)(\*\/)/gi
	  ],
	  rep: [
		"$1<span class='str'>[str]$2[str]</span>$3",
		"<span class='comment'>[comment]$1$2[comment]</span>",
		"<span class='comment'>[comment]$1$2$3[comment]</span>"
	  ]
	}
  },
  java: {
	name: "Java",
	words: [
	  'abstract', 'assert', 'boolean', 'break', 'byte', 'case', 'catch', 'char',
	  'class', 'const', 'continue', 'default', 'do', 'double', 'else', 'enum',
	  'extends', 'finally', 'final', 'float', 'for', 'goto', 'if', 'implements',
	  'import', 'instanceof', 'int', 'interface', 'long', 'native', 'new',
	  'package', 'private', 'protected', 'public', 'return', 'short', 'static',
	  'strictfp', 'super', 'switch', 'synchronized', 'this', 'throws', 'throws',
	  'transient', 'try', 'void', 'volatile', 'while' 
	],
	comments: {
	  find: [
		/(?!<)(\'|\")(.*?)(\'|\")/g,
		/(\/\/)+(.*?)(?=<br>)/gi,
		/(\/\*)(.*?)(\*\/)/gi
	  ],
	  rep: [
		"$1<span class='str'>[str]$2[str]</span>$3",
		"<span class='comment'>[comment]$1$2[comment]</span>",
		"<span class='comment'>[comment]$1$2$3[comment]</span>"
	  ]
	}
  },
  as2: {
	name: "Actionscript 2.0",
	words: [
	  '_root'
	],
	comments: {
	  find: [
		/(?!<)(\'|\")(.*?)(\'|\")/g,
		/(\/\/)+(.*?)(?=<br>)/gi,
		/(\/\*)(.*?)(\*\/)/gi
	  ],
	  rep: [
		"$1<span class='str'>[str]$2[str]</span>$3",
		"<span class='comment'>[comment]$1$2[comment]</span>",
		"<span class='comment'>[comment]$1$2$3[comment]</span>"
	  ]
	}
  }, 
  cpp: {
	name: "C++",
	words: [
	  'asm', 'auto', 'bool', 'break', 'case', 'catch', 'char', 'class', 'cin', 
	  'const_cast', 'const', 'continue', 'cout', 'default', 'delete', 'double',
	  'do', 'dynamic_cast', 'else', 'endl', 'enum', 'explicit', 'export',
	  'extern', 'false', 'float', 'for', 'friend', 'goto', 'if', '#include',
	  'inline', 'int', 'long', 'mutable', 'namespace', 'new', 'operator',
	  'private', 'protected', 'public', 'register', 'reinterpret_cast',
	  'return', 'short', 'signed',  'sizeof', 'static_cast', 'static', 'struct',
	  'switch', 'template', 'this', 'throw', 'true', 'try', 'typedef', 'typeid',
	  'typename', 'union', 'unsigned', 'using', 'virtual', 'void', 'volatile',
	  'wchar_t', 'while'
	],
	comments: {
	  find: [
		/(?!<)(\'|\")(.*?)(\'|\")/g,
		/(\/\/)+(.*?)(?=<br>)/gi,
		/(\/\*)(.*?)(\*\/)/gi,
		/(#include[ |]&lt;)(.*?)(&gt;)/g
	  ],
	  rep: [
		"$1<span class='str'>[str]$2[str]</span>$3",
		"<span class='comment'>[comment]$1$2[comment]</span>",
		"<span class='comment'>[comment]$1$2$3[comment]</span>",
		"$1<span class='str'>$2</span>$3"
	  ]
	}
  }, 
  c: {
	name: "C",
	words: [
	  'auto', 'break', 'case', 'catch', 'char', 'const', 'continue', 'default',	   'double', 'do', 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if',
	  '#include', 'int', 'long', 'register', 'return', 'short', 'signed', 
	  'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'unsigned',
	  'void', 'volatile', 'while'
	],
	comments: {
	  find: [
		/(?!<)(\'|\")(.*?)(\'|\")/g,
		/(\/\/)+(.*?)(?=<br>)/gi,
		/(\/\*)(.*?)(\*\/)/gi
	  ],
	  rep: [
		"$1<span class='str'>[str]$2[str]</span>$3",
		"<span class='comment'>[comment]$1$2[comment]</span>",
		"<span class='comment'>[comment]$1$2$3[comment]</span>"
	  ]
	}
  },
  py: {
	name: "Python",
	words: [
	  'class', 'and', 'as', 'assert', 'break', 'continue', 'def', 'del', 'elif',
	  'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if',
	  'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise',
	  'return', 'try', 'while', 'with', 'yield'
	],
	comments: {
	  find: [
		/(\#)+(.*?)(?=<br>)/gi,
		/(\"\"\")(.*?)(\"\"\")/gi,
		/(?!<)(\'|\")(.*?)(\'|\")/g
	  ],
	  rep: [
		"<span class=comment>[comment]$1$2[comment]</span>",
		"<span class=comment>[comment]$1$2$3[comment]</span>",
		"$1<span class='str'>[str]$2[str]</span>$3"
	  ]
	}
  }
};

function getLines(html){
  var totalLines = html.match(/<(.*?)>/g).length;
  var op = '<span class="lineNums">';
  for(var i=0; i<=totalLines; i++){
	if(i == totalLines){
	  b = '';
	}
	else{
	  b = '<br/>';
	}
	op += (i+1)+'<br/>';
  }
  op += '</span>';
  return op;
}

function stripTags(HTML){
  var sHTML = HTML.split("[comment]");
  for(var i=1; i<sHTML.length; i+=2){
	sHTML[i] = sHTML[i].replace(/<[span|\/span](.*?)[^><]*>/g, "");
  }
  HTML = sHTML.join('');

  sHTML = HTML.split("[str]");
  for(var i=1; i<sHTML.length; i+=2){
	sHTML[i] = sHTML[i].replace(/<[span|\/span](.*?)[^><]*>/g, "");
  }
  HTML = sHTML.join('');

  return HTML;
}

function doHighlight(obj, lang){
  if(lang == ''){
	lang = 'none';
  }
  var HTML = obj.innerHTML;
  var lines = getLines(HTML);
  for(var i=0; i<keywords[lang]['comments']['find'].length; i++){
	var reg = new RegExp(keywords[lang]['comments']['find'][i]);
	HTML = HTML.replace(reg, keywords[lang]['comments']['rep'][i]);
  }
  for(var i=0; i<keywords[lang]['words'].length; i++){
	if(keywords[lang]['words'][i] == 'class' || keywords[lang]['words'][i] == 'id'){
	  var standardFind = "(?!<span )("+keywords[lang]['words'][i]+")(?!=)";
	}
	else if(keywords[lang]['words'][i] == 'as' || keywords[lang]['words'][i] == 'in' || keywords[lang]['words'][i] == 'is' || keywords[lang]['words'][i] == 'or'){
	  var standardFind = "(\s)("+keywords[lang]['words'][i]+")(\s)";
	}
	else if(keywords[lang]['words'][i] == 'or'){
	  var standardFind = "(?!<span class=\'keyw)("+keywords[lang]['words'][i]+")(?!d\')";
	}
	else{
	  var standardFind = "(?!<span )("+keywords[lang]['words'][i]+")";
	}
	var reg = new RegExp(standardFind, "g");
	HTML = HTML.replace(reg, "<span class='keyword'>"+keywords[lang]['words'][i]+"</span>");
  }
  HTML = HTML.replace(/(\d)/g, "<span class='digit'>$1</span>");
  HTML = stripTags(HTML);
  var op = "<b>"+keywords[lang]['name']+"</b><br/>"+lines+"<div class='codeLines'>"+HTML+"</div>";
  return op;
}

function highlight(){
  var tags = document.getElementsByTagName('div');
  for(var i=0; i<tags.length; i++){
	if(tags[i].name == 'code'){
	  tags[i].innerHTML = doHighlight(tags[i], tags[i].className);
	}
  }
  return;
}
</script>

<body onload="highlight();">

<div name="code" class="cpp" id="sourceCode">
#include "stdafx.h"<br/><br/>

#include&lt;iostream&gt;<br/>
using namespace std;<br/><br/>

class Shape{<br/>
protected:<br/>
	int xPos;<br/>
	int yPos;<br/>
public:<br/>
	virtual double area(void) = 0;<br/>
	virtual int* getPos(void){<br/>
		int a[] = {xPos, yPos};<br/>
		return a;<br/>
	}<br/>
	virtual void setPos(int x, int y){<br/>
		xPos = x;<br/>
		yPos = y;<br/>
	}<br/>
	virtual void move(int toX, int toY){<br/>
		xPos += toX;<br/>
		yPos += toY;<br/>
	}<br/>
};<br/>
<br/>
class Square: public Shape{<br/>
protected:<br/>
	int sideLen;<br/>
public:<br/>
	Square(void){<br/>
		setPos(0, 0);<br/>
	}<br/>
	Square(int x, int y, int sides){<br/>
		setPos(x, y);<br/>
		sideLen = sides;<br/>
	}<br/>
	virtual double area(void){<br/>
		return sideLen*sideLen;<br/>
	}<br/>
};<br/>
<br/>
class Circle: public Shape{<br/>
protected:<br/>
	int radius;<br/>
public:<br/>
	Circle(void){<br/>
		setPos(0, 0);<br/>
		setRadius(0);<br/>
	}<br/>
	Circle(int x, int y, int rad){<br/>
		setPos(x, y);<br/>
		setRadius(rad);<br/>
	}<br/>
	virtual void setRadius(int rad){<br/>
		radius = rad;<br/>
	}<br/>
	virtual int getRadius(void){<br/>
		return radius;<br/>
	}<br/>
	virtual double area(void){<br/>
		return 3.14*radius*radius;<br/>
	}<br/>
};<br/>
<br/>
class Wheel: public Circle{<br/>
protected:<br/>
	int spokes;<br/>
public:<br/>
	Wheel(void){<br/>
		Circle::Circle();<br/>
		setSpokes(0);<br/>
	}<br/>
	Wheel(int x, int y, int rad, int s){<br/>
		Circle::Circle(x, y, rad);<br/>
		setSpokes(s);<br/>
	}<br/>
	virtual void setSpokes(int s){<br/>
		spokes = s;<br/>
	}<br/>
	virtual int getSpokes(void){<br/>
		return spokes;<br/>
	}<br/>
};<br/>
<br/>
int main(){<br/>
	Shape* test = new Square(15, 15, 5);<br/>
	Shape* cir = new Circle(15, 15, 2);<br/>
	cout &lt;&lt; test-&gt;getPos()[0] &lt;&lt; endl;<br/>
	cout &lt;&lt; cir-&gt;area() &lt;&lt; endl;<br/>
}
</div>

<br/>

<div name="code" class="py" id="sourceCode">
import Queue<br/>
import socket<br/>
import threading<br/><br/>

policyTxt = "&lt;?xml version=\"1.0\"?&gt;&lt;cross-domain-policy&gt;&lt;allow-access-from domain=\"*\" to-ports=\"2727\" /&gt;&lt;/cross-domain-policy&gt;\0"<br/>
			#Thread class :)<br/>
class ClientThread(threading.Thread):<br/>
   def run(self):<br/>
	  while True:<br/>
		 client = clientPool.get()<br/>
		 if client != None:<br/>
			print 'Received connection:', client[1][0]<br/>
			while True:<br/>
				message = client[0].recv(1024)<br/>
				if message:<br/>
					print message<br/>
					if message != "&lt;policy-file-request/&gt;\0":<br/>
						reply = "ECHO: "+ message<br/>
						client[0].send(reply)<br/>
				if message == "&lt;policy-file-request/&gt;\0":<br/>
					client[0].send(policyTxt)<br/>
				if message == "&lt;close-connection/&gt;\0":<br/>
					print "Connection closed: ", client[1]<br/>
					client[0].close();<br/>
					break;<br/><br/>

clientPool = Queue.Queue(0)<br/><br/>

for x in xrange(10):<br/>
   ClientThread().start()<br/>
<br/>
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br/>
server.bind(('', 2727))<br/>
server.listen(5)<br/>
<br/>
while True:<br/>
   clientPool.put(server.accept())
</div>


NOTE - I haven't tested this in anything other than IE 7, so it may not be cross browser.

Pros:
Works with line numbers natively
Scrolls to width
Seems to be fast loading
Easy to add new keywords to

Cons:
May not work in other browsers
Requires code type to be typed into it
Skin is not currently built to support 2 colored columns

It is free for use, or not, whatever :D
Was This Post Helpful? 0
  • +
  • -

#20 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Code Syntax

Posted 05 December 2008 - 05:39 PM

It seems to have reverted to all black and white now.
Was This Post Helpful? 0
  • +
  • -

#21 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: Code Syntax

Posted 05 December 2008 - 05:42 PM

There are some pieces of code it won't highlight. It's working though for about 85% of [code] posted.
Was This Post Helpful? 0
  • +
  • -

#22 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Code Syntax

Posted 05 December 2008 - 05:42 PM

Ah ok. Thanks for the response.
Was This Post Helpful? 0
  • +
  • -

#23 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Code Syntax

Posted 06 December 2008 - 12:11 AM

View Postskyhawk133, on 3 Dec, 2008 - 07:31 PM, said:

...
I think I've turned off highlighting for any code that has comments with a ' (apostrophe) so there should be issues, but they also won't get highlighted.
...


Dumb question: Does the code that matches the VB comment delimiter match <whitespace'> or just the " ' "? I'm just curious how this works.

The new highlighting looks great. Thanks!

This post has been edited by n8wxs: 06 December 2008 - 12:12 AM

Was This Post Helpful? 0
  • +
  • -

#24 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: Code Syntax

Posted 06 December 2008 - 06:31 AM

Quote

There are some pieces of code it won't highlight. It's working though for about 85% of [code] posted.

I've noticed that all code enclosed within the CODE tags are highlighted properly, but not for some posts which contain code within the CODE=vb block.

Here's an example page:
Click here. (Check out Martyr2's post)
Was This Post Helpful? 0
  • +
  • -

#25 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: Code Syntax

Posted 06 December 2008 - 09:55 AM

I'm not sure if it's a bug, and oversight, or a feature, but the highlighting encounters a ' (apostrophe) in VB comments, and it highlights the rest of the code (all of it) until it encounters another ' (apostrophe). What it should do is end the comment at the first new line/break. Since it works fine with every other language, I simply disabled highlighting for any code that uses ' (apostrophe) as a comment.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2