Skip to content

Commit

Permalink
Merge pull request #277 from Schlaefer/feature/1.6/yaml-fence
Browse files Browse the repository at this point in the history
recognize '---' as meta data fence
  • Loading branch information
james2doyle committed Mar 19, 2016
2 parents 6b3632d + ad54ded commit 8b77408
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 12 additions & 7 deletions plugins/phile/parserMeta/Classes/Parser/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public function __construct(array $config = null)
public function parse($rawData)
{
$rawData = trim($rawData);
$fences = $this->config['fences'];

$start = substr($rawData, 0, 4);
if ($start === '<!--') {
$stop = '-->';
} elseif (substr($start, 0, 2) === '/*') {
$start = '/*';
$stop = '*/';
} else {
$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) {
return [];
}

Expand Down
12 changes: 11 additions & 1 deletion plugins/phile/parserMeta/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
/**
* config file
*/
return array();
return [
/**
* open and close tokens for meta tags
*/
'fences' =>
[
'c' => ['open' => '/*', 'close' => '*/'],
'html' => ['open' => '<!--', 'close' => '-->'],
'yaml' => ['open' => '---', 'close' => '---']
]
];
14 changes: 14 additions & 0 deletions tests/Phile/Model/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class MetaTest extends \PHPUnit_Framework_TestCase
Description: This description will go in the meta description tag
Date: 2014-08-01
-->
";

/**
* @var string meta data in YAML front matter format
*/
protected $metaTestData3 = "---
Title: Welcome
---
";

/**
Expand Down Expand Up @@ -87,4 +95,10 @@ public function testSpacedKey()
$meta->get('spaced_key')
);
}

public function testYamlFrontMatterFormat()
{
$meta = new \Phile\Model\Meta($this->metaTestData3);
$this->assertEquals('Welcome', $meta['title']);
}
}

0 comments on commit 8b77408

Please sign in to comment.