From e1ae62e8f127ee93843b5ed0e34f47757eaa9588 Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Fri, 29 Jul 2022 18:00:49 +0200 Subject: [PATCH 1/4] fixes Pages without meta block are rendered empty #351 The raw-content has to be split into meta and content. This was done in two places before (Page and Parser) essentially duplicating the same functionality. Now this responsibility is delegated to the Parser, it is the party with the knowledge on how to detect and extract the meta from the content. The evaluation of meta and content is still done twice as before, so there's still room for performance improvement. But at least the implementations are merged into one code place. --- Changelog.md | 3 + lib/Phile/Model/Page.php | 19 ++---- lib/Phile/ServiceLocator/MetaInterface.php | 8 +++ .../phile/parserMeta/Classes/Parser/Meta.php | 64 +++++++++++++------ plugins/phile/parserMeta/tests/MetaTest.php | 14 ++++ 5 files changed, 76 insertions(+), 32 deletions(-) diff --git a/Changelog.md b/Changelog.md index 75eb3cbd..fd585e82 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,9 @@ - Adds support for PHP 8 and removes support for PHP 7. - Updated internal cache engine Phpfastcache from 6 to 9. +### Fixed +- Pages without meta block are rendered empty #351 + ## Release 1.11.1 Please see the [release notes](https://github.com/PhileCMS/Phile/releases/tag/1.11.1) or the [complete change-log](https://github.com/PhileCMS/Phile/compare/1.11.0...1.11.1) for more information 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..a8054083 100644 --- a/lib/Phile/ServiceLocator/MetaInterface.php +++ b/lib/Phile/ServiceLocator/MetaInterface.php @@ -21,4 +21,12 @@ interface MetaInterface * @return array with key/value store */ public function parse($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..759be995 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) { - $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->findFenceStop($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,47 @@ public function parse($rawData) return $meta; } + /** + * Implements MetaInterface::extractContent + * + * {@inheritdoc} + */ + public function extractContent(?string $rawData): string + { + list(, $content) = $this->findFenceStop($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 findFenceStop(string $rawData): array + { + $rawData = trim($rawData); + $fences = $this->config['fences']; + + $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..9767e0fe 100644 --- a/plugins/phile/parserMeta/tests/MetaTest.php +++ b/plugins/phile/parserMeta/tests/MetaTest.php @@ -28,4 +28,18 @@ public function testYamlWithYamlFrontMatter() $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->parse($raw); + $this->assertSame([], $meta); + } } From 2b5d416d1c29d22b4eb75559acef7fca1d0faed6 Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Fri, 29 Jul 2022 18:20:13 +0200 Subject: [PATCH 2/4] Fixes improper method name --- plugins/phile/parserMeta/Classes/Parser/Meta.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/phile/parserMeta/Classes/Parser/Meta.php b/plugins/phile/parserMeta/Classes/Parser/Meta.php index 759be995..93d6121f 100644 --- a/plugins/phile/parserMeta/Classes/Parser/Meta.php +++ b/plugins/phile/parserMeta/Classes/Parser/Meta.php @@ -41,7 +41,7 @@ public function __construct(array $config = null) */ public function parse($rawData) { - list($meta) = $this->findFenceStop($rawData); + list($meta) = $this->splitRawIntoMetaAndContent($rawData); if ($meta === null) { return []; @@ -63,7 +63,7 @@ public function parse($rawData) */ public function extractContent(?string $rawData): string { - list(, $content) = $this->findFenceStop($rawData); + list(, $content) = $this->splitRawIntoMetaAndContent($rawData); return $content; } @@ -74,7 +74,7 @@ public function extractContent(?string $rawData): string * @param string $rawData Text to inspect * @return array array with [meta-data, content] */ - protected function findFenceStop(string $rawData): array + protected function splitRawIntoMetaAndContent(string $rawData): array { $rawData = trim($rawData); $fences = $this->config['fences']; From 06f7d2a7a17c1d188ec200377e9ddb5374852bf9 Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Fri, 29 Jul 2022 18:29:58 +0200 Subject: [PATCH 3/4] Renames `MetaInterface::parse` to `MetaInterface::extractMeta` Aligns the method with `MetaInterface::extractContent` --- Changelog.md | 3 +++ lib/Phile/Model/Meta.php | 2 +- lib/Phile/ServiceLocator/MetaInterface.php | 2 +- plugins/phile/parserMeta/Classes/Parser/Meta.php | 2 +- plugins/phile/parserMeta/tests/MetaTest.php | 4 ++-- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index fd585e82..dc2cb072 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,9 @@ ### 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 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/ServiceLocator/MetaInterface.php b/lib/Phile/ServiceLocator/MetaInterface.php index a8054083..c19132b9 100644 --- a/lib/Phile/ServiceLocator/MetaInterface.php +++ b/lib/Phile/ServiceLocator/MetaInterface.php @@ -20,7 +20,7 @@ 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) diff --git a/plugins/phile/parserMeta/Classes/Parser/Meta.php b/plugins/phile/parserMeta/Classes/Parser/Meta.php index 93d6121f..2e1a8ac5 100644 --- a/plugins/phile/parserMeta/Classes/Parser/Meta.php +++ b/plugins/phile/parserMeta/Classes/Parser/Meta.php @@ -39,7 +39,7 @@ public function __construct(array $config = null) * * {@inheritdoc} */ - public function parse($rawData) + public function extractMeta($rawData) { list($meta) = $this->splitRawIntoMetaAndContent($rawData); diff --git a/plugins/phile/parserMeta/tests/MetaTest.php b/plugins/phile/parserMeta/tests/MetaTest.php index 9767e0fe..61f80c1a 100644 --- a/plugins/phile/parserMeta/tests/MetaTest.php +++ b/plugins/phile/parserMeta/tests/MetaTest.php @@ -24,7 +24,7 @@ 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']); } @@ -39,7 +39,7 @@ public function testDataWithoutMetaDataBlock() $content = $parser->extractContent($raw); $this->assertSame($raw, $content); - $meta = $parser->parse($raw); + $meta = $parser->extractMeta($raw); $this->assertSame([], $meta); } } From 7351a729927d195f8ffd3c795ae5d0f130b54a20 Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Fri, 29 Jul 2022 18:34:38 +0200 Subject: [PATCH 4/4] Remove dead code --- plugins/phile/parserMeta/Classes/Parser/Meta.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugins/phile/parserMeta/Classes/Parser/Meta.php b/plugins/phile/parserMeta/Classes/Parser/Meta.php index 2e1a8ac5..869731e8 100644 --- a/plugins/phile/parserMeta/Classes/Parser/Meta.php +++ b/plugins/phile/parserMeta/Classes/Parser/Meta.php @@ -76,9 +76,6 @@ public function extractContent(?string $rawData): string */ protected function splitRawIntoMetaAndContent(string $rawData): array { - $rawData = trim($rawData); - $fences = $this->config['fences']; - $meta = null; $content = $rawData;