Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / Injector / PurifierLinkify.php
bloba7686297c2d3e8a1de4a0ec043f7860d36aa5ed0
1 <?php
3 require_once 'HTMLPurifier/Injector.php';
5 HTMLPurifier_ConfigSchema::define(
6 'AutoFormat', 'PurifierLinkify', false, 'bool', '
7 <p>
8 Internal auto-formatter that converts configuration directives in
9 syntax <a>%Namespace.Directive</a> to links. <code>a</code> tags
10 with the <code>href</code> attribute must be allowed.
11 This directive has been available since 2.0.1.
12 </p>
13 ');
15 HTMLPurifier_ConfigSchema::define(
16 'AutoFormatParam', 'PurifierLinkifyDocURL', '#%s', 'string', '
17 <p>
18 Location of configuration documentation to link to, let %s substitute
19 into the configuration\'s namespace and directive names sans the percent
20 sign. This directive has been available since 2.0.1.
21 </p>
22 ');
24 /**
25 * Injector that converts configuration directive syntax %Namespace.Directive
26 * to links
28 class HTMLPurifier_Injector_PurifierLinkify extends HTMLPurifier_Injector
31 var $name = 'PurifierLinkify';
32 var $docURL;
33 var $needed = array('a' => array('href'));
35 function prepare($config, &$context) {
36 $this->docURL = $config->get('AutoFormatParam', 'PurifierLinkifyDocURL');
37 return parent::prepare($config, $context);
40 function handleText(&$token) {
41 if (!$this->allowsElement('a')) return;
42 if (strpos($token->data, '%') === false) return;
44 $bits = preg_split('#%([a-z0-9]+\.[a-z0-9]+)#Si', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
45 $token = array();
47 // $i = index
48 // $c = count
49 // $l = is link
50 for ($i = 0, $c = count($bits), $l = false; $i < $c; $i++, $l = !$l) {
51 if (!$l) {
52 if ($bits[$i] === '') continue;
53 $token[] = new HTMLPurifier_Token_Text($bits[$i]);
54 } else {
55 $token[] = new HTMLPurifier_Token_Start('a',
56 array('href' => str_replace('%s', $bits[$i], $this->docURL)));
57 $token[] = new HTMLPurifier_Token_Text('%' . $bits[$i]);
58 $token[] = new HTMLPurifier_Token_End('a');