diff --git a/code/DocumentationPage.php b/code/DocumentationPage.php index 5c5935d..b0410be 100644 --- a/code/DocumentationPage.php +++ b/code/DocumentationPage.php @@ -141,10 +141,6 @@ class DocumentationPage extends ViewableData { return $link; } - function setFullPath($path) { - $this->fullPath = $path; - } - function getLang() { return $this->lang; } @@ -169,6 +165,16 @@ class DocumentationPage extends ViewableData { return $this->title; } + /** + * Set a variable from the metadata field on this class + * + * @param String key + * @param mixed value + */ + public function setMetaData($key, $value) { + $this->$key = $value; + } + /** * @return string */ @@ -182,7 +188,7 @@ class DocumentationPage extends ViewableData { return $path; } - + /** * @return String */ @@ -205,6 +211,6 @@ class DocumentationPage extends ViewableData { */ function getHTML($baselink = null) { // if this is not a directory then we can to parse the file - return DocumentationParser::parse($this->getPath(true), $baselink); + return DocumentationParser::parse($this, $baselink); } } \ No newline at end of file diff --git a/code/DocumentationParser.php b/code/DocumentationParser.php index 9974ab4..d4988a6 100644 --- a/code/DocumentationParser.php +++ b/code/DocumentationParser.php @@ -303,4 +303,27 @@ class DocumentationParser { return $md; } + + /** + * Strips out the metadata for a page + * + * @param DocumentationPage + */ + public static function retrieve_meta_data(DocumentationPage &$page) { + if($md = $page->getMarkdown()) { + $matches = preg_match_all('/ + (?[A-Za-z0-9_-]+): + \s* + (?.*) + /x', $md, $meta); + + if($matches) { + foreach($meta['key'] as $index => $key) { + if(isset($meta['value'][$index])) { + $page->setMetaData($key, $meta['value'][$index]); + } + } + } + } + } } \ No newline at end of file diff --git a/code/DocumentationService.php b/code/DocumentationService.php index b9d742f..737f54f 100644 --- a/code/DocumentationService.php +++ b/code/DocumentationService.php @@ -387,8 +387,7 @@ class DocumentationService { if($handle) { $extensions = DocumentationService::get_valid_extensions(); - $firstFile = false; - + // ensure we end with a slash $base = rtrim($base, '/') .'/'; @@ -406,9 +405,6 @@ class DocumentationService { } } - // save this file as a backup if we don't have one to fail back to - if(!$firstFile && !is_dir($base . $file)) $firstFile = $file; - // the folder is the one that we are looking for. if(strtolower($name) == strtolower($formatted)) { diff --git a/tests/DocumentationParserTest.php b/tests/DocumentationParserTest.php index 0945f73..0d47ed0 100644 --- a/tests/DocumentationParserTest.php +++ b/tests/DocumentationParserTest.php @@ -5,13 +5,13 @@ class DocumentationParserTest extends SapphireTest { function testRewriteCodeBlocks() { - $page = new DocumentationPage(); - $page->setRelativePath('test.md'); - $page->setEntity(new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/')); - $page->setLang('en'); - $page->setVersion('2.4'); - $result = DocumentationParser::rewrite_code_blocks($page->getMarkdown()); - $expected = <<setRelativePath('test.md'); + $page->setEntity(new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/')); + $page->setLang('en'); + $page->setVersion('2.4'); + $result = DocumentationParser::rewrite_code_blocks($page->getMarkdown()); + $expected = << code block with multiple @@ -23,16 +23,16 @@ lines Normal text after code block HTML; - $this->assertContains($expected, $result, 'Custom code blocks with ::: prefix'); - - $expected = <<assertContains($expected, $result, 'Custom code blocks with ::: prefix'); + + $expected = << code block without formatting prefix HTML; - $this->assertContains($expected, $result, 'Traditional markdown code blocks'); - } + $this->assertContains($expected, $result, 'Traditional markdown code blocks'); + } function testImageRewrites() { // Page on toplevel @@ -205,4 +205,16 @@ HTML; $result ); } + + function testRetrieveMetaData() { + $page = new DocumentationPage(); + $page->setRelativePath('MetaDataTest.md'); + $page->setEntity(new DocumentationEntity('parser', '2.4', BASE_PATH . '/sapphiredocs/tests/docs-parser/')); + + DocumentationParser::retrieve_meta_data($page); + + $this->assertEquals('Dr. Foo Bar.', $page->Author); + $this->assertEquals("Foo Bar's Test page.", $page->getTitle()); + $this->assertEquals("Foo Bar's Test page.", $page->Title); + } } \ No newline at end of file diff --git a/tests/docs-parser/en/MetaDataTest.md b/tests/docs-parser/en/MetaDataTest.md new file mode 100644 index 0000000..03123a8 --- /dev/null +++ b/tests/docs-parser/en/MetaDataTest.md @@ -0,0 +1,5 @@ +Title: Foo Bar's Test page. +Author: Dr. Foo Bar. +Another: Test. + +# Content