-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathApiMarkdownLaTeX.php
145 lines (125 loc) · 3.53 KB
/
ApiMarkdownLaTeX.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\apidoc\helpers;
use cebe\markdown\latex\GithubMarkdown;
use yii\apidoc\models\TypeDoc;
use yii\apidoc\renderers\BaseRenderer;
use yii\helpers\Markdown;
/**
* A Markdown helper with support for class reference links.
*
* @author Carsten Brandt <[email protected]>
* @since 2.0
*/
class ApiMarkdownLaTeX extends GithubMarkdown
{
use ApiMarkdownTrait;
/**
* @var BaseRenderer
*/
public static $renderer;
protected $renderingContext;
/**
* @inheritdoc
*/
protected function renderApiLink($block)
{
// TODO allow break also on camel case
$latex = '\texttt{';
$latex .= str_replace(
['\\textbackslash', '::'],
['\allowbreak{}\\textbackslash', '\allowbreak{}::\allowbreak{}'],
$this->escapeLatex(strip_tags($block[1]))
);
$latex .= '}';
return $latex;
}
/**
* @inheritdoc
*/
protected function renderBrokenApiLink($block)
{
return $this->renderApiLink($block);
}
/**
* @inheritdoc
* @since 2.0.5
*/
protected function translateBlockType($type)
{
$key = ucfirst($type) . ':';
if (isset(ApiMarkdown::$blockTranslations[$key])) {
$translation = ApiMarkdown::$blockTranslations[$key];
} else {
$translation = $key;
}
return "$translation ";
}
protected function renderHeadline($block)
{
foreach ($block['content'] as $i => &$item) {
if ($item[0] === 'inlineCode') {
unset($block['content'][$i]);
}
}
return parent::renderHeadline($block);
}
/**
* Renders a blockquote
*/
protected function renderQuote($block)
{
return '\begin{quote}' . $this->renderAbsy($block['content']) . "\\end{quote}\n";
}
/**
* @inheritDoc
*/
protected function renderCode($block)
{
$language = $block['language'] ?? 'text';
// replace No-Break Space characters in code block, which do not render in LaTeX
$content = preg_replace("/[\x{00a0}\x{202f}]/u", ' ', $block['content']);
return implode("\n", [
"\\begin{minted}{" . "$language}",
$content,
'\end{minted}',
'',
]);
}
/**
* @inheritDoc
*/
protected function renderInlineCode($block)
{
// replace No-Break Space characters in code block, which do not render in LaTeX
$content = preg_replace("/[\x{00a0}\x{202f}]/u", ' ', $block[1]);
return '\\mintinline{text}{' . str_replace("\n", ' ', $content) . '}';
}
/**
* Converts markdown into HTML
*
* @param string $content
* @param TypeDoc $context
* @param bool $paragraph
* @return string
*/
public static function process($content, $context = null, $paragraph = false)
{
if (!isset(Markdown::$flavors['api-latex'])) {
Markdown::$flavors['api-latex'] = new static;
}
if (is_string($context)) {
$context = static::$renderer->apiContext->getType($context);
}
Markdown::$flavors['api-latex']->renderingContext = $context;
if ($paragraph) {
return Markdown::processParagraph($content, 'api-latex');
} else {
return Markdown::process($content, 'api-latex');
}
}
}