You are here

spoiler.module in Spoiler 6

Same filename and directory in other branches
  1. 5 spoiler.module
  2. 7 spoiler.module

Spoiler module: This module allows users to hide potential "spoiler" content by inserting them between [spoiler][/spoiler] tags. These tags will be converted to HTML by this filter. If JavaScript is available, then a button will be presented to the user which can be clicked to display the content. If JavaScript is unavailable, then the relevant CSS rules will set the foreground and background colours to the same value, thereby rendering the text invisible, until highlighted.

File

spoiler.module
View source
<?php

/**
 * @file
 *   Spoiler module: This module allows users to hide potential "spoiler"
 * content by inserting them between [spoiler][/spoiler] tags. These tags will
 * be converted to HTML by this filter. If JavaScript is available, then a
 * button will be presented to the user which can be clicked to display the
 * content. If JavaScript is unavailable, then the relevant CSS rules will set
 * the foreground and background colours to the same value, thereby rendering
 * the text invisible, until highlighted.
 */

/**
 * Implementation of hook_init().
 */
function spoiler_init() {
  $path = drupal_get_path('module', 'spoiler');
  drupal_add_css($path . '/spoiler.css');
  drupal_add_js($path . '/spoiler.js');
}

/**
 * Implementation of hook_theme().
 */
function spoiler_theme() {
  return array(
    'spoiler' => array(
      'arguments' => array(
        'spoiler' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_filter().
 */
function spoiler_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Spoiler filter'),
      );
    case 'description':
      return t('Converts [spoiler][/spoiler] tags to markup which hides the content within. When Javascript is available, a button is made available which can be clicked to view the hidden content. Alternatively, when Javascript is disabled, the filter sets the foreground and background colours to the same value, effectively rendering the content within invisible until highlighted.');
    case 'process':
      return _spoiler_filter_process($text);
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function spoiler_filter_tips($delta, $format, $long = FALSE) {
  return t('Potential spoilers can be hidden between [spoiler][/spoiler] tags to hide them by default.');
}

/**
 * Replace [spoiler] tags with markup.
 *
 * @param $text
 *   The text with the [spoiler] tags that need to be replaced with HTML tags.
 *
 * @return $text
 *   Filtered text.
 */
function _spoiler_filter_process($text) {

  // Keep it simple. Use of the lazy quantifier allows use of multiple spoiler
  // blocks but does not address nested spoilers.
  return preg_replace_callback('#\\[\\s*spoiler\\s*\\](.*?)\\[\\s*/\\s*spoiler\\s*\\]#is', create_function('$matches', 'return theme("spoiler", $matches[1]);'), $text);
}
function theme_spoiler($spoiler) {
  return '<div class="spoiler"><div class="spoiler-warning">' . t('<span>Spoiler:</span> Highlight to view') . '</div><div class="spoiler-content">' . $spoiler . '</div></div>';
}

Functions

Namesort descending Description
spoiler_filter Implementation of hook_filter().
spoiler_filter_tips Implementation of hook_filter_tips().
spoiler_init Implementation of hook_init().
spoiler_theme Implementation of hook_theme().
theme_spoiler
_spoiler_filter_process Replace [spoiler] tags with markup.