From c00570ae55149dea783d59a343678ec747e12320 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Fri, 24 Feb 2017 15:11:51 +0000 Subject: [PATCH] FIX Allow multi-line headers --- code/DocumentationParser.php | 35 +++++++++++++++++----------- code/models/DocumentationPage.php | 28 ++++++++++++---------- tests/DocumentationParserTest.php | 2 ++ tests/docs-parser/en/MetaDataTest.md | 3 +++ 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/code/DocumentationParser.php b/code/DocumentationParser.php index 8317519..fd8d40d 100755 --- a/code/DocumentationParser.php +++ b/code/DocumentationParser.php @@ -527,22 +527,29 @@ class DocumentationParser * * @param DocumentationPage */ - public static function retrieve_meta_data(DocumentationPage &$page) + 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]); + $md = $page->getMarkdown(); + if ($md) { + // get the text up to the first empty line + $extPattern = "/^(.+)\n\r*\n/Uis"; + $matches = preg_match($extPattern, $md, $block); + + if ($matches && $block[1]) { + + // find the key/value pairs + $lines = preg_split('/\v+/', $block[1]); + $key = ''; + $value = ''; + foreach ($lines as $line) { + if (strpos($line, ':') !== false) { + list($key, $value) = explode(':', $line, 2); + $key = trim($key); + $value = trim($value); + } else { + $value .= ' ' . trim($line); } + $page->setMetaData($key, $value); } } } diff --git a/code/models/DocumentationPage.php b/code/models/DocumentationPage.php index 6da3b29..190bfd9 100755 --- a/code/models/DocumentationPage.php +++ b/code/models/DocumentationPage.php @@ -281,24 +281,28 @@ class DocumentationPage extends ViewableData public function populateMetaDataFromText(&$md, $removeMetaData = false) { if ($md) { - // get the text up to the first whiteline - $extPattern = "/^(.+)\n(\r)*\n/Uis"; + // get the text up to the first empty line + $extPattern = "/^(.+)\n\r*\n/Uis"; $matches = preg_match($extPattern, $md, $block); if ($matches && $block[1]) { $metaDataFound = false; // find the key/value pairs - $intPattern = '/(?[A-Za-z][A-Za-z0-9_-]+)[\t]*:[\t]*(?[^:\n\r\/]+)/x'; - $matches = preg_match_all($intPattern, $block[1], $meta); - - foreach ($meta['key'] as $index => $key) { - if (isset($meta['value'][$index])) { - // check if a property exists for this key - if (property_exists(get_class(), $key)) { - $this->$key = $meta['value'][$index]; - $metaDataFound = true; - } + $lines = preg_split('/\v+/', $block[1]); + $key = ''; + $value = ''; + foreach ($lines as $line) { + if (strpos($line, ':') !== false) { + list($key, $value) = explode(':', $line, 2); + $key = trim($key); + $value = trim($value); + } else { + $value .= ' ' . trim($line); + } + if (property_exists(get_class(), $key)) { + $this->$key = $value; + $metaDataFound = true; } } diff --git a/tests/DocumentationParserTest.php b/tests/DocumentationParserTest.php index 302c59b..57ccecf 100755 --- a/tests/DocumentationParserTest.php +++ b/tests/DocumentationParserTest.php @@ -399,6 +399,8 @@ HTML; $this->assertEquals('Dr. Foo Bar.', $this->metaDataPage->author); $this->assertEquals("Foo Bar's Test page.", $this->metaDataPage->getTitle()); + $this->assertEquals("Foo Bar's Test page.", $this->metaDataPage->getTitle()); + $this->assertEquals("A long intro that splits over many lines", $this->metaDataPage->getIntroduction()); } public function testRewritingRelativeLinksToFiles() diff --git a/tests/docs-parser/en/MetaDataTest.md b/tests/docs-parser/en/MetaDataTest.md index 03123a8..e51b484 100755 --- a/tests/docs-parser/en/MetaDataTest.md +++ b/tests/docs-parser/en/MetaDataTest.md @@ -1,5 +1,8 @@ Title: Foo Bar's Test page. Author: Dr. Foo Bar. Another: Test. +Introduction: A long intro that +splits over +many lines # Content