Example #1
0
 /**
  * Process DOM node.
  *
  * @param DOMDocument $doc  Document node.
  * @param DOMElement $node  Element node.
  *
  * @return string  The plaintext representation.
  */
 protected function _node($doc, $node)
 {
     $out = '';
     if (!empty($this->_params['nestingLimit']) && $this->_nestingLevel > $this->_params['nestingLimit']) {
         $this->_nestingLevel--;
         return;
     }
     $this->_nestingLevel++;
     if ($node->hasChildNodes()) {
         foreach ($node->childNodes as $child) {
             if ($this->_params['callback'] && ($txt = call_user_func($this->_params['callback'], $doc, $child)) !== null) {
                 $out .= $txt;
                 continue;
             }
             if ($child instanceof DOMElement) {
                 switch (Horde_String::lower($child->tagName)) {
                     case 'h1':
                     case 'h2':
                     case 'h3':
                         $out .= "\n\n" . strtoupper($this->_node($doc, $child)) . "\n\n";
                         break;
                     case 'h4':
                     case 'h5':
                     case 'h6':
                         $out .= "\n\n" . ucwords($this->_node($doc, $child)) . "\n\n";
                         break;
                     case 'b':
                     case 'strong':
                         $out .= strtoupper($this->_node($doc, $child));
                         break;
                     case 'u':
                         $out .= '_' . $this->_node($doc, $child) . '_';
                         break;
                     case 'em':
                     case 'i':
                         $out .= '/' . $this->_node($doc, $child) . '/';
                         break;
                     case 'hr':
                         $out .= "\n-------------------------\n";
                         break;
                     case 'ol':
                     case 'ul':
                     case 'dl':
                         ++$this->_indent;
                         $out .= "\n" . $this->_node($doc, $child) . "\n";
                         --$this->_indent;
                         break;
                     case 'p':
                         if ($tmp = $this->_node($doc, $child)) {
                             if (!strspn(substr($out, -2), "\n")) {
                                 $out .= "\n";
                             }
                             if (strlen(trim($tmp))) {
                                 $out .= $tmp . "\n";
                             }
                         }
                         break;
                     case 'table':
                         if ($tmp = $this->_node($doc, $child)) {
                             $out .= "\n\n" . $tmp . "\n\n";
                         }
                         break;
                     case 'tr':
                         $out .= "\n  " . trim($this->_node($doc, $child));
                         break;
                     case 'th':
                         $out .= strtoupper($this->_node($doc, $child)) . " \t";
                         break;
                     case 'td':
                         $out .= $this->_node($doc, $child) . " \t";
                         break;
                     case 'li':
                     case 'dd':
                     case 'dt':
                         $out .= "\n" . str_repeat('  ', $this->_indent) . '* ' . $this->_node($doc, $child);
                         break;
                     case 'a':
                         $out .= $this->_node($doc, $child) . $this->_buildLinkList($doc, $child);
                         break;
                     case 'blockquote':
                         $tmp = trim(preg_replace('/\\s*\\n{3,}/', "\n\n", $this->_node($doc, $child)));
                         if (class_exists('Horde_Text_Flowed')) {
                             $flowed = new Horde_Text_Flowed($tmp, $this->_params['charset']);
                             $flowed->setMaxLength($this->_params['width']);
                             $flowed->setOptLength($this->_params['width']);
                             $tmp = $flowed->toFlowed(true);
                         }
                         if (!strspn(substr($out, -1), " \r\n\t")) {
                             $out .= "\n";
                         }
                         $out .= "\n" . rtrim($tmp) . "\n\n";
                         break;
                     case 'div':
                         $out .= $this->_node($doc, $child) . "\n";
                         break;
                     case 'br':
                         $out .= "\n";
                         break;
                     default:
                         $out .= $this->_node($doc, $child);
                         break;
                 }
             } elseif ($child instanceof DOMText) {
                 $tmp = $child->textContent;
                 $out .= strspn(substr($out, -1), " \r\n\t") ? ltrim($child->textContent) : $child->textContent;
             }
         }
     }
     if (!empty($this->_params['nestingLimit'])) {
         $this->_nestingLevel--;
     }
     return $out;
 }