/[drupal]/contributions/modules/olf/olf.module
ViewVC logotype

Contents of /contributions/modules/olf/olf.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Tue Oct 10 14:51:18 2006 UTC (3 years, 1 month ago) by m3avrck
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
Changes since 1.2: +2 -2 lines
File MIME type: text/x-php
better url recognization based on url(), thanks webchick for the idea
1 <?php
2 // $Id: olf.module,v 1.2 2006/10/08 21:51:51 m3avrck Exp $
3
4 function olf_help($section) {
5 switch ($section) {
6 case 'admin/modules#description':
7 return t('Automatically adds a configurable CSS class to all outgoing links.');
8 }
9 }
10
11
12 function olf_filter($op, $delta = 0, $format = -1, $text = '') {
13 switch ($op) {
14 case 'list':
15 return array(0 => t('Outgoing links filter'));
16
17 case 'description':
18 return t('Adds a CSS class to all outgoing links.');
19
20 case 'settings':
21 $form['filter_olf'] = array(
22 '#type' => 'fieldset',
23 '#title' => t('Outgoing links filter'),
24 '#collapsible' => TRUE,
25 '#collapsed' => TRUE
26 );
27 $form['filter_olf']['olf_class_'. $format] = array(
28 '#type' => 'textfield',
29 '#title' => t('CSS class name'),
30 '#default_value' => variable_get('olf_class_'. $format, 'outgoing'),
31 '#description' => t('The class name that should be applied to all outgoing links.')
32 );
33
34 return $form;
35
36 case 'process':
37 global $olf_class;
38 $olf_class = variable_get('olf_class_'. $format, 'outgoing');
39 // find all <a> tags and match their href so we can test if the link is outgoing or not
40 $text = preg_replace_callback('!<a.*?href="([^"]+)".*?>!', olf_replace1, $text);
41 return $text;
42
43 default:
44 return $text;
45 }
46 }
47
48 function olf_replace1($match) {
49 global $olf_class;
50 $link = $match[0];
51
52 $site_url = url(NULL, NULL, NULL, TRUE);
53
54 // if the link is external and it's not an absolute link to the current site
55 if (strpos($match[1], ':') !== FALSE && strpos($match[1], $site_url) === FALSE) {
56 // if there is no class
57 if (strpos($match[0], 'class="') === FALSE) {
58 // it is faster to use PHP's string functions then complex regexes
59 // in this case we strip off the last > in the <a> and then append our class and close the tag
60 $link = substr($match[0], 0, -1);
61 $link .= ' class="'. $olf_class .'">';
62 }
63 else {
64 // append outgoing class if this <a> already has a class
65 $link = preg_replace('!class="([^"]+)"!', 'class="${1} '. $olf_class .'"', $match[0]);
66 }
67 }
68
69 return $link;
70 }

  ViewVC Help
Powered by ViewVC 1.1.2