highlight_string() source code anyone?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

highlight_string() source code anyone?

Post by Chris Corbyn »

Anybody have a working source code that does the same as highlight_string() ?

I'm looking to port it into JavaScript (as part of a web based editor).

I have started writing my own JavaScript function but it's actually extrememly complex to get it working correctly.

If somebody could point me in the direction of some existing source code it'd be great.

Thanks :-D
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

*speaks in a looming undertone* Be careful my boy, it gonna require humongous amounts of (*gasp*) RegExps...
If you overcome that nothing remains to do...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

n00b Saibot wrote:*speaks in a looming undertone* Be careful my boy, it gonna require humongous amounts of (*gasp*) RegExps...
That's fantastic - i LOVE regexps.

That's not a joke by the way :wink:

I already started the function getting deeper into regexps but it's very twisty, probably easier to base it on existsing code.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what's wrong with highlight_string() ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

highlight_string() wouldn't work client-side ;)

two options:
  1. pull up the original C source of the function from the php source files
  2. use XMLHTTP object to have the server do it for you :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Thanks feyd. I'll get the source in C from the PHP files...

I've never written C but I'm guessing I'd be able to get the jist of it (the order they run the expressions in etc).

I'll let you know how it goes :D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Uggghhh :? C :?: I think this is what I need (???) I'll have to find where they defined T_ENCAPSED_STRING etc etc though. I'll probably just get stuck and cry :cry:

Code: Select all

ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini TSRMLS_DC)
{
	zval token;
	int token_type;
	char *last_color = syntax_highlighter_ini->highlight_html;
	char *next_color;
	int in_string=0;

	zend_printf(&quote;<code>&quote;);
	zend_printf(&quote;<font color=\&quote;%s\&quote;>\n&quote;, last_color);
	/* highlight stuff coming back from zendlex() */
	token.type = 0;
	while ((token_type=lex_scan(&token TSRMLS_CC))) {
		switch (token_type) {
			case T_INLINE_HTML:
				next_color = syntax_highlighter_ini->highlight_html;
				break;
			case T_COMMENT:
				next_color = syntax_highlighter_ini->highlight_comment;
				break;
			case T_OPEN_TAG:
			case T_OPEN_TAG_WITH_ECHO:
				next_color = syntax_highlighter_ini->highlight_default;
				break;
			case T_CLOSE_TAG:
				next_color = syntax_highlighter_ini->highlight_default;
				break;
			case T_CONSTANT_ENCAPSED_STRING:
				next_color = syntax_highlighter_ini->highlight_string;
				break;
			case '&quote;':
				next_color = syntax_highlighter_ini->highlight_string;
				in_string = !in_string;
				break;				
			case T_WHITESPACE:
				zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC);  /* no color needed */
				token.type = 0;
				continue;
				break;
			default:
				if (in_string) {
					next_color = syntax_highlighter_ini->highlight_string;
				} else if (token.type == 0) {
					next_color = syntax_highlighter_ini->highlight_keyword;
				} else {
					next_color = syntax_highlighter_ini->highlight_default;
				}
				break;
		}

		if (last_color != next_color) {
			if (last_color != syntax_highlighter_ini->highlight_html) {
				zend_printf(&quote;</font>&quote;);
			}
			last_color = next_color;
			if (last_color != syntax_highlighter_ini->highlight_html) {
				zend_printf(&quote;<font color=\&quote;%s\&quote;>&quote;, last_color);
			}
		}
		switch (token_type) {
			case T_END_HEREDOC:
				zend_html_puts(token.value.str.val, token.value.str.len TSRMLS_CC);
				{
					char *ptr = LANG_SCNG(yy_text);
					if (ptrїLANG_SCNG(yy_leng) - 1] != ';') {
						zend_html_putc('\n');
					}
				}
				break;
			default:
				zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC);
				break;
		}

		if (token.type == IS_STRING) {
			switch (token_type) {
				case T_OPEN_TAG:
				case T_OPEN_TAG_WITH_ECHO:
				case T_CLOSE_TAG:
				case T_WHITESPACE:
				case T_COMMENT:
					break;
				default:
					efree(token.value.str.val);
					break;
			}
		} else if (token_type == T_END_HEREDOC) {
			efree(token.value.str.val);
		}
		token.type = 0;
	}
	if (last_color != syntax_highlighter_ini->highlight_html) {
		zend_printf(&quote;</font>\n&quote;);
	}
	zend_printf(&quote;</font>\n&quote;);
	zend_printf(&quote;</code>&quote;);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

lex_scan() looks like it's doing the bulk of the work..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:lex_scan() looks like it's doing the bulk of the work..
This is a built in C function? It gets T_CONSTANT and all that stuff?

That's a problem then :roll:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's not a built-in
Post Reply