Blame view

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