/**
  * Generates and renders in rich html format
  * @see LipsumGenerator::FORMAT_RICH_HTML
  * @return LipsumGenerator $this for chainability
  */
 private function render_rich_html()
 {
     $this->render = '';
     $is_first_p = true;
     $this->stats = array();
     // useful to avoid the same tag to be repeated without plain text between
     $last_closed_tag = false;
     if (empty($this->rich_html_config)) {
         $this->set_rich_html_config();
     }
     for ($i = 0; $i < $this->number_of_paragraphs; $i++) {
         $paragraph = $this->get_paragraph();
         $rendered_p = '';
         if ($is_first_p) {
             $this->force_beginning(&$paragraph);
         } else {
             $rendered_p .= "\n";
         }
         $rendered_p .= HtmlHelper::open_tag('p');
         //'<p>';
         foreach ($paragraph as $sentence) {
             // stores the currently opened html tag
             $current_tag = '';
             // stores how many words are left before the current tag have to be closed
             $words_until_close_tag = 0;
             foreach ($sentence as $index => $word) {
                 if ($words_until_close_tag < 0) {
                     $words_until_close_tag = 0;
                 }
                 if (empty($current_tag) && empty($last_closed_tag)) {
                     $tag_to_add = '';
                     foreach ($this->rich_html_config as $tag => $tag_config) {
                         $rnd = rand(0, 99);
                         if ($rnd < $tag_config['percent']) {
                             //v(array($rnd, $tag, $index));
                             if (isset($this->rich_html_config[$tag_to_add]['percent']) && $this->rich_html_config[$tag_to_add]['percent'] < $tag_config['percent']) {
                                 $tag_to_add = $tag;
                                 $this->stats[$tag]++;
                             }
                         }
                     }
                 }
                 if (!empty($tag_to_add)) {
                     // insert the current tag!
                     $current_tag = $tag_to_add;
                     //$rendered_p .= $this->open_tag($tag_to_add);
                     $rendered_p .= HtmlHelper::open_tag($tag_to_add, $this->rich_html_config[$tag_to_add]['params']);
                     $words_until_close_tag = rand(1, $this->rich_html_config[$tag_to_add]['max_words']);
                     $tag_to_add = '';
                 }
                 $rendered_p .= $word;
                 $last_closed_tag = '';
                 if (!empty($current_tag)) {
                     if ($words_until_close_tag == 0) {
                         $rendered_p .= HtmlHelper::close_tag($current_tag);
                         $last_closed_tag = $current_tag;
                         $current_tag = '';
                     } else {
                         $words_until_close_tag--;
                     }
                 }
                 $rendered_p .= ' ';
             }
             // close the last tag if it's still open at the paragraph end
             $rendered_p .= HtmlHelper::close_tag($current_tag);
         }
         $rendered_p .= HtmlHelper::close_tag('p');
         //"</p>";
         $this->render .= $rendered_p;
         $is_first_p = false;
     }
     return $this;
 }