[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\PipeTables;
   9  use s9e\TextFormatter\Plugins\ParserBase;
  10  class Parser extends ParserBase
  11  {
  12      protected $pos;
  13      protected $table;
  14      protected $tableTag;
  15      protected $tables;
  16      protected $text;
  17  	public function parse($text, array $matches)
  18      {
  19          $this->text = $text;
  20          if ($this->config['overwriteMarkdown'])
  21              $this->overwriteMarkdown();
  22          if ($this->config['overwriteEscapes'])
  23              $this->overwriteEscapes();
  24          $this->captureTables();
  25          $this->processTables();
  26          unset($this->tables);
  27          unset($this->text);
  28      }
  29  	protected function addLine($line)
  30      {
  31          $ignoreLen = 0;
  32          if (!isset($this->table))
  33          {
  34              $this->table = [];
  35              \preg_match('/^ */', $line, $m);
  36              $ignoreLen = \strlen($m[0]);
  37              $line      = \substr($line, $ignoreLen);
  38          }
  39          $line = \preg_replace('/^( *)\\|/', '$1 ', $line);
  40          $line = \preg_replace('/\\|( *)$/', ' $1', $line);
  41          $this->table['rows'][] = ['line' => $line, 'pos' => $this->pos + $ignoreLen];
  42      }
  43  	protected function addTableBody()
  44      {
  45          $i   = 1;
  46          $cnt = \count($this->table['rows']);
  47          while (++$i < $cnt)
  48              $this->addTableRow('TD', $this->table['rows'][$i]);
  49          $this->createBodyTags($this->table['rows'][2]['pos'], $this->pos);
  50      }
  51  	protected function addTableCell($tagName, $align, $text)
  52      {
  53          $startPos  = $this->pos;
  54          $endPos    = $startPos + \strlen($text);
  55          $this->pos = $endPos;
  56          \preg_match('/^( *).*?( *)$/', $text, $m);
  57          if ($m[1])
  58          {
  59              $ignoreLen = \strlen($m[1]);
  60              $this->createIgnoreTag($startPos, $ignoreLen);
  61              $startPos += $ignoreLen;
  62          }
  63          if ($m[2])
  64          {
  65              $ignoreLen = \strlen($m[2]);
  66              $this->createIgnoreTag($endPos - $ignoreLen, $ignoreLen);
  67              $endPos -= $ignoreLen;
  68          }
  69          $this->createCellTags($tagName, $startPos, $endPos, $align);
  70      }
  71  	protected function addTableHead()
  72      {
  73          $this->addTableRow('TH', $this->table['rows'][0]);
  74          $this->createHeadTags($this->table['rows'][0]['pos'], $this->pos);
  75      }
  76  	protected function addTableRow($tagName, $row)
  77      {
  78          $this->pos = $row['pos'];
  79          foreach (\explode('|', $row['line']) as $i => $str)
  80          {
  81              if ($i > 0)
  82              {
  83                  $this->createIgnoreTag($this->pos, 1);
  84                  ++$this->pos;
  85              }
  86              $align = (empty($this->table['cols'][$i])) ? '' : $this->table['cols'][$i];
  87              $this->addTableCell($tagName, $align, $str);
  88          }
  89          $this->createRowTags($row['pos'], $this->pos);
  90      }
  91  	protected function captureTables()
  92      {
  93          unset($this->table);
  94          $this->tables = [];
  95          $this->pos = 0;
  96          foreach (\explode("\n", $this->text) as $line)
  97          {
  98              if (\strpos($line, '|') === \false)
  99                  $this->endTable();
 100              else
 101                  $this->addLine($line);
 102              $this->pos += 1 + \strlen($line);
 103          }
 104          $this->endTable();
 105      }
 106  	protected function createBodyTags($startPos, $endPos)
 107      {
 108          $this->parser->addTagPair('TBODY', $startPos, 0, $endPos, 0, -103);
 109      }
 110  	protected function createCellTags($tagName, $startPos, $endPos, $align)
 111      {
 112          if ($startPos === $endPos)
 113              $tag = $this->parser->addSelfClosingTag($tagName, $startPos, 0, -101);
 114          else
 115              $tag = $this->parser->addTagPair($tagName, $startPos, 0, $endPos, 0, -101);
 116          if ($align)
 117              $tag->setAttribute('align', $align);
 118      }
 119  	protected function createHeadTags($startPos, $endPos)
 120      {
 121          $this->parser->addTagPair('THEAD', $startPos, 0, $endPos, 0, -103);
 122      }
 123  	protected function createIgnoreTag($pos, $len)
 124      {
 125          $this->tableTag->cascadeInvalidationTo($this->parser->addIgnoreTag($pos, $len, 1000));
 126      }
 127  	protected function createRowTags($startPos, $endPos)
 128      {
 129          $this->parser->addTagPair('TR', $startPos, 0, $endPos, 0, -102);
 130      }
 131  	protected function createSeparatorTag(array $row)
 132      {
 133          $this->createIgnoreTag($row['pos'] - 1, 1 + \strlen($row['line']));
 134      }
 135  	protected function createTableTags($startPos, $endPos)
 136      {
 137          $this->tableTag = $this->parser->addTagPair('TABLE', $startPos, 0, $endPos, 0, -104);
 138      }
 139  	protected function endTable()
 140      {
 141          if ($this->hasValidTable())
 142          {
 143              $this->table['cols'] = $this->parseColumnAlignments($this->table['rows'][1]['line']);
 144              $this->tables[]      = $this->table;
 145          }
 146          unset($this->table);
 147      }
 148  	protected function hasValidTable()
 149      {
 150          return (isset($this->table) && \count($this->table['rows']) > 2 && $this->isValidSeparator($this->table['rows'][1]['line']));
 151      }
 152  	protected function isValidSeparator($line)
 153      {
 154          return (bool) \preg_match('/^ *:?-+:?(?:(?:\\+| *\\| *):?-+:?)+ *$/', $line);
 155      }
 156  	protected function overwriteBlockquoteCallback(array $m)
 157      {
 158          return \strtr($m[0], '>', ' ');
 159      }
 160  	protected function overwriteEscapes()
 161      {
 162          if (\strpos($this->text, '\\|') !== \false)
 163              $this->text = \preg_replace('/\\\\[\\\\|]/', '..', $this->text);
 164      }
 165  	protected function overwriteInlineCodeCallback(array $m)
 166      {
 167          return \strtr($m[0], '|', '.');
 168      }
 169  	protected function overwriteMarkdown()
 170      {
 171          if (\strpos($this->text, '`') !== \false)
 172              $this->text = \preg_replace_callback('/`[^`]*`/', [$this, 'overwriteInlineCodeCallback'], $this->text);
 173          if (\strpos($this->text, '>') !== \false)
 174              $this->text = \preg_replace_callback('/^(?:> ?)+/m', [$this, 'overwriteBlockquoteCallback'], $this->text);
 175      }
 176  	protected function parseColumnAlignments($line)
 177      {
 178          $align = [
 179              0b00 => '',
 180              0b01 => 'right',
 181              0b10 => 'left',
 182              0b11 => 'center'
 183          ];
 184          $cols = [];
 185          \preg_match_all('/(:?)-+(:?)/', $line, $matches, \PREG_SET_ORDER);
 186          foreach ($matches as $m)
 187          {
 188              $key = (!empty($m[1]) ? 2 : 0) + (!empty($m[2]) ? 1 : 0);
 189              $cols[] = $align[$key];
 190          }
 191          return $cols;
 192      }
 193  	protected function processCurrentTable()
 194      {
 195          $firstRow = $this->table['rows'][0];
 196          $lastRow  = \end($this->table['rows']);
 197          $this->createTableTags($firstRow['pos'], $lastRow['pos'] + \strlen($lastRow['line']));
 198          $this->addTableHead();
 199          $this->createSeparatorTag($this->table['rows'][1]);
 200          $this->addTableBody();
 201      }
 202  	protected function processTables()
 203      {
 204          foreach ($this->tables as $table)
 205          {
 206              $this->table = $table;
 207              $this->processCurrentTable();
 208          }
 209      }
 210  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1