[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Autolink/ -> Parser.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2021 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\Autolink;
   9  
  10  use s9e\TextFormatter\Plugins\ParserBase;
  11  
  12  class Parser extends ParserBase
  13  {
  14      /**
  15      * {@inheritdoc}
  16      */
  17  	public function parse($text, array $matches)
  18      {
  19          foreach ($matches as $m)
  20          {
  21              // Linkify the trimmed URL
  22              $this->linkifyUrl($m[0][1], $this->trimUrl($m[0][0]));
  23          }
  24      }
  25  
  26      /**
  27      * Linkify given URL at given position
  28      *
  29      * @param  integer $tagPos URL's position in the text
  30      * @param  string  $url    URL
  31      * @return void
  32      */
  33  	protected function linkifyUrl($tagPos, $url)
  34      {
  35          // Create a zero-width end tag right after the URL
  36          $endPos = $tagPos + strlen($url);
  37          $endTag = $this->parser->addEndTag($this->config['tagName'], $endPos, 0);
  38  
  39          // If the URL starts with "www." we prepend "http://"
  40          if ($url[3] === '.')
  41          {
  42              $url = 'http://' . $url;
  43          }
  44  
  45          // Create a zero-width start tag right before the URL, with a slightly worse priority to
  46          // allow specialized plugins to use the URL instead
  47          $startTag = $this->parser->addStartTag($this->config['tagName'], $tagPos, 0, 1);
  48          $startTag->setAttribute($this->config['attrName'], $url);
  49  
  50          // Pair the tags together
  51          $startTag->pairWith($endTag);
  52  
  53          // Protect the tag's content from partial replacements with a low priority tag
  54          $contentTag = $this->parser->addVerbatim($tagPos, $endPos - $tagPos, 1000);
  55          $startTag->cascadeInvalidationTo($contentTag);
  56      }
  57  
  58      /**
  59      * Remove trailing punctuation from given URL
  60      *
  61      * We remove most ASCII non-letters and Unicode punctuation from the end of the string.
  62      * Exceptions:
  63      *  - dashes and underscores, (base64 IDs could end with one)
  64      *  - equal signs, (because of "foo?bar=")
  65      *  - trailing slashes,
  66      *  - closing parentheses. (they are balanced separately)
  67      *
  68      * @param  string $url Original URL
  69      * @return string      Trimmed URL
  70      */
  71  	protected function trimUrl($url)
  72      {
  73          return preg_replace('#(?:(?![-=)/_])[\\s!-.:-@[-`{-~\\pP])+$#Du', '', $url);
  74      }
  75  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1