You are here

autofloat.module in AutoFloat 6

Autofloat module: A filter that floats images left and right automatically.

File

autofloat.module
View source
<?php

/**
 * @file
 * Autofloat module: A filter that floats images left and right automatically.
 */

/**
 * Implements hook_help().
 */
function autofloat_help($path, $arg) {
  switch ($path) {
    case 'admin/help#autofloat':

      // Return a line-break version of the README.txt.
      return _filter_autop(file_get_contents(dirname(__FILE__) . '/README.txt'));
  }
}

/**
 * Implements hook_init().
 */
function autofloat_init() {

  // Determine the setting to use autofloat.css or not.
  if (variable_get('autofloat_css', 1)) {
    drupal_add_css(drupal_get_path('module', 'autofloat') . '/autofloat.css', $type = 'module', $media = 'all', $preprocess = TRUE);
  }
}

/**
 * Implements hook_menu().
 */
function autofloat_menu() {
  $items['admin/settings/autofloat'] = array(
    'title' => 'AutoFloat',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'autofloat_admin_settings',
    ),
    'access arguments' => array(
      'administer filters',
    ),
    'description' => 'Filter settings. Start floating left or right, exclude module\'s css and define selectors/rejectors.',
    'file' => 'autofloat.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_filter_info().
 */
function autofloat_filter($op, $delta = 0, $format = -1, $text = '') {
  if ($op == 'list') {
    return array(
      0 => t('AutoFloat'),
    );
  }
  switch ($delta) {
    case 0:
      switch ($op) {
        case 'description':
          return t('Images will float left and right unless escaped with a class "nofloat".');
        case 'prepare':
          return $text;
        case 'process':
          return _autofloat($text, $format);
        case 'settings':
          $form['autofloat']['notice'] = array(
            '#value' => t('<p>These settings are shared by all the input formats where <em>Autofloat</em> is enabled:</p>'),
          );
          return $form;
      }
      break;
  }
}

/**
 * Returns the filter information for the filter help page.
 */
function autofloat_filter_tips($delta, $format, $long = FALSE) {
  $output = t('Images will float left and right unless escaped with <em>class="nofloat"</em>');
  return $output;
}

/**
 * Find a rejected 'div', a selected 'span', a link with image or an image.
 */
function _autofloat($text, $format) {
  $selector = variable_get('autofloat_span', 'caption');

  // Divide the variable into two selectors if a comma is found.
  $selector = explode(',', $selector);

  // Make sure both array keys exist, if not the regex breaks.
  if (isset($selector[0]) == FALSE) {
    $selector[0] = '';
  }
  if (isset($selector[1]) == FALSE) {
    $selector[1] = '';
  }
  $rejector = variable_get('autofloat_div', 'flickr-photoset');

  // Divide the variable into two rejectors if a comma is found.
  $rejector = explode(',', $rejector);

  // Make sure both array keys exist, if not the regex breaks.
  if (isset($rejector[0]) == FALSE) {
    $rejector[0] = '';
  }
  if (isset($rejector[1]) == FALSE) {
    $rejector[1] = '';
  }

  // Regex in four parts (divided by '|') matching a rejected 'div', a
  // selected 'span', a link with image or an image. A pattern inside an
  // already found pattern gets ignored, to avoid floats inside floats.
  $text = preg_replace_callback('@<(
  div ([^>]*)(class\\s*=\\s*["\'](nofloat|' . trim($rejector[0]) . '|' . trim($rejector[1]) . ')["\'])([^>]*)>(.*?)</div| # find a rejected div
  span([^>]*)(class\\s*=\\s*["\'](float|' . trim($selector[0]) . '|' . trim($selector[1]) . ')["\'])([^>]*)>(.*?)</span| # find a selected span
  (a[^>]*)(href=)([^>]*)(><img[^>]*>(.*?)(</img>)?</a)| # find a link with image
  img (.+?) # find an image
  )>@isx', 'autofloat_filter_callback', $text);
  return $text;
}

/**
 * Filter callback. Conditionally apply a wrapper with an alternating class.
 */
function autofloat_filter_callback($matches) {
  static $count = 0;

  // See if no class of nofloat is found and we are not dealing with a div.
  if (preg_match('@((class\\s*=\\s*["\']nofloat)|(^<div))@i', $matches[0]) == 0) {

    // Start on the left or right depending on the settings.
    if (variable_get('autofloat_start', 0) == 0) {
      $zebra = $count % 2 ? 'even' : 'odd';
    }
    else {
      $zebra = $count % 2 ? 'odd' : 'even';
    }
    $count++;

    // Wrap our element in a span with alternating class.
    return '<span class="autofloat-' . $zebra . '">' . $matches[0] . '</span>';
  }
  else {
    return $matches[0];
  }
}

Functions

Namesort descending Description
autofloat_filter Implements hook_filter_info().
autofloat_filter_callback Filter callback. Conditionally apply a wrapper with an alternating class.
autofloat_filter_tips Returns the filter information for the filter help page.
autofloat_help Implements hook_help().
autofloat_init Implements hook_init().
autofloat_menu Implements hook_menu().
_autofloat Find a rejected 'div', a selected 'span', a link with image or an image.