Example #1
0
 /**
  * Builds a HTML version of the StructuredText fragment.
  *
  * @api
  *
  * @param \Prismic\LinkResolver $linkResolver the link resolver
  *
  * @param lambda $htmlSerializer an optional function to generate custom HTML code
  * @return string the HTML version of the StructuredText fragment
  */
 public function asHtml($linkResolver = null, $htmlSerializer = null)
 {
     $groups = array();
     foreach ($this->blocks as $block) {
         $count = count($groups);
         if ($count > 0) {
             $lastOne = $groups[$count - 1];
             if ('ul' == $lastOne->getTag() && $block instanceof ListItemBlock && !$block->isOrdered()) {
                 $lastOne->addBlock($block);
             } elseif ('ol' == $lastOne->getTag() && $block instanceof ListItemBlock && $block->isOrdered()) {
                 $lastOne->addBlock($block);
             } elseif ($block instanceof ListItemBlock && !$block->isOrdered()) {
                 $newBlockGroup = new BlockGroup("ul", array());
                 $newBlockGroup->addBlock($block);
                 array_push($groups, $newBlockGroup);
             } else {
                 if ($block instanceof ListItemBlock && $block->isOrdered()) {
                     $newBlockGroup = new BlockGroup("ol", array());
                     $newBlockGroup->addBlock($block);
                     array_push($groups, $newBlockGroup);
                 } else {
                     $newBlockGroup = new BlockGroup(null, array());
                     $newBlockGroup->addBlock($block);
                     array_push($groups, $newBlockGroup);
                 }
             }
         } else {
             $tag = $block instanceof ListItemBlock && !$block->isOrdered() ? "ul" : ($block instanceof ListItemBlock && $block->isOrdered() ? "li" : null);
             $newBlockGroup = new BlockGroup($tag, array());
             $newBlockGroup->addBlock($block);
             array_push($groups, $newBlockGroup);
         }
     }
     $html = "";
     foreach ($groups as $group) {
         $maybeTag = $group->getTag();
         if (isset($maybeTag)) {
             $html = $html . "<" . $group->getTag() . ">";
             foreach ($group->getBlocks() as $block) {
                 $html = $html . StructuredText::asHtmlBlock($block, $linkResolver, $htmlSerializer);
             }
             $html = $html . "</" . $group->getTag() . ">";
         } else {
             foreach ($group->getBlocks() as $block) {
                 $html = $html . StructuredText::asHtmlBlock($block, $linkResolver, $htmlSerializer);
             }
         }
     }
     return $html;
 }