diff -ruN cdfold/codefilter/INSTALL codefilter/INSTALL --- cdfold/codefilter/INSTALL 2005-04-12 12:09:28.000000000 -0400 +++ codefilter/INSTALL 2005-08-06 16:28:53.468250416 -0400 @@ -25,8 +25,29 @@ 5. (optionally) Edit your theme to provide a div.codeblock style for blocks of code. +Syntax Highlighting +------------------- + +If you want generic syntax highlighting you will need to do the following: + +1. Download GeSHi (http://qbnz.com/highlighter/) + +2. Install it somewhere where the filter can access it (like in the filter directory). + +3. Go to admin -> settings -> codefilter to configure and enable syntax highlighting. + +See the README.txt for more usage info. + Author ------ Steven Wittens + +Updates +------- + +Aug 6th, 2005 - vfilby@gmail.com + Added generic syntax highlighting via GeSHi + + diff -ruN cdfold/codefilter/README.txt codefilter/README.txt --- cdfold/codefilter/README.txt 2005-04-12 12:09:28.000000000 -0400 +++ codefilter/README.txt 2005-08-06 16:26:18.922744904 -0400 @@ -5,5 +5,36 @@ that users can post code without having to worry about escaping with < and > +Syntax Highlighting +------------------- + +To enable syntax highlighting you must download GeSHi +(http://qbnz.com/highlighter/) and extract it somewhere the filter can +access it. Then go to admin -> settings -> codefilter and configure the +location, default language and enable syntax highlighting. + +There are two options that you can use to control syntax highlighting in +the tag itself like html arguments. + + highlight [true|false] Turns the highlighting on or off for the current + ... + + language This must be a valid language that GeSHi + supports + +Example +------- + + +... + + +Updates +------- + +Aug 6th, 2005 - vfilby@gmail.com + - added generic syntax highlighting via GeSHi + + Send comments to unconed@drupal.org. diff -ruN cdfold/codefilter/codefilter.module codefilter/codefilter.module --- cdfold/codefilter/codefilter.module 2005-04-23 04:08:14.000000000 -0400 +++ codefilter/codefilter.module 2005-08-06 16:31:50.688308872 -0400 @@ -29,10 +29,100 @@ function codefilter_menu($may_cache) { if (!$may_cache) { drupal_set_html_head(''); + if( variable_get( 'geshi_enabled', FALSE ) == TRUE ) { + + include_once( $_SERVER[DOCUMENT_ROOT].'/modules/codefilter/geshi/geshi.php' ); + } + } else { + $items[] = array( 'path' => 'admin/settings/codefilter', + 'title' => 'Codefilter Config', + 'callback' => 'codefilter_admin', + 'access' => user_access( 'administer filters' ), + 'type' => MENU_NORMAL_ITEM + ); + + return $items; } } /** + * Implementation of hook_admin() + */ +function codefilter_admin() { + + if( $_POST['op'] ) { + $data = $_POST['edit']; + + if( $data['geshi_enabled'] != variable_get( 'geshi_enabled', FALSE ) ) { + drupal_set_message( "Changed highlight syntax ".$data['geshi_enabled'] ); + variable_set( 'geshi_enabled', $data['geshi_enabled'] ); + } + + if( $data['geshi_dir'] != variable_get( 'geshi_dir', $_SERVER[DOCUMENT_ROOT].'/modules/codefilter/geshi' ) ) { + drupal_set_message( "Changed GeSHi directory" ); + variable_set( 'geshi_dir', $data['geshi_dir'] ); + } + + if( $data['geshi_default_lang'] != variable_get( 'geshi_default_lang', 'c' ) ) { + drupal_set_message( "Default GeSHi language changed to ". $data['geshi_default_lang'] ); + variable_set( 'geshi_default_lang', $data['geshi_default_lang'] ); + } + } + + + $output = ""; + + $group = form_checkbox( t('Highlight syntax'), 'geshi_enabled', + TRUE, + variable_get( 'geshi_enabled', FALSE ), + t('Enable generic syntax highlighting') + ); + + $form = form_group( t('General Settings'), $group ); + + + $group = form_textfield( t('GeSHi Directory'), 'geshi_dir', variable_get( 'geshi_dir', $_SERVER[DOCUMENT_ROOT].'/modules/codefilter/geshi' ), + 70, 100, + t('Location of directory where GeSHi resides') + ); + + + $lang_dir = variable_get( 'geshi_dir', $_SERVER[DOCUMENT_ROOT].'/modules/codefilter/geshi' ).'/geshi'; + if( is_dir( $lang_dir ) ) { + if( $dir = opendir( $lang_dir ) ) { + while( ($file = readdir( $dir ) ) !== false ) { + if( $file[0] == '.' ) + continue; + $lang = str_replace( ".php", "", $file ); + $languages[$lang] = $lang; + } + closedir( $dir ); + } else { + drupal_set_message( "Permission denied for " . $lang_dir ); + } + } else { + drupal_set_message( $lang_dir . " is not a directory" ); + } + + $group .= form_select( t('Default highlight language'), 'geshi_default_lang', variable_get( 'geshi_default_lang', 'c' ), + $languages, + t('Pattern files found in the geshi directory. If the directory is incorrecly set then this will also be incorrect. '. + 'Langurage can be specificed in the <code> tag by adding a lang attribute i.e. lang="ada"' ), + '', + 0, + 0 + ); + + $form .= form_group( t('GeSHi Settings'), $group ); + $form .= form_submit( t('Submit' ) ); + + $output .= form( $form ); + + print theme( 'page', $output ); +} + + +/** * Processes chunks of escaped PHP code into HTML. */ function codefilter_process_php($text) { @@ -63,9 +153,44 @@ /** * Processes chunks of escaped code into HTML. */ -function codefilter_process_code($text) { - // Inline or block level piece? - $multiline = ereg("[\n\r]", $text); +function codefilter_process_code($text, $flags = '') { + + // flags to change the language or disable highlighting can + // be passed like so everything + // after code get's passed in the variable flags. + if( preg_match( '@(lang|language)=\\\\\\\"([^\"]*)\\\\\\\"@', $flags, $matches ) ) { + $lang = $matches[2]; + } else { + $lang = variable_get( 'geshi_default_lang', 'c' ); + } + if( preg_match( '@highlight=\\\\\\\"(true|false)\\\\\\\"@i', $flags, $matches ) ) { + $highlight = (strtolower( $matches[1] ) =='true' ? TRUE : FALSE); + } else { + $highlight = variable_get( 'geshi_enabled', FALSE ); + } + + + // Inline or block level piece? + $multiline = ereg("[\n\r]", $text); + + if( $highlight ) { + + // geshi will reencode these + $text = decode_entities( $text ); + + // undo nl and p formatting + $text = preg_replace( "@
@", "", $text ); + $text = preg_replace( "@

|

@", "", $text ); + + $geshi = new GeSHi( $text, $lang, variable_get( 'geshi_dir', $_SERVER[DOCUMENT_ROOT].'/modules/codefilter/geshi' ).'/geshi' ); + + $text = ''. $geshi->parse_code() . ''; + if ($multiline) + $text = '
'. $text .'
'; + return $text; + + } + // Note, pay attention to odd preg_replace-with-/e behaviour on slashes $text = preg_replace("/^\n/", '', preg_replace('@@', '', str_replace('\"', '"', $text))); // Replace indentation with non-breaking spaces @@ -103,12 +228,12 @@ case 'prepare': // Note: we use the bytes 0xFE and 0xFF to replace < > during the filtering process. // These bytes are not valid in UTF-8 data and thus least likely to cause problems. - $text = preg_replace('@(.+?)@se', "'\xFEcode\xFF'. codefilter_escape('\\1') .'\xFE/code\xFF'", $text); + $text = preg_replace('@(.+?)
@se', "'\xFEcode\\1\xFF'. codefilter_escape('\\2') .'\xFE/code\xFF'", $text); $text = preg_replace('@[\[<](\?php|%)(.+?)(\?|%)[\]>]@se', "'\xFEphp\xFF'. codefilter_escape('\\2') .'\xFE/php\xFF'", $text); return $text; case "process": - $text = preg_replace('@\xFEcode\xFF(.+?)\xFE/code\xFF@se', "codefilter_process_code('$1')", $text); + $text = preg_replace('@\xFEcode(.*?)\xFF(.+?)\xFE/code\xFF@se', "codefilter_process_code('$2', '$1')", $text); $text = preg_replace('@\xFEphp\xFF(.+?)\xFE/php\xFF@se', "codefilter_process_php('$1')", $text); return $text;