You are here

autofloat.module in AutoFloat 7

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', array(
      'group' => CSS_DEFAULT,
      'every_page' => TRUE,
    ));
  }
}

/**
 * Implements hook_menu().
 */
function autofloat_menu() {
  $items['admin/config/content/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_info() {
  $filters['filter_autofloat'] = array(
    'title' => t('Auto Float'),
    'description' => t('Images will float left and right unless escaped with a class "nofloat".'),
    'process callback' => '_autofloat_filter',
    'settings callback' => '_autofloat_filter_settings',
    'tips callback' => '_autofloat_filter_tips',
    'weight' => 10,
  );
  return $filters;
}

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

/**
 * Notice in the AutoFloat settings on the text format configuration page.
 */
function _autofloat_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
  $elements['notice'] = array(
    '#markup' => t('!config_link are shared by all the text formats where it is enabled.', array(
      '!config_link' => l(t('AutoFloat Filter settings'), 'admin/config/content/autofloat', array(
        'attributes' => array(
          'title' => 'Unified settings page',
        ),
      )),
    )),
  );
  return $elements;
}

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

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

  // Make sure a variable exists, if not the regex breaks.
  if (isset($selector[0]) == FALSE) {
    $select = '';
  }
  else {

    // Set the first selector.
    $select = trim($selector[0]);
  }
  foreach (array_slice($selector, 1) as $value) {

    // Concatenate with subsequent selectors, divided with OR symbol.
    $select .= '|' . trim($value);
  }
  $rejector = variable_get('autofloat_div', 'flickr-photoset');

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

  // Make sure a variable exists, if not the regex breaks.
  if (isset($rejector[0]) == FALSE) {
    $reject = '';
  }
  else {

    // Set the first rejector.
    $reject = trim($rejector[0]);
  }
  foreach (array_slice($rejector, 1) as $value) {

    // Concatenate with subsequent rejectors, divided with OR symbol.
    $reject .= '|' . trim($value);
  }

  // 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|' . $reject . ')["\'])([^>]*)>(.*?)</div| # find a rejected div
  span([^>]*)(class\\s*=\\s*["\'](float|' . $select . ')["\'])([^>]*)>(.*?)</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_callback Filter callback. Conditionally apply a wrapper with an alternating class.
autofloat_filter_info Implements hook_filter_info().
autofloat_help Implements hook_help().
autofloat_init Implements hook_init().
autofloat_menu Implements hook_menu().
_autofloat_filter Find a rejected 'div', a selected 'span', a link with image or an image.
_autofloat_filter_settings Notice in the AutoFloat settings on the text format configuration page.
_autofloat_filter_tips Returns the filter information for the filter help page.