mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 09:05:56 +00:00
Merge pull request #130 from dhensby/pulls/better-headers
FIX Allow multi-line headers
This commit is contained in:
commit
d474be2841
@ -527,22 +527,29 @@ class DocumentationParser
|
|||||||
*
|
*
|
||||||
* @param DocumentationPage
|
* @param DocumentationPage
|
||||||
*/
|
*/
|
||||||
public static function retrieve_meta_data(DocumentationPage &$page)
|
public static function retrieve_meta_data(DocumentationPage $page)
|
||||||
{
|
{
|
||||||
if ($md = $page->getMarkdown()) {
|
$md = $page->getMarkdown();
|
||||||
$matches = preg_match_all(
|
if ($md) {
|
||||||
'/
|
// get the text up to the first empty line
|
||||||
(?<key>[A-Za-z0-9_-]+):
|
$extPattern = "/^(.+)\n\r*\n/Uis";
|
||||||
\s*
|
$matches = preg_match($extPattern, $md, $block);
|
||||||
(?<value>.*)
|
|
||||||
/x', $md, $meta
|
if ($matches && $block[1]) {
|
||||||
);
|
|
||||||
|
// find the key/value pairs
|
||||||
if ($matches) {
|
$lines = preg_split('/\v+/', $block[1]);
|
||||||
foreach ($meta['key'] as $index => $key) {
|
$key = '';
|
||||||
if (isset($meta['value'][$index])) {
|
$value = '';
|
||||||
$page->setMetaData($key, $meta['value'][$index]);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,24 +281,28 @@ class DocumentationPage extends ViewableData
|
|||||||
public function populateMetaDataFromText(&$md, $removeMetaData = false)
|
public function populateMetaDataFromText(&$md, $removeMetaData = false)
|
||||||
{
|
{
|
||||||
if ($md) {
|
if ($md) {
|
||||||
// get the text up to the first whiteline
|
// get the text up to the first empty line
|
||||||
$extPattern = "/^(.+)\n(\r)*\n/Uis";
|
$extPattern = "/^(.+)\n\r*\n/Uis";
|
||||||
$matches = preg_match($extPattern, $md, $block);
|
$matches = preg_match($extPattern, $md, $block);
|
||||||
|
|
||||||
if ($matches && $block[1]) {
|
if ($matches && $block[1]) {
|
||||||
$metaDataFound = false;
|
$metaDataFound = false;
|
||||||
|
|
||||||
// find the key/value pairs
|
// find the key/value pairs
|
||||||
$intPattern = '/(?<key>[A-Za-z][A-Za-z0-9_-]+)[\t]*:[\t]*(?<value>[^:\n\r\/]+)/x';
|
$lines = preg_split('/\v+/', $block[1]);
|
||||||
$matches = preg_match_all($intPattern, $block[1], $meta);
|
$key = '';
|
||||||
|
$value = '';
|
||||||
foreach ($meta['key'] as $index => $key) {
|
foreach ($lines as $line) {
|
||||||
if (isset($meta['value'][$index])) {
|
if (strpos($line, ':') !== false) {
|
||||||
// check if a property exists for this key
|
list($key, $value) = explode(':', $line, 2);
|
||||||
if (property_exists(get_class(), $key)) {
|
$key = trim($key);
|
||||||
$this->$key = $meta['value'][$index];
|
$value = trim($value);
|
||||||
$metaDataFound = true;
|
} else {
|
||||||
}
|
$value .= ' ' . trim($line);
|
||||||
|
}
|
||||||
|
if (property_exists(get_class(), $key)) {
|
||||||
|
$this->$key = $value;
|
||||||
|
$metaDataFound = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -399,6 +399,8 @@ HTML;
|
|||||||
|
|
||||||
$this->assertEquals('Dr. Foo Bar.', $this->metaDataPage->author);
|
$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("Foo Bar's Test page.", $this->metaDataPage->getTitle());
|
||||||
|
$this->assertEquals("A long intro that splits over many lines", $this->metaDataPage->getIntroduction());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRewritingRelativeLinksToFiles()
|
public function testRewritingRelativeLinksToFiles()
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
Title: Foo Bar's Test page.
|
Title: Foo Bar's Test page.
|
||||||
Author: Dr. Foo Bar.
|
Author: Dr. Foo Bar.
|
||||||
Another: Test.
|
Another: Test.
|
||||||
|
Introduction: A long intro that
|
||||||
|
splits over
|
||||||
|
many lines
|
||||||
|
|
||||||
# Content
|
# Content
|
||||||
|
Loading…
x
Reference in New Issue
Block a user