Example #1
0
 /**
  * Decode string data to create a Page object
  *
  * @param Page $page Page using to create the page
  * @param array ($options) default page field(s)
  * @return Page
  */
 public function parse($page, array $options = [])
 {
     $sections = [];
     if (!empty($page->sections)) {
         $sections = array_map(function ($section) {
             $section->content = $this->contentParser->parse($section->content);
             return $section;
         }, (array) $page->sections);
         unset($page->sections);
     }
     $content = $this->contentParser->parse($page->content);
     unset($page->content);
     return new Page((array) $page, $content, $sections);
 }
Example #2
0
 /**
  * Decode string data to create a Page object
  *
  * @param mixed $data Data using to create the page
  * @param array ($options) default page field(s)
  * @param bool ($parseContent)
  * @return Page
  */
 public function parse($data, array $options = [], $parseContent = true)
 {
     $pageTokens = preg_split($this->delimiterMatcher, ltrim($data), 2);
     $metaRaw = trim($pageTokens[0]);
     $meta = $this->metadataParser->decode($metaRaw);
     $content = '';
     $sections = [];
     if (!empty($pageTokens[1])) {
         // Split into sections
         $content = preg_split($this->sectionMatcher, $pageTokens[1] . ' ', -1, PREG_SPLIT_DELIM_CAPTURE);
         if (($len = count($content)) > 1) {
             for ($i = 1; $i < $len; ++$i) {
                 if ($i % 2 === 1) {
                     $metaRaw = trim($content[$i]);
                     $lines = explode("\n", str_replace(["\r\n", "\n\r", "\r"], "\n", $metaRaw), 2);
                     $sectionMeta = ['name' => $lines[0]];
                     if (count($lines) === 2) {
                         $sectionMeta += (array) $this->metadataParser->decode($lines[1]);
                     }
                 } else {
                     $val = $content[$i];
                     if ($parseContent) {
                         $val = $this->contentParser->parse($val);
                     }
                     $sections[] = new Section($sectionMeta, $val);
                 }
             }
         }
         // First section is always the main content
         $content = $content[0];
         if ($parseContent) {
             $content = $this->contentParser->parse($content);
         }
     }
     // Append $options to $meta
     $meta += $options;
     return new Page($meta, $content, $sections);
 }