Exemplo n.º 1
0
 /**
  * This method recieves a string or Tag object and will decide if
  * it should be placed into a new line or added to the current one.
  *
  * @param mixed   $str         the Tag object or string to be added to the output
  * @param array   &$r          the current output
  * @param string  &$line       the current line that is passed to the output array when its full
  * @param boolean &$isInline   flag if the previous content allowed inline content
  * @param array   &$oneLine    array of tag id's used to put verry short content into one line
  * @param mixed   &$lastWasTag weather or not the last entry was a tag. (false if not, TagName if yes).
  * 
  * @return void
  */
 private static function _toLine($str, &$r, &$line, &$isInline, &$oneLine, &$lastWasTag)
 {
     /*
      * Get current line width and tab symbol
      */
     $width = Cleaner::getLineWidth();
     $tabs = Generator::getTabs();
     /*
      * Check if the given content is Tag object.
      */
     if (is_object($str)) {
         /*
          * Initiate additional variables.
          */
         $Tag = $str;
         $isInline = in_array($Tag->realName, TagInfo::$inlineTags);
         $isClose = $Tag->wasOpened();
         $str = trim($Tag->__tostring());
         $isTag = $Tag->name;
         if ($isClose) {
             /*
              * Update Tabs because they were decresed by closing.
              */
             $tabs = Generator::getTabs();
             /*
              * Check if one-line is possible
              * (TagID is last in $oneLine array and content is not to long.)
              */
             if (!empty($oneLine) && end($oneLine) === $Tag->ID && !in_array($Tag->name, TagInfo::$inlineTags) && strlen(rtrim(end($r))) + strlen(trim($line)) + strlen(trim($str)) <= $width) {
                 /*
                  * Remove the closing comment.
                  */
                 $str = preg_replace('/<!--[^>]*>/', '', $str);
                 /*
                  * Add the current line and content to the last line in $r.
                  */
                 $r[key($r)] .= trim($line) . trim($str);
                 /*
                  * Reinitiate the vars.
                  */
                 $line = '';
                 $lastWasTag = $isTag;
                 $oneLine = array();
                 return;
             }
             /*
              * Remove TagID from $oneLine checker.
              */
             unset($oneLine[key($oneLine)]);
         } else {
             /*
              * Try to add the current TagID to the $oneLine checker
              */
             if (self::$maxOneLine > 0) {
                 if (count($oneLine) > self::$maxOneLine) {
                     /*
                      * It's to long - shorten it!
                      */
                     array_splice($oneLine, 0, self::$maxOneLine);
                 }
                 $oneLine[] = $Tag->ID;
             }
         }
     } else {
         /*
          * Its not a tag.
          */
         $isTag = false;
         $isInline = true;
     }
     /*
      * Unify all spaces, tabs etc. to one space.
      */
     $str = preg_replace('/(\\s)+/s', ' ', $str);
     if ($isInline) {
         /*
          * Try to add the current content to the current line.
          */
         if ($line == '') {
             /*
              * Its a fresh line - add tabs.
              */
             $line .= $tabs;
         }
         /*
          * Split the content into single words.
          */
         $strs = preg_split('/\\s/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
         /*
          * Fore each word.
          */
         foreach ($strs as $s) {
             $lineWidth = strlen(trim($line));
             /*
              * Current Line is full - add it to the output and start a new one.
              */
             if ($lineWidth && $lineWidth + strlen(trim($s[0])) > $width) {
                 $r[] = $line;
                 $line = '';
                 $line .= $tabs;
             }
             /*
              * Check if the current word has to be seperated by a space.
              */
             $glue = '';
             if ($s[1] > 0 && strlen(trim($line)) && substr($str, $s[1] - 1, 1) == ' ') {
                 $glue = ' ';
             }
             /*
              * Add it to the current line.
              */
             $line .= $glue . trim($s[0]);
         }
     } else {
         if (strlen(trim($line))) {
             /*
              * If there is content in the current line - add it to the output.
              */
             $r[] = $line;
             $line = '';
         }
         /*
          * Add content to output.
          */
         $r[] = $tabs . $str;
         $isInline = true;
     }
     $lastWasTag = $isTag;
 }