mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
FEATURE: added parser for metadata inside documentation. MINOR: removed outdated code
This commit is contained in:
parent
768345a8b9
commit
e571b5973f
@ -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);
|
||||
}
|
||||
}
|
@ -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('/
|
||||
(?<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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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)) {
|
||||
|
||||
|
@ -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 = <<<HTML
|
||||
$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 = <<<HTML
|
||||
<pre class="brush: php">
|
||||
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 = <<<HTML
|
||||
$this->assertContains($expected, $result, 'Custom code blocks with ::: prefix');
|
||||
|
||||
$expected = <<<HTML
|
||||
<pre>
|
||||
code block
|
||||
without formatting prefix
|
||||
</pre>
|
||||
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);
|
||||
}
|
||||
}
|
5
tests/docs-parser/en/MetaDataTest.md
Normal file
5
tests/docs-parser/en/MetaDataTest.md
Normal file
@ -0,0 +1,5 @@
|
||||
Title: Foo Bar's Test page.
|
||||
Author: Dr. Foo Bar.
|
||||
Another: Test.
|
||||
|
||||
# Content
|
Loading…
Reference in New Issue
Block a user