getMarkdown(); // Pre-processing $md = self::rewrite_image_links($md, $page); $md = self::rewrite_relative_links($md, $page, $baselink); $md = self::rewrite_api_links($md, $page); $md = self::rewrite_heading_anchors($md, $page); // $md = self::rewrite_code_blocks($md, $page); require_once('../sapphiredocs/thirdparty/markdown.php'); $html = Markdown($md); return $html; } function rewrite_code_blocks($md) { $started = false; $inner = false; $lines = split("\n", $md); foreach($lines as $i => $line) { if(preg_match('/^\t*:::\s*(.*)/', $line, $matches)) { // first line $started = true; $lines[$i] = sprintf('
', $matches[1]); } elseif($started && preg_match('/^\t(.*)/', $line, $matches)) { // remove first tab (subsequent tabs are part of the code) $lines[$i] = $matches[1]; $inner = true; } elseif($started && $inner) { // last line, close pre $lines[$i] = '' . "\n" . $line; $started = $inner = false; } } return join("\n", $lines); } static function rewrite_image_links($md, $page) { // Links with titles $re = '/ ! \[ (.*?) # image title (non greedy) \] \( (.*?) # image url (non greedy) \) /x'; preg_match_all($re, $md, $images); if($images) foreach($images[0] as $i => $match) { $title = $images[1][$i]; $url = $images[2][$i]; // Don't process absolute links (based on protocol detection) $urlParts = parse_url($url); if($urlParts && isset($urlParts['scheme'])) continue; // Rewrite URL (relative or absolute) $baselink = Director::makeRelative(dirname($page->getPath())); $relativeUrl = rtrim($baselink, '/') . '/' . ltrim($url, '/'); // Resolve relative paths while(strpos($relativeUrl, '/..') !== FALSE) { $relativeUrl = preg_replace('/\w+\/\.\.\//', '', $relativeUrl); } // Replace any double slashes (apart from protocol) $relativeUrl = preg_replace('/([^:])\/{2,}/', '$1/', $relativeUrl); // Make it absolute again $absoluteUrl = Director::absoluteBaseURL() . $relativeUrl; // Replace in original content $md = str_replace( $match, sprintf('![%s](%s)', $title, $absoluteUrl), $md ); } return $md; } /** * Rewrite links with special "api:" prefix, from two possible formats: * 1. [api:DataObject] * 2. (My Title)(api:DataObject) * * Hack: Replaces any backticks with "
" blocks,
* as the currently used markdown parser doesn't resolve links in backticks,
* but does resolve in "" blocks.
*
* @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;
}
/**
*
*/
static function rewrite_heading_anchors($md, $page) {
$re = '/
\#+(.*)
/x';
$md = preg_replace_callback($re, array('DocumentationParser', '_rewrite_heading_anchors_callback'), $md);
return $md;
}
static function _rewrite_heading_anchors_callback($matches) {
$heading = $matches[0];
$headingText = $matches[1];
if(preg_match('/\{\#.*\}/', $headingText)) return $heading;
if(!isset(self::$heading_counts[$headingText])) {
self::$heading_counts[$headingText] = 1;
}
else {
self::$heading_counts[$headingText]++;
$headingText .= "-" . self::$heading_counts[$headingText];
}
return sprintf("%s {#%s}", preg_replace('/\n/', '', $heading), self::generate_html_id($headingText));
}
/**
* Generate an html element id from a string
*
* @return String
*/
static function generate_html_id($title) {
$t = $title;
$t = str_replace('&','-and-',$t);
$t = str_replace('&','-and-',$t);
$t = ereg_replace('[^A-Za-z0-9]+','-',$t);
$t = ereg_replace('-+','-',$t);
$t = trim($t, '-');
return $t;
}
/**
* Resolves all relative links within markdown.
*
* @param String $md Markdown content
* @param DocumentationPage $page
* @param String $baselink
* @return String Markdown
*/
static function rewrite_relative_links($md, $page, $baselink) {
$re = '/
([^\!]?) # exclude image format
\[
(.*?) # link title (non greedy)
\]
\(
(.*?) # link url (non greedy)
\)
/x';
preg_match_all($re, $md, $matches);
// relative path (to module base folder), without the filename
$relativePath = dirname($page->getRelativePath());
if($relativePath == '.') $relativePath = '';
if($matches) {
foreach($matches[0] as $i => $match) {
$title = $matches[2][$i];
$url = $matches[3][$i];
// Don't process API links
if(preg_match('/^api:/', $url)) continue;
// Don't process absolute links (based on protocol detection)
$urlParts = parse_url($url);
if($urlParts && isset($urlParts['scheme'])) continue;
// Rewrite URL (relative or absolute)
if(preg_match('/^\//', $url)) {
$relativeUrl = $baselink . $url;
} else {
$relativeUrl = $baselink . '/' . $relativePath . '/' . $url;
}
// Resolve relative paths
while(strpos($relativeUrl, '..') !== FALSE) {
$relativeUrl = preg_replace('/\w+\/\.\.\//', '', $relativeUrl);
}
// Replace any double slashes (apart from protocol)
$relativeUrl = preg_replace('/([^:])\/{2,}/', '$1/', $relativeUrl);
// Replace in original content
$md = str_replace(
$match,
sprintf('%s[%s](%s)', $matches[1][$i], $title, $relativeUrl),
$md
);
}
}
return $md;
}
}