diff --git a/Changelog.md b/Changelog.md index 75eb3cbd..dc2cb072 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,12 @@ ### Changes - Adds support for PHP 8 and removes support for PHP 7. - Updated internal cache engine Phpfastcache from 6 to 9. +- `Phile\ServiceLocacator\MetaInterface` changed: + - Renames `MetaInterface::parse` to `MetaInterface::extractMeta` + - Introduces `MetaInterface::extractContent` + +### Fixed +- Pages without meta block are rendered empty #351 ## Release 1.11.1 diff --git a/lib/Phile/Model/Meta.php b/lib/Phile/Model/Meta.php index c8c4301c..5d56dfca 100644 --- a/lib/Phile/Model/Meta.php +++ b/lib/Phile/Model/Meta.php @@ -89,7 +89,7 @@ protected function parseRawData($rawData) * @var \Phile\ServiceLocator\MetaInterface $metaParser */ $metaParser = ServiceLocator::getService('Phile_Parser_Meta'); - $data = $metaParser->parse($rawData); + $data = $metaParser->extractMeta($rawData); foreach ($data as $key => $value) { $this->set($key, $value); diff --git a/lib/Phile/Model/Page.php b/lib/Phile/Model/Page.php index da491248..c609aaa5 100644 --- a/lib/Phile/Model/Page.php +++ b/lib/Phile/Model/Page.php @@ -8,6 +8,7 @@ use Phile\Core\Router; use Phile\Core\ServiceLocator; use Phile\Repository\Page as Repository; +use Phile\ServiceLocator\MetaInterface; /** * the Model class for a page @@ -173,25 +174,15 @@ public function getMeta() /** * parse the raw content */ - protected function parseRawData() + protected function parseRawData(): void { $this->meta = new Meta($this->rawData); $content = ''; if ($this->rawData !== null) { - $rawData = trim($this->rawData); - $fences = [ - 'c' => ['open' => '/*', 'close' => '*/'], - 'html' => ['open' => ''], - 'yaml' => ['open' => '---', 'close' => '---'] - ]; - foreach ($fences as $fence) { - if (strncmp($fence['open'], $rawData, strlen($fence['open'])) === 0) { - $sub = substr($rawData, strlen($fence['open'])); - list(, $content) = explode($fence['close'], $sub, 2); - break; - } - } + /** @var MetaInterface */ + $metaParser = ServiceLocator::getService('Phile_Parser_Meta'); + $content = $metaParser->extractContent($this->rawData); } $this->content = $content; diff --git a/lib/Phile/ServiceLocator/MetaInterface.php b/lib/Phile/ServiceLocator/MetaInterface.php index fb1540f8..c19132b9 100644 --- a/lib/Phile/ServiceLocator/MetaInterface.php +++ b/lib/Phile/ServiceLocator/MetaInterface.php @@ -20,5 +20,13 @@ interface MetaInterface * * @return array with key/value store */ - public function parse($rawData); + public function extractMeta($rawData); + + /** + * Parses text and extracts the content-part (meta-data is removed) + * + * @param string $rawData Text to inspect + * @return string Text without meta-data + */ + public function extractContent(?string $rawData): string; } diff --git a/plugins/phile/parserMeta/Classes/Parser/Meta.php b/plugins/phile/parserMeta/Classes/Parser/Meta.php index 7cda6af7..869731e8 100644 --- a/plugins/phile/parserMeta/Classes/Parser/Meta.php +++ b/plugins/phile/parserMeta/Classes/Parser/Meta.php @@ -35,31 +35,18 @@ public function __construct(array $config = null) } /** - * parse the content and extract meta information + * Implements MetaInterface::parse * - * @param string $rawData raw page data - * @return array with key/value store + * {@inheritdoc} */ - public function parse($rawData) + public function extractMeta($rawData) { - $rawData = trim($rawData); - $fences = $this->config['fences']; - - $start = $stop = null; - foreach ($fences as $fence) { - $start = $fence['open']; - $length = strlen($start); - if (substr($rawData, 0, $length) === $start) { - $stop = $fence['close']; - break; - } - } - - if ($stop === null) { + list($meta) = $this->splitRawIntoMetaAndContent($rawData); + + if ($meta === null) { return []; } - $meta = trim(substr($rawData, strlen($start), strpos($rawData, $stop, strlen($start)) - (strlen($stop) + 1))); if (strtolower($this->config['format']) === 'yaml') { $meta = Yaml::parse($meta); } else { @@ -69,6 +56,44 @@ public function parse($rawData) return $meta; } + /** + * Implements MetaInterface::extractContent + * + * {@inheritdoc} + */ + public function extractContent(?string $rawData): string + { + list(, $content) = $this->splitRawIntoMetaAndContent($rawData); + + return $content; + } + + /** + * Inspects the text and splits meta-data and content + * + * @param string $rawData Text to inspect + * @return array array with [meta-data, content] + */ + protected function splitRawIntoMetaAndContent(string $rawData): array + { + $meta = null; + $content = $rawData; + + if ($rawData !== null) { + $rawData = trim($rawData); + $fences = $this->config['fences']; + foreach ($fences as $fence) { + if (strncmp($fence['open'], $rawData, strlen($fence['open'])) === 0) { + $sub = substr($rawData, strlen($fence['open'])); + list($meta, $content) = explode($fence['close'], $sub, 2); + break; + } + } + } + + return [$meta, $content]; + } + /** * convert meta data keys * diff --git a/plugins/phile/parserMeta/tests/MetaTest.php b/plugins/phile/parserMeta/tests/MetaTest.php index 6c62d588..61f80c1a 100644 --- a/plugins/phile/parserMeta/tests/MetaTest.php +++ b/plugins/phile/parserMeta/tests/MetaTest.php @@ -24,8 +24,22 @@ public function testYamlWithYamlFrontMatter() 'fences' => ['yaml' => ['open' => '---', 'close' => '---']], 'format' => 'YAML' ]); - $meta = $parser->parse($raw); + $meta = $parser->extractMeta($raw); $this->assertSame('foo', $meta['title']); $this->assertSame(['bar', 'baz'], $meta['tags']); } + + public function testDataWithoutMetaDataBlock() + { + $raw = "# Hello World\n## Hello you too"; + $parser = new Meta([ + 'fences' => ['yaml' => ['open' => '---', 'close' => '---']], + ]); + + $content = $parser->extractContent($raw); + $this->assertSame($raw, $content); + + $meta = $parser->extractMeta($raw); + $this->assertSame([], $meta); + } }