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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Mar 31, 2005 6:52 am
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
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Thu Mar 31, 2005 6:58 am
*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...
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Mar 31, 2005 6:59 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 31, 2005 12:34 pm
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Mar 31, 2005 2:25 pm
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Mar 31, 2005 3:16 pm
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;);
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 31, 2005 3:19 pm
lex_scan() looks like it's doing the bulk of the work..
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu Mar 31, 2005 3:38 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 31, 2005 3:50 pm
it's not a built-in