Page 1 of 1
highlight_string() source code anyone?
Posted: Thu Mar 31, 2005 6:52 am
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

Posted: Thu Mar 31, 2005 6:58 am
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...
Posted: Thu Mar 31, 2005 6:59 am
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
I already started the function getting deeper into regexps but it's very twisty, probably easier to base it on existsing code.
Posted: Thu Mar 31, 2005 11:42 am
by John Cartwright
what's wrong with highlight_string() ?
Posted: Thu Mar 31, 2005 12:34 pm
by feyd
highlight_string() wouldn't work client-side
two options:
- pull up the original C source of the function from the php source files
- use XMLHTTP object to have the server do it for you

Posted: Thu Mar 31, 2005 2:25 pm
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

Posted: Thu Mar 31, 2005 3:16 pm
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
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("e;<code>"e;);
zend_printf("e;<font color=\"e;%s\"e;>\n"e;, 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 '"e;':
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("e;</font>"e;);
}
last_color = next_color;
if (last_color != syntax_highlighter_ini->highlight_html) {
zend_printf("e;<font color=\"e;%s\"e;>"e;, 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("e;</font>\n"e;);
}
zend_printf("e;</font>\n"e;);
zend_printf("e;</code>"e;);
}
Posted: Thu Mar 31, 2005 3:19 pm
by feyd
lex_scan() looks like it's doing the bulk of the work..
Posted: Thu Mar 31, 2005 3:38 pm
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

Posted: Thu Mar 31, 2005 3:50 pm
by feyd
it's not a built-in