mirror of
https://github.com/flextype/flextype.git
synced 2025-08-11 23:54:06 +02:00
Flextype Core: new Frontmatter Parser - added.
This commit is contained in:
@@ -339,24 +339,6 @@ class Entries
|
||||
return Entries::$shortcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Front matter parser
|
||||
*
|
||||
* $content = Entries::frontMatterParser($content);
|
||||
*
|
||||
* @param string $content Content to parse
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
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))];
|
||||
}
|
||||
|
||||
/**
|
||||
* Process entry
|
||||
*
|
||||
@@ -379,7 +361,7 @@ class Entries
|
||||
} else {
|
||||
|
||||
// Create $entry_frontmatter and $entry_content
|
||||
$entry = Entries::frontMatterParser($entry);
|
||||
$entry = FrontmatterParser::parse($entry);
|
||||
$entry_frontmatter = $entry['matter'];
|
||||
$entry_content = $entry['body'];
|
||||
|
||||
|
35
flextype/parsers/FrontmatterParser.php
Normal file
35
flextype/parsers/FrontmatterParser.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Flextype
|
||||
*
|
||||
* @author Sergey Romanenko <awilum@yandex.ru>
|
||||
* @link http://flextype.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
class FrontmatterParser {
|
||||
|
||||
/**
|
||||
* Get [matter] and [body] from a content.
|
||||
* PHP implementation of Jekyll Front Matter.
|
||||
*
|
||||
* $content = Entries::frontMatterParser($content);
|
||||
*
|
||||
* @param string $content Content to parse
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public static function parse(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))];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user