You are here

autofloat.module in AutoFloat 7.2

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_page_build().
 */
function autofloat_page_build(&$page) {

  // Determine the setting to use autofloat.css or not.
  if (variable_get('autofloat_css', 1)) {
    drupal_add_css(drupal_get_path('module', 'autofloat') . '/css/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) {
  $selector = variable_get('autofloat_span', 'flickr-wrap');

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

  // Make sure both array keys exist.
  if (isset($selector[0]) == FALSE) {
    $selector[0] = 'non-existing-class-value';
  }
  if (isset($selector[1]) == FALSE) {
    $selector[1] = 'non-existing-class-value';
  }
  $rejector = variable_get('autofloat_div', 'flickr-photoset');

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

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

  // Load string to DOMDocument.
  $doc = filter_dom_load($text);
  $xpath = new DOMXPath($doc);

  // Iterate through the string neglecting elements nested inside those already
  // processed or those to reject.
  foreach ($xpath
    ->query('//span[not(ancestor-or-self::*[contains(@class, "nofloat")] or ancestor-or-self::*[contains(@class, "' . $rejector[0] . '")] or ancestor-or-self::*[contains(@class, "' . $rejector[1] . '")] or ancestor::span[contains(@class, "float") or contains(@class, "' . $selector[0] . '") or contains(@class, "' . $selector[1] . '")])] | //img[not(ancestor-or-self::*[contains(@class, "nofloat")] or ancestor-or-self::*[contains(@class, "' . $rejector[0] . '")] or ancestor-or-self::*[contains(@class, "' . $rejector[1] . '")] or ancestor::span[contains(@class, "float") or contains(@class, "' . $selector[0] . '") or contains(@class, "' . $selector[1] . '")])]') as $tag) {

    // 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++;

    // Add an alternating class value but maintain the existing ones.
    $tag
      ->setAttribute('class', 'autofloat-' . $zebra . ' ' . $tag
      ->getAttribute('class'));
  }

  // Convert a DOM object back to an HTML snippet.
  $subject = filter_dom_serialize($doc);
  return $subject;
}

Functions

Namesort descending Description
autofloat_filter_info Implements hook_filter_info().
autofloat_help Implements hook_help().
autofloat_menu Implements hook_menu().
autofloat_page_build Implements hook_page_build().
_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.