FEATURE Parsing of API links

This commit is contained in:
Ingo Schommer 2010-08-01 04:46:37 +00:00
parent 277bca7b11
commit b96b2b4016
3 changed files with 73 additions and 1 deletions

View File

@ -8,6 +8,11 @@
*/
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
@ -33,12 +38,60 @@ class DocumentationParser {
// Pre-processing
$md = self::rewrite_relative_links($md, $page, $baselink);
$md = self::rewrite_api_links($md, $page);
$html = Markdown($md);
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.
*

View File

@ -3,6 +3,24 @@
* @package sapphiredocs
*/
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() {
// Page on toplevel

View File

@ -8,4 +8,5 @@ test
[link: subfolder page](subfolder/subpage)
[link: with anchor](/test#anchor)
[link: http](http://silverstripe.org)
[link: api](api:DataObject)
[link: api](api:DataObject)
[api:DataObject::$has_one]