Markdown.php 2.5KB

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\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. * @inheritdoc
  51. */
  52. protected function renderParagraph($block)
  53. {
  54. return rtrim($this->renderAbsy($block['content'])) . "\n\n";
  55. }
  56. /**
  57. * Renders an inline code span `` ` ``.
  58. * @param array $element
  59. * @return string
  60. */
  61. protected function renderInlineCode($element)
  62. {
  63. return Console::ansiFormat($element[1], [Console::UNDERLINE]);
  64. }
  65. /**
  66. * Renders empathized elements.
  67. * @param array $element
  68. * @return string
  69. */
  70. protected function renderEmph($element)
  71. {
  72. return Console::ansiFormat($this->renderAbsy($element[1]), [Console::ITALIC]);
  73. }
  74. /**
  75. * Renders strong elements.
  76. * @param array $element
  77. * @return string
  78. */
  79. protected function renderStrong($element)
  80. {
  81. return Console::ansiFormat($this->renderAbsy($element[1]), [Console::BOLD]);
  82. }
  83. /**
  84. * Renders the strike through feature.
  85. * @param array $element
  86. * @return string
  87. */
  88. protected function renderStrike($element)
  89. {
  90. return Console::ansiFormat($this->parseInline($this->renderAbsy($element[1])), [Console::CROSSED_OUT]);
  91. }
  92. }