You are here

geshifilter.module in GeSHi Filter for syntax highlighting 5.2

An input filter for syntax highlighting using the GeSHi library.

File

geshifilter.module
View source
<?php

/**
 * @file
 * An input filter for syntax highlighting using the GeSHi library.
 */
define('GESHIFILTER_DEFAULT_PLAINTEXT', 'GESHIFILTER_DEFAULT_PLAINTEXT');
define('GESHIFILTER_DEFAULT_DONOTHING', 'GESHIFILTER_DEFAULT_DONOTHING');

// GeSHi CSS modes
// Inline CSS
define('GESHIFILTER_CSS_INLINE', 1);

// Usage of CSS classes and an automatically managaged external stylesheet
define('GESHIFILTER_CSS_CLASSES_AUTOMATIC', 2);

// Only add CSS classes to markup, admin/themer is responsible for defining the CSS rules
define('GESHIFILTER_CSS_CLASSES_ONLY', 3);
define('GESHIFILTER_ATTRIBUTES_LANGUAGE', 'type lang language');
define('GESHIFILTER_ATTRIBUTE_LINE_NUMBERING', 'linenumbers');
define('GESHIFILTER_ATTRIBUTE_LINE_NUMBERING_START', 'start');
define('GESHIFILTER_ATTRIBUTE_FANCY_N', 'fancy');
define('GESHIFILTER_BRACKETS_ANGLE', 1);
define('GESHIFILTER_BRACKETS_SQUARE', 2);
define('GESHIFILTER_BRACKETS_BOTH', 3);
define('GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE', 0);
define('GESHIFILTER_LINE_NUMBERS_DEFAULT_NORMAL', 1);
define('GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY5', 5);
define('GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY10', 10);
define('GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY20', 20);

/**
 * Implementation of hook_help().
 */
function geshifilter_help($section) {
  switch ($section) {
    case 'admin/settings/geshifilter':
    case 'admin/help#geshifilter':
      $output = '<p>' . t('The GeSHi filter module provides a filter for syntax highlighting of inline source code or blocks of source code based on the PHP library !GeSHi.', array(
        '!GeSHi' => l('GeSHi (Generic Syntax Highlighter)', 'http://qbnz.com/highlighter/'),
      )) . '</p>';
      if ($section == 'admin/help#geshifilter') {
        $output .= '<p>' . t('The GeSHi filter module for Drupal requires the GeSHi library (version 1.0.x) to work. The GeSHi filter is actually just a Drupal wrapper module around the GeSHi library. Because of <a href="!repositorypolicies">drupal.org repository policies</a> however, the GeSHi library is not included in the GeSHi filter package, so you should <a href="!geshi">download</a> and install the GeSHi library separately.', array(
          '!repositorypolicies' => url('http://drupal.org/node/66113'),
          '!geshi' => url('http://qbnz.com/highlighter/'),
        )) . '</p>';
        $output .= t('<p>Quick overview of how to set up and use the GeSHi filter:</p><ul><li>Install the GeSHi library and specify its path on the <a href="!geshifilter_settings">GeSHi filter administration page</a>.</li><li>Configure the <a href="!geshifilter_settings">general GeSHi filter settings</a>.</li><li><a href="!geshifilter_languages">Enable the relevant languages</a> for your site and set their language tags if needed.</li><li>Enable the GeSHi filter in the desired !inputformats.</li><li>Check for !filterconflicts and resolve them.</li><li>Use the input format during content submission as described in the !filtertips.</li></ul>', array(
          '!geshifilter_settings' => url('admin/settings/geshifilter'),
          '!geshifilter_languages' => url('admin/settings/geshifilter/languages/all'),
          '!inputformats' => l(t('input formats'), 'admin/settings/filters'),
          '!filterconflicts' => l(t('filter conflicts'), 'admin/settings/geshifilter/filterconflicts'),
          '!filtertips' => l(t('filter tips'), 'filter/tips'),
        ));
      }
      return $output;
      break;
    case 'admin/settings/geshifilter/languages':
    case 'admin/settings/geshifilter/languages/enabled':
    case 'admin/settings/geshifilter/languages/all':
    case 'admin/settings/geshifilter/languages/disabled':
      $output = '<p>' . t('Here you can enable/disable the desired languages to use. It is suggested to disable languages that are not relevant for you site not only to avoid unnecessary cluttering of the GeSHi filter configuration pages and the !filtertips, but also to make the GeSHi filter processing lighter.', array(
        '!filtertips' => l(t('filter tips'), 'filter/tips'),
      )) . '</p>';
      if (!geshifilter_use_format_specific_options()) {
        $output .= '<p>' . t('You can also define the language specific tags here.') . '</p>';
      }
      return $output;
      break;
  }
}

/**
 * Implementation of hook_menu().
 */
function geshifilter_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/geshifilter',
      'title' => t('GeSHi Filter'),
      'description' => t('Configure the GeSHi filter.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'geshifilter_admin_general_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
    $items[] = array(
      'path' => 'admin/settings/geshifilter/general',
      'title' => t('General settings'),
      'description' => t('General GeSHi filter settings.'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/settings/geshifilter/filterconflicts',
      'title' => t('Filter conflicts'),
      'description' => t('Information on possible conflicts with other filters.'),
      'callback' => 'geshifilter_admin_filter_conflicts',
      'type' => MENU_LOCAL_TASK,
      'weight' => 10,
    );

    // language settings
    $items[] = array(
      'path' => 'admin/settings/geshifilter/languages',
      'title' => t('Languages'),
      'description' => t('Enable the desired languages and configure their settings.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'geshifilter_admin_per_language_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/settings/geshifilter/languages/enabled',
      'title' => t('Enabled'),
      'description' => t('Show the enabled languages'),
      'weight' => 3,
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/settings/geshifilter/languages/all',
      'title' => t('All'),
      'description' => t('Show all the available languages'),
      'weight' => 1,
      'type' => MENU_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/settings/geshifilter/languages/disabled',
      'title' => t('Disabled'),
      'description' => t('Show the disabled languages'),
      'weight' => 6,
      'type' => MENU_LOCAL_TASK,
    );

    // clear available languages cache
    $items[] = array(
      'path' => 'geshifilter/clearavailablelanguagescache',
      'title' => t('Clear available languages cache'),
      'callback' => 'geshifilter_clear_available_languages_cache',
      'type' => MENU_CALLBACK,
      'access' => user_access('administer site configuration'),
    );

    // callback for generating CSS rules
    $items[] = array(
      'path' => 'admin/settings/geshifilter/generate_css',
      'callback' => 'geshifilter_generate_language_css_rules',
      'type' => MENU_CALLBACK,
      'access' => user_access('administer site configuration'),
    );
  }
  else {

    // Since the filtered content is cached, it is not possible to know on which
    // pages the css file is actually needed. Thus it is included on all pages.
    if (variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE) == GESHIFILTER_CSS_CLASSES_AUTOMATIC) {
      if ($stylesheet_file = variable_get('geshifilter_languages_css', NULL)) {
        drupal_add_css($stylesheet_file);
      }
    }
    drupal_add_css(drupal_get_path('module', 'geshifilter') . '/geshifilter.css');
  }
  return $items;
}

/**
 * Implementation of hook_filter_tips().
 */
function geshifilter_filter_tips($delta, $format, $long = false) {
  require_once 'geshifilter.filtertips.inc';
  return _geshifilter_filter_tips($delta, $format, $long);
}

/**
 * Implementation of hook_filter().
 */
function geshifilter_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        'GeSHi filter',
      );
    case 'description':
      return t('Enables syntax highlighting of inline/block source code using the GeSHi engine');
    case 'prepare':
      require_once 'geshifilter.pages.inc';
      return _geshifilter_prepare($format, $text);
    case 'process':
      require_once 'geshifilter.pages.inc';
      return _geshifilter_process($format, $text);
    case 'settings':
      require_once 'geshifilter.admin.inc';
      return _geshifilter_filter_settings($format);
    case 'no cache':
      return false;
    default:
      return $text;
  }
}

/**
 * Menu callback for admin settings
 */
function geshifilter_admin_general_settings() {
  require_once 'geshifilter.admin.inc';
  return _geshifilter_admin_general_settings();
}

/**
 * Menu callback for per language settings
 */
function geshifilter_admin_per_language_settings($view = 'enabled') {
  require_once 'geshifilter.admin.inc';
  return _geshifilter_admin_per_language_settings($view);
}

/**
 * Menu callback for filter conflicts page
 */
function geshifilter_admin_filter_conflicts($check_only = FALSE) {
  require_once 'geshifilter.conflicts.inc';
  return _geshifilter_admin_filter_conflicts($check_only);
}

/**
 * Implementation of hook_requirements()
 */
function geshifilter_requirements($phase) {
  require_once 'geshifilter.inc';
  $requirements = array();
  if ($phase == 'runtime') {

    // check if GeSHi library is available
    $geshi_library = _geshifilter_check_geshi_library();
    if (!$geshi_library['loaded']) {
      $requirements[] = array(
        'title' => 'GeSHi filter',
        'value' => t('GeSHi library not found.'),
        'description' => t('You should install the GeSHi library and set its path in the <a href="!geshisettings">GeSHi settings</a>.', array(
          '!geshisettings' => url('admin/settings/geshifilter'),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    else {
      $requirements[] = array(
        'title' => 'GeSHi filter',
        'value' => t('Found GeSHi library version %version', array(
          '%version' => GESHI_VERSION,
        )),
        // GESHI_VERSION is defined in GeSHi library
        'severity' => REQUIREMENT_OK,
      );
    }

    // Warn if GeSHi filter is configured to automatically managed external stylesheet when it's not possible
    if (variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE) == GESHIFILTER_CSS_CLASSES_AUTOMATIC && !_geshifilter_managed_external_stylesheet_possible()) {
      $requirements[] = array(
        'title' => 'GeSHi filter CSS mode',
        'value' => t('GeSHi filter can not automatically manage an external style sheet when the download method is private.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('Change the CSS mode of the <a href="!geshi">GeSHi filter</a> or change the <a href="!filesystem">download mode</a> to public.', array(
          '!geshi' => url('admin/settings/geshifilter'),
          '!filesystem' => url('admin/settings/file-system'),
        )),
      );
    }

    // check for filter conflicts
    if (geshifilter_admin_filter_conflicts(TRUE) > 0) {
      $requirements[] = array(
        'title' => 'GeSHi filter',
        'value' => t('Some filter conflicts were detected.'),
        'description' => l(t('View and resolve the detected filter conflicts'), 'admin/settings/geshifilter/filterconflicts'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}

/**
 * Helper function for checking if an automatically managed style sheet is possible
 *
 * @return boolean indicating if an automatically managed style sheet is possible
 */
function _geshifilter_managed_external_stylesheet_possible() {
  $directory = file_directory_path();
  return is_dir($directory) && is_writable($directory) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC;
}

/**
 * Callback function for clearing the available languages cache
 */
function geshifilter_clear_available_languages_cache() {
  variable_del('geshifilter_available_languages_cache');
  drupal_set_message('Flushed cache of available languages.');
  drupal_goto();
}

/**
 * Callback function for generating the CSS rules for the syntax highlighting
 */
function geshifilter_generate_language_css_rules() {
  require_once 'geshifilter.admin.inc';
  drupal_set_header("Content-type: text/css");
  $output = _geshifilter_generate_languages_css_rules();
  print $output;
  exit;
}

Functions

Namesort descending Description
geshifilter_admin_filter_conflicts Menu callback for filter conflicts page
geshifilter_admin_general_settings Menu callback for admin settings
geshifilter_admin_per_language_settings Menu callback for per language settings
geshifilter_clear_available_languages_cache Callback function for clearing the available languages cache
geshifilter_filter Implementation of hook_filter().
geshifilter_filter_tips Implementation of hook_filter_tips().
geshifilter_generate_language_css_rules Callback function for generating the CSS rules for the syntax highlighting
geshifilter_help Implementation of hook_help().
geshifilter_menu Implementation of hook_menu().
geshifilter_requirements Implementation of hook_requirements()
_geshifilter_managed_external_stylesheet_possible Helper function for checking if an automatically managed style sheet is possible

Constants