[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Litedown/Parser/Passes/ -> Images.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 Images extends AbstractPass
  13  {
  14      use LinkAttributesSetter;
  15  
  16      /**
  17      * {@inheritdoc}
  18      */
  19  	public function parse()
  20      {
  21          $pos = $this->text->indexOf('![');
  22          if ($pos === false)
  23          {
  24              return;
  25          }
  26          if ($this->text->indexOf('](', $pos) !== false)
  27          {
  28              $this->parseInlineImages();
  29          }
  30          if ($this->text->hasReferences)
  31          {
  32              $this->parseReferenceImages();
  33          }
  34      }
  35  
  36      /**
  37      * Add an image tag for given text span
  38      *
  39      * @param  integer $startPos Start tag position
  40      * @param  integer $endPos   End tag position
  41      * @param  integer $endLen   End tag length
  42      * @param  string  $linkInfo URL optionally followed by space and a title
  43      * @param  string  $alt      Value for the alt attribute
  44      * @return void
  45      */
  46  	protected function addImageTag($startPos, $endPos, $endLen, $linkInfo, $alt)
  47      {
  48          $tag = $this->parser->addTagPair('IMG', $startPos, 2, $endPos, $endLen);
  49          $this->setLinkAttributes($tag, $linkInfo, 'src');
  50          $tag->setAttribute('alt', $this->text->decode($alt));
  51  
  52          // Overwrite the markup
  53          $this->text->overwrite($startPos, $endPos + $endLen - $startPos);
  54      }
  55  
  56      /**
  57      * Parse inline images markup
  58      *
  59      * @return void
  60      */
  61  	protected function parseInlineImages()
  62      {
  63          preg_match_all(
  64              '/!\\[(?:[^\\x17[\\]]|\\[[^\\x17[\\]]*\\])*\\]\\(( *(?:[^\\x17\\s()]|\\([^\\x17\\s()]*\\))*(?=[ )]) *(?:"[^\\x17]*?"|\'[^\\x17]*?\'|\\([^\\x17)]*\\))? *)\\)/',
  65              $this->text,
  66              $matches,
  67              PREG_OFFSET_CAPTURE | PREG_SET_ORDER
  68          );
  69          foreach ($matches as $m)
  70          {
  71              $linkInfo = $m[1][0];
  72              $startPos = $m[0][1];
  73              $endLen   = 3 + strlen($linkInfo);
  74              $endPos   = $startPos + strlen($m[0][0]) - $endLen;
  75              $alt      = substr($m[0][0], 2, strlen($m[0][0]) - $endLen - 2);
  76  
  77              $this->addImageTag($startPos, $endPos, $endLen, $linkInfo, $alt);
  78          }
  79      }
  80  
  81      /**
  82      * Parse reference images markup
  83      *
  84      * @return void
  85      */
  86  	protected function parseReferenceImages()
  87      {
  88          preg_match_all(
  89              '/!\\[((?:[^\\x17[\\]]|\\[[^\\x17[\\]]*\\])*)\\](?: ?\\[([^\\x17[\\]]+)\\])?/',
  90              $this->text,
  91              $matches,
  92              PREG_OFFSET_CAPTURE | PREG_SET_ORDER
  93          );
  94          foreach ($matches as $m)
  95          {
  96              $startPos = $m[0][1];
  97              $endPos   = $startPos + 2 + strlen($m[1][0]);
  98              $endLen   = 1;
  99              $alt      = $m[1][0];
 100              $id       = $alt;
 101  
 102              if (isset($m[2][0], $this->text->linkReferences[$m[2][0]]))
 103              {
 104                  $endLen = strlen($m[0][0]) - strlen($alt) - 2;
 105                  $id        = $m[2][0];
 106              }
 107              elseif (!isset($this->text->linkReferences[$id]))
 108              {
 109                  continue;
 110              }
 111  
 112              $this->addImageTag($startPos, $endPos, $endLen, $this->text->linkReferences[$id], $alt);
 113          }
 114      }
 115  }


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