Back to index

php5  5.3.10
zend_highlight.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_highlight.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 #include <zend_language_parser.h>
00024 #include "zend_compile.h"
00025 #include "zend_highlight.h"
00026 #include "zend_ptr_stack.h"
00027 #include "zend_globals.h"
00028 
00029 ZEND_API void zend_html_putc(char c)
00030 {
00031        switch (c) {
00032               case '\n':
00033                      ZEND_PUTS("<br />");
00034                      break;
00035               case '<':
00036                      ZEND_PUTS("&lt;");
00037                      break;
00038               case '>':
00039                      ZEND_PUTS("&gt;");
00040                      break;
00041               case '&':
00042                      ZEND_PUTS("&amp;");
00043                      break;
00044               case ' ':
00045                      ZEND_PUTS("&nbsp;");
00046                      break;
00047               case '\t':
00048                      ZEND_PUTS("&nbsp;&nbsp;&nbsp;&nbsp;");
00049                      break;
00050               default:
00051                      ZEND_PUTC(c);
00052                      break;
00053        }
00054 }
00055 
00056 
00057 ZEND_API void zend_html_puts(const char *s, uint len TSRMLS_DC)
00058 {
00059        const char *ptr=s, *end=s+len;
00060 
00061 #ifdef ZEND_MULTIBYTE
00062        char *filtered;
00063        int filtered_len;
00064 
00065        if (LANG_SCNG(output_filter)) {
00066               LANG_SCNG(output_filter)(&filtered, &filtered_len, s, len TSRMLS_CC);
00067               ptr = filtered;
00068               end = filtered + filtered_len;
00069        }
00070 #endif /* ZEND_MULTIBYTE */
00071 
00072        while (ptr<end) {
00073               if (*ptr==' ') {
00074                      do {
00075                             zend_html_putc(*ptr);
00076                      } while ((++ptr < end) && (*ptr==' '));
00077               } else {
00078                      zend_html_putc(*ptr++);
00079               }
00080        }
00081 
00082 #ifdef ZEND_MULTIBYTE
00083        if (LANG_SCNG(output_filter)) {
00084               efree(filtered);
00085        }
00086 #endif /* ZEND_MULTIBYTE */
00087 }
00088 
00089 
00090 ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini TSRMLS_DC)
00091 {
00092        zval token;
00093        int token_type;
00094        char *last_color = syntax_highlighter_ini->highlight_html;
00095        char *next_color;
00096 
00097        zend_printf("<code>");
00098        zend_printf("<span style=\"color: %s\">\n", last_color);
00099        /* highlight stuff coming back from zendlex() */
00100        token.type = 0;
00101        while ((token_type=lex_scan(&token TSRMLS_CC))) {
00102               switch (token_type) {
00103                      case T_INLINE_HTML:
00104                             next_color = syntax_highlighter_ini->highlight_html;
00105                             break;
00106                      case T_COMMENT:
00107                      case T_DOC_COMMENT:
00108                             next_color = syntax_highlighter_ini->highlight_comment;
00109                             break;
00110                      case T_OPEN_TAG:
00111                      case T_OPEN_TAG_WITH_ECHO:
00112                             next_color = syntax_highlighter_ini->highlight_default;
00113                             break;
00114                      case T_CLOSE_TAG:
00115                             next_color = syntax_highlighter_ini->highlight_default;
00116                             break;
00117                      case '"':
00118                      case T_ENCAPSED_AND_WHITESPACE:
00119                      case T_CONSTANT_ENCAPSED_STRING:
00120                             next_color = syntax_highlighter_ini->highlight_string;
00121                             break;
00122                      case T_WHITESPACE:
00123                             zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC);  /* no color needed */
00124                             token.type = 0;
00125                             continue;
00126                             break;
00127                      default:
00128                             if (token.type == 0) {
00129                                    next_color = syntax_highlighter_ini->highlight_keyword;
00130                             } else {
00131                                    next_color = syntax_highlighter_ini->highlight_default;
00132                             }
00133                             break;
00134               }
00135 
00136               if (last_color != next_color) {
00137                      if (last_color != syntax_highlighter_ini->highlight_html) {
00138                             zend_printf("</span>");
00139                      }
00140                      last_color = next_color;
00141                      if (last_color != syntax_highlighter_ini->highlight_html) {
00142                             zend_printf("<span style=\"color: %s\">", last_color);
00143                      }
00144               }
00145 
00146               zend_html_puts(LANG_SCNG(yy_text), LANG_SCNG(yy_leng) TSRMLS_CC);
00147 
00148               if (token.type == IS_STRING) {
00149                      switch (token_type) {
00150                             case T_OPEN_TAG:
00151                             case T_OPEN_TAG_WITH_ECHO:
00152                             case T_CLOSE_TAG:
00153                             case T_WHITESPACE:
00154                             case T_COMMENT:
00155                             case T_DOC_COMMENT:
00156                                    break;
00157                             default:
00158                                    efree(token.value.str.val);
00159                                    break;
00160                      }
00161               } else if (token_type == T_END_HEREDOC) {
00162                      efree(token.value.str.val);
00163               }
00164               token.type = 0;
00165        }
00166 
00167        if (last_color != syntax_highlighter_ini->highlight_html) {
00168               zend_printf("</span>\n");
00169        }
00170        zend_printf("</span>\n");
00171        zend_printf("</code>");
00172 }
00173 
00174 ZEND_API void zend_strip(TSRMLS_D)
00175 {
00176        zval token;
00177        int token_type;
00178        int prev_space = 0;
00179 
00180        token.type = 0;
00181        while ((token_type=lex_scan(&token TSRMLS_CC))) {
00182               switch (token_type) {
00183                      case T_WHITESPACE:
00184                             if (!prev_space) {
00185                                    zend_write(" ", sizeof(" ") - 1);
00186                                    prev_space = 1;
00187                             }
00188                                           /* lack of break; is intentional */
00189                      case T_COMMENT:
00190                      case T_DOC_COMMENT:
00191                             token.type = 0;
00192                             continue;
00193                      
00194                      case T_END_HEREDOC:
00195                             zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00196                             efree(token.value.str.val);
00197                             /* read the following character, either newline or ; */
00198                             if (lex_scan(&token TSRMLS_CC) != T_WHITESPACE) {
00199                                    zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00200                             }
00201                             zend_write("\n", sizeof("\n") - 1);
00202                             prev_space = 1;
00203                             token.type = 0;
00204                             continue;
00205 
00206                      default:
00207                             zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00208                             break;
00209               }
00210 
00211               if (token.type == IS_STRING) {
00212                      switch (token_type) {
00213                             case T_OPEN_TAG:
00214                             case T_OPEN_TAG_WITH_ECHO:
00215                             case T_CLOSE_TAG:
00216                             case T_WHITESPACE:
00217                             case T_COMMENT:
00218                             case T_DOC_COMMENT:
00219                                    break;
00220 
00221                             default:
00222                                    efree(token.value.str.val);
00223                                    break;
00224                      }
00225               }
00226               prev_space = token.type = 0;
00227        }
00228 }
00229 
00230 /*
00231  * Local variables:
00232  * tab-width: 4
00233  * c-basic-offset: 4
00234  * indent-tabs-mode: t
00235  * End:
00236  */
00237