You are here

geshinode.module in GeSHi Filter for syntax highlighting 5.2

Same filename and directory in other branches
  1. 6 geshinode.module

File

geshinode.module
View source
<?php

/**
 * Implementation of hook_menu()
 */
function geshinode_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/geshifilter/geshinode',
      'title' => t('GeSHi node'),
      'description' => t('Settings of the GeSHi source code node type.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'geshinode_settings',
      ),
      'type' => MENU_LOCAL_TASK,
    );
  }
  return $items;
}

/**
 * Implementation of hook_help().
 */
function geshinode_help($section) {
  if ($section == 'admin/settings/geshifilter/geshinode') {
    return '<p>' . t('Settings of the GeSHi source code node type.') . '</p>';
  }
}

/**
 * Implementation of hook_node_info().
 */
function geshinode_node_info() {
  return array(
    'geshinode' => array(
      'name' => t('Source code node'),
      'module' => 'geshinode',
      'description' => t('Source code with GeSHi syntax highlighting.'),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function geshinode_perm() {
  return array(
    'create source code node',
    'edit source code node',
    'edit own source code node',
  );
}

/**
 * Implementation of hook_access().
 */
function geshinode_access($op, $node) {
  global $user;
  if ($op == 'create') {
    return user_access('create source code node');
  }
  if ($op == 'update' || $op == 'delete') {
    return user_access('edit source code node') || user_access('edit own source code node') && $user->uid == $node->uid;
  }
}

/**
 * Implementation of hook_form().
 */
function geshinode_form(&$node, &$param) {
  require_once './' . drupal_get_path('module', 'geshifilter') . '/geshifilter.inc';
  $type = node_get_types('type', $node);

  // We need to define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5,
  );
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Source code'),
    '#default_value' => $node->body,
    '#rows' => 20,
    '#required' => TRUE,
  );
  $form['language'] = array(
    '#type' => 'select',
    '#title' => t('Syntax highlighting mode'),
    '#default_value' => $node->language ? $node->language : variable_get('geshinode_default_language', 'php'),
    '#options' => _geshifilter_get_enabled_languages(),
    '#description' => t('Select the syntax highlighting mode to use.'),
  );
  return $form;
}

/**
 * Implementation of hook_validate().
 */
function geshinode_validate($node) {

  ///TODO
}

/**
 * Implementation of hook_insert().
 */
function geshinode_insert($node) {
  db_query("INSERT INTO {geshinode} (nid, vid, language) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->language);
}

/**
 * Implementation of hook_update().
 */
function geshinode_update($node) {
  if ($node->revision) {
    geshinode_insert($node);
  }
  else {
    db_query("UPDATE {geshinode} SET language = '%s' WHERE vid=%d", $node->language, $node->vid);
  }
}

/**
 * Implementation of hook_delete().
 */
function geshinode_delete(&$node) {
  db_query("DELETE FROM {geshinode} WHERE nid = '%d'", $node->nid);
}

/**
 * Implementation of hook_load().
 */
function geshinode_load($node) {
  return db_fetch_object(db_query('SELECT language FROM {geshinode} WHERE vid = %d', $node->vid));
}

/**
 * Implementation of hook_view()
 */
function geshinode_view($node, $teaser = FALSE, $page = FALSE) {
  require_once './' . drupal_get_path('module', 'geshifilter') . '/geshifilter.pages.inc';
  $node->readmore = strlen($node->teaser) < strlen($node->body);
  $line_numbering = variable_get('geshinode_line_numbering', GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE);
  $source_code = $teaser ? $node->teaser : $node->body;
  $node->content['body'] = array(
    '#value' => geshifilter_process($source_code, $node->language, $line_numbering),
    '#weight' => 0,
  );
  return $node;
}

/**
 * Callback for geshinode settings form
 */
function geshinode_settings() {
  require_once './' . drupal_get_path('module', 'geshifilter') . '/geshifilter.inc';
  $form = array();
  $form['geshinode_line_numbering'] = array(
    '#type' => 'select',
    '#title' => t('Line numbering'),
    '#default_value' => variable_get('geshinode_line_numbering', GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE),
    '#options' => array(
      GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE => t('no line numbers'),
      GESHIFILTER_LINE_NUMBERS_DEFAULT_NORMAL => t('normal line numbers'),
      GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY5 => t('fancy line numbers (every @n lines)', array(
        '@n' => GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY5,
      )),
      GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY10 => t('fancy line numbers (every @n lines)', array(
        '@n' => GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY10,
      )),
      GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY20 => t('fancy line numbers (every @n lines)', array(
        '@n' => GESHIFILTER_LINE_NUMBERS_DEFAULT_FANCY20,
      )),
    ),
  );
  $form['geshinode_default_language'] = array(
    '#type' => 'select',
    '#title' => t('Default language'),
    '#options' => _geshifilter_get_enabled_languages(),
    '#default_value' => variable_get('geshinode_default_language', 'php'),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
geshinode_access Implementation of hook_access().
geshinode_delete Implementation of hook_delete().
geshinode_form Implementation of hook_form().
geshinode_help Implementation of hook_help().
geshinode_insert Implementation of hook_insert().
geshinode_load Implementation of hook_load().
geshinode_menu Implementation of hook_menu()
geshinode_node_info Implementation of hook_node_info().
geshinode_perm Implementation of hook_perm().
geshinode_settings Callback for geshinode settings form
geshinode_update Implementation of hook_update().
geshinode_validate Implementation of hook_validate().
geshinode_view Implementation of hook_view()