2010-06-24 16:22:41 +02:00
|
|
|
<?php
|
2011-07-01 08:49:31 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
/**
|
2013-02-19 10:23:22 +01:00
|
|
|
* Parser wrapping the Markdown Extra parser.
|
|
|
|
*
|
|
|
|
* @see http://michelf.com/projects/php-markdown/extra/
|
2010-12-21 10:42:44 +01:00
|
|
|
*
|
2012-04-08 11:36:16 +02:00
|
|
|
* @package docsviewer
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
|
|
|
class DocumentationParser {
|
2010-12-21 10:42:44 +01:00
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
const CODE_BLOCK_BACKTICK = 1;
|
|
|
|
const CODE_BLOCK_COLON = 2;
|
|
|
|
|
2010-08-01 06:46:37 +02:00
|
|
|
/**
|
2013-02-19 10:23:22 +01:00
|
|
|
* @var string Rewriting of api links in the format "[api:MyClass]" or "[api:MyClass::$my_property]".
|
2010-08-01 06:46:37 +02:00
|
|
|
*/
|
2013-02-19 10:23:22 +01:00
|
|
|
public static $api_link_base = 'http://api.silverstripe.org/search/lookup/?q=%s&version=%s&module=%s';
|
2010-09-03 07:29:15 +02:00
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $heading_counts = array();
|
2010-09-03 07:29:15 +02:00
|
|
|
|
2010-06-24 16:22:41 +02:00
|
|
|
/**
|
2012-04-08 11:23:49 +02:00
|
|
|
* 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 extensions as well so /install/ in a
|
2013-02-19 10:23:22 +01:00
|
|
|
* web browser will match /install.md or /INSTALL.md.
|
2010-08-01 06:46:32 +02:00
|
|
|
*
|
|
|
|
* Filepath: /var/www/myproject/src/cms/en/folder/subfolder/page.md
|
|
|
|
* URL: http://myhost/mywebroot/dev/docs/2.4/cms/en/folder/subfolder/page
|
|
|
|
* Webroot: http://myhost/mywebroot/
|
|
|
|
* Baselink: dev/docs/2.4/cms/en/
|
|
|
|
* Pathparts: folder/subfolder/page
|
|
|
|
*
|
|
|
|
* @param DocumentationPage $page
|
2013-02-19 10:23:22 +01:00
|
|
|
* @param String $baselink Link relative to webroot, up until the "root"
|
|
|
|
* of the module. Necessary to rewrite relative
|
|
|
|
* links
|
2010-06-24 16:22:41 +02:00
|
|
|
*
|
2010-08-01 10:25:00 +02:00
|
|
|
* @return String
|
2010-06-24 16:22:41 +02:00
|
|
|
*/
|
2010-08-01 06:46:32 +02:00
|
|
|
public static function parse(DocumentationPage $page, $baselink = null) {
|
2014-09-15 11:47:45 +02:00
|
|
|
if(!$page || (!$page instanceof DocumentationPage)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
|
2013-05-22 21:35:03 +02:00
|
|
|
$md = $page->getMarkdown(true);
|
2011-07-01 03:19:35 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
// Pre-processing
|
|
|
|
$md = self::rewrite_image_links($md, $page);
|
|
|
|
$md = self::rewrite_relative_links($md, $page, $baselink);
|
2011-01-14 02:03:46 +01:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
$md = self::rewrite_api_links($md, $page);
|
|
|
|
$md = self::rewrite_heading_anchors($md, $page);
|
2014-09-26 10:15:40 +02:00
|
|
|
|
2012-05-17 23:48:57 +02:00
|
|
|
$md = self::rewrite_code_blocks($md);
|
2014-09-26 10:15:40 +02:00
|
|
|
|
2014-07-27 02:39:44 +02:00
|
|
|
$parser = new ParsedownExtra();
|
2014-09-26 10:15:40 +02:00
|
|
|
$parser->setBreaksEnabled(false);
|
|
|
|
|
|
|
|
$text = $parser->text($md);
|
|
|
|
|
|
|
|
return $text;
|
2010-08-01 10:25:00 +02:00
|
|
|
}
|
|
|
|
|
2012-05-17 23:48:57 +02:00
|
|
|
public static function rewrite_code_blocks($md) {
|
2011-01-10 10:18:44 +01:00
|
|
|
$started = false;
|
|
|
|
$inner = false;
|
2013-02-19 10:23:22 +01:00
|
|
|
$mode = false;
|
|
|
|
$end = false;
|
2014-09-26 10:15:40 +02:00
|
|
|
$debug = false;
|
2013-02-19 10:23:22 +01:00
|
|
|
|
2012-05-17 23:48:57 +02:00
|
|
|
$lines = explode("\n", $md);
|
2013-02-19 10:23:22 +01:00
|
|
|
$output = array();
|
|
|
|
|
2011-01-10 10:18:44 +01:00
|
|
|
foreach($lines as $i => $line) {
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) var_dump('Line '. ($i + 1) . ' '. $line);
|
|
|
|
|
2014-07-27 01:25:28 +02:00
|
|
|
// if line just contains whitespace, continue down the page.
|
|
|
|
// Prevents code blocks with leading tabs adding an extra line.
|
2014-09-26 10:15:40 +02:00
|
|
|
if(preg_match('/^\s$/', $line) && !$started) {
|
2014-07-27 01:25:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-27 02:39:44 +02:00
|
|
|
if(!$started && preg_match('/^[\t]*:::\s*(.*)/', $line, $matches)) {
|
2011-01-10 11:59:22 +01:00
|
|
|
// first line with custom formatting
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) var_dump('Starts a new block with :::');
|
|
|
|
|
2011-01-10 10:18:44 +01:00
|
|
|
$started = true;
|
2013-02-19 10:23:22 +01:00
|
|
|
$mode = self::CODE_BLOCK_COLON;
|
2014-09-26 10:15:40 +02:00
|
|
|
|
|
|
|
$output[$i] = sprintf('```%s', (isset($matches[1])) ? trim($matches[1]) : "");
|
|
|
|
|
2014-07-27 02:39:44 +02:00
|
|
|
} else if(!$started && preg_match('/^\t*```\s*(.*)/', $line, $matches)) {
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) var_dump('Starts a new block with ```');
|
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
$started = true;
|
|
|
|
$mode = self::CODE_BLOCK_BACKTICK;
|
2014-09-26 10:15:40 +02:00
|
|
|
|
|
|
|
$output[$i] = sprintf('```%s', (isset($matches[1])) ? trim($matches[1]) : "");
|
2014-07-27 02:39:44 +02:00
|
|
|
} else if($started && $mode == self::CODE_BLOCK_BACKTICK) {
|
2013-02-19 10:23:22 +01:00
|
|
|
// inside a backtick fenced box
|
|
|
|
if(preg_match('/^\t*```\s*/', $line, $matches)) {
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) var_dump('End a block with ```');
|
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
// end of the backtick fenced box. Unset the line that contains the backticks
|
|
|
|
$end = true;
|
|
|
|
}
|
|
|
|
else {
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) var_dump('Still in a block with ```');
|
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
// still inside the line.
|
2014-09-26 10:15:40 +02:00
|
|
|
if(!$started) {
|
|
|
|
$output[$i - 1] = '```';
|
|
|
|
}
|
|
|
|
|
|
|
|
$output[$i] = $line;
|
2013-02-19 10:23:22 +01:00
|
|
|
$inner = true;
|
|
|
|
}
|
2014-07-27 02:39:44 +02:00
|
|
|
} else if(preg_match('/^[\ ]{0,3}?[\t](.*)/', $line, $matches)) {
|
2014-09-26 10:15:40 +02:00
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
// inner line of block, or first line of standard markdown code block
|
2011-01-10 11:59:22 +01:00
|
|
|
// regex removes first tab (any following tabs are part of the code).
|
2014-09-26 10:15:40 +02:00
|
|
|
if(!$started) {
|
|
|
|
if($debug) var_dump('Start code block because of tab. No fence');
|
|
|
|
|
|
|
|
$output[$i - 1] = '```';
|
|
|
|
} else {
|
|
|
|
if($debug) var_dump('Content is still tabbed so still inner');
|
|
|
|
}
|
|
|
|
|
|
|
|
$output[$i] = $matches[1];
|
2011-01-10 10:18:44 +01:00
|
|
|
$inner = true;
|
2011-01-10 11:59:22 +01:00
|
|
|
$started = true;
|
2014-09-26 10:15:40 +02:00
|
|
|
} else if($started && $inner && trim($line) === "") {
|
|
|
|
if($debug) var_dump('Inner line of code block');
|
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
// still inside a colon based block, if the line is only whitespace
|
|
|
|
// then continue with with it. We can continue with it for now as
|
|
|
|
// it'll be tidied up later in the $end section.
|
|
|
|
$inner = true;
|
|
|
|
$output[$i] = $line;
|
2014-07-27 02:39:44 +02:00
|
|
|
} else if($started && $inner) {
|
2013-02-19 10:23:22 +01:00
|
|
|
// line contains something other than whitespace, or tabbed. E.g
|
|
|
|
// > code
|
|
|
|
// > \n
|
|
|
|
// > some message
|
|
|
|
//
|
|
|
|
// So actually want to reset $i to the line before this new line
|
|
|
|
// and include this line. The edge case where this will fail is
|
|
|
|
// new the following segment contains a code block as well as it
|
|
|
|
// will not open.
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) {
|
|
|
|
var_dump('Contains something that isnt code. So end the code.');
|
|
|
|
}
|
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
$end = true;
|
|
|
|
$output[$i] = $line;
|
2014-09-26 10:15:40 +02:00
|
|
|
$i = $i - 1;
|
2014-07-27 02:39:44 +02:00
|
|
|
} else {
|
2013-02-19 10:23:22 +01:00
|
|
|
$output[$i] = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($end) {
|
2014-09-26 10:15:40 +02:00
|
|
|
if($debug) var_dump('End of code block');
|
2013-02-19 10:23:22 +01:00
|
|
|
$output = self::finalize_code_output($i, $output);
|
|
|
|
|
2011-01-10 11:59:22 +01:00
|
|
|
// reset state
|
2013-02-19 10:23:22 +01:00
|
|
|
$started = $inner = $mode = $end = false;
|
2011-01-10 10:18:44 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-19 10:23:22 +01:00
|
|
|
|
|
|
|
if($started) {
|
2014-09-26 10:15:40 +02:00
|
|
|
$output = self::finalize_code_output($i+1, $output);
|
2013-02-19 10:23:22 +01:00
|
|
|
}
|
|
|
|
|
2014-09-26 10:15:40 +02:00
|
|
|
return implode("\n", $output);
|
2013-02-19 10:23:22 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-26 10:15:40 +02:00
|
|
|
* Adds the closing code backticks. Removes trailing whitespace.
|
|
|
|
*
|
2013-02-19 10:23:22 +01:00
|
|
|
* @param int
|
|
|
|
* @param array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function finalize_code_output($i, $output) {
|
2014-09-26 10:15:40 +02:00
|
|
|
if(isset($output[$i]) && trim($output[$i])) {
|
|
|
|
$output[$i] .= "\n```\n";
|
2013-02-19 10:23:22 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-09-26 10:15:40 +02:00
|
|
|
$output[$i] = "```";
|
2013-02-19 10:23:22 +01:00
|
|
|
}
|
2010-08-01 23:14:41 +02:00
|
|
|
|
2013-02-19 10:23:22 +01:00
|
|
|
return $output;
|
2010-08-01 23:14:41 +02:00
|
|
|
}
|
2010-08-01 23:13:40 +02:00
|
|
|
|
2014-09-06 01:13:12 +02:00
|
|
|
public static function rewrite_image_links($md, $page) {
|
2010-08-01 10:25:00 +02:00
|
|
|
// Links with titles
|
|
|
|
$re = '/
|
|
|
|
!
|
|
|
|
\[
|
|
|
|
(.*?) # image title (non greedy)
|
|
|
|
\]
|
|
|
|
\(
|
|
|
|
(.*?) # image url (non greedy)
|
|
|
|
\)
|
|
|
|
/x';
|
|
|
|
preg_match_all($re, $md, $images);
|
2011-01-14 02:03:46 +01:00
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
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())
|
|
|
|
);
|
|
|
|
|
|
|
|
// if the image starts with a slash, it's absolute
|
|
|
|
if(substr($url, 0, 1) == '/') {
|
|
|
|
$relativeUrl = str_replace(BASE_PATH, '', Controller::join_links(
|
|
|
|
$page->getEntity()->getPath(),
|
|
|
|
$url
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$relativeUrl = rtrim($baselink, '/') . '/' . ltrim($url, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve relative paths
|
|
|
|
while(strpos($relativeUrl, '/..') !== FALSE) {
|
|
|
|
$relativeUrl = preg_replace('/\w+\/\.\.\//', '', $relativeUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make it absolute again
|
|
|
|
$absoluteUrl = Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
$relativeUrl
|
|
|
|
);
|
|
|
|
|
|
|
|
// Replace any double slashes (apart from protocol)
|
|
|
|
// $absoluteUrl = preg_replace('/([^:])\/{2,}/', '$1/', $absoluteUrl);
|
|
|
|
|
|
|
|
// Replace in original content
|
|
|
|
$md = str_replace(
|
|
|
|
$match,
|
|
|
|
sprintf('![%s](%s)', $title, $absoluteUrl),
|
|
|
|
$md
|
|
|
|
);
|
2010-08-01 10:25:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $md;
|
2010-08-01 06:46:32 +02:00
|
|
|
}
|
2010-08-01 06:46:39 +02:00
|
|
|
|
2010-08-01 06:46:37 +02:00
|
|
|
/**
|
2010-08-01 06:46:39 +02:00
|
|
|
* Rewrite links with special "api:" prefix, from two possible formats:
|
|
|
|
* 1. [api:DataObject]
|
|
|
|
* 2. (My Title)(api:DataObject)
|
|
|
|
*
|
|
|
|
* Hack: Replaces any backticks with "<code>" blocks,
|
|
|
|
* as the currently used markdown parser doesn't resolve links in backticks,
|
|
|
|
* but does resolve in "<code>" blocks.
|
|
|
|
*
|
2010-08-01 06:46:37 +02:00
|
|
|
* @param String $md
|
|
|
|
* @param DocumentationPage $page
|
|
|
|
* @return String
|
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public static function rewrite_api_links($md, $page) {
|
2010-08-01 06:46:37 +02:00
|
|
|
// Links with titles
|
|
|
|
$re = '/
|
2010-08-01 06:46:39 +02:00
|
|
|
`?
|
2010-08-01 06:46:37 +02:00
|
|
|
\[
|
|
|
|
(.*?) # link title (non greedy)
|
|
|
|
\]
|
|
|
|
\(
|
|
|
|
api:(.*?) # link url (non greedy)
|
|
|
|
\)
|
2010-08-01 06:46:39 +02:00
|
|
|
`?
|
2010-08-01 06:46:37 +02:00
|
|
|
/x';
|
|
|
|
preg_match_all($re, $md, $linksWithTitles);
|
2010-12-21 10:42:44 +01:00
|
|
|
if($linksWithTitles) {
|
|
|
|
foreach($linksWithTitles[0] as $i => $match) {
|
|
|
|
$title = $linksWithTitles[1][$i];
|
|
|
|
$subject = $linksWithTitles[2][$i];
|
2014-09-15 11:47:45 +02:00
|
|
|
|
|
|
|
$url = sprintf(
|
|
|
|
self::$api_link_base,
|
|
|
|
$subject,
|
|
|
|
$page->getVersion(),
|
|
|
|
$page->getEntity()->getKey()
|
|
|
|
);
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
$md = str_replace(
|
|
|
|
$match,
|
2014-07-27 02:39:44 +02:00
|
|
|
sprintf('<code>[%s](%s)</code>', $title, $url),
|
2010-12-21 10:42:44 +01:00
|
|
|
$md
|
|
|
|
);
|
|
|
|
}
|
2010-08-01 06:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bare links
|
|
|
|
$re = '/
|
2010-08-01 06:46:39 +02:00
|
|
|
`?
|
2010-08-01 06:46:37 +02:00
|
|
|
\[
|
|
|
|
api:(.*?)
|
|
|
|
\]
|
2010-08-01 06:46:39 +02:00
|
|
|
`?
|
2010-08-01 06:46:37 +02:00
|
|
|
/x';
|
|
|
|
preg_match_all($re, $md, $links);
|
2010-12-21 10:42:44 +01:00
|
|
|
if($links) {
|
|
|
|
foreach($links[0] as $i => $match) {
|
|
|
|
$subject = $links[1][$i];
|
2014-09-15 11:47:45 +02:00
|
|
|
$url = sprintf(
|
|
|
|
self::$api_link_base,
|
|
|
|
$subject,
|
|
|
|
$page->getVersion(),
|
|
|
|
$page->getEntity()->getKey()
|
|
|
|
);
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
$md = str_replace(
|
|
|
|
$match,
|
2014-07-27 02:39:44 +02:00
|
|
|
sprintf('<code>[%s](%s)</code>', $subject, $url),
|
2010-12-21 10:42:44 +01:00
|
|
|
$md
|
|
|
|
);
|
|
|
|
}
|
2010-08-01 06:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $md;
|
|
|
|
}
|
|
|
|
|
2010-09-03 07:29:15 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2012-05-17 23:48:57 +02:00
|
|
|
public static function rewrite_heading_anchors($md, $page) {
|
2011-01-13 08:20:55 +01:00
|
|
|
$re = '/^\#+(.*)/m';
|
2010-09-03 07:29:15 +02:00
|
|
|
$md = preg_replace_callback($re, array('DocumentationParser', '_rewrite_heading_anchors_callback'), $md);
|
|
|
|
|
|
|
|
return $md;
|
|
|
|
}
|
|
|
|
|
2014-09-06 01:13:12 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2012-05-17 23:48:57 +02:00
|
|
|
public static function _rewrite_heading_anchors_callback($matches) {
|
2010-09-03 07:29:15 +02:00
|
|
|
$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
|
|
|
|
*/
|
2014-09-06 01:13:12 +02:00
|
|
|
public static function generate_html_id($title) {
|
2010-09-03 07:29:15 +02:00
|
|
|
$t = $title;
|
|
|
|
$t = str_replace('&','-and-',$t);
|
|
|
|
$t = str_replace('&','-and-',$t);
|
2012-05-17 23:48:57 +02:00
|
|
|
$t = preg_replace('/[^A-Za-z0-9]+/','-',$t);
|
|
|
|
$t = preg_replace('/-+/','-',$t);
|
2010-09-03 07:29:15 +02:00
|
|
|
$t = trim($t, '-');
|
2011-01-15 22:47:43 +01:00
|
|
|
$t = strtolower($t);
|
2010-09-03 07:29:15 +02:00
|
|
|
|
|
|
|
return $t;
|
|
|
|
}
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
/**
|
|
|
|
* Resolves all relative links within markdown.
|
|
|
|
*
|
|
|
|
* @param String $md Markdown content
|
|
|
|
* @param DocumentationPage $page
|
2014-09-15 11:47:45 +02:00
|
|
|
*
|
2010-08-01 06:46:32 +02:00
|
|
|
* @return String Markdown
|
|
|
|
*/
|
2014-09-15 11:47:45 +02:00
|
|
|
public static function rewrite_relative_links($md, $page) {
|
|
|
|
$baselink = $page->getEntity()->Link();
|
|
|
|
|
2010-08-01 06:46:32 +02:00
|
|
|
$re = '/
|
2010-08-01 10:25:00 +02:00
|
|
|
([^\!]?) # exclude image format
|
2010-08-01 06:46:32 +02:00
|
|
|
\[
|
|
|
|
(.*?) # link title (non greedy)
|
|
|
|
\]
|
|
|
|
\(
|
|
|
|
(.*?) # link url (non greedy)
|
|
|
|
\)
|
|
|
|
/x';
|
|
|
|
preg_match_all($re, $md, $matches);
|
|
|
|
|
2011-01-16 21:17:56 +01:00
|
|
|
// relative path (relative to module base folder), without the filename.
|
|
|
|
// For "sapphire/en/current/topics/templates", this would be "templates"
|
2014-09-19 13:29:22 +02:00
|
|
|
$relativePath = dirname($page->getRelativePath());
|
2014-09-21 01:20:31 +02:00
|
|
|
|
|
|
|
if(strpos($page->getRelativePath(), 'index.md')) {
|
|
|
|
$relativeLink = $page->getRelativeLink();
|
|
|
|
} else {
|
|
|
|
$relativeLink = dirname($page->getRelativeLink());
|
|
|
|
}
|
|
|
|
|
2014-09-15 11:47:45 +02:00
|
|
|
if($relativePath == '.') {
|
|
|
|
$relativePath = '';
|
|
|
|
}
|
2014-09-21 01:20:31 +02:00
|
|
|
|
|
|
|
if($relativeLink == ".") {
|
|
|
|
$relativeLink = '';
|
|
|
|
}
|
2010-08-01 06:46:32 +02:00
|
|
|
|
2011-07-04 06:58:15 +02:00
|
|
|
// file base link
|
|
|
|
$fileBaseLink = Director::makeRelative(dirname($page->getPath()));
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
if($matches) {
|
|
|
|
foreach($matches[0] as $i => $match) {
|
|
|
|
$title = $matches[2][$i];
|
|
|
|
$url = $matches[3][$i];
|
2010-08-01 06:46:32 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
// Don't process API links
|
|
|
|
if(preg_match('/^api:/', $url)) continue;
|
2010-08-01 06:46:32 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
// Don't process absolute links (based on protocol detection)
|
|
|
|
$urlParts = parse_url($url);
|
|
|
|
if($urlParts && isset($urlParts['scheme'])) continue;
|
2011-01-16 21:17:56 +01:00
|
|
|
|
2011-07-04 06:58:15 +02:00
|
|
|
// for images we need to use the file base path
|
|
|
|
if(preg_match('/_images/', $url)) {
|
|
|
|
$relativeUrl = Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
$fileBaseLink,
|
|
|
|
$url
|
|
|
|
);
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
2011-07-04 06:58:15 +02:00
|
|
|
else {
|
|
|
|
// Rewrite public URL
|
|
|
|
if(preg_match('/^\//', $url)) {
|
|
|
|
// Absolute: Only path to module base
|
2014-09-21 01:20:31 +02:00
|
|
|
$relativeUrl = Controller::join_links($baselink, $url, '/');
|
2011-07-04 06:58:15 +02:00
|
|
|
} else {
|
|
|
|
// Relative: Include path to module base and any folders
|
2014-09-21 01:20:31 +02:00
|
|
|
$relativeUrl = Controller::join_links($baselink, $relativeLink, $url, '/');
|
2011-07-04 06:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
// Resolve relative paths
|
|
|
|
while(strpos($relativeUrl, '..') !== FALSE) {
|
2013-11-05 02:49:29 +01:00
|
|
|
$relativeUrl = preg_replace('/[-\w]+\/\.\.\//', '', $relativeUrl);
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
2010-09-05 04:29:07 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
// Replace any double slashes (apart from protocol)
|
|
|
|
$relativeUrl = preg_replace('/([^:])\/{2,}/', '$1/', $relativeUrl);
|
2014-09-15 11:47:45 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
// Replace in original content
|
|
|
|
$md = str_replace(
|
|
|
|
$match,
|
|
|
|
sprintf('%s[%s](%s)', $matches[1][$i], $title, $relativeUrl),
|
|
|
|
$md
|
|
|
|
);
|
2010-09-05 04:29:07 +02:00
|
|
|
}
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
2010-10-08 05:31:19 +02:00
|
|
|
|
2010-12-21 10:42:44 +01:00
|
|
|
return $md;
|
2010-06-24 16:22:41 +02:00
|
|
|
}
|
2011-01-14 04:29:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 02:49:29 +01:00
|
|
|
}
|