Commit 1abe28c6 authored by Sean Madsen's avatar Sean Madsen

Merge branch '78' into 'master'

Allow books to define external redirects

Closes #78

See merge request documentation/docs-publisher!75
parents da19cbe8 68524d82
......@@ -73,8 +73,18 @@ class ExceptionListener {
$redirects = file($redirectsFile);
foreach ($redirects as $redirect) {
$rule = StringTools::parseRedirectRule($redirect);
if ($rule && $rule['from'] == $path) {
return "/$edition/${rule['to']}$fragment";
if (empty($rule)) {
// Skip any rules that are invalid
break;
}
$ruleMatchesRequest = ($rule['from'] == $path);
if ($ruleMatchesRequest) {
if ($rule['type'] == 'internal') {
return "/$edition/${rule['to']}$fragment";
}
else {
return $rule['to'];
}
}
}
......
......@@ -25,17 +25,24 @@ class StringTools {
/**
* Look at one redirect rule, as it is written in a text file, and determine
* the "from" and "to" elements.
*
* See StringToolsTest::redirectRuleProvider() for examples
*
* @param string $rule
* e.g. "foo/bar baz/bat" or "#Ignorable comment"
*
* @return null|array
* An array with keys "from" and "to".
* e.g. ["from" => "old/page", "to"=> "new/page"]
* Will be NULL if the input is not a valid redirect rule
*/
* Will be NULL if the input is not a valid redirect rule.
*
* If valid, will be an array which looks like this
* [
* "from" => "old/page",
* "to"=> "new/page",
* "type" => "internal"
* ]
*
* The "type" setting above, will either be "internal" (for redirects within
* one guide, or "external" for redirects which point outside of the guide.)
*/
public static function parseRedirectRule($rule) {
$rule = trim($rule);
......@@ -54,8 +61,15 @@ class StringTools {
$from = $redirectParts[0] ?? NULL;
$to = $redirectParts[1] ?? NULL;
$isValid = (!empty($from) && !empty($to));
return ($from && $to) ? ['from' => $from, 'to' => $to] : NULL;
if ($isValid) {
$type = (preg_match('_^https?://_', $to)) ? 'external' : 'internal';
return ['from' => $from, 'to' => $to, 'type' => $type];
}
else {
return NULL;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment