ApiMarkdownLaTeX.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\apidoc\helpers;
  8. use cebe\markdown\latex\GithubMarkdown;
  9. use yii\apidoc\models\TypeDoc;
  10. use yii\apidoc\renderers\BaseRenderer;
  11. use yii\helpers\Markdown;
  12. /**
  13. * A Markdown helper with support for class reference links.
  14. *
  15. * @author Carsten Brandt <mail@cebe.cc>
  16. * @since 2.0
  17. */
  18. class ApiMarkdownLaTeX extends GithubMarkdown
  19. {
  20. use ApiMarkdownTrait;
  21. /**
  22. * @var BaseRenderer
  23. */
  24. public static $renderer;
  25. protected $renderingContext;
  26. /**
  27. * @inheritdoc
  28. */
  29. protected function renderApiLink($block)
  30. {
  31. // TODO allow break also on camel case
  32. $latex = '\texttt{'.str_replace(['\\textbackslash', '::'], ['\allowbreak{}\\textbackslash', '\allowbreak{}::\allowbreak{}'], $this->escapeLatex(strip_tags($block[1]))).'}';
  33. return $latex;
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. protected function renderBrokenApiLink($block)
  39. {
  40. return $this->renderApiLink($block);
  41. }
  42. /**
  43. * @inheritdoc
  44. * @since 2.0.5
  45. */
  46. protected function translateBlockType($type)
  47. {
  48. $key = ucfirst($type) . ':';
  49. if (isset(ApiMarkdown::$blockTranslations[$key])) {
  50. $translation = ApiMarkdown::$blockTranslations[$key];
  51. } else {
  52. $translation = $key;
  53. }
  54. return "$translation ";
  55. }
  56. /**
  57. * Renders a blockquote
  58. */
  59. protected function renderQuote($block)
  60. {
  61. if (isset($block['blocktype'])) {
  62. // TODO render nice icon for different block types: note, info, warning, tip
  63. //$class = ' class="' . $block['blocktype'] . '"';
  64. }
  65. return '\begin{quote}' . $this->renderAbsy($block['content']) . "\\end{quote}\n";
  66. }
  67. /**
  68. * Converts markdown into HTML
  69. *
  70. * @param string $content
  71. * @param TypeDoc $context
  72. * @param bool $paragraph
  73. * @return string
  74. */
  75. public static function process($content, $context = null, $paragraph = false)
  76. {
  77. if (!isset(Markdown::$flavors['api-latex'])) {
  78. Markdown::$flavors['api-latex'] = new static;
  79. }
  80. if (is_string($context)) {
  81. $context = static::$renderer->apiContext->getType($context);
  82. }
  83. Markdown::$flavors['api-latex']->renderingContext = $context;
  84. if ($paragraph) {
  85. return Markdown::processParagraph($content, 'api-latex');
  86. } else {
  87. return Markdown::process($content, 'api-latex');
  88. }
  89. }
  90. }