Replace the Q tag with a CSS class

Last modified: March 11, 2007 - 13:40

After reading Mark Pilgrim's article about the Q tag, I decided, "me too!" Except that it doesn't display correctly in Internet Explorer, I wrote a filter, first for WordPress, and now for Drupal, which takes <q>text</q> or <q cite="[url]">text</q> with just <span class="q">text</span>. In my articles, I still use the Q tag, in the hopes of one day running a script to make a ranked list of the URLs I've quoted over the years. Note that the escaping below is correct, and necessary for the description to appear correctly in administer » modules.

You'll need to save the module as quotable.module. It adds a filter called "Quotable" which you will have to enable for your input formats under administer » input formats.

<?php
/**
* Implementation of hook_help().
*/
function quotable_help($section) {
  switch (
$section) {
    case
'admin/modules#description':
      return
t('Adds a filter that replaces &lt;q&gt;text&lt;/q&gt; or &lt;q cite="http://example.com/"&gt;text&lt;/q&gt; with &lt;span class="q"&gt;text&lt;/span&gt;');
      break;
  }
}

/**
* Implementation of hook_filter().
*/
function quotable_filter($op, $delta = 0, $format = -1, $text = '') {
  switch (
$op) {
    case
'list':
      return array(
0 => t('Quotable filter'));
    case
'description':
      return
t('Replaces the &lt;q cite="[url]"&gt; element with a &lt;span class="q"&gt; wrapper');
    case
'process':
     
$replace = preg_replace("/<q[^>]*>(([^<]|<[^\/]|<\/[^q])*)<\/q>/", "<span class=\"q\">&ldquo;\$1&rdquo;</span>", $text);
      return
$replace;
    default:
      return
$text;
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.