View Single Post
Old 04-07-2009, 09:51 AM   #1 (permalink)
Kalle
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default Support for CSS classes in PHP highlight

Hello there

A recent thread on the php internals mailing list brought up a topic about making it possible for the engine to spit out class="xx" instead of style="color: xx" when doing a highlight via: highlight_file()/highlight_string()/php -s.

I wrote up a quick patch to do this with alot of new ini directives to control class names, but all in all it wasn't a good way around the problem, so I rewrote my patch making it possible to trigger CSS classes in highlight and prefix the class names to avoid naming conflicts. I have attached the patch below for those whos interested in trying it out:

c Code:
Index: ZendEngine2/zend_highlight.c
===================================================================
RCS file: /repository/ZendEngine2/zend_highlight.c,v
retrieving revision 1.49.2.3.2.2.2.6
diff -u -r1.49.2.3.2.2.2.6 zend_highlight.c
--- ZendEngine2/zend_highlight.c    31 Dec 2008 11:15:32 -0000  1.49.2.3.2.2.2.6
+++ ZendEngine2/zend_highlight.c    2 Apr 2009 17:59:48 -0000
@@ -86,37 +86,47 @@
 #endif /* ZEND_MULTIBYTE */
 }
 
-
 ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini TSRMLS_DC)
 {
    zval token;
    int token_type;
+   zend_bool inline_styles = syntax_highlighter_ini->inline_styles;
+   char *class_prefix = syntax_highlighter_ini->class_prefix;
    char *last_color = syntax_highlighter_ini->highlight_html;
+   char *class = "html";
    char *next_color;
 
    zend_printf("<code>");
-   zend_printf("<span style=\"color: %s\">\n", last_color);
+
+   if (inline_styles) {
+      zend_printf("<span style=\"color: %s\">\n", last_color);
+   } else {
+      zend_printf("<span class=\"%s%s\">", class_prefix, class);
+   }
+
    /* highlight stuff coming back from zendlex() */
    token.type = 0;
    while ((token_type=lex_scan(&token TSRMLS_CC))) {
       switch (token_type) {
          case T_INLINE_HTML:
+            class = "html";
             next_color = syntax_highlighter_ini->highlight_html;
             break;
          case T_COMMENT:
          case T_DOC_COMMENT:
+            class = "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:
+            class = "default";
             next_color = syntax_highlighter_ini->highlight_default;
             break;
          case '"':
          case T_ENCAPSED_AND_WHITESPACE:
          case T_CONSTANT_ENCAPSED_STRING:
+            class = "string";
             next_color = syntax_highlighter_ini->highlight_string;
             break;
          case T_WHITESPACE:
@@ -125,7 +135,9 @@
             continue;
             break;
          default:
+            class = "default";
             if (token.type == 0) {
+               class = "keyword";
                next_color = syntax_highlighter_ini->highlight_keyword;
             } else {
                next_color = syntax_highlighter_ini->highlight_default;
@@ -139,7 +151,11 @@
          }
          last_color = next_color;
          if (last_color != syntax_highlighter_ini->highlight_html) {
-            zend_printf("<span style=\"color: %s\">", last_color);
+            if (inline_styles) {
+               zend_printf("<span style=\"color: %s\">", last_color);
+            } else {
+               zend_printf("<span class=\"%s%s\">", class_prefix, class);
+            }
          }
       }
       switch (token_type) {
@@ -177,7 +193,11 @@
             zend_printf("</span>");
          }
          if (syntax_highlighter_ini->highlight_comment != syntax_highlighter_ini->highlight_html) {
-            zend_printf("<span style=\"color: %s\">", syntax_highlighter_ini->highlight_comment);
+            if (inline_styles) {
+               zend_printf("<span style=\"color: %s\">", syntax_highlighter_ini->highlight_comment);
+            } else {
+               zend_printf("<span class=\"%scomment\">", class_prefix);
+            }
          }
       }
       zend_html_puts(LANG_SCNG(yy_text), (LANG_SCNG(yy_limit) - LANG_SCNG(yy_text)) TSRMLS_CC);
Index: ZendEngine2/zend_highlight.h
===================================================================
RCS file: /repository/ZendEngine2/zend_highlight.h,v
retrieving revision 1.25.2.1.2.1.2.2
diff -u -r1.25.2.1.2.1.2.2 zend_highlight.h
--- ZendEngine2/zend_highlight.h    31 Dec 2008 11:15:32 -0000  1.25.2.1.2.1.2.2
+++ ZendEngine2/zend_highlight.h    2 Apr 2009 17:59:58 -0000
@@ -36,6 +36,8 @@
    char *highlight_default;
    char *highlight_string;
    char *highlight_keyword;
+   zend_bool inline_styles;
+   char *class_prefix;
 } zend_syntax_highlighter_ini;
 
 
Index: ext/standard/basic_functions.c
===================================================================
RCS file: /repository/php-src/ext/standard/basic_functions.c,v
retrieving revision 1.725.2.31.2.64.2.87
diff -u -r1.725.2.31.2.64.2.87 basic_functions.c
--- ext/standard/basic_functions.c  27 Mar 2009 02:32:56 -0000    1.725.2.31.2.64.2.87
+++ ext/standard/basic_functions.c  2 Apr 2009 17:59:21 -0000
@@ -5090,6 +5090,8 @@
    syntax_highlighter_ini->highlight_html    = INI_STR("highlight.html");
    syntax_highlighter_ini->highlight_keyword = INI_STR("highlight.keyword");
    syntax_highlighter_ini->highlight_string  = INI_STR("highlight.string");
+   syntax_highlighter_ini->inline_styles     = INI_BOOL("highlight.inline_styles");
+   syntax_highlighter_ini->class_prefix      = INI_STR("highlight.class_prefix");
 }
 /* }}} */
 
Index: main/main.c
===================================================================
RCS file: /repository/php-src/main/main.c,v
retrieving revision 1.640.2.23.2.57.2.47
diff -u -r1.640.2.23.2.57.2.47 main.c
--- main/main.c 27 Mar 2009 02:34:06 -0000   1.640.2.23.2.57.2.47
+++ main/main.c 2 Apr 2009 17:57:59 -0000
@@ -394,13 +394,15 @@
 /* {{{ PHP_INI
  */

 PHP_INI_BEGIN()
-   PHP_INI_ENTRY_EX("define_syslog_variables",    "0",                PHP_INI_ALL,    NULL,         php_ini_boolean_displayer_cb)
-   PHP_INI_ENTRY_EX("highlight.bg",         HL_BG_COLOR,      PHP_INI_ALL,   NULL,      php_ini_color_displayer_cb)
+   PHP_INI_ENTRY_EX("define_syslog_variables",    "0",            PHP_INI_ALL,    NULL,         php_ini_boolean_displayer_cb)
+   PHP_INI_ENTRY_EX("highlight.bg",      HL_BG_COLOR,      PHP_INI_ALL,   NULL,      php_ini_color_displayer_cb)
    PHP_INI_ENTRY_EX("highlight.comment",    HL_COMMENT_COLOR, PHP_INI_ALL, NULL,            php_ini_color_displayer_cb)
    PHP_INI_ENTRY_EX("highlight.default",    HL_DEFAULT_COLOR, PHP_INI_ALL, NULL,            php_ini_color_displayer_cb)
-   PHP_INI_ENTRY_EX("highlight.html",   HL_HTML_COLOR,      PHP_INI_ALL,   NULL,      php_ini_color_displayer_cb)
+   PHP_INI_ENTRY_EX("highlight.html",  HL_HTML_COLOR,      PHP_INI_ALL,   NULL,      php_ini_color_displayer_cb)
    PHP_INI_ENTRY_EX("highlight.keyword",    HL_KEYWORD_COLOR, PHP_INI_ALL, NULL,            php_ini_color_displayer_cb)
    PHP_INI_ENTRY_EX("highlight.string",      HL_STRING_COLOR,   PHP_INI_ALL,   NULL,      php_ini_color_displayer_cb)
+   PHP_INI_ENTRY_EX("highlight.inline_styles",    "1",            PHP_INI_ALL,    NULL,         php_ini_boolean_displayer_cb)
+   PHP_INI_ENTRY("highlight.class_prefix",        "",   PHP_INI_ALL, NULL)
 
    STD_PHP_INI_BOOLEAN("allow_call_time_pass_reference""1",  PHP_INI_SYSTEM|PHP_INI_PERDIR,        OnUpdateBool,   allow_call_time_pass_reference,    zend_compiler_globals,  compiler_globals)
    STD_PHP_INI_BOOLEAN("asp_tags",                "0",        PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateBool,   asp_tags,                zend_compiler_globals,  compiler_globals)

It adds two new INI settings (both PHP_INI_ALL):
  • highlight.inline_styles (default: 1), set to 0 to activate the use of CSS classes
  • highlight.class_prefix (default: ""), set to a string prefix to prevent naming conflicts


Example usage:
Code:
C:\> php -d highlight.inline_styles=0 -s -f "somefile.php"
PHP Code:
<?php
ini_set
('highlight.inline_styles'0);
ini_set('highlight.class_prefix''php-');

show_source(__FILE__);
?>
Thought it might be helpful to others around these corners of php world :)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote