Merge pull request #130 from dhensby/pulls/better-headers

FIX Allow multi-line headers
This commit is contained in:
Daniel Hensby 2017-02-27 10:29:31 +00:00 committed by GitHub
commit d474be2841
4 changed files with 42 additions and 26 deletions

View File

@ -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(
'/
(?<key>[A-Za-z0-9_-]+):
\s*
(?<value>.*)
/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);
}
}
}

View File

@ -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 = '/(?<key>[A-Za-z][A-Za-z0-9_-]+)[\t]*:[\t]*(?<value>[^:\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;
}
}

View File

@ -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()

View File

@ -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