Remove horde/Core dependency from horde/Text_Filter
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 20 Jul 2010 20:30:59 +0000 (14:30 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 20 Jul 2010 21:21:38 +0000 (15:21 -0600)
framework/Core/lib/Horde/Core/Factory/TextFilter.php
framework/Core/lib/Horde/Core/Text/Filter/Bbcode.php [new file with mode: 0644]
framework/Core/lib/Horde/Core/Text/Filter/Highlightquotes.php [new file with mode: 0644]
framework/Core/package.xml
framework/Text_Filter/lib/Horde/Text/Filter/Bbcode.php
framework/Text_Filter/lib/Horde/Text/Filter/Highlightquotes.php
framework/Text_Filter/package.xml

index 48929e9..a1f4ae5 100644 (file)
@@ -103,6 +103,10 @@ class Horde_Core_Factory_TextFilter
         $lc_driver = Horde_String::lower($driver);
 
         switch ($lc_driver) {
+        case 'bbcode':
+            $driver = 'Horde_Core_Text_Filter_Bbcode';
+            break;
+
         case 'emails':
             $driver = 'Horde_Core_Text_Filter_Emails';
             break;
@@ -111,6 +115,10 @@ class Horde_Core_Factory_TextFilter
             $driver = 'Horde_Core_Text_Filter_Emoticons';
             break;
 
+        case 'highlightquotes':
+            $driver = 'Horde_Core_Text_Filter_Highlightquotes';
+            break;
+
         case 'linkurls':
             if (!isset($params['callback'])) {
                 $params['callback'] = 'Horde::externalUrl';
diff --git a/framework/Core/lib/Horde/Core/Text/Filter/Bbcode.php b/framework/Core/lib/Horde/Core/Text/Filter/Bbcode.php
new file mode 100644 (file)
index 0000000..c6c8ab3
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Class that extends the base Bbcode class to allow output of Horde urls.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Core
+ */
+class Horde_Core_Text_Filter_Bbcode extends Horde_Text_Filter_Bbcode
+{
+    /**
+     * Return link for use in getPatterns() regexp.
+     *
+     * @var string $url    The URL.
+     * @var string $title  The link title.
+     *
+     * @return string  The opening <a> tag.
+     */
+    protected function _link($url, $title)
+    {
+        return Horde::link($url, $title);
+    }
+
+}
diff --git a/framework/Core/lib/Horde/Core/Text/Filter/Highlightquotes.php b/framework/Core/lib/Horde/Core/Text/Filter/Highlightquotes.php
new file mode 100644 (file)
index 0000000..5320ced
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Class that extends the base Highlightquotes class to allow toggling of
+ * quoteblocks via javascript.
+ *
+ * CSS class names "toggleQuoteHide" and "toggleQuoteShow" are used to style
+ * toggle text.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Core
+ */
+class Horde_Core_Text_Filter_Highlightquotes extends Horde_Text_Filter_Highlightquotes
+{
+     /**
+     * Constructor.
+     *
+     * @param array $params  Parameters that the filter instance needs.
+     *                       Additional Parameters to base driver:
+     * <pre>
+     * 'noJS' - (boolean) Don't add javascript toggle code.
+     *          DEFAULT: false
+     * 'outputJS' - (boolean) Add necessary JS files?
+     *              DEFAULT: true
+     * </pre>
+     */
+    public function __construct(array $params = array())
+    {
+        $params = array_merge(array(
+            'noJS' => false,
+            'outputJS' => true
+        ), $params);
+
+        parent::__construct($params);
+
+        if (!$this->_params['noJS'] && $this->_params['outputJS']) {
+            Horde::addScriptFile('prototype.js', 'horde');
+        }
+    }
+
+    /**
+     * Add HTML code at the beginning of a large block of quoted lines.
+     *
+     * @param array $lines     Lines.
+     * @param integer $qcount  Number of lines in quoted level.
+     *
+     * @return string  HTML code.
+     */
+    protected function _beginLargeBlock($lines, $qcount)
+    {
+        if ($this->_params['noJS']) {
+            return '';
+        }
+
+        return (($this->_params['citeblock']) ? '<br />' : '') .
+            '<div class="toggleQuoteParent">' .
+            '<span ' . ($this->_params['outputJS'] ? 'onclick="[ this, this.next(), this.next(1) ].invoke(\'toggle\')" ' : '') .
+            'class="widget toggleQuoteShow"' . ($this->_params['hideBlocks'] ? '' : ' style="display:none"') . '>' . htmlspecialchars(sprintf(_("[Show Quoted Text - %d lines]"), $qcount)) . '</span>' .
+            '<span ' . ($this->_params['outputJS'] ? 'onclick="[ this, this.previous(), this.next() ].invoke(\'toggle\')" ' : "") .
+            'class="widget toggleQuoteHide"' . ($this->_params['hideBlocks'] ? ' style="display:none"' : '') . '>' . htmlspecialchars(_("[Hide Quoted Text]")) . '</span>';
+    }
+
+    /**
+     * Add HTML code at the end of a large block of quoted lines.
+     *
+     * @param array $lines     Lines.
+     * @param integer $qcount  Number of lines in quoted level.
+     *
+     * @return string  HTML code.
+     */
+    protected function _endLargeBlock($lines, $qcount)
+    {
+        return $this->_params['noJS']
+            ? ''
+            : '</div>';
+    }
+
+}
index b92c753..67ad707 100644 (file)
@@ -177,8 +177,10 @@ Application Framework.</description>
       </dir> <!-- /lib/Horde/Core/Prefs -->
       <dir name="Text">
        <dir name="Filter">
+        <file name="Bbcode.php" role="php" />
         <file name="Emails.php" role="php" />
         <file name="Emoticons.php" role="php" />
+        <file name="Highlightquotes.php" role="php" />
        </dir> <!-- /lib/Horde/Core/Text/Filter -->
       </dir> <!-- /lib/Horde/Core/Text -->
       <dir name="Tree">
@@ -420,8 +422,10 @@ Application Framework.</description>
    <install as="Horde/Core/Perms/Ui.php" name="lib/Horde/Core/Perms/Ui.php" />
    <install as="Horde/Core/Prefs/Ui.php" name="lib/Horde/Core/Prefs/Ui.php" />
    <install as="Horde/Core/Prefs/Ui/Widgets.php" name="lib/Horde/Core/Prefs/Ui/Widgets.php" />
+   <install as="Horde/Core/Text/Filter/Bbcode.php" name="lib/Horde/Core/Text/Filter/Bbcode.php" />
    <install as="Horde/Core/Text/Filter/Emails.php" name="lib/Horde/Core/Text/Filter/Emails.php" />
    <install as="Horde/Core/Text/Filter/Emoticons.php" name="lib/Horde/Core/Text/Filter/Emoticons.php" />
+   <install as="Horde/Core/Text/Filter/Highlightquotes.php" name="lib/Horde/Core/Text/Filter/Highlightquotes.php" />
    <install as="Horde/Core/Tree/Html.php" name="lib/Horde/Core/Tree/Html.php" />
    <install as="Horde/Core/Tree/Javascript.php" name="lib/Horde/Core/Tree/Javascript.php" />
    <install as="Horde/Core/Autoloader.php" name="lib/Horde/Core/Autoloader.php" />
index b3b1cc0..5173bca 100644 (file)
@@ -105,7 +105,8 @@ class Horde_Text_Filter_Bbcode extends Horde_Text_Filter_Base
             '[quote]' => '<blockquote>', '[/quote]' => '</blockquote>',
             '[list]' => '<ul>', '[/list]' => '</ul>',
             '[numlist]' => '<ol>', '[/numlist]' => '</ol>',
-            '[*]' => '<li>');
+            '[*]' => '<li>'
+        );
 
         /* When checking URLs we validate part of them, but it is up
          * to the user to write them correctly (in particular the
@@ -113,35 +114,42 @@ class Horde_Text_Filter_Bbcode extends Horde_Text_Filter_Base
          * expression in Mail_RFC822's isValidInetAddress() function,
          * slightly modified. */
         $regexp = array(
-            "#\[url\]((http|https)://([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\[/url\]#U" =>
-            Horde::link("$1", "$1") . "$1</a>",
+            "#\[url\]((http|https)://([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\[/url\]#U" => $this->_link("$1", "$1") . "$1</a>",
 
-            "#\[url\=((http|https)://([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\]([^<>]+)\[/url\]#U" =>
-            Horde::link("$1", "$1") . "$9</a>",
+            "#\[url\=((http|https)://([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\]([^<>]+)\[/url\]#U" => $this->_link("$1", "$1") . "$9</a>",
 
-            "#\[url\](([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\[/url\]#U" =>
-            Horde::link("http://$1", "http://$1") . "$1</a>",
+            "#\[url\](([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\[/url\]#U" => $this->_link("http://$1", "http://$1") . "$1</a>",
 
-            "#\[url\=(([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\]([^<>]+)\[/url\]#U" =>
-            Horde::link("http://$1", "http://$1") . "$8</a>",
+            "#\[url\=(([a-zA-Z\d][\w-]*)(\.[a-zA-Z\d][\w-]*)+(:(\d+))?(/([^<>]+))*)\]([^<>]+)\[/url\]#U" => $this->_link("http://$1", "http://$1") . "$8</a>",
 
-            "#\[email\](([*+!.&\#$|\'\\%\/0-9a-zA-Z^_`{}=?~:-]+)@(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z]{2,4}))\[/email\]#U" =>
-            Horde::link("mailto:$1", "mailto:$1") . "$1</a>",
+            "#\[email\](([*+!.&\#$|\'\\%\/0-9a-zA-Z^_`{}=?~:-]+)@(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z]{2,4}))\[/email\]#U" => $this->_link("mailto:$1", "mailto:$1") . "$1</a>",
 
-            "#\[email\=(([*+!.&\#$|\'\\%\/0-9a-zA-Z^_`{}=?~:-]+)@(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z]{2,4}))\]([^<>]+)\[/email\]#U" =>
-            Horde::link("mailto:$1", "mailto:$1") . "$5</a>",
+            "#\[email\=(([*+!.&\#$|\'\\%\/0-9a-zA-Z^_`{}=?~:-]+)@(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z]{2,4}))\]([^<>]+)\[/email\]#U" => $this->_link("mailto:$1", "mailto:$1") . "$5</a>",
 
-            "#\[img\](.*)\[/img\]#U" =>
-            "<img src=\"$1\" alt=\"$1\" />",
+            "#\[img\](.*)\[/img\]#U" => "<img src=\"$1\" alt=\"$1\" />",
 
-            "#\[img\=(.*)\](.*)\[/img\]#U" =>
-            "<img src=\"$1\" alt=\"$2\" title=\"$2\" />",
+            "#\[img\=(.*)\](.*)\[/img\]#U" => "<img src=\"$1\" alt=\"$2\" title=\"$2\" />",
 
-            "#\[color\=(.*)\](.*)\[/color\]#U" =>
-            "<span style=\"color: $1;\">$2</span>"
+            "#\[color\=(.*)\](.*)\[/color\]#U" => "<span style=\"color: $1;\">$2</span>"
         );
 
-        return array('replace' => $replace, 'regexp' => $regexp);
+        return array(
+            'regexp' => $regexp,
+            'replace' => $replace
+        );
+    }
+
+    /**
+     * Return link for use in getPatterns() regexp.
+     *
+     * @var string $url    The URL.
+     * @var string $title  The link title.
+     *
+     * @return string  The opening <a> tag.
+     */
+    protected function _link($url, $title)
+    {
+        return '<a href="' . $url . '" title="' . $title . '">';
     }
 
 }
index 971063f..335e6cb 100644 (file)
@@ -4,19 +4,18 @@
  * levels.
  *
  * CSS class names called "quoted1" ... "quoted{$cssLevels}" must be present.
- * CSS class names "toggleQuoteHide" and "toggleQuoteShow" are used to style
- * toggle text.
  *
  * The text to be passed in must have already been passed through
  * htmlspecialchars().
  *
  * Parameters:
  * <pre>
- * 'citeblock'  -- Display cite blocks? (DEFAULT: true)
- * 'cssLevels'  -- Number of defined CSS class names. (DEFAULT: 5)
- * 'hideBlocks' -- Hide quoted text blocks by default? (DEFAULT: false)
- * 'noJS' -     -- Don't add javscript toggle code (DEFAULT: false)
- * 'outputJS'   -- Add necessary JS files? (DEFAULT: true)
+ * 'citeblock'  -- Display cite blocks?
+ *                 DEFAULT: true
+ * 'cssLevels'  -- Number of defined CSS class names.
+ *                 DEFAULT: 5
+ * 'hideBlocks' -- Hide large quoted text blocks by default?
+ *                 DEFAULT: false
  * </pre>
  *
  * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
@@ -40,12 +39,18 @@ class Horde_Text_Filter_Highlightquotes extends Horde_Text_Filter_Base
     protected $_params = array(
         'citeblock' => true,
         'cssLevels' => 5,
-        'hideBlocks' => false,
-        'noJS' => true,
-        'outputJS' => true
+        'hideBlocks' => false
     );
 
     /**
+     * The number of quoted lines to exceed to trigger large block
+     * processing.
+     *
+     * @var integer
+     */
+    protected $_qlimit = 8;
+
+    /**
      * Executes any code necessaray before applying the filter patterns.
      *
      * @param string $text  The text before the filtering.
@@ -146,29 +151,20 @@ class Horde_Text_Filter_Highlightquotes extends Horde_Text_Filter_Base
     }
 
     /**
-     * TODO
+     * Process a batch of lines at the same quoted level.
      *
-     * @param array $lines     TODO
-     * @param integer $qcount  TODO
+     * @param array $lines     Lines.
+     * @param integer $qcount  Number of lines in quoted level.
      *
-     * @return string  TODO
+     * @return string  The rendered lines.
      */
     protected function _process($lines, $qcount)
     {
         $curr = reset($lines);
         $out = implode("\n", $this->_removeBr($curr['lines']));
 
-        if (!$this->_params['noJS'] && ($qcount > 8)) {
-            if ($this->_params['outputJS']) {
-                Horde::addScriptFile('prototype.js', 'horde');
-            }
-
-            $out .= (($this->_params['citeblock']) ? '<br />' : '') .
-                '<div class="toggleQuoteParent">' .
-                '<span ' . ($this->_params['outputJS'] ? 'onclick="[ this, this.next(), this.next(1) ].invoke(\'toggle\')" ' : '') .
-                'class="widget toggleQuoteShow"' . ($this->_params['hideBlocks'] ? '' : ' style="display:none"') . '>' . htmlspecialchars(sprintf(_("[Show Quoted Text - %d lines]"), $qcount)) . '</span>' .
-                '<span ' . ($this->_params['outputJS'] ? 'onclick="[ this, this.previous(), this.next() ].invoke(\'toggle\')" ' : "") .
-                'class="widget toggleQuoteHide"' . ($this->_params['hideBlocks'] ? ' style="display:none"' : '') . '>' . htmlspecialchars(_("[Hide Quoted Text]")) . '</span>';
+        if ($qcount > $this->_qlimit) {
+            $out .= $this->_beginLargeBlock($lines, $qcount);
         }
 
         $level = 0;
@@ -184,7 +180,7 @@ class Horde_Text_Filter_Highlightquotes extends Horde_Text_Filter_Base
                     /* Add quote block start tags for each cite level. */
                     $out .= ($this->_params['citeblock'] ? '<div class="citation ' : '<font class="') .
                         'quoted' . (($i % $this->_params['cssLevels']) + 1) . '"' .
-                        ((($i == 0) && ($qcount > 8) && $this->_params['hideBlocks']) ? ' style="display:none"' : '') .
+                        ((($i == 0) && ($qcount > $this->_qlimit) && $this->_params['hideBlocks']) ? ' style="display:none"' : '') .
                         '>';
                 }
             }
@@ -197,9 +193,37 @@ class Horde_Text_Filter_Highlightquotes extends Horde_Text_Filter_Base
             $out .= $this->_params['citeblock'] ? '</div>' : '</font>';
         }
 
-        return (!$this->_params['noJS'] && ($qcount > 8))
-            ? $out . '</div>'
-            : $out;
+        if ($qcount > $this->_qlimit) {
+            $out .= $this->_endLargeBlock($lines, $qcount);
+        }
+
+        return $out;
+    }
+
+    /**
+     * Add HTML code at the beginning of a large block of quoted lines.
+     *
+     * @param array $lines     Lines.
+     * @param integer $qcount  Number of lines in quoted level.
+     *
+     * @return string  HTML code.
+     */
+    protected function _beginLargeBlock($lines, $qcount)
+    {
+        return '';
+    }
+
+    /**
+     * Add HTML code at the end of a large block of quoted lines.
+     *
+     * @param array $lines     Lines.
+     * @param integer $qcount  Number of lines in quoted level.
+     *
+     * @return string  HTML code.
+     */
+    protected function _endLargeBlock($lines, $qcount)
+    {
+        return '';
     }
 
     /**
index e4585d3..5bfbf5e 100644 (file)
@@ -37,8 +37,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Add Horde_Text_Filter_Exception::.
- * Remove Horde/Core dependency in the Emoticons driver.
+ <notes>* Remove Horde/Core dependency.
+ * Add Horde_Text_Filter_Exception::.
  * Html2text converter now uses XML parser to generate output.
  * Add ability to define filters to use with preg_replace_callback().
  * Add 'noprefetch' parameter to XSS filter (Ticket #8836).
@@ -202,10 +202,6 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <min>1.5.4</min>
    </pearinstaller>
    <package>
-    <name>Core</name>
-    <channel>pear.horde.org</channel>
-   </package>
-   <package>
     <name>Exception</name>
     <channel>pear.horde.org</channel>
    </package>