mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 09:05:56 +00:00
FEATURE Parsing of API links
This commit is contained in:
parent
277bca7b11
commit
b96b2b4016
@ -9,6 +9,11 @@
|
|||||||
|
|
||||||
class DocumentationParser {
|
class DocumentationParser {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var String Rewriting of api links in the format "[api:MyClass]" or "[api:MyClass::$my_property]".
|
||||||
|
*/
|
||||||
|
static $api_link_base = 'http://api.silverstripe.org/search/lookup/?q=%s&version=%s&module=%s';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a given path to the documentation for a file. Performs a case insensitive
|
* Parse a given path to the documentation for a file. Performs a case insensitive
|
||||||
* lookup on the file system. Automatically appends the file extension to one of the markdown
|
* lookup on the file system. Automatically appends the file extension to one of the markdown
|
||||||
@ -33,12 +38,60 @@ class DocumentationParser {
|
|||||||
|
|
||||||
// Pre-processing
|
// Pre-processing
|
||||||
$md = self::rewrite_relative_links($md, $page, $baselink);
|
$md = self::rewrite_relative_links($md, $page, $baselink);
|
||||||
|
$md = self::rewrite_api_links($md, $page);
|
||||||
|
|
||||||
$html = Markdown($md);
|
$html = Markdown($md);
|
||||||
|
|
||||||
return DBField::create('HTMLText', $html);
|
return DBField::create('HTMLText', $html);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param String $md
|
||||||
|
* @param DocumentationPage $page
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
static function rewrite_api_links($md, $page) {
|
||||||
|
// Links with titles
|
||||||
|
$re = '/
|
||||||
|
\[
|
||||||
|
(.*?) # link title (non greedy)
|
||||||
|
\]
|
||||||
|
\(
|
||||||
|
api:(.*?) # link url (non greedy)
|
||||||
|
\)
|
||||||
|
/x';
|
||||||
|
preg_match_all($re, $md, $linksWithTitles);
|
||||||
|
if($linksWithTitles) foreach($linksWithTitles[0] as $i => $match) {
|
||||||
|
$title = $linksWithTitles[1][$i];
|
||||||
|
$subject = $linksWithTitles[2][$i];
|
||||||
|
$url = sprintf(self::$api_link_base, $subject, $page->getVersion(), $page->getEntity()->getModuleFolder());
|
||||||
|
$md = str_replace(
|
||||||
|
$match,
|
||||||
|
sprintf('[%s](%s)', $title, $url),
|
||||||
|
$md
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bare links
|
||||||
|
$re = '/
|
||||||
|
\[
|
||||||
|
api:(.*?)
|
||||||
|
\]
|
||||||
|
/x';
|
||||||
|
preg_match_all($re, $md, $links);
|
||||||
|
if($links) foreach($links[0] as $i => $match) {
|
||||||
|
$subject = $links[1][$i];
|
||||||
|
$url = sprintf(self::$api_link_base, $subject, $page->getVersion(), $page->getEntity()->getModuleFolder());
|
||||||
|
$md = str_replace(
|
||||||
|
$match,
|
||||||
|
sprintf('[%s](%s)', $subject, $url),
|
||||||
|
$md
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $md;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves all relative links within markdown.
|
* Resolves all relative links within markdown.
|
||||||
*
|
*
|
||||||
|
@ -4,6 +4,24 @@
|
|||||||
*/
|
*/
|
||||||
class DocumentationParserTest extends SapphireTest {
|
class DocumentationParserTest extends SapphireTest {
|
||||||
|
|
||||||
|
function testApiLinks() {
|
||||||
|
// Page on toplevel
|
||||||
|
$page = new DocumentationPage(
|
||||||
|
'test.md',
|
||||||
|
new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'),
|
||||||
|
'en',
|
||||||
|
'2.4'
|
||||||
|
);
|
||||||
|
$result = DocumentationParser::rewrite_api_links($page->getMarkdown(), $page, 'mycontroller/cms/2.4/en/');
|
||||||
|
$this->assertContains(
|
||||||
|
'[link: api](http://api.silverstripe.org/search/lookup/?q=DataObject&version=2.4&module=mymodule)',
|
||||||
|
$result
|
||||||
|
);
|
||||||
|
$this->assertContains( '[DataObject::$has_one](http://api.silverstripe.org/search/lookup/?q=DataObject::$has_one&version=2.4&module=mymodule)',
|
||||||
|
$result
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function testRelativeLinks() {
|
function testRelativeLinks() {
|
||||||
// Page on toplevel
|
// Page on toplevel
|
||||||
$page = new DocumentationPage(
|
$page = new DocumentationPage(
|
||||||
|
@ -9,3 +9,4 @@ test
|
|||||||
[link: with anchor](/test#anchor)
|
[link: with anchor](/test#anchor)
|
||||||
[link: http](http://silverstripe.org)
|
[link: http](http://silverstripe.org)
|
||||||
[link: api](api:DataObject)
|
[link: api](api:DataObject)
|
||||||
|
[api:DataObject::$has_one]
|
Loading…
x
Reference in New Issue
Block a user