Markdown.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\console;
  8. use cebe\markdown\block\FencedCodeTrait;
  9. use cebe\markdown\inline\CodeTrait;
  10. use cebe\markdown\inline\EmphStrongTrait;
  11. use cebe\markdown\inline\StrikeoutTrait;
  12. use yii\helpers\Console;
  13. /**
  14. * A Markdown parser that enhances markdown for reading in console environments.
  15. *
  16. * Based on [cebe/markdown](https://github.com/cebe/markdown).
  17. *
  18. * @author Carsten Brandt <mail@cebe.cc>
  19. * @since 2.0
  20. */
  21. class Markdown extends \cebe\markdown\Parser
  22. {
  23. use FencedCodeTrait;
  24. use CodeTrait;
  25. use EmphStrongTrait;
  26. use StrikeoutTrait;
  27. /**
  28. * @var array these are "escapeable" characters. When using one of these prefixed with a
  29. * backslash, the character will be outputted without the backslash and is not interpreted
  30. * as markdown.
  31. */
  32. protected $escapeCharacters = [
  33. '\\', // backslash
  34. '`', // backtick
  35. '*', // asterisk
  36. '_', // underscore
  37. '~', // tilde
  38. ];
  39. /**
  40. * Renders a code block.
  41. *
  42. * @param array $block
  43. * @return string
  44. */
  45. protected function renderCode($block)
  46. {
  47. return Console::ansiFormat($block['content'], [Console::NEGATIVE]) . "\n\n";
  48. }
  49. /**
  50. * Render a paragraph block.
  51. *
  52. * @param string $block
  53. * @return string
  54. */
  55. protected function renderParagraph($block)
  56. {
  57. return rtrim($this->renderAbsy($block['content'])) . "\n\n";
  58. }
  59. /**
  60. * Renders an inline code span `` ` ``.
  61. * @param array $element
  62. * @return string
  63. */
  64. protected function renderInlineCode($element)
  65. {
  66. return Console::ansiFormat($element[1], [Console::UNDERLINE]);
  67. }
  68. /**
  69. * Renders empathized elements.
  70. * @param array $element
  71. * @return string
  72. */
  73. protected function renderEmph($element)
  74. {
  75. return Console::ansiFormat($this->renderAbsy($element[1]), [Console::ITALIC]);
  76. }
  77. /**
  78. * Renders strong elements.
  79. * @param array $element
  80. * @return string
  81. */
  82. protected function renderStrong($element)
  83. {
  84. return Console::ansiFormat($this->renderAbsy($element[1]), [Console::BOLD]);
  85. }
  86. /**
  87. * Renders the strike through feature.
  88. * @param array $element
  89. * @return string
  90. */
  91. protected function renderStrike($element)
  92. {
  93. return Console::ansiFormat($this->parseInline($this->renderAbsy($element[1])), [Console::CROSSED_OUT]);
  94. }
  95. }