You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseMarkdown.php 3.2KB

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\helpers;
  8. use Yii;
  9. use yii\base\InvalidParamException;
  10. /**
  11. * BaseMarkdown provides concrete implementation for [[Markdown]].
  12. *
  13. * Do not use BaseMarkdown. Use [[Markdown]] instead.
  14. *
  15. * @author Carsten Brandt <mail@cebe.cc>
  16. * @since 2.0
  17. */
  18. class BaseMarkdown
  19. {
  20. /**
  21. * @var array a map of markdown flavor names to corresponding parser class configurations.
  22. */
  23. public static $flavors = [
  24. 'original' => [
  25. 'class' => 'cebe\markdown\Markdown',
  26. 'html5' => true,
  27. ],
  28. 'gfm' => [
  29. 'class' => 'cebe\markdown\GithubMarkdown',
  30. 'html5' => true,
  31. ],
  32. 'gfm-comment' => [
  33. 'class' => 'cebe\markdown\GithubMarkdown',
  34. 'html5' => true,
  35. 'enableNewlines' => true,
  36. ],
  37. 'extra' => [
  38. 'class' => 'cebe\markdown\MarkdownExtra',
  39. 'html5' => true,
  40. ],
  41. ];
  42. /**
  43. * @var string the markdown flavor to use when none is specified explicitly.
  44. * Defaults to `original`.
  45. * @see $flavors
  46. */
  47. public static $defaultFlavor = 'original';
  48. /**
  49. * Converts markdown into HTML.
  50. *
  51. * @param string $markdown the markdown text to parse
  52. * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
  53. * @return string the parsed HTML output
  54. * @throws \yii\base\InvalidParamException when an undefined flavor is given.
  55. */
  56. public static function process($markdown, $flavor = 'original')
  57. {
  58. $parser = static::getParser($flavor);
  59. return $parser->parse($markdown);
  60. }
  61. /**
  62. * Converts markdown into HTML but only parses inline elements.
  63. *
  64. * This can be useful for parsing small comments or description lines.
  65. *
  66. * @param string $markdown the markdown text to parse
  67. * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
  68. * @return string the parsed HTML output
  69. * @throws \yii\base\InvalidParamException when an undefined flavor is given.
  70. */
  71. public static function processParagraph($markdown, $flavor = 'original')
  72. {
  73. $parser = static::getParser($flavor);
  74. return $parser->parseParagraph($markdown);
  75. }
  76. /**
  77. * @param string $flavor
  78. * @return \cebe\markdown\Parser
  79. * @throws \yii\base\InvalidParamException when an undefined flavor is given.
  80. */
  81. protected static function getParser($flavor)
  82. {
  83. /* @var $parser \cebe\markdown\Markdown */
  84. if (!isset(static::$flavors[$flavor])) {
  85. throw new InvalidParamException("Markdown flavor '$flavor' is not defined.'");
  86. } elseif (!is_object($config = static::$flavors[$flavor])) {
  87. $parser = Yii::createObject($config);
  88. if (is_array($config)) {
  89. foreach ($config as $name => $value) {
  90. $parser->{$name} = $value;
  91. }
  92. }
  93. static::$flavors[$flavor] = $parser;
  94. }
  95. return static::$flavors[$flavor];
  96. }
  97. }