| 1 |
<?php
|
| 2 |
// $Id: urlclass.module,v 1.5 2008/07/18 09:52:41 sanduhrs Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Simple filter module for Drupal
|
| 7 |
*
|
| 8 |
* Filters content for URLs and adds a class reflecting their target.
|
| 9 |
*
|
| 10 |
* @author
|
| 11 |
* Stefan Auditor <stefan.auditor@erdfisch.de>
|
| 12 |
* for erdfisch http://erdfisch.de
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_help().
|
| 17 |
*/
|
| 18 |
function urlclass_help($path, $arg) {
|
| 19 |
switch ($path) {
|
| 20 |
case 'admin/help#urlclass':
|
| 21 |
$output = '<p>'. t('The URLClass module automatically adds a CSS class to <a>-elements reflecting their target.') .'</p>';
|
| 22 |
$output .= '<p>'. t('Use Input Formats to enable the URL filter') .'</p>';
|
| 23 |
$output .= t('<ol><li>Select an existing Input Format or add a new one</li><li>Configure the Input Format</li><li>Enable URL class filter and Save configuration</li><li>Rearrange the weight of the URL filter depending on what filters exist in the format</li></ol>');
|
| 24 |
$output .= '<p>'. t('You can enable the urlfilter for an input format from <a href="%admin-filters">Administer >> Site Configuration >> Input Filter</a>.', array('%admin-filters' => url('admin/settings/filters'))) .'</p>';
|
| 25 |
return $output;
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Implementation of hook_init().
|
| 31 |
*/
|
| 32 |
function urlclass_init() {
|
| 33 |
drupal_add_css(drupal_get_path('module', 'urlclass') .'/urlclass.css', 'module');
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_filter().
|
| 38 |
*/
|
| 39 |
function urlclass_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 40 |
switch ($op) {
|
| 41 |
case 'list':
|
| 42 |
return array(0 => t('URL Class filter'));
|
| 43 |
|
| 44 |
case 'description':
|
| 45 |
return t('Adds a CSS class to a URL.');
|
| 46 |
|
| 47 |
case 'process':
|
| 48 |
$text = preg_replace_callback('/<a.+?href=\"(http:\/\/.+?)\"[^>]*>.+?<\/a>/i', '_urlclass_replace', $text);
|
| 49 |
return $text;
|
| 50 |
|
| 51 |
default:
|
| 52 |
return $text;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Callback function for the filter
|
| 58 |
*/
|
| 59 |
function _urlclass_replace($match) {
|
| 60 |
$url = parse_url($match[1]);
|
| 61 |
$domain = explode('.', $url['host']);
|
| 62 |
$domain = $domain[(count($domain)-2)];
|
| 63 |
|
| 64 |
if (stristr($match[0], 'class')) $match[0] = str_replace('class="', 'class="uc-'. check_plain($domain) .' ', $match[0]);
|
| 65 |
else $match[0] = str_replace('">', '" class="uc-'. check_plain($domain) .'">', $match[0]);
|
| 66 |
|
| 67 |
return $match[0];
|
| 68 |
}
|