To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

Changeset 1815

Show
Ignore:
Timestamp:
07/21/10 15:41:25 (19 months ago)
Author:
johnnyoffline
Message:

updated the aHtml::limitWords to accept the append_ellipses option that aString::limitWord already accepts -- 1.4 and trunk

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/apostrophePlugin/trunk/lib/toolkit/aHtml.class.php

    r1722 r1815  
    428428  ); 
    429429 
    430         public static function limitWords($string, $word_limit) 
     430        public static function limitWords($string, $word_limit, $options = array()) 
    431431        { 
    432     # TBB: tag-aware, doesn't split in the middle of tags  
    433     # (we will probably use fancier tags with attributes later, 
    434     # so this is important). Tags must be valid XHTML unless 
    435     # all allowed tags  
     432          # TBB: tag-aware, doesn't split in the middle of tags  
     433          # (we will probably use fancier tags with attributes later, 
     434          # so this is important). Tags must be valid XHTML unless 
     435          # all allowed tags  
    436436          $words = preg_split("/(\<.*?\>|\s+)/", $string, -1,  
    437       PREG_SPLIT_DELIM_CAPTURE); 
    438     $wordCount = 0; 
    439     # Balance tags that need balancing. We don't have strict XHTML 
    440     # coming from OpenOffice (oh, if only) so we'll have to keep a 
    441     # list of the tags that are containers. 
    442     $open = array(); 
    443     $result = ""; 
    444     $count = 0; 
    445     foreach ($words as $word) { 
    446       if ($count >= $word_limit) { 
    447         break; 
    448       } elseif (preg_match("/\<.*?\/\>/", $word)) { 
    449         # XHTML non-container tag, we don't have to guess 
    450         $result .= $word; 
    451         continue; 
    452       } elseif (preg_match("/\<(\w+)/s", $word, $matches)) { 
    453         $tag = $matches[1]; 
    454         $result .= $word; 
    455         if (isset(aHtml::$nonContainerTags[$tag])) { 
    456           continue; 
    457         } 
    458         $open[] = $tag; 
    459       } elseif (preg_match("/\<\/(\w+)/s", $word, $matches)) { 
    460         $tag = $matches[1]; 
    461         if (!count($open)) { 
    462           # Groan, extra close tag, ignore 
    463           continue; 
    464         } 
    465         $last = array_pop($open);     
    466         if ($last !== $tag) { 
    467           # They closed the wrong tag. Again, ignore for now, but  
    468           # we might want to work on a better solution 
    469           continue; 
    470         } 
    471         $result .= $word; 
    472       } elseif (preg_match("/^\s+$/s", $word)) { 
    473         $result .= $word; 
    474       } else { 
    475         if (strlen($word)) { 
    476           $count++; 
    477           $result .= $word; 
    478         } 
    479       } 
    480     } 
    481     for ($i = count($open) - 1; ($i >= 0); $i--) { 
    482       $result .= "</" . $open[$i] . ">"; 
    483     } 
    484     return $result; 
    485   } 
     437            PREG_SPLIT_DELIM_CAPTURE); 
     438          $wordCount = 0; 
     439          # Balance tags that need balancing. We don't have strict XHTML 
     440          # coming from OpenOffice (oh, if only) so we'll have to keep a 
     441          # list of the tags that are containers. 
     442          $open = array(); 
     443          $result = ""; 
     444          $count = 0; 
     445                $num_words = count($words); 
     446          foreach ($words as $word) { 
     447            if ($count >= $word_limit) { 
     448              break; 
     449            } elseif (preg_match("/\<.*?\/\>/", $word)) { 
     450              # XHTML non-container tag, we don't have to guess 
     451              $result .= $word; 
     452              continue; 
     453            } elseif (preg_match("/\<(\w+)/s", $word, $matches)) { 
     454              $tag = $matches[1]; 
     455              $result .= $word; 
     456              if (isset(aHtml::$nonContainerTags[$tag])) { 
     457                continue; 
     458              } 
     459              $open[] = $tag; 
     460            } elseif (preg_match("/\<\/(\w+)/s", $word, $matches)) { 
     461              $tag = $matches[1]; 
     462              if (!count($open)) { 
     463                # Groan, extra close tag, ignore 
     464                continue; 
     465              } 
     466              $last = array_pop($open);     
     467              if ($last !== $tag) { 
     468                # They closed the wrong tag. Again, ignore for now, but  
     469                # we might want to work on a better solution 
     470                continue; 
     471              } 
     472              $result .= $word; 
     473            } elseif (preg_match("/^\s+$/s", $word)) { 
     474              $result .= $word; 
     475            } else { 
     476              if (strlen($word)) { 
     477                $count++; 
     478                $result .= $word; 
     479              } 
     480            } 
     481          } 
     482 
     483                $append_ellipsis = false; 
     484                if (isset($options['append_ellipsis'])) 
     485                { 
     486                        $append_ellipsis = $options['append_ellipsis']; 
     487                } 
     488                if ($append_ellipsis == true && $num_words > $word_limit) 
     489                { 
     490                        $result .= '&hellip;'; 
     491                } 
     492 
     493          for ($i = count($open) - 1; ($i >= 0); $i--) { 
     494            $result .= "</" . $open[$i] . ">"; 
     495          } 
     496          return $result; 
     497        } 
    486498 
    487499  public static function toText($html)