| 1 |
<?php
|
| 2 |
// $Id: headinganchors.module,v 1.3 2007/05/16 17:41:24 deviantintegral Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This is a module which takes <h2> and <h3> headings and adds id attributes to them
|
| 7 |
* to allow them to be linked to in a URL. The following rules are used to create an
|
| 8 |
* attribute:
|
| 9 |
* - All non alphanumerics are removed
|
| 10 |
* - Case is preserved to allow for some measure of readability
|
| 11 |
* - HTML tags are stripped from in the middle of heading tags for the anchors
|
| 12 |
*
|
| 13 |
* Example: <h2>I Link To <a href="http://www.csaonline.ca/">csaonline.ca</a></h2>
|
| 14 |
* becomes:
|
| 15 |
* <h2 id="ILinkTocsaonline.ca">I Link To <a href="http://www.csaonline.ca/">csaonline.ca</a></h2>
|
| 16 |
* and can be referenced with /url#ILinkTocsaonline.ca
|
| 17 |
*
|
| 18 |
* Multiple headings with the same text will have the same ID. This should be changed to allow
|
| 19 |
* multiple subheadings.
|
| 20 |
*
|
| 21 |
* This file was built from the filter example on the Drupal website.
|
| 22 |
*/
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_help().
|
| 26 |
*
|
| 27 |
*/
|
| 28 |
function headinganchors_help($section) {
|
| 29 |
switch ($section) {
|
| 30 |
case 'admin/modules#description':
|
| 31 |
// This description is shown in the listing at admin/modules.
|
| 32 |
return t('A module to create named anchors based on HTML headings.');
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_filter_tips().
|
| 38 |
*/
|
| 39 |
function headinganchors_filter_tips($format, $long = FALSE) {
|
| 40 |
if ($long) {
|
| 41 |
return t('Every instance of second and third level headings (<h2> and <h3>) will be modified to include an id attribute for anchor linking.');
|
| 42 |
}
|
| 43 |
else {
|
| 44 |
return t('Insert heading anchors automatically.');
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Implementation of hook_filter().
|
| 50 |
*
|
| 51 |
* The bulk of filtering work is done here.
|
| 52 |
*/
|
| 53 |
function headinganchors_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 54 |
if ($op == 'list') {
|
| 55 |
return array(
|
| 56 |
0 => t('Headings to Anchors'));
|
| 57 |
}
|
| 58 |
// We currently only define one filter, so we don't need to switch on $delta
|
| 59 |
switch ($op) {
|
| 60 |
// Admin interface description
|
| 61 |
case 'description':
|
| 62 |
return t('Inserts named anchors for heading tags automatically.');
|
| 63 |
|
| 64 |
// Ensure that the content is regenerated on every preview
|
| 65 |
case 'no cache':
|
| 66 |
return TRUE;
|
| 67 |
|
| 68 |
// Need to find out if this is required
|
| 69 |
case 'prepare':
|
| 70 |
return $text;
|
| 71 |
|
| 72 |
// Here, we pull out the required information and add the anchor
|
| 73 |
case 'process':
|
| 74 |
// matches[1] is the level of the heading
|
| 75 |
// matches[2] is the text of the heading
|
| 76 |
// matches[][$count] is the current heading being modified
|
| 77 |
$matches = array();
|
| 78 |
// In the future, there should probably be some way to define which headings are modified
|
| 79 |
// We assume that the HTML is valid and that the tags match each other.
|
| 80 |
preg_match_all("/<h([2-3])>(.*)<\/h[2-3]>/", $text, $matches, PREG_PATTERN_ORDER);
|
| 81 |
$count = 0;
|
| 82 |
$anchors = array();
|
| 83 |
foreach ($matches[0] as $match) {
|
| 84 |
// Pull out the level and the heading
|
| 85 |
$level = $matches[1][$count];
|
| 86 |
$heading = $matches[2][$count];
|
| 87 |
|
| 88 |
// We do strip_tags to get rid of any extra HTML as we don't want that in the id.
|
| 89 |
// Strip out amp; from entity as it's ugly
|
| 90 |
$anchor = preg_replace("/&/", "", strip_tags($matches[2][$count]));
|
| 91 |
// Then, we punt that result and strip out anything which isn't alphanumeric
|
| 92 |
$anchor = preg_replace("/[^A-Za-z0-9]/", "", $anchor);
|
| 93 |
// Remove leading digits
|
| 94 |
$anchor = preg_replace("/^[0-9]+/", "", $anchor);
|
| 95 |
|
| 96 |
// Look for duplicate ID's, and add -number to make them unique
|
| 97 |
if (array_key_exists($anchor, $anchors)) {
|
| 98 |
$anchors[$anchor]++;
|
| 99 |
}
|
| 100 |
else {
|
| 101 |
$anchors[$anchor] = 0;
|
| 102 |
}
|
| 103 |
if ($anchors[$anchor] > 0) {
|
| 104 |
$anchor .= "-" . $anchors[$anchor];
|
| 105 |
}
|
| 106 |
|
| 107 |
$pattern = "/<h".$level.">".preg_quote($heading, '/')."<\/h".$level.">/";
|
| 108 |
|
| 109 |
// Now, append everything together to create the new heading
|
| 110 |
$replacement = "<h".$level." id=\"".$anchor."\">".$heading."</h".$level.">";
|
| 111 |
|
| 112 |
// Now, find the heading we were editing and replace it in the text.
|
| 113 |
// Limit of one so that identical headers are only replaced one at a time
|
| 114 |
$text = preg_replace($pattern, $replacement, $text, 1);
|
| 115 |
$count++;
|
| 116 |
}
|
| 117 |
return $text;
|
| 118 |
}
|
| 119 |
}
|
| 120 |
?>
|