Fix front matter gathering, was very eager on some cases

Cette révision appartient à :
Stéphane Goetz 2016-04-28 23:33:26 +02:00
Parent b1c679edce
révision d25a001325
2 fichiers modifiés avec 23 ajouts et 12 suppressions

Voir le fichier

@ -14,12 +14,15 @@ class Content extends Entry
/** @var array */
protected $attributes;
/** @var bool */
protected $manuallySetContent = false;
/**
* @return string
*/
public function getContent()
{
if (!$this->content) {
if (!$this->content && !$this->manuallySetContent) {
$this->content = file_get_contents($this->getPath());
}
@ -35,6 +38,7 @@ class Content extends Entry
*/
public function setContent($content)
{
$this->manuallySetContent = true;
$this->content = $content;
}
@ -106,10 +110,15 @@ class Content extends Entry
// Parse the different attributes
$lines = preg_split('/\n/', $sections[0]);
foreach ($lines as $line) {
$parts = preg_split('/:/', $line, 2);
if (count($parts) !== 2) continue;
$key = strtolower(trim($parts[0]));
$value = trim($parts[1]);
$trimmed = trim($line);
if ($trimmed == '') continue; // skip empty lines
if ($trimmed[0] == '#') continue; // can be taken as comments
$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;
}

Voir le fichier

@ -15,23 +15,25 @@ class ContentTest extends \PHPUnit_Framework_TestCase
public function providerTestAttributes()
{
return array(
['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\ntitle :This is another title\n---\nThis is content\n", ['title' => 'This is another title']],
["title: This is a simple title\nthis is not metadata\n---\nThis is content\n", ['title' => 'This is a simple title']],
["title: This is only metatada, no content", []],
["title: This is almost only metadata\n---\n", ["title" => "This is almost only metadata"]]
['This is content', [], "This is content"],
["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'], "This is content"],
["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 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
*/
public function testAttributes($content, $attributes)
public function testAttributes($content, $attributes, $finalContent)
{
$obj = $this->createContent($content);
$this->assertEquals($attributes, $obj->getAttribute());
$this->assertEquals($finalContent, trim($obj->getContent()));
}
public function testNoAttributes()