Fix #28: array_shift() E_NOTICE in owncloud.log
[grauphel.git] / lib / converter / html.php
index eeb2b665c594dcf7130a15b5275a79269eebb38e..11cf105ceb2a01c1928ff9e9f4137c0bc309688d 100644 (file)
@@ -30,14 +30,13 @@ use \XMLReader;
  * @version   Release: @package_version@
  * @link      http://cweiske.de/grauphel.htm
  */
  * @version   Release: @package_version@
  * @link      http://cweiske.de/grauphel.htm
  */
-class Html
+class Html extends Base
 {
     protected static $tagMap = array(
         'list'      => 'ul',
         'list-item' => 'li',
         'bold'      => 'b',
         'italic'    => 'i',
 {
     protected static $tagMap = array(
         'list'      => 'ul',
         'list-item' => 'li',
         'bold'      => 'b',
         'italic'    => 'i',
-        'monospace' => 'tt',
     );
 
     protected static $styleClassMap = array(
     );
 
     protected static $styleClassMap = array(
@@ -48,6 +47,10 @@ class Html
         'size:huge'  => 'huge',
     );
 
         'size:huge'  => 'huge',
     );
 
+    protected static $styleMap = array(
+        'monospace' => 'font-family:monospace; white-space: pre-wrap'
+    );
+
     public $internalLinkHandler;
 
 
     public $internalLinkHandler;
 
 
@@ -80,17 +83,23 @@ class Html
         );
 
         $withinLink = false;
         );
 
         $withinLink = false;
+        $nesting = array();
         $store = &$html;
         while ($reader->read()) {
             switch ($reader->nodeType) {
             case XMLReader::ELEMENT:
                 //echo $reader->name . "\n";
         $store = &$html;
         while ($reader->read()) {
             switch ($reader->nodeType) {
             case XMLReader::ELEMENT:
                 //echo $reader->name . "\n";
+                array_unshift($nesting, $reader->name);
                 if (isset(static::$tagMap[$reader->name])) {
                     $store .= '<' . static::$tagMap[$reader->name] . '>';
                 } else if (isset(static::$styleClassMap[$reader->name])) {
                     $store .= '<span class="'
                         . static::$styleClassMap[$reader->name]
                         . '">';
                 if (isset(static::$tagMap[$reader->name])) {
                     $store .= '<' . static::$tagMap[$reader->name] . '>';
                 } else if (isset(static::$styleClassMap[$reader->name])) {
                     $store .= '<span class="'
                         . static::$styleClassMap[$reader->name]
                         . '">';
+                } else if (isset(static::$styleMap[$reader->name])) {
+                    $store .= '<span style="'
+                        . static::$styleMap[$reader->name]
+                        . '">';
                 } else if (substr($reader->name, 0, 5) == 'link:') {
                     $withinLink = true;
                     $linkText    = '';
                 } else if (substr($reader->name, 0, 5) == 'link:') {
                     $withinLink = true;
                     $linkText    = '';
@@ -98,10 +107,13 @@ class Html
                 }
                 break;
             case XMLReader::END_ELEMENT:
                 }
                 break;
             case XMLReader::END_ELEMENT:
+                array_shift($nesting);
                 if (isset(static::$tagMap[$reader->name])) {
                     $store .= '</' . static::$tagMap[$reader->name] . '>';
                 } else if (isset(static::$styleClassMap[$reader->name])) {
                     $store .= '</span>';
                 if (isset(static::$tagMap[$reader->name])) {
                     $store .= '</' . static::$tagMap[$reader->name] . '>';
                 } else if (isset(static::$styleClassMap[$reader->name])) {
                     $store .= '</span>';
+                } else if (isset(static::$styleMap[$reader->name])) {
+                    $store .= '</span>';
                 } else if (substr($reader->name, 0, 5) == 'link:') {
                     $withinLink = false;
                     $store      = &$html;
                 } else if (substr($reader->name, 0, 5) == 'link:') {
                     $withinLink = false;
                     $store      = &$html;
@@ -118,7 +130,11 @@ class Html
                 break;
             case XMLReader::TEXT:
             case XMLReader::SIGNIFICANT_WHITESPACE:
                 break;
             case XMLReader::TEXT:
             case XMLReader::SIGNIFICANT_WHITESPACE:
-                $store .= nl2br(htmlspecialchars($reader->value));
+                $text = htmlspecialchars($reader->value);
+                if ($nesting[0] != 'monospace') {
+                    $text = nl2br($text);
+                }
+                $store .= $text;
                 break;
             default:
                 throw new Exception(
                 break;
             default:
                 throw new Exception(
@@ -159,39 +175,5 @@ class Html
     {
         return $linkUrl . '.htm';
     }
     {
         return $linkUrl . '.htm';
     }
-
-    /**
-     * Re-arranges the XML of formatted links to that clean link tags can
-     * be generated.
-     *
-     * Tomboy 1.15.2 allows link formatting, and the resulting XML is a
-     * mess of multiple(!) link tags that are within or around other formatting
-     * tags.
-     *
-     * This method tries to re-arrange the links so that only a single link tag
-     * appears with all the formatting inside.
-     * 
-     * @param string $xmlContent Tomboy note content
-     *
-     * @return string XML content, with re-arranged link tags.
-     */
-    protected function fixNastyLinks($xmlContent)
-    {
-        preg_match_all(
-            '#(?:<.*>)?<link:internal>.+</link:internal><link:internal>.+</link:internal>#U',
-            $xmlContent,
-            $matches
-        );
-
-        foreach ($matches[0] as $nastyLink) {
-            $cleaner = str_replace('</link:internal><link:internal>', '', $nastyLink);
-            $cleaner = preg_replace('#<([a-z]+)><(link:internal)>#U', '<\2><\1>', $cleaner);
-            $cleaner = preg_replace('#</(link:internal)></([a-z]+)>#U', '</\2></\1>', $cleaner);
-            $cleaner = str_replace('</link:internal><link:internal>', '', $cleaner);
-            $xmlContent = str_replace($nastyLink, $cleaner, $xmlContent);
-        }
-
-        return $xmlContent;
-    }
 }
 ?>
 }
 ?>