* @since 2.0 */ class ApiMarkdown extends GithubMarkdown { use ApiMarkdownTrait; /** * @var BaseRenderer */ public static $renderer; protected $renderingContext; /** * Renders a code block */ protected function renderCode($block) { if (isset($block['language'])) { $class = isset($block['language']) ? ' class="language-' . $block['language'] . '"' : ''; return "
" . $this->highlight(implode("\n", $block['content']) . "\n", $block['language']) . '
';
} else {
return parent::renderCode($block);
}
}
public static function highlight($code, $language)
{
if ($language !== 'php') {
return htmlspecialchars($code, ENT_NOQUOTES, 'UTF-8');
}
// TODO improve code highlighting
if (strncmp($code, '\n and tags added by php
$text = substr(trim($text), 36, -16);
return $text;
}
protected function inlineMarkers()
{
return array_merge(parent::inlineMarkers(), [
'[[' => 'parseApiLinks',
]);
}
/**
* @inheritDoc
*/
protected function renderHeadline($block)
{
$content = $this->parseInline($block['content']);
$hash = Inflector::slug(strip_tags($content));
$hashLink = "¶";
$tag = 'h' . $block['level'];
return "<$tag>$content $hashLink$tag>";
}
/**
* Converts markdown into HTML
*
* @param string $content
* @param TypeDoc $context
* @param boolean $paragraph
* @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);
}
Markdown::$flavors['api']->renderingContext = $context;
if ($paragraph) {
return Markdown::processParagraph($content, 'api');
} else {
return Markdown::process($content, 'api');
}
}
}