Fix front matter gathering, was very eager on some cases

This commit is contained in:
Stéphane Goetz 2016-04-28 23:33:26 +02:00
parent b1c679edce
commit d25a001325
2 changed files with 23 additions and 12 deletions

View File

@ -14,12 +14,15 @@ class Content extends Entry
/** @var array */ /** @var array */
protected $attributes; protected $attributes;
/** @var bool */
protected $manuallySetContent = false;
/** /**
* @return string * @return string
*/ */
public function getContent() public function getContent()
{ {
if (!$this->content) { if (!$this->content && !$this->manuallySetContent) {
$this->content = file_get_contents($this->getPath()); $this->content = file_get_contents($this->getPath());
} }
@ -35,6 +38,7 @@ class Content extends Entry
*/ */
public function setContent($content) public function setContent($content)
{ {
$this->manuallySetContent = true;
$this->content = $content; $this->content = $content;
} }
@ -106,10 +110,15 @@ class Content extends Entry
// Parse the different attributes // Parse the different attributes
$lines = preg_split('/\n/', $sections[0]); $lines = preg_split('/\n/', $sections[0]);
foreach ($lines as $line) { foreach ($lines as $line) {
$parts = preg_split('/:/', $line, 2); $trimmed = trim($line);
if (count($parts) !== 2) continue; if ($trimmed == '') continue; // skip empty lines
$key = strtolower(trim($parts[0])); if ($trimmed[0] == '#') continue; // can be taken as comments
$value = trim($parts[1]);
$re = "/^([-\\w]*)\\s*?:(.*)/";
if (!preg_match($re, $trimmed, $parts)) break; //Break as soon as we have a line that doesn't match
$key = strtolower(trim($parts[1]));
$value = trim($parts[2]);
$this->attributes[$key] = $value; $this->attributes[$key] = $value;
} }

View File

@ -15,23 +15,25 @@ class ContentTest extends \PHPUnit_Framework_TestCase
public function providerTestAttributes() public function providerTestAttributes()
{ {
return array( return array(
['This is content', []], ['This is content', [], "This is content"],
["title: This is a simple title\n---\nThis is content\n", ['title' => 'This is a simple title']], ["title: This is a simple title\n---\nThis is content\n", ['title' => 'This is a simple title'], "This is content"],
["title: This is a simple title\ntitle :This is another title\n---\nThis is content\n", ['title' => 'This is another title']], ["title: This is a simple title\ntitle :This is another title\n---\nThis is content\n", ['title' => 'This is another title'], "This is content"],
["title: This is a simple title\nthis is not metadata\n---\nThis is content\n", ['title' => 'This is a simple title']], ["title: This is a simple title\nthis is not metadata\n---\nThis is content\n", ['title' => 'This is a simple title'], "This is content"],
["title: This is only metatada, no content", []], ["title: This is only metatada, no content", [], "title: This is only metatada, no content"],
["title: This is almost only metadata\n---\n", ["title" => "This is almost only metadata"]] ["title: This is almost only metadata\n---\n", ["title" => "This is almost only metadata"], ""],
["# Some content\n\nhi\n```yml\nvalue: true\n```\n----\n Follow up", [], "# Some content\n\nhi\n```yml\nvalue: true\n```\n----\n Follow up"],
); );
} }
/** /**
* @dataProvider providerTestAttributes * @dataProvider providerTestAttributes
*/ */
public function testAttributes($content, $attributes) public function testAttributes($content, $attributes, $finalContent)
{ {
$obj = $this->createContent($content); $obj = $this->createContent($content);
$this->assertEquals($attributes, $obj->getAttribute()); $this->assertEquals($attributes, $obj->getAttribute());
$this->assertEquals($finalContent, trim($obj->getContent()));
} }
public function testNoAttributes() public function testNoAttributes()