No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

108 líneas
3.3KB

  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. * Defaults to [[$defaultFlavor]], if not set.
  54. * @return string the parsed HTML output
  55. * @throws \yii\base\InvalidParamException when an undefined flavor is given.
  56. */
  57. public static function process($markdown, $flavor = null)
  58. {
  59. $parser = static::getParser($flavor);
  60. return $parser->parse($markdown);
  61. }
  62. /**
  63. * Converts markdown into HTML but only parses inline elements.
  64. *
  65. * This can be useful for parsing small comments or description lines.
  66. *
  67. * @param string $markdown the markdown text to parse
  68. * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
  69. * Defaults to [[$defaultFlavor]], if not set.
  70. * @return string the parsed HTML output
  71. * @throws \yii\base\InvalidParamException when an undefined flavor is given.
  72. */
  73. public static function processParagraph($markdown, $flavor = null)
  74. {
  75. $parser = static::getParser($flavor);
  76. return $parser->parseParagraph($markdown);
  77. }
  78. /**
  79. * @param string $flavor the markdown flavor to use. See [[$flavors]] for available values.
  80. * Defaults to [[$defaultFlavor]], if not set.
  81. * @return \cebe\markdown\Parser
  82. * @throws \yii\base\InvalidParamException when an undefined flavor is given.
  83. */
  84. protected static function getParser($flavor)
  85. {
  86. if ($flavor === null) {
  87. $flavor = static::$defaultFlavor;
  88. }
  89. /* @var $parser \cebe\markdown\Markdown */
  90. if (!isset(static::$flavors[$flavor])) {
  91. throw new InvalidParamException("Markdown flavor '$flavor' is not defined.'");
  92. } elseif (!is_object($config = static::$flavors[$flavor])) {
  93. static::$flavors[$flavor] = Yii::createObject($config);
  94. }
  95. return static::$flavors[$flavor];
  96. }
  97. }