mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 09:05:56 +00: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;
|
return $link;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setFullPath($path) {
|
|
||||||
$this->fullPath = $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLang() {
|
function getLang() {
|
||||||
return $this->lang;
|
return $this->lang;
|
||||||
}
|
}
|
||||||
@ -169,6 +165,16 @@ class DocumentationPage extends ViewableData {
|
|||||||
return $this->title;
|
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
|
* @return string
|
||||||
*/
|
*/
|
||||||
@ -205,6 +211,6 @@ class DocumentationPage extends ViewableData {
|
|||||||
*/
|
*/
|
||||||
function getHTML($baselink = null) {
|
function getHTML($baselink = null) {
|
||||||
// if this is not a directory then we can to parse the file
|
// 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;
|
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,7 +387,6 @@ class DocumentationService {
|
|||||||
|
|
||||||
if($handle) {
|
if($handle) {
|
||||||
$extensions = DocumentationService::get_valid_extensions();
|
$extensions = DocumentationService::get_valid_extensions();
|
||||||
$firstFile = false;
|
|
||||||
|
|
||||||
// ensure we end with a slash
|
// ensure we end with a slash
|
||||||
$base = rtrim($base, '/') .'/';
|
$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.
|
// the folder is the one that we are looking for.
|
||||||
if(strtolower($name) == strtolower($formatted)) {
|
if(strtolower($name) == strtolower($formatted)) {
|
||||||
|
|
||||||
|
@ -5,13 +5,13 @@
|
|||||||
class DocumentationParserTest extends SapphireTest {
|
class DocumentationParserTest extends SapphireTest {
|
||||||
|
|
||||||
function testRewriteCodeBlocks() {
|
function testRewriteCodeBlocks() {
|
||||||
$page = new DocumentationPage();
|
$page = new DocumentationPage();
|
||||||
$page->setRelativePath('test.md');
|
$page->setRelativePath('test.md');
|
||||||
$page->setEntity(new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'));
|
$page->setEntity(new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'));
|
||||||
$page->setLang('en');
|
$page->setLang('en');
|
||||||
$page->setVersion('2.4');
|
$page->setVersion('2.4');
|
||||||
$result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
|
$result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
|
||||||
$expected = <<<HTML
|
$expected = <<<HTML
|
||||||
<pre class="brush: php">
|
<pre class="brush: php">
|
||||||
code block
|
code block
|
||||||
with multiple
|
with multiple
|
||||||
@ -23,16 +23,16 @@ lines
|
|||||||
Normal text after code block
|
Normal text after code block
|
||||||
HTML;
|
HTML;
|
||||||
|
|
||||||
$this->assertContains($expected, $result, 'Custom code blocks with ::: prefix');
|
$this->assertContains($expected, $result, 'Custom code blocks with ::: prefix');
|
||||||
|
|
||||||
$expected = <<<HTML
|
$expected = <<<HTML
|
||||||
<pre>
|
<pre>
|
||||||
code block
|
code block
|
||||||
without formatting prefix
|
without formatting prefix
|
||||||
</pre>
|
</pre>
|
||||||
HTML;
|
HTML;
|
||||||
$this->assertContains($expected, $result, 'Traditional markdown code blocks');
|
$this->assertContains($expected, $result, 'Traditional markdown code blocks');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testImageRewrites() {
|
function testImageRewrites() {
|
||||||
// Page on toplevel
|
// Page on toplevel
|
||||||
@ -205,4 +205,16 @@ HTML;
|
|||||||
$result
|
$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…
x
Reference in New Issue
Block a user