Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Parser.php
001 <?php
002
003 /*
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2016 The s9e Authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Plugins\PipeTables;
009 use s9e\TextFormatter\Plugins\ParserBase;
010 class Parser extends ParserBase
011 {
012 protected $pos;
013 protected $table;
014 protected $tableTag;
015 protected $tables;
016 protected $text;
017 public function parse($text, array $matches)
018 {
019 $this->text = $text;
020 if ($this->config['overwriteMarkdown'])
021 $this->overwriteMarkdown();
022 if ($this->config['overwriteEscapes'])
023 $this->overwriteEscapes();
024 $this->captureTables();
025 $this->processTables();
026 unset($this->tables);
027 unset($this->text);
028 }
029 protected function addLine($line)
030 {
031 $ignoreLen = 0;
032 if (!isset($this->table))
033 {
034 $this->table = array();
035 \preg_match('/^ */', $line, $m);
036 $ignoreLen = \strlen($m[0]);
037 $line = \substr($line, $ignoreLen);
038 }
039 $line = \preg_replace('/^( *)\\|/', '$1 ', $line);
040 $line = \preg_replace('/\\|( *)$/', ' $1', $line);
041 $this->table['rows'][] = array('line' => $line, 'pos' => $this->pos + $ignoreLen);
042 }
043 protected function addTableBody()
044 {
045 $i = 1;
046 $cnt = \count($this->table['rows']);
047 while (++$i < $cnt)
048 $this->addTableRow('TD', $this->table['rows'][$i]);
049 $this->createBodyTags($this->table['rows'][2]['pos'], $this->pos);
050 }
051 protected function addTableCell($tagName, $align, $text)
052 {
053 $startPos = $this->pos;
054 $endPos = $startPos + \strlen($text);
055 $this->pos = $endPos;
056 \preg_match('/^( *).*?( *)$/', $text, $m);
057 if ($m[1])
058 {
059 $ignoreLen = \strlen($m[1]);
060 $this->createIgnoreTag($startPos, $ignoreLen);
061 $startPos += $ignoreLen;
062 }
063 if ($m[2])
064 {
065 $ignoreLen = \strlen($m[2]);
066 $this->createIgnoreTag($endPos - $ignoreLen, $ignoreLen);
067 $endPos -= $ignoreLen;
068 }
069 $this->createCellTags($tagName, $startPos, $endPos, $align);
070 }
071 protected function addTableHead()
072 {
073 $this->addTableRow('TH', $this->table['rows'][0]);
074 $this->createHeadTags($this->table['rows'][0]['pos'], $this->pos);
075 }
076 protected function addTableRow($tagName, $row)
077 {
078 $this->pos = $row['pos'];
079 foreach (\explode('|', $row['line']) as $i => $str)
080 {
081 if ($i > 0)
082 {
083 $this->createIgnoreTag($this->pos, 1);
084 ++$this->pos;
085 }
086 $align = (empty($this->table['cols'][$i])) ? '' : $this->table['cols'][$i];
087 $this->addTableCell($tagName, $align, $str);
088 }
089 $this->createRowTags($row['pos'], $this->pos);
090 }
091 protected function captureTables()
092 {
093 unset($this->table);
094 $this->tables = array();
095 $this->pos = 0;
096 foreach (\explode("\n", $this->text) as $line)
097 {
098 if (\strpos($line, '|') === \false)
099 $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, -3);
109 }
110 protected function createCellTags($tagName, $startPos, $endPos, $align)
111 {
112 $tag = $this->parser->addTagPair($tagName, $startPos, 0, $endPos, 0, -1);
113 if ($align)
114 $tag->setAttribute('align', $align);
115 }
116 protected function createHeadTags($startPos, $endPos)
117 {
118 $this->parser->addTagPair('THEAD', $startPos, 0, $endPos, 0, -3);
119 }
120 protected function createIgnoreTag($pos, $len)
121 {
122 $this->tableTag->cascadeInvalidationTo($this->parser->addIgnoreTag($pos, $len, 1000));
123 }
124 protected function createRowTags($startPos, $endPos)
125 {
126 $this->parser->addTagPair('TR', $startPos, 0, $endPos, 0, -2);
127 }
128 protected function createSeparatorTag(array $row)
129 {
130 $this->createIgnoreTag($row['pos'] - 1, 1 + \strlen($row['line']));
131 }
132 protected function createTableTags($startPos, $endPos)
133 {
134 $this->tableTag = $this->parser->addTagPair('TABLE', $startPos, 0, $endPos, 0, -4);
135 }
136 protected function endTable()
137 {
138 if ($this->hasValidTable())
139 {
140 $this->table['cols'] = $this->parseColumnAlignments($this->table['rows'][1]['line']);
141 $this->tables[] = $this->table;
142 }
143 unset($this->table);
144 }
145 protected function hasValidTable()
146 {
147 return (isset($this->table) && \count($this->table['rows']) > 2 && $this->isValidSeparator($this->table['rows'][1]['line']));
148 }
149 protected function isValidSeparator($line)
150 {
151 return (bool) \preg_match('/^ *:?-+:?(?:(?:\\+| *\\| *):?-+:?)+ *$/', $line);
152 }
153 protected function overwriteBlockquoteCallback(array $m)
154 {
155 return \strtr($m[0], '>', ' ');
156 }
157 protected function overwriteEscapes()
158 {
159 if (\strpos($this->text, '\\|') !== \false)
160 $this->text = \preg_replace('/\\\\[\\\\|]/', '..', $this->text);
161 }
162 protected function overwriteInlineCodeCallback(array $m)
163 {
164 return \strtr($m[0], '|', '.');
165 }
166 protected function overwriteMarkdown()
167 {
168 if (\strpos($this->text, '`') !== \false)
169 $this->text = \preg_replace_callback('/`[^`]*`/', array($this, 'overwriteInlineCodeCallback'), $this->text);
170 if (\strpos($this->text, '>') !== \false)
171 $this->text = \preg_replace_callback('/^(?:> ?)+/m', array($this, 'overwriteBlockquoteCallback'), $this->text);
172 }
173 protected function parseColumnAlignments($line)
174 {
175 $align = array(
176 0 => '',
177 1 => 'right',
178 2 => 'left',
179 3 => 'center'
180 );
181 $cols = array();
182 \preg_match_all('/(:?)-+(:?)/', $line, $matches, \PREG_SET_ORDER);
183 foreach ($matches as $m)
184 {
185 $key = (!empty($m[1]) ? 2 : 0) + (!empty($m[2]) ? 1 : 0);
186 $cols[] = $align[$key];
187 }
188 return $cols;
189 }
190 protected function processCurrentTable()
191 {
192 $firstRow = $this->table['rows'][0];
193 $lastRow = \end($this->table['rows']);
194 $this->createTableTags($firstRow['pos'], $lastRow['pos'] + \strlen($lastRow['line']));
195 $this->addTableHead();
196 $this->createSeparatorTag($this->table['rows'][1]);
197 $this->addTableBody();
198 }
199 protected function processTables()
200 {
201 foreach ($this->tables as $table)
202 {
203 $this->table = $table;
204 $this->processCurrentTable();
205 }
206 }
207 }