From 93330e86db8472d450d6af6771e961f087c67ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Wed, 11 Nov 2015 00:09:47 +0100 Subject: [PATCH] Add metadata support, fixes #210 --- docs/05_More_Examples/Hello_World_de.md | 5 ++ libs/Tree/Content.php | 81 ++++++++++++++++++++++--- tests/Tree/ContentTest.php | 56 +++++++++++++++++ 3 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 docs/05_More_Examples/Hello_World_de.md create mode 100644 tests/Tree/ContentTest.php diff --git a/docs/05_More_Examples/Hello_World_de.md b/docs/05_More_Examples/Hello_World_de.md new file mode 100644 index 0000000..6b02b2e --- /dev/null +++ b/docs/05_More_Examples/Hello_World_de.md @@ -0,0 +1,5 @@ +title: Hallo Welt +date: 12th December 1984 +------- + +This is a page which has attributes and a overriden Title diff --git a/libs/Tree/Content.php b/libs/Tree/Content.php index bb1151f..5d29f51 100644 --- a/libs/Tree/Content.php +++ b/libs/Tree/Content.php @@ -2,21 +2,18 @@ class Content extends Entry { - /** - * @var string - */ + /** @var string */ protected $content; - /** - * @var Content - */ + /** @var Content */ protected $previous; - /** - * @var Content - */ + /** @var Content */ protected $next; + /** @var array */ + protected $attributes; + /** * @return string */ @@ -26,6 +23,10 @@ class Content extends Entry $this->content = file_get_contents($this->getPath()); } + if ($this->attributes === null) { + $this->parseAttributes(); + } + return $this->content; } @@ -74,6 +75,68 @@ class Content extends Entry return $this->name == 'index' || $this->name == '_index'; } + public function getTitle() + { + if ($title = $this->getAttribute('title')) { + return $title; + } + + return parent::getTitle(); + } + + protected function parseAttributes() + { + // We set an empty array first to + // avoid a loop when "parseAttributes" + // is called in "getContent" + $this->attributes = []; + + $content = $this->getContent(); + $sections = preg_split('/\s+-{3,}\s+/', $content, 2); + + // Only do it if we have two sections + if (count($sections) != 2) { + return; + } + + // 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]); + $this->attributes[$key] = $value; + } + + // Only remove the content if we have at least one attribute + if (count($this->attributes) > 0) { + $this->setContent($sections[1]); + } + } + + public function setAttributes(array $attributes) + { + $this->attributes = $attributes; + } + + public function getAttribute($key = null) + { + if ($this->attributes === null) { + $this->parseAttributes(); + } + + if (is_null($key)) { + return $this->attributes; + } + + if (!array_key_exists($key, $this->attributes)) { + return null; + } + + return $this->attributes[$key]; + } + public function dump() { $dump = parent::dump(); diff --git a/tests/Tree/ContentTest.php b/tests/Tree/ContentTest.php new file mode 100644 index 0000000..f539e17 --- /dev/null +++ b/tests/Tree/ContentTest.php @@ -0,0 +1,56 @@ +setContent($content); + + return $obj; + } + + 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"]] + ); + } + + /** + * @dataProvider providerTestAttributes + */ + public function testAttributes($content, $attributes) + { + $obj = $this->createContent($content); + + $this->assertEquals($attributes, $obj->getAttribute()); + } + + public function testNoAttributes() + { + $content = "This is a content with a separator\n----\n this wasn't an attribute"; + + $obj = $this->createContent($content); + + $this->assertEquals($content, $obj->getContent()); + } + + public function testContentPreserved() + { + $content = "this was an attribute, but also a separator\n----\nand it works"; + $with_attribute = "title: a title\n----\n$content"; + + $obj = $this->createContent($with_attribute); + + $this->assertEquals($content, $obj->getContent()); + $this->assertEquals('a title', $obj->getAttribute('title')); + } +}