]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/PurifierLinkify.php
[add] HTML Purifier added to clean code
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / Injector / PurifierLinkify.php
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/PurifierLinkify.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/Injector/PurifierLinkify.php
new file mode 100644 (file)
index 0000000..d7dd7d9
--- /dev/null
@@ -0,0 +1,71 @@
+<?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