Commit 73cb5f8a by Carsten Brandt

changed syntax for referencing the guide in apidoc

New syntax: ``` [link to guide](guide:file-name.md) [link to guide](guide:file-name.md#subsection) ``` fixes #4719
parent 0ed670fe
...@@ -48,18 +48,15 @@ class ApiController extends BaseController ...@@ -48,18 +48,15 @@ class ApiController extends BaseController
// setup reference to guide // setup reference to guide
if ($this->guide !== null) { if ($this->guide !== null) {
$guideUrl = $this->guide; $renderer->guideUrl = $guideUrl = $this->guide;
$referenceFile = $guideUrl . '/' . BaseRenderer::GUIDE_PREFIX . 'references.txt';
} else { } else {
$guideUrl = './'; $guideUrl = './';
$referenceFile = $targetDir . '/' . BaseRenderer::GUIDE_PREFIX . 'references.txt'; $renderer->guideUrl = $targetDir;
} }
if (file_exists($referenceFile)) { if (file_exists($renderer->generateGuideUrl('README.md'))) {
$renderer->guideUrl = $guideUrl; $renderer->guideUrl = $guideUrl;
$renderer->guideReferences = []; } else {
foreach (explode("\n", file_get_contents($referenceFile)) as $reference) { $renderer->guideUrl = null;
$renderer->guideReferences[BaseRenderer::GUIDE_PREFIX . $reference]['url'] = $renderer->generateGuideUrl($reference);
}
} }
// search for files to process // search for files to process
......
...@@ -70,13 +70,6 @@ class GuideController extends BaseController ...@@ -70,13 +70,6 @@ class GuideController extends BaseController
FileHelper::copyDirectory(rtrim($source, '/\\') . '/images', $targetDir . '/images'); FileHelper::copyDirectory(rtrim($source, '/\\') . '/images', $targetDir . '/images');
} }
$this->stdout('done.' . PHP_EOL, Console::FG_GREEN); $this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
// generate api references.txt
$references = [];
foreach ($files as $file) {
$references[] = basename($file, '.md');
}
file_put_contents($targetDir . '/guide-references.txt', implode("\n", $references));
} }
......
...@@ -89,6 +89,21 @@ class ApiMarkdown extends GithubMarkdown ...@@ -89,6 +89,21 @@ class ApiMarkdown extends GithubMarkdown
} }
/** /**
* @inheritdoc
*/
protected function parseLink($markdown)
{
list($result, $skip) = parent::parseLink($markdown);
// add special syntax for linking to the guide
$result = preg_replace_callback('/href="guide:([A-z0-9-.#]+)"/i', function($match) {
return 'href="' . static::$renderer->generateGuideUrl($match[1]) . '"';
}, $result, 1);
return [$result, $skip];
}
/**
* Converts markdown into HTML * Converts markdown into HTML
* *
* @param string $content * @param string $content
......
...@@ -43,7 +43,6 @@ abstract class BaseRenderer extends Component ...@@ -43,7 +43,6 @@ abstract class BaseRenderer extends Component
*/ */
public $controller; public $controller;
public $guideUrl; public $guideUrl;
public $guideReferences = [];
public function init() public function init()
...@@ -198,6 +197,12 @@ abstract class BaseRenderer extends Component ...@@ -198,6 +197,12 @@ abstract class BaseRenderer extends Component
*/ */
public function generateGuideUrl($file) public function generateGuideUrl($file)
{ {
return rtrim($this->guideUrl, '/') . '/' . static::GUIDE_PREFIX . basename($file, '.md') . '.html'; $hash = '';
if (($pos = strpos($file, '#')) !== false) {
$hash = substr($file, $pos);
$file = substr($file, 0, $pos);
}
return rtrim($this->guideUrl, '/') . '/' . static::GUIDE_PREFIX . basename($file, '.md') . '.html' . $hash;
} }
} }
...@@ -264,7 +264,7 @@ class BaseYii ...@@ -264,7 +264,7 @@ class BaseYii
* will be loaded using the `@yii/bootstrap` alias which points to the directory where bootstrap extension * will be loaded using the `@yii/bootstrap` alias which points to the directory where bootstrap extension
* files are installed and all classes from other `yii` namespaces will be loaded from the yii framework directory. * files are installed and all classes from other `yii` namespaces will be loaded from the yii framework directory.
* *
* Also the [guide section on autoloading][guide-concept-autoloading]. * Also the [guide section on autoloading](guide:concept-autoloading).
* *
* @param string $className the fully qualified class name without a leading backslash "\" * @param string $className the fully qualified class name without a leading backslash "\"
* @throws UnknownClassException if the class does not exist in the class file * @throws UnknownClassException if the class does not exist in the class file
......
...@@ -87,7 +87,7 @@ abstract class Application extends Module ...@@ -87,7 +87,7 @@ abstract class Application extends Module
* This namespace will be used to load controller classes by prepending it to the controller class name. * This namespace will be used to load controller classes by prepending it to the controller class name.
* The default namespace is `app\controllers`. * The default namespace is `app\controllers`.
* *
* Please refer to the [guide about class autoloading][guide-concept-autoloading] for more details. * Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details.
*/ */
public $controllerNamespace = 'app\\controllers'; public $controllerNamespace = 'app\\controllers';
/** /**
......
...@@ -94,7 +94,7 @@ class Module extends ServiceLocator ...@@ -94,7 +94,7 @@ class Module extends ServiceLocator
* For example, if the namespace of this module is "foo\bar", then the default * For example, if the namespace of this module is "foo\bar", then the default
* controller namespace would be "foo\bar\controllers". * controller namespace would be "foo\bar\controllers".
* *
* See also the [guide section on autoloading][guide-concept-autoloading] to learn more about * See also the [guide section on autoloading](guide:concept-autoloading) to learn more about
* defining namespaces and how classes are loaded. * defining namespaces and how classes are loaded.
*/ */
public $controllerNamespace; public $controllerNamespace;
......
...@@ -45,7 +45,7 @@ use yii\helpers\StringHelper; ...@@ -45,7 +45,7 @@ use yii\helpers\StringHelper;
* *
* The `tableName` method only has to return the name of the database table associated with the class. * The `tableName` method only has to return the name of the database table associated with the class.
* *
* > Tip: You may also use the [Gii code generator][guide-gii] to generate ActiveRecord classes from your * > Tip: You may also use the [Gii code generator](guide:start-gii) to generate ActiveRecord classes from your
* > database tables. * > database tables.
* *
* Class instances are obtained in one of two ways: * Class instances are obtained in one of two ways:
...@@ -67,7 +67,7 @@ use yii\helpers\StringHelper; ...@@ -67,7 +67,7 @@ use yii\helpers\StringHelper;
* $orders = $user->orders; * $orders = $user->orders;
* ``` * ```
* *
* For more details and usage information on ActiveRecord, see the [guide article on ActiveRecord][guide-active-record]. * For more details and usage information on ActiveRecord, see the [guide article on ActiveRecord](guide:db-active-record).
* *
* @method ActiveQuery hasMany(string $class, array $link) see BaseActiveRecord::hasMany() for more info * @method ActiveQuery hasMany(string $class, array $link) see BaseActiveRecord::hasMany() for more info
* @method ActiveQuery hasOne(string $class, array $link) see BaseActiveRecord::hasOne() for more info * @method ActiveQuery hasOne(string $class, array $link) see BaseActiveRecord::hasOne() for more info
......
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