[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Litedown/Parser/Passes/ -> Links.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\Litedown\Parser\Passes;
   9  
  10  use s9e\TextFormatter\Plugins\Litedown\Parser\LinkAttributesSetter;
  11  
  12  class Links extends AbstractPass
  13  {
  14      use LinkAttributesSetter;
  15  
  16      /**
  17      * {@inheritdoc}
  18      */
  19  	public function parse()
  20      {
  21          if ($this->text->indexOf('](') !== false)
  22          {
  23              $this->parseInlineLinks();
  24          }
  25          if ($this->text->indexOf('<') !== false)
  26          {
  27              $this->parseAutomaticLinks();
  28          }
  29          if ($this->text->hasReferences)
  30          {
  31              $this->parseReferenceLinks();
  32          }
  33      }
  34  
  35      /**
  36      * Add an image tag for given text span
  37      *
  38      * @param  integer $startPos Start tag position
  39      * @param  integer $endPos   End tag position
  40      * @param  integer $endLen   End tag length
  41      * @param  string  $linkInfo    URL optionally followed by space and a title
  42      * @return void
  43      */
  44  	protected function addLinkTag($startPos, $endPos, $endLen, $linkInfo)
  45      {
  46          // Give the link a slightly worse priority if this is a implicit reference and a slightly
  47          // better priority if it's an explicit reference or an inline link or to give it precedence
  48          // over possible BBCodes such as [b](https://en.wikipedia.org/wiki/B)
  49          $priority = ($endLen === 1) ? 1 : -1;
  50  
  51          $tag = $this->parser->addTagPair('URL', $startPos, 1, $endPos, $endLen, $priority);
  52          $this->setLinkAttributes($tag, $linkInfo, 'url');
  53  
  54          // Overwrite the markup without touching the link's text
  55          $this->text->overwrite($startPos, 1);
  56          $this->text->overwrite($endPos,   $endLen);
  57      }
  58  
  59      /**
  60      * Capture and return labels used in current text
  61      *
  62      * @return array Labels' text position as keys, lowercased text content as values
  63      */
  64  	protected function getLabels()
  65      {
  66          preg_match_all(
  67              '/\\[((?:[^\\x17[\\]]|\\[[^\\x17[\\]]*\\])*)\\]/',
  68              $this->text,
  69              $matches,
  70              PREG_OFFSET_CAPTURE
  71          );
  72          $labels = [];
  73          foreach ($matches[1] as $m)
  74          {
  75              $labels[$m[1] - 1] = strtolower($m[0]);
  76          }
  77  
  78          return $labels;
  79      }
  80  
  81      /**
  82      * Parse automatic links markup
  83      *
  84      * @return void
  85      */
  86  	protected function parseAutomaticLinks()
  87      {
  88          preg_match_all(
  89              '/<[-+.\\w]++([:@])[^\\x17\\s>]+?(?:>|\\x1B7)/',
  90              $this->text,
  91              $matches,
  92              PREG_OFFSET_CAPTURE
  93          );
  94          foreach ($matches[0] as $i => $m)
  95          {
  96              // Re-escape escape sequences in automatic links
  97              $content  = substr($this->text->decode(str_replace("\x1B", "\\\x1B", $m[0])), 1, -1);
  98              $startPos = $m[1];
  99              $endPos   = $startPos + strlen($m[0]) - 1;
 100  
 101              $tagName  = ($matches[1][$i][0] === ':') ? 'URL' : 'EMAIL';
 102              $attrName = strtolower($tagName);
 103  
 104              $this->parser->addTagPair($tagName, $startPos, 1, $endPos, 1)
 105                           ->setAttribute($attrName, $content);
 106          }
 107      }
 108  
 109      /**
 110      * Parse inline links markup
 111      *
 112      * @return void
 113      */
 114  	protected function parseInlineLinks()
 115      {
 116          preg_match_all(
 117              '/\\[(?:[^\\x17[\\]]|\\[[^\\x17[\\]]*\\])*\\]\\(( *(?:\\([^\\x17\\s()]*\\)|[^\\x17\\s)])*(?=[ )]) *(?:"[^\\x17]*?"|\'[^\\x17]*?\'|\\([^\\x17)]*\\))? *)\\)/',
 118              $this->text,
 119              $matches,
 120              PREG_OFFSET_CAPTURE | PREG_SET_ORDER
 121          );
 122          foreach ($matches as $m)
 123          {
 124              $linkInfo = $m[1][0];
 125              $startPos = $m[0][1];
 126              $endLen   = 3 + strlen($linkInfo);
 127              $endPos   = $startPos + strlen($m[0][0]) - $endLen;
 128  
 129              $this->addLinkTag($startPos, $endPos, $endLen, $linkInfo);
 130          }
 131      }
 132  
 133      /**
 134      * Parse reference links markup
 135      *
 136      * @return void
 137      */
 138  	protected function parseReferenceLinks()
 139      {
 140          $labels = $this->getLabels();
 141          foreach ($labels as $startPos => $id)
 142          {
 143              $labelPos = $startPos + 2 + strlen($id);
 144              $endPos   = $labelPos - 1;
 145              $endLen   = 1;
 146  
 147              if ($this->text->charAt($labelPos) === ' ')
 148              {
 149                  ++$labelPos;
 150              }
 151              if (isset($labels[$labelPos], $this->text->linkReferences[$labels[$labelPos]]))
 152              {
 153                  $id     = $labels[$labelPos];
 154                  $endLen = $labelPos + 2 + strlen($id) - $endPos;
 155              }
 156              if (isset($this->text->linkReferences[$id]))
 157              {
 158                  $this->addLinkTag($startPos, $endPos, $endLen, $this->text->linkReferences[$id]);
 159              }
 160          }
 161      }
 162  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1