XhtmlRenderer.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 
00013 class XhtmlRenderer
00014 {
00015      var $linkedObjectIds = array();
00016      var $encodeHtml = false;
00017      
00023      var $footnotes       = array();
00024 
00025 
00032      function renderElement( $child )
00033      {
00034           global $conf;
00035           
00036                     $attr = array();
00037                     $val  = '';
00038                     $praefix = '';
00039                     $suffix  = '';
00040                     $empty   = false;
00041 
00042                     if   ( count($child->children) > 0 )
00043                     {
00044                          $subChild1 = $child->children[0];
00045                          
00046                          if   ( !empty($subChild1->class) )
00047                               $attr['class'] = $subChild1->class;
00048                          
00049                          if   ( !empty($subChild1->style) )
00050                               $attr['style'] = $subChild1->style;
00051                          
00052                          if   ( !empty($subChild1->title) )
00053                               $attr['title'] = $subChild1->title;
00054                     }
00055                     
00056                     switch( strtolower(get_class($child)) )
00057                     {
00058                          case 'tableofcontentelement':
00059                               $tag = 'p';
00060                               foreach( $this->children as $h)
00061                               {
00062                                    if   ( strtolower(get_class($h))=='headlineelement' )
00063                                    {
00064                                         $child->children[] = new RawElement(str_repeat('&nbsp;&nbsp;',$h->level));
00065                                         $t = new TextElement( $h->getText() );
00066                                         $l = new LinkElement();
00067                                         $l->fragment=$h->getName();
00068                                         $l->children[] = $t;
00069                                         $child->children[] = $l;
00070                                         $child->children[] = new LineBreakElement();
00071                                    }
00072                               }
00073                               break;
00074 
00075                          case 'rawelement':
00076                               $tag = '';
00077                               $val = $child->src;
00078                               
00079                               break;
00080 
00081                          case 'textelement':
00082                               $tag = '';
00083 //                            $tag = 'span';
00084 
00085                               $val = $child->text;
00086                               if   ( $this->encodeHtml )
00087                                    $val = Text::encodeHtml( $val );
00088                               $val = Text::replaceHtmlChars( $val );
00089                               break;
00090 
00091                          case 'footnoteelement':
00092                               $tag = 'a';
00093                               $attr['href'] = '#footnote';
00094                               
00095                               $title = '';
00096                               foreach( $child->children as $c )
00097                                    $title .= $this->renderElement($c);
00098                               $attr['title'] = strip_tags($title);
00099                                
00100                               $nr = 1;
00101                               foreach( $this->footnotes as $fn )
00102                                    if ( strtolower(get_class($fn))=='linebreakelement')
00103                                         $nr++;
00104                                         
00105                               $val = $nr;
00106                               if   ( @$conf['editor']['footnote']['bracket'])
00107                                    $val = '('.$nr.')';
00108                               if   ( @$conf['editor']['footnote']['sup'])
00109                                    $val = '<sup><small>'.$nr.'</small></sup>';
00110                                         
00111 
00112                               if   ( $nr == 1 )
00113                               {
00114                                    $this->footnotes[] = new RawElement('&mdash;');
00115                                    $le = new LinkElement();
00116                                    $le->name = "footnote";
00117                                    $this->footnotes[] = $le;
00118                                    $this->footnotes[] = new RawElement('&mdash;');
00119                               }
00120                               $this->footnotes[] = new LineBreakElement();
00121                               $this->footnotes[] = new RawElement($val);
00122                               $this->footnotes[] = new RawElement(' ');
00123                               foreach( $child->children as $c )
00124                                    $this->footnotes[] = $c;
00125                               
00126                               $child->children = array();
00127 
00128                               break;
00129 
00130                          case 'codeelement':
00131                               
00132                               if   ( empty($child->language) )
00133                                    // Wenn keine Sprache verfügbar, dann ein einfaches PRE-Element erzeugen.
00134                                    $tag = 'pre';
00135                               else
00136                               {
00137                                    // Wenn Sprache verfügbar, dann den GESHI-Parser bemühen.
00138                                    $tag    = '';
00139                                    $source = '';
00140                                    foreach( $child->children as $c )
00141                                         if   ( strtolower(get_class($c)) == 'textelement')
00142                                              $source .= $c->text."\n";
00143                                    $child->children = array();
00144                                    require_once('./geshi/geshi.php');
00145                                    $geshi = new Geshi($source,$child->language);
00146                                    $val = $geshi->parse_code(); 
00147                               }
00148                               break;
00149 
00150                          case 'quoteelement':
00151                               $tag = 'blockquote';
00152                               break;
00153 
00154 
00155                          case 'paragraphelement':
00156                               $tag = 'p';
00157                               break;
00158 
00159                          case 'speechelement':
00160                               $tag     = 'cite';
00161                               
00162                               // Danke an: http://www.apostroph.de/tueddelchen.php
00163                               //TODO: Abhängigkeit von Spracheinstellung implementieren.
00164                               $language = 'de';
00165                               switch( $language )
00166                               {
00167                                    case 'de': // deutsche Notation
00168                                         $praefix = '&bdquo;';
00169                                         $suffix  = '&ldquo;';
00170                                         break;
00171                                    case 'fr':
00172                                         $praefix = '&laquo;';
00173                                         $suffix  = '&raquo;';
00174                                         break;
00175                                    default: // englische Notation
00176                                         $praefix = '&ldquo;';
00177                                         $suffix  = '&rdquo;';
00178                               }
00179                               
00180                               if   ( $conf['editor']['html']['override_speech'] )
00181                               {
00182                                    $praefix = $conf['editor']['html']['override_speech_open' ];
00183                                    $suffix  = $conf['editor']['html']['override_speech_close'];
00184                               }
00185                               break;
00186 
00187                          case 'linebreakelement':
00188                               $tag   = 'br';
00189                               $empty = true;
00190                               break;
00191 
00192                          case 'linkelement':
00193                               $tag = 'a';
00194                               if   ( !empty($child->name) )
00195                                    $attr['name'] = $child->name;
00196                               else
00197                                    $attr['href'] = htmlspecialchars($child->getUrl());
00198 
00199                               if   ( Object::available( $child->objectId ) )
00200                               {
00201                                    $file = new File( $child->objectId );
00202                                    $file->load();
00203                                    $attr['title'] = $file->description;
00204                                    unset( $file );
00205                               }
00206                               break;
00207 
00208                          case 'imageelement':
00209                               $empty       = true;
00210                               $attr['alt'] = '';
00211                               
00212                               if   ( ! Object::available( $child->objectId ) )
00213                               {
00214                                    $tag = '';
00215                               }
00216                               elseif    ( empty($attr['title']) )
00217                               {
00218                                    $tag = 'img';
00219                                    $attr['src']    = $child->getUrl();
00220                                    $attr['border'] = '0';
00221                                    
00222                                    // Breite/Höhe des Bildes bestimmen.
00223                                    $image = new File( $child->objectId );
00224                                    
00225                                    $image->load();
00226                                    $attr['alt'   ]    = $image->name;
00227                                    $attr['title' ]    = $image->description;
00228                                    
00229                                    $image->getImageSize();
00230                                    $attr['width' ]    = $image->width;
00231                                    $attr['height']    = $image->height;
00232                                    unset($image);
00233                               }
00234                               else
00235                               {
00236                                    $tag = 'dl';
00237                                    
00238                                    if   ( empty($attr['class']) )
00239                                         $attr['class'] = "image";
00240                                         
00241                                    $child->children = array();
00242                                    $dt = new DefinitionListItemElement();
00243                                    $dt->children[] = new TextElement('(image)');
00244                                    $dt->children[] = $child;
00245                                    $child->children[] = $dt;
00246 
00247                                    $dd = new DefinitionListEntryElement();
00248                                    $dd->children[] = new TextElement('(image)');
00249                                    $dd->children[] = new TextElement($attr['title']);
00250                                    $child->children[] = $dd;
00251                               }
00252                               break;
00253 
00254                          case 'strongelement':
00255                               $tag = 'strong';
00256                               break;
00257 
00258                          case 'emphaticelement':
00259                               $tag = 'em';
00260                               break;
00261 
00262                          case 'insertedelement':
00263                               $tag = 'ins';
00264                               break;
00265 
00266                          case 'removedelement':
00267                               $tag = 'del';
00268                               break;
00269 
00270                          case 'headlineelement':
00271                               $tag = 'h'.$child->level;
00272                               
00273                               $l = new LinkElement();
00274                               $l->name = $child->getName();
00275                               $child->children[] = $l;
00276                               
00277                               break;
00278 
00279                          case 'tableelement':
00280                               $tag = 'table';
00281                               break;
00282 
00283                          case 'tablelineelement':
00284                               $tag = 'tr';
00285                               break;
00286 
00287                          case 'definitionlistelement':
00288                               $items = $child->children;
00289                               $newChildren = array();
00290                               foreach( $items as $item )
00291                               {
00292                                    $def = new DefinitionItemElement();
00293                                    $def->key = $item->key;
00294                                    $item->key = ''; 
00295                                    $newChildren[] = $def;
00296                                    $newChildren[] = $item;
00297                               }
00298 //                            Html::debug($newChildren,'Children-neu');
00299                               $child->children = $newChildren;
00300                               $tag = 'dl';
00301                               break;
00302 
00303                          case 'definitionitemelement':
00304                               if   ( !empty($child->key) )
00305                               {
00306                                    $tag = 'dt';
00307                                    $val = $child->key;
00308                               }
00309                               else
00310                               {
00311                                    $tag = 'dd';
00312                               }
00313                               break;
00314 
00315                          case 'tablecellelement':
00316                               if   ( $child->isHeading )
00317                                    $tag = 'th'; else $tag = 'td';
00318                                    
00319                               if   ( $child->rowSpan > 1 )
00320                                    $attr['rowspan'] = $child->rowSpan;
00321                               if   ( $child->colSpan > 1 )
00322                                    $attr['colspan'] = $child->colSpan;
00323                               break;
00324 
00325                          case 'listelement':
00326                               $tag = 'ul';
00327                               break;
00328                               
00329                          case 'teletypeelement':
00330                               $tag = 'code';
00331                               break;
00332                               
00333                          case 'numberedlistelement':
00334                               $tag = 'ol';
00335                               break;
00336                               
00337                          case 'listentryelement':
00338                               $tag = 'li';
00339                               break;
00340 
00341                          default:
00342                               
00343                               $tag = 'unknown-element';
00344                               $attr['class'] = strtolower(get_class($child));
00345                               break;
00346                     }                   
00347 
00348                     $val .= $praefix;
00349                     foreach( $child->children as $c )
00350                     {
00351                          $val .= $this->renderElement( $c );
00352                     }
00353 
00354                     $val .= $suffix;
00355 //                  echo "text:$val";
00356                     return $this->renderHtmlElement($tag,$val,$empty,$attr);
00357           
00358      }
00359 
00360 
00361 
00371      function renderHtmlElement( $tag,$value,$empty,$attr=array() )
00372      {
00373           global $conf;
00374           if   ( $tag == '' )
00375                return $value;
00376                
00377           $val = '<'.$tag;
00378           foreach( $attr as $attrName=>$attrInhalt )
00379           {
00380                $val .= ' '.$attrName.'="'.$attrInhalt.'"';
00381           }
00382           
00383           if   ( $value == '' && $empty )
00384           {
00385                // Inhalt ist leer, also Kurzform verwenden.
00386                // Die Kurzform ist abhängig vom Rendermode.
00387                // SGML=<tag>
00388                // XML=<tag />
00389                if   ( $conf['editor']['html']['rendermode'] == 'xml' )
00390                {
00391                     $val .= ' />';
00392                     return $val;
00393                }
00394                else 
00395                {
00396                     $val .= '>';
00397                     return $val;
00398                }    
00399           }
00400           
00401           $val .= '>'.$value.'</'.$tag.'>';
00402           return $val;
00403      }
00404 
00405      
00406      
00412      function render()
00413      {
00414           $this->renderedText = '';
00415           $this->footnotes    = array();
00416           
00417           foreach( $this->children as $child )
00418                $this->renderedText .= $this->renderElement( $child );
00419 
00420           foreach( $this->footnotes as $child )
00421                $this->renderedText .= $this->renderElement( $child );
00422                
00423           return $this->renderedText;
00424      }
00425 }
00426 
00427 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8