Index: chessboard.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/chessboard/chessboard.module,v
retrieving revision 1.68
diff -u -p -r1.68 chessboard.module
--- chessboard.module	13 Nov 2010 22:16:53 -0000	1.68
+++ chessboard.module	14 Nov 2010 20:07:29 -0000
@@ -100,81 +100,18 @@ function chessboard_field($op, &$node, $
  * Implementation of hook_filter_tips().
  */
 function chessboard_filter_tips($delta, $format, $long=FALSE) {
-  if ($long) {
-    return t('<h4>Chessboard Diagrams</h4>
-<h5>Basic Usage</h5>
-<p>Use the pair of <code>[chessboard]</code> and <code>[/chessboard]</code> tags to define a chessboard. The following formats are recognized:</p>
-<ul>
-  <li>The piece placement field (i.e., the first field) of the FEN (Forsyth-Edwards Notation) syntax. E.g., for the starting position:
-    <code>[chessboard]rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[/chessboard]</code>
-  </li>
-  <li>A simple and direct piece placement format. E.g., for the starting position:
-<pre>[chessboard]
-rnbqkbnr
-pppppppp
---------
---------
---------
---------
-PPPPPPPP
-RNBQKBNR
-[/chessboard]</pre>
-Note that a dash (-) character represents an empty square.
-  </li>
-  <li>A mixture of the previous two formats. This is possible because the previous formats are in fact compatible to each other. E.g., for the starting position:
-<pre>[chessboard]
-rnbqkbnr
-pppppppp
-8/8/8/8
-PPPPPPPP
-RNBQKBNR
-[/chessboard]</pre>
-  However, the use of this format is NOT encouraged as it may lead to confusion.
-  </li>
-</ul>
-<p>The renderer also supports a special feature that will converts \'x\' character to a marked square. For example, to display the diagram below which shows the squares a knight can control, you can write</p>
-<pre>[chessboard]
---------
----x-x--
---x---x-
-----N---
---x---x-
----x-x--
---------
---------
-[/chessboard]</pre>
-<p>or</p>
-<pre>[chessboard]8/3x1x2/2x3x1/4N3/2x3x1/3x1x2/8/8[/chessboard]</pre>
-
-<h5>Advanced Usage</h5>
-<p>You can configure the appearance of the chessboard with a few flags, with the following syntax:</p>
-<pre>[chessboard](<i>flags</i>)board[/chessboard]</pre>
-<p>The following flags are supported:</p>
-<ul>
-  <li>Borders. Valid flags are T, B, L, and R, which correspond to the top border, the bottom border, the left border, and the right border respectively. The corresponding borders of the chessboard will be rendered for the flags specified. (Default: none is specified.)</li>
-  <li>Number of Files. Valid flags are positive integers such as 4, 6, and 12. (Default: 8.)</li>
-  <li>Color of the Upper Left Square. Valid flags are d and l, which correspond to the dark color and the light color. (Default: l.)</li>
-</ul>
-<p>These flags are especially useful in displaying a portion of a chessboard. For example, the following sentence illustrates a smothered mate at the upper-right corner:</p>
-<pre>[chessboard](d3TR)
--rk
-Npp
----
-[/chessboard]
-</pre>
-<p>Any portion of a chessboard can be displayed by using these flags.</p>
-');
-  }
-  else {
-    return t('Renders chessboard diagrams.');
-  }
+  return _chessboard_filter_0_tips(NULL, NULL, $long);
 }
 
-
 /**
  * Implementation of hook_filter().
  */
 function chessboard_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
+  static $filter;
+  if (!isset($filter)) {
+    $filter = &new stdClass();
+  }
+
   switch ($op) {
     case 'list':
       return array(0 => t('Chessboard'));
@@ -183,10 +120,10 @@ function chessboard_filter($op, $delta =
       return t('Renders chessboard diagrams.');
 
     case 'process':
-      // Prepare the callback function.
-      _chessboard_filter_callback(NULL, $format);
+      $filter->format = $format;
+      $filter->settings = array();
 
-      return preg_replace_callback('@\[chessboard\](.*?)\[/chessboard\]@si', '_chessboard_filter_callback', $text);
+      return _chessboard_filter_0($text, $filter);
 
     default:
       return $text;
@@ -259,24 +196,114 @@ function chessboard_render_elements(&$el
 }
 
 /**
- * Helper function for chessboard_filter().
+ * Chessboard filter. Provides filtering of input into chessboard diagrams.
+ *
+ * @param $filter
+ *   The filter object containing settings for the given format.
+ *
+ * @param $text
+ *   The content to filter.
+ */
+function _chessboard_filter_0($text, $filter) {
+  // Prepare the callback function.
+  _chessboard_filter_callback(NULL, $filter);
+
+  return preg_replace_callback('@\[chessboard\](.*?)\[/chessboard\]@si', '_chessboard_filter_callback', $text);
+}
+
+/**
+ * Filter tips callback for chessboard filter.
+ */
+function _chessboard_filter_0_tips($filter, $format, $long = FALSE) {
+  if ($long) {
+    return t('<h4>Chessboard Diagrams</h4>
+<h5>Basic Usage</h5>
+<p>Use the pair of <code>[chessboard]</code> and <code>[/chessboard]</code> tags to define a chessboard. The following formats are recognized:</p>
+<ul>
+  <li>The piece placement field (i.e., the first field) of the FEN (Forsyth-Edwards Notation) syntax. E.g., for the starting position:
+    <code>[chessboard]rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[/chessboard]</code>
+  </li>
+  <li>A simple and direct piece placement format. E.g., for the starting position:
+<pre>[chessboard]
+rnbqkbnr
+pppppppp
+--------
+--------
+--------
+--------
+PPPPPPPP
+RNBQKBNR
+[/chessboard]</pre>
+Note that a dash (-) character represents an empty square.
+  </li>
+  <li>A mixture of the previous two formats. This is possible because the previous formats are in fact compatible to each other. E.g., for the starting position:
+<pre>[chessboard]
+rnbqkbnr
+pppppppp
+8/8/8/8
+PPPPPPPP
+RNBQKBNR
+[/chessboard]</pre>
+  However, the use of this format is NOT encouraged as it may lead to confusion.
+  </li>
+</ul>
+<p>The renderer also supports a special feature that will converts \'x\' character to a marked square. For example, to display the diagram below which shows the squares a knight can control, you can write</p>
+<pre>[chessboard]
+--------
+---x-x--
+--x---x-
+----N---
+--x---x-
+---x-x--
+--------
+--------
+[/chessboard]</pre>
+<p>or</p>
+<pre>[chessboard]8/3x1x2/2x3x1/4N3/2x3x1/3x1x2/8/8[/chessboard]</pre>
+
+<h5>Advanced Usage</h5>
+<p>You can configure the appearance of the chessboard with a few flags, with the following syntax:</p>
+<pre>[chessboard](<i>flags</i>)board[/chessboard]</pre>
+<p>The following flags are supported:</p>
+<ul>
+  <li>Borders. Valid flags are T, B, L, and R, which correspond to the top border, the bottom border, the left border, and the right border respectively. The corresponding borders of the chessboard will be rendered for the flags specified. (Default: none is specified.)</li>
+  <li>Number of Files. Valid flags are positive integers such as 4, 6, and 12. (Default: 8.)</li>
+  <li>Color of the Upper Left Square. Valid flags are d and l, which correspond to the dark color and the light color. (Default: l.)</li>
+</ul>
+<p>These flags are especially useful in displaying a portion of a chessboard. For example, the following sentence illustrates a smothered mate at the upper-right corner:</p>
+<pre>[chessboard](d3TR)
+-rk
+Npp
+---
+[/chessboard]
+</pre>
+<p>Any portion of a chessboard can be displayed by using these flags.</p>
+');
+  }
+  else {
+    return t('Renders chessboard diagrams.');
+  }
+}
+
+/**
+ * preg_replace callback to make chessboard diagrams out of chessboard syntax.
  *
  * @param $matches
  *   An array of matches found by preg_replace_callback(). Elements 0 and 1 of
  *   $matches must be the complete match and the piece placement field.
  *
- * @param $set_format
- *   The text format. This should only be set during a preparatory call before
- *   preg_replace_callback().
+ * @param $set_filter
+ *   The filter object containing settings for the given format. This should
+ *   only be set during a preparatory call before preg_replace_callback().
  *
  * @return
  *   A <code>string</code> containing the replacement text.
  */
-function _chessboard_filter_callback($matches, $set_format = NULL) {
-  static $format;
-  if (isset($set_format)) {
+function _chessboard_filter_callback($matches, $set_filter = NULL) {
+  static $filter;
+  if (isset($set_filter)) {
     // This is a preparatory call.
-    $format = $set_format;
+    $filter = $set_filter;
     return;
   }
   $item = array(
@@ -288,13 +315,13 @@ function _chessboard_filter_callback($ma
   chessboard_field('sanitize', $node, $field, $items, FALSE, FALSE);
   $element = chessboard_formatter_view($items);
   $output = chessboard_render_elements($element);
-  if (isset($element['#attached']) && filter_format_allowcache($format)) {
+  if (isset($element['#attached']) && filter_format_allowcache($filter->format)) {
     // Store attached structures so we can process these when cached filtered text is being output.
     $data['#attached'] = $element['#attached'];
-    $cid = $format . ':chessboard-filter-attached' . md5(serialize($element['#attached']));
+    $cid = $filter->format . ':chessboard-filter-attached' . md5(serialize($element['#attached']));
     $expire = time() + (60 * 60 * 24) + (60 * 60);
     cache_set($cid, $data, 'cache_filter', $expire);
-    $list_cid = $format . ':chessboard-filter-attached-cids';
+    $list_cid = $filter->format . ':chessboard-filter-attached-cids';
     $cache = cache_get($list_cid, 'cache_filter');
     $cids = isset($cache->data) ? $cache->data : array();
     $cids[$cid] = TRUE;
