geshifilter.inc

  1. <?php
  2. // $Id: geshifilter.inc,v 1.2 2008/01/18 17:36:43 soxofaan Exp $
  3.  
  4. function _geshifilter_get_geshi_dir() {
  5.   return variable_get('geshifilter_geshi_dir', drupal_get_path('module', 'geshifilter') .'/geshi');
  6. }
  7.  
  8. /**
  9.  * Helper function for loading/checking the GeSHi library v 1.0.x (if not already)
  10.  * Returns an array with keys 'success', 'loaded' and 'message'
  11.  */
  12. function _geshifilter_check_geshi_library($use_cache=TRUE, $geshi_dir=NULL, $load_when_found=TRUE) {
  13.   static $geshi_library_cache = NULL;
  14.   if ($use_cache && $geshi_library_cache !== NULL) {
  15.     // get from cache
  16.     $geshi_library = $geshi_library_cache;
  17.   }
  18.   else {
  19.     // initialisation
  20.     $geshi_library = array('success' => NULL, 'loaded' => FALSE, 'message' => NULL);
  21.     // no cache
  22.     if (!$geshi_dir) {
  23.       $geshi_dir = _geshifilter_get_geshi_dir();
  24.     }
  25.     if (!is_dir($geshi_dir)) {
  26.       $geshi_library['success'] = FALSE;
  27.       $geshi_library['message'] = t('GeSHi library error: %dir is not a directory.', array('%dir' => $geshi_dir));
  28.     }
  29.     elseif (is_file($geshi_dir .'/geshi.php')) {
  30.       // GeSHi 1.0.x found (probably, we can only be sure by loading it)
  31.       $geshi_library['success'] = TRUE;
  32.       if ($load_when_found) {
  33.         require_once($geshi_dir .'/geshi.php');
  34.         // check version
  35.         $geshi_library_version = explode('.', GESHI_VERSION);
  36.         if (!($geshi_library_version[0] == '1' &&  $geshi_library_version[1] == '0')) {
  37.           $geshi_library['success'] = FALSE;
  38.           $geshi_library['loaded'] = FALSE;
  39.           $geshi_library['message'] = t('GeSHi library error: The detected version of GeSHi library (%version) is not supported. A version from the 1.0.x branch is required.', array('%version' => GESHI_VERSION));
  40.         }
  41.         else {
  42.           $geshi_library['loaded'] = TRUE;
  43.         }
  44.       }
  45.     }
  46.     else {
  47.       $geshi_library['success'] = FALSE;
  48.       $geshi_library['message'] = t('GeSHi library error: Could not find a known version of the GeSHi library at %dir.' , array('%dir' => $geshi_dir));
  49.     }
  50.     // store in cache if needed
  51.     if ($use_cache) {
  52.       $geshi_library_cache = $geshi_library;
  53.     }
  54.   }
  55.   return $geshi_library;
  56. }
  57.  
  58. /**
  59.  * List of available languages.
  60.  * @return an array mapping language code to array with the language path and full language name
  61.  */
  62. function _geshifilter_get_available_languages() {
  63.   // try to get it from cache (database actually)
  64.   $available_languages = variable_get('geshifilter_available_languages', NULL);
  65.   if ($available_languages === NULL) {
  66.     // not in cache: build the array of available_languages
  67.     $geshi_library = _geshifilter_check_geshi_library();
  68.     $available_languages = array();
  69.     if ($geshi_library['success']) {
  70.       $dirs = array(_geshifilter_get_geshi_dir() .'/geshi', drupal_get_path('module', 'geshifilter') .'/geshi-extra');
  71.       foreach ($dirs as $dir) {
  72.         foreach (file_scan_directory($dir, '\.[pP][hH][pP]$') as $filename => $fileinfo) {
  73.           // short name
  74.           $name = $fileinfo->name;
  75.           // get full name
  76.           $geshi = new GeSHi('', $name);
  77.           $geshi->set_language_path($dir);
  78.           $fullname = $geshi->get_language_name();
  79.           unset($geshi);
  80.           // store
  81.           $available_languages[$name] = array('language_path' => $dir, 'fullname' => $fullname);
  82.         }
  83.       }
  84.       ksort($available_languages);
  85.       // save array to database
  86.       variable_set('geshifilter_available_languages', $available_languages);
  87.     }
  88.   }
  89.   return $available_languages;
  90. }
  91.  
  92. /**
  93.  * List of enabled languages.
  94.  * (with caching)
  95.  * @return array with enabled languages mapping language code to full name.
  96.  */
  97. function _geshifilter_get_enabled_languages() {
  98.   static $enabled_languages = NULL;
  99.   if ($enabled_languages === NULL) {
  100.     $enabled_languages = array();
  101.     $languages = _geshifilter_get_available_languages();
  102.     foreach ($languages as $language => $language_data) {
  103.       if (variable_get("geshifilter_language_enabled_{$language}", FALSE)) {
  104.         $enabled_languages[$language] = $language_data['fullname'];
  105.       }
  106.     }
  107.   }
  108.   return $enabled_languages;
  109. }
  110.  
  111. /**
  112.  * Helper function for gettings the tags
  113.  * (with caching)
  114.  */
  115. function _geshifilter_get_tags($format) {
  116.   static $geshifilter_tags_cache = array();
  117.   if (!isset($geshifilter_tags_cache[$format])) {
  118.     $generic_code_tags = _geshifilter_tag_split(geshifilter_tags($format));
  119.     $language_tags = array();
  120.     $tag_to_lang = array();
  121.     $enabled_languages = _geshifilter_get_enabled_languages();
  122.     foreach ($enabled_languages as $language => $fullname) {
  123.       $lang_tags = _geshifilter_tag_split(geshifilter_language_tags($language, $format));
  124.       foreach ($lang_tags as $lang_tag) {
  125.         $language_tags[] = $lang_tag;
  126.         $tag_to_lang[$lang_tag] = $language;
  127.       }
  128.     }
  129.     $geshifilter_tags_cache[$format] = array($generic_code_tags, $language_tags, $tag_to_lang);
  130.   }
  131.   return $geshifilter_tags_cache[$format];
  132. }
  133.  
  134. /**
  135.  * helper function for generating a GeSHi object
  136.  * @param $language the language to generate a GeSHi object for
  137.  */
  138. function _geshifilter_geshi_factory($source_code, $language) {
  139.   $available_languages = _geshifilter_get_available_languages();
  140.   $geshi = new GeSHi($source_code, $language);
  141.   $geshi->set_language_path($available_languages[$language]['language_path']);
  142.   return $geshi;
  143. }
  144.  
  145. /**
  146.  * Helper function for splitting a string on white spaces.
  147.  * Using explode(' ', $string) is not enough because it returns empty elements
  148.  * if $string contains consecutive spaces.
  149.  */
  150. function _geshifilter_whitespace_explode($string) {
  151.   return preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
  152. }
  153.  
  154. function _geshifilter_tag_split($string) {
  155.   return preg_split('/\s+|<|>|\[|\]/', $string, -1, PREG_SPLIT_NO_EMPTY);
  156. }
  157.  
  158. // General settings
  159. function geshifilter_use_format_specific_options() {
  160.   return variable_get('geshifilter_format_specific_options', FALSE);
  161. }
  162.  
  163. function geshifilter_tags($format = NULL) {
  164.   if (!geshifilter_use_format_specific_options() || $format === NULL) {
  165.     return variable_get('geshifilter_tags', 'code blockcode');
  166.   }
  167.   return variable_get("geshifilter_tags_{$format}", geshifilter_tags());
  168. }
  169.  
  170. function _geshifilter_php_delimeters($format = NULL) {
  171.   if (!geshifilter_use_format_specific_options() || $format === NULL) {
  172.     return variable_get('geshifilter_enable_php_delimiters', FALSE);
  173.   }
  174.   return variable_get("geshifilter_enable_php_delimiters_{$format}", _geshifilter_php_delimeters());
  175. }
  176.  
  177. function _geshifilter_brackets($format = NULL) {
  178.   if (!geshifilter_use_format_specific_options() || $format === NULL) {
  179.     return variable_get('geshifilter_brackets', GESHIFILTER_BRACKETS_BOTH);
  180.   }
  181.   return variable_get("geshifilter_brackets_{$format}", _geshifilter_brackets());
  182. }
  183.  
  184. function geshifilter_language_tags($language, $format = NULL) {
  185.   if (!geshifilter_use_format_specific_options() || $format === NULL)
  186.     return variable_get("geshifilter_language_tags_{$language}", '');
  187.   return variable_get("geshifilter_language_tags_{$language}_{$format}", geshifilter_language_tags($language));
  188. }