/** * 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); }
/** * Encode structured data * * @param ParserInterface $parser * @param mixed $data Structured data * @return string Encoded data */ private function encode(ParserInterface $parser, $data) { return $parser->encode($data); }