--- /dev/null
+<?php\r
+\r
+/**\r
+ * Injector that converts configuration directive syntax %Namespace.Directive\r
+ * to links\r
+ */\r
+class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector\r
+{\r
+ /**\r
+ * @type string\r
+ */\r
+ public $name = 'PurifierLinkify';\r
+\r
+ /**\r
+ * @type string\r
+ */\r
+ public $docURL;\r
+\r
+ /**\r
+ * @type array\r
+ */\r
+ public $needed = array('a' => array('href'));\r
+\r
+ /**\r
+ * @param HTMLPurifier_Config $config\r
+ * @param HTMLPurifier_Context $context\r
+ * @return string\r
+ */\r
+ public function prepare($config, $context)\r
+ {\r
+ $this->docURL = $config->get('AutoFormat.PurifierLinkify.DocURL');\r
+ return parent::prepare($config, $context);\r
+ }\r
+\r
+ /**\r
+ * @param HTMLPurifier_Token $token\r
+ */\r
+ public function handleText(&$token)\r
+ {\r
+ if (!$this->allowsElement('a')) {\r
+ return;\r
+ }\r
+ if (strpos($token->data, '%') === false) {\r
+ return;\r
+ }\r
+\r
+ $bits = preg_split('#%([a-z0-9]+\.[a-z0-9]+)#Si', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);\r
+ $token = array();\r
+\r
+ // $i = index\r
+ // $c = count\r
+ // $l = is link\r
+ for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {\r
+ if (!$l) {\r
+ if ($bits[$i] === '') {\r
+ continue;\r
+ }\r
+ $token[] = new HTMLPurifier_Token_Text($bits[$i]);\r
+ } else {\r
+ $token[] = new HTMLPurifier_Token_Start(\r
+ 'a',\r
+ array('href' => str_replace('%s', $bits[$i], $this->docURL))\r
+ );\r
+ $token[] = new HTMLPurifier_Token_Text('%' . $bits[$i]);\r
+ $token[] = new HTMLPurifier_Token_End('a');\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+// vim: et sw=4 sts=4\r