How to Highlight Mixed Language Source Code

Web pages contain many snippets from different languages, like HTML, PHP, CSS, and JavaScript. Chili 1.x cannot highlight mixed language source code automatically, but the forthcoming released Chili 2.0 will be is able to do that.

Meanwhile, if you are using a Chili + Enzymes setup, then it’s possible to write an Enzymes template capable of making Chili 1.x highlight a web page perfectly (expanding on my previous php template).

Here is the new updated (2007/02/19) chili-web.php Enzymes template. At the moment it supports XHTML, and embedded CSS, JavaScript, and PHP snippets.

'.htmlentities( $code ).''; } function snippet_php( $open, $code, $close ) { return snippet_wrap( 'html', $open ).snippet_wrap( 'php', $code ).snippet_wrap( 'html', $close ); } function php_inside_html( $matches ) { list( $all, $before, $open, $code, $close ) = $matches; if( '' == $open ) return snippet_wrap( 'html', $all ); else return snippet_wrap( 'html', $before ).snippet_php( $open, $code, $close ); } function php_inside_css( $matches ) { list( $all, $before, $open, $code, $close ) = $matches; if( '' == $open ) return snippet_wrap( 'css', $all ); else return snippet_wrap( 'css', $before ).snippet_php( $open, $code, $close ); } function php_inside_javascript( $matches ) { list( $all, $before, $open, $code, $close ) = $matches; if( '' == $open ) return snippet_wrap( 'javascript', $all ); else return snippet_wrap( 'javascript', $before ).snippet_php( $open, $code, $close ); } function php_inside( $class, $code ) { return preg_replace_callback( '/(?:(.*?)())|(?:.+)/si', "php_inside_$class", $code ); } function snippet_web( $class, $open, $code, $close ) { return snippet_wrap( 'html', $open ).php_inside( $class, $code ).snippet_wrap( 'html', $close ); } function format_code( $matches ) { list( $all, $before, $open, $tag, $code, $close ) = $matches; if( '' == $open ) { return php_inside( 'html', $all ); } else { switch( $tag ) { case 'script': $class = 'javascript'; break; case 'style' : $class = 'css'; break; default : $class = 'html'; } return php_inside( 'html', $before ).snippet_web( $class, $open, $code, $close ); } } function format_clean( $out ) { do { $len = strlen( $out ); $out = preg_replace( '/([^<]*)/is', '$1', $out ); } while( $len != strlen( $out ) ); return $out; } } ?>

)(.*?)())|(?:.+)/is', 'format_code', $this->result );
	echo format_clean( $out );
?>

The WordPress file /wp-content /themes /default /comments-popup.php is a page that uses all the supported languages, so it’s a good test for the previous template:

<?php echo get_option('blogname'); ?> - Comments on <?php the_title(); ?>

Comments

RSS feed for comments on this post.

ping_status) { ?>

The URL to TrackBack this entry is:

post_password) && $_COOKIE['wp-postpass_'. COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie echo(get_the_password_form()); } else { ?>
  1. by — @

No comments yet.

comment_status) { ?>

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:

" />


ID); ?>

Sorry, the comment form is closed at this time.

Powered by WordPress

Chili & Enzymes confirm to be a perfect match.

4 Replies to “How to Highlight Mixed Language Source Code”

  1. http://www.smelldelicious.com/v/vspfiles/templates/Perfume2/images/logos/dg.gif

    Hi Everybody

    As the holidays approach and your trying to find his or her bday or holiday mens cologne gifts you may need help finding the most current cologne for your girlfriend, wife, husband or…just a special person. So if your locating the top colognes & womens bday gifts – specifically since there are hundreds of perfumes and colognes to choose from.

    Smell Delicious.com storefront specializes in Women’s Perfume & stocks Lancetti Parfums & Sportsfragrance and hundreds of additional womens fragrances.

    Here is some of our popular mens perfumes and fragrancesThe Superior decision for the perfect person
    Bruno Banani & Burberry & Kate Spade & Kathy Hilton & Carla Fracci & Carlo Corinto & Niki Taylor & Navy & Laurent Dornel & Lulu Guinness & Gai Mattiolo & Gale Hayman & Christian Lacroix & Christina Aguilera & Compagne Europeene Parfums & Comptoir Sud Pacifique & Disney & Dlish & Jesus del Pozo & Jil Sander & Demeter & Designer Parfums ltd & Entre Filles & Erox & Alberta Ferretti & Alessandro Dell Acqua & Entre Filles & Erox & Asprey & Aubusson & Marmol & Son & Mediterraneum & Payot & Max Mara Perfumes.

    http://www.perfume.com/images/family_banners/aromatic_men.jpg?1268443664

Leave a Reply

Your email address will not be published. Required fields are marked *