PurifierLinkify.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Injector that converts configuration directive syntax %Namespace.Directive
  4. * to links
  5. */
  6. class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector
  7. {
  8. public $name = 'PurifierLinkify';
  9. public $docURL;
  10. public $needed = array('a' => array('href'));
  11. public function prepare($config, $context) {
  12. $this->docURL = $config->get('AutoFormat.PurifierLinkify.DocURL');
  13. return parent::prepare($config, $context);
  14. }
  15. public function handleText(&$token) {
  16. if (!$this->allowsElement('a')) return;
  17. if (strpos($token->data, '%') === false) return;
  18. $bits = preg_split('#%([a-z0-9]+\.[a-z0-9]+)#Si', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
  19. $token = array();
  20. // $i = index
  21. // $c = count
  22. // $l = is link
  23. for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
  24. if (!$l) {
  25. if ($bits[$i] === '') continue;
  26. $token[] = new HTMLPurifier_Token_Text($bits[$i]);
  27. } else {
  28. $token[] = new HTMLPurifier_Token_Start('a',
  29. array('href' => str_replace('%s', $bits[$i], $this->docURL)));
  30. $token[] = new HTMLPurifier_Token_Text('%' . $bits[$i]);
  31. $token[] = new HTMLPurifier_Token_End('a');
  32. }
  33. }
  34. }
  35. }
  36. // vim: et sw=4 sts=4