Markdown.php 3.4 KB
Newer Older
1 2
<?php
/**
Carsten Brandt committed
3 4 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
6 7 8 9 10 11 12 13 14
 */

namespace yii\apidoc\helpers;

use phpDocumentor\Reflection\DocBlock\Type\Collection;
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\TypeDoc;
use yii\apidoc\templates\BaseRenderer;

Carsten Brandt committed
15
/**
16
 * A Markdown helper with support for class reference links.
Carsten Brandt committed
17 18 19 20
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
21 22 23 24 25 26 27 28 29 30 31 32 33 34
class Markdown extends \yii\helpers\Markdown
{
	/**
	 * @var BaseRenderer
	 */
	public static $renderer;

	/**
	 * Converts markdown into HTML
	 *
	 * @param string $content
	 * @param TypeDoc $context
	 * @return string
	 */
Qiang Xue committed
35
	public static function process($content, $context = null)
36
	{
37 38 39 40
		if (is_string($context)) {
			$context = static::$renderer->context->getType($context);
		}

41 42 43 44 45
		$content = trim(parent::process($content, []));
		if (!strncmp($content, '<p>', 3) && substr($content, -4, 4) == '</p>') {
			$content = substr($content, 3, -4);
		}

46
		$content = preg_replace_callback('/\[\[([\w\d\\\\\(\):$]+)(\|[^\]]*)?\]\]/xm', function($matches) use ($context) {
47 48 49 50 51 52
			$object = $matches[1];
			$title = (empty($matches[2]) || $matches[2] == '|') ? null : substr($matches[2], 1);

			if (($pos = strpos($object, '::')) !== false) {
				$typeName = substr($object, 0, $pos);
				$subjectName = substr($object, $pos + 2);
53 54 55 56
				if ($context !== null) {
					// Collection resolves relative types
					$typeName = (new Collection([$typeName], $context->phpDocContext))->__toString();
				}
57 58
				$type = static::$renderer->context->getType($typeName);
				if ($type === null) {
59 60 61 62
					static::$renderer->context->errors[] = [
						'file' => ($context !== null) ? $context->sourceFile : null,
						'message' => 'broken link to ' . $typeName . '::' . $subjectName . (($context !== null) ? ' in ' . $context->name : ''),
					];
63 64 65 66 67 68 69 70 71 72 73
					return '<span style="background: #f00;">' . $typeName . '::' . $subjectName . '</span>';
				} else {
					if (($subject = $type->findSubject($subjectName)) !== null) {
						if ($title === null) {
							$title = $type->name . '::' . $subject->name;
							if ($subject instanceof MethodDoc) {
								$title .= '()';
							}
						}
						return static::$renderer->subjectLink($subject, $title);
					} else {
74 75 76 77
						static::$renderer->context->errors[] = [
							'file' => ($context !== null) ? $context->sourceFile : null,
							'message' => 'broken link to ' . $type->name . '::' . $subjectName . (($context !== null) ? ' in ' . $context->name : ''),
						];
78 79 80
						return '<span style="background: #ff0;">' . $type->name . '</span><span style="background: #f00;">::' . $subjectName . '</span>';
					}
				}
81
			} elseif ($context !== null && ($subject = $context->findSubject($object)) !== null) {
82 83
				return static::$renderer->subjectLink($subject, $title);
			}
84 85 86 87
			if ($context !== null) {
				// Collection resolves relative types
				$object = (new Collection([$object], $context->phpDocContext))->__toString();
			}
88 89 90
			if (($type = static::$renderer->context->getType($object)) !== null) {
				return static::$renderer->typeLink($type, $title);
			}
91 92 93 94
			static::$renderer->context->errors[] = [
				'file' => ($context !== null) ? $context->sourceFile : null,
				'message' => 'broken link to ' . $object . (($context !== null) ? ' in ' . $context->name : ''),
			];
95 96 97 98 99
			return '<span style="background: #f00;">' . $object . '</span>';
		}, $content);

		return $content;
	}
Qiang Xue committed
100
}