Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/Phile/Model/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 5 additions & 14 deletions lib/Phile/Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' => '<!--', 'close' => '-->'],
'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;
Expand Down
10 changes: 9 additions & 1 deletion lib/Phile/ServiceLocator/MetaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
63 changes: 44 additions & 19 deletions plugins/phile/parserMeta/Classes/Parser/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
*
Expand Down
16 changes: 15 additions & 1 deletion plugins/phile/parserMeta/tests/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}