ApiMarkdown.php 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\apidoc\helpers;

10
use cebe\markdown\GithubMarkdown;
11 12
use phpDocumentor\Reflection\DocBlock\Type\Collection;
use yii\apidoc\models\TypeDoc;
13
use yii\apidoc\renderers\BaseRenderer;
14
use yii\helpers\Inflector;
15
use yii\helpers\Markdown;
16 17 18 19 20 21 22

/**
 * A Markdown helper with support for class reference links.
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
23
class ApiMarkdown extends GithubMarkdown
24
{
25 26
    use ApiMarkdownTrait;

27 28 29 30 31
    /**
     * @var BaseRenderer
     */
    public static $renderer;

32
    protected $renderingContext;
33

34

35 36 37 38 39 40 41 42
    /**
     * Renders a code block
     */
    protected function renderCode($block)
    {
        if (isset($block['language'])) {
            $class = isset($block['language']) ? ' class="language-' . $block['language'] . '"' : '';

43
            return "<pre><code$class>" . $this->highlight($block['content'] . "\n", $block['language']) . "</code></pre>\n";
44 45 46 47 48 49 50 51
        } else {
            return parent::renderCode($block);
        }
    }

    public static function highlight($code, $language)
    {
        if ($language !== 'php') {
52
            return htmlspecialchars($code, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        }

        // TODO improve code highlighting
        if (strncmp($code, '<?php', 5) === 0) {
            $text = @highlight_string(trim($code), true);
        } else {
            $text = highlight_string("<?php ".trim($code), true);
            $text = str_replace('&lt;?php', '', $text);
            if (($pos = strpos($text, '&nbsp;')) !== false) {
                $text = substr($text, 0, $pos) . substr($text, $pos + 6);
            }
        }
        // remove <code><span style="color: #000000">\n and </span>tags added by php
        $text = substr(trim($text), 36, -16);

        return $text;
    }

    /**
     * @inheritDoc
     */
    protected function renderHeadline($block)
    {
76
        $content = $this->renderAbsy($block['content']);
77
        $hash = Inflector::slug(strip_tags($content));
78
        $hashLink = "<a href=\"#$hash\" name=\"$hash\" class=\"hashlink\">&para;</a>";
79

80
        $tag = 'h' . $block['level'];
81 82 83
        return "<$tag>$content $hashLink</$tag>";
    }

84 85 86
    /**
     * @inheritdoc
     */
87
    protected function renderLink($block)
88
    {
89
        $result = parent::renderLink($block);
90 91 92 93 94 95

        // 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);

96
        return $result;
97 98
    }

99 100 101
    /**
     * Converts markdown into HTML
     *
102 103 104
     * @param string $content
     * @param TypeDoc $context
     * @param boolean $paragraph
105 106 107 108 109 110 111 112 113 114 115
     * @return string
     */
    public static function process($content, $context = null, $paragraph = false)
    {
        if (!isset(Markdown::$flavors['api'])) {
            Markdown::$flavors['api'] = new static;
        }

        if (is_string($context)) {
            $context = static::$renderer->apiContext->getType($context);
        }
116
        Markdown::$flavors['api']->renderingContext = $context;
117 118 119 120 121 122 123

        if ($paragraph) {
            return Markdown::processParagraph($content, 'api');
        } else {
            return Markdown::process($content, 'api');
        }
    }
124
}