Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

108 lines
3.2KB

  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. }