1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-23 05:16:03 +02:00

Flextype Core: Add ability to work with different types of content #212 #186

- FrontmatterParser implementation
This commit is contained in:
Awilum
2019-08-10 23:57:40 +03:00
parent 70eadd487e
commit c986506a98
2 changed files with 60 additions and 5 deletions

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained Flextype Community.
*/
namespace Flextype;
use const PHP_EOL;
use function array_slice;
use function count;
use function implode;
use function ltrim;
use function preg_split;
use function trim;
class FrontmatterParser
{
/**
* Front matter parser
*
* @param string $content Content to parse
*
* @return array
*
* @access public
*/
public static function frontMatterParser(string $content) : array
{
$parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . ltrim($content));
if (count($parts) < 3) {
return ['matter' => [], 'body' => $content];
}
return ['matter' => trim($parts[1]), 'body' => implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2))];
}
public static function encode($input) : string
{
return '';
}
public static function decode(string $input)
{
return self::frontMatterParser($input);
}
}

View File

@@ -18,12 +18,9 @@ class Parser
], 'yaml' => [
'name' => 'yaml',
'ext' => 'yaml',
], 'md' => [
'name' => 'md',
], 'frontmatter' => [
'name' => 'frontmatter',
'ext' => 'md',
], 'toml' => [
'name' => 'toml',
'ext' => 'toml',
],
];
@@ -37,6 +34,10 @@ class Parser
case 'yaml':
return JsonParser::encode($input);
break;
case 'frontmatter':
return FrontmatterParser::encode($input);
break;
default:
// code...
@@ -54,6 +55,10 @@ class Parser
case 'yaml':
return YamlParser::decode($input);
break;
case 'frontmatter':
return FrontmatterParser::decode($input);
break;
default:
// code...