Announce PHP 7 support.
[htmlpurifier.git] / library / HTMLPurifier / Injector / PurifierLinkify.php
blobcb9046f334657880c5b0100f0ae55236622ccbfd
1 <?php
3 /**
4 * Injector that converts configuration directive syntax %Namespace.Directive
5 * to links
6 */
7 class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector
9 /**
10 * @type string
12 public $name = 'PurifierLinkify';
14 /**
15 * @type string
17 public $docURL;
19 /**
20 * @type array
22 public $needed = array('a' => array('href'));
24 /**
25 * @param HTMLPurifier_Config $config
26 * @param HTMLPurifier_Context $context
27 * @return string
29 public function prepare($config, $context)
31 $this->docURL = $config->get('AutoFormat.PurifierLinkify.DocURL');
32 return parent::prepare($config, $context);
35 /**
36 * @param HTMLPurifier_Token $token
38 public function handleText(&$token)
40 if (!$this->allowsElement('a')) {
41 return;
43 if (strpos($token->data, '%') === false) {
44 return;
47 $bits = preg_split('#%([a-z0-9]+\.[a-z0-9]+)#Si', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
48 $token = array();
50 // $i = index
51 // $c = count
52 // $l = is link
53 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
54 if (!$l) {
55 if ($bits[$i] === '') {
56 continue;
58 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
59 } else {
60 $token[] = new HTMLPurifier_Token_Start(
61 'a',
62 array('href' => str_replace('%s', $bits[$i], $this->docURL))
64 $token[] = new HTMLPurifier_Token_Text('%' . $bits[$i]);
65 $token[] = new HTMLPurifier_Token_End('a');
71 // vim: et sw=4 sts=4