[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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\Litedown\Parser\Passes; 9 class Emphasis extends AbstractPass 10 { 11 protected $closeEm; 12 protected $closeStrong; 13 protected $emPos; 14 protected $emEndPos; 15 protected $remaining; 16 protected $strongPos; 17 protected $strongEndPos; 18 public function parse() 19 { 20 $this->parseEmphasisByCharacter('*', '/\\*+/'); 21 $this->parseEmphasisByCharacter('_', '/_+/'); 22 } 23 protected function adjustEndingPositions() 24 { 25 if ($this->closeEm && $this->closeStrong) 26 if ($this->emPos < $this->strongPos) 27 $this->emEndPos += 2; 28 else 29 ++$this->strongEndPos; 30 } 31 protected function adjustStartingPositions() 32 { 33 if (isset($this->emPos) && $this->emPos === $this->strongPos) 34 if ($this->closeEm) 35 $this->emPos += 2; 36 else 37 ++$this->strongPos; 38 } 39 protected function closeSpans() 40 { 41 if ($this->closeEm) 42 { 43 --$this->remaining; 44 $this->parser->addTagPair('EM', $this->emPos, 1, $this->emEndPos, 1); 45 $this->emPos = \null; 46 } 47 if ($this->closeStrong) 48 { 49 $this->remaining -= 2; 50 $this->parser->addTagPair('STRONG', $this->strongPos, 2, $this->strongEndPos, 2); 51 $this->strongPos = \null; 52 } 53 } 54 protected function parseEmphasisByCharacter($character, $regexp) 55 { 56 $pos = $this->text->indexOf($character); 57 if ($pos === \false) 58 return; 59 foreach ($this->getEmphasisByBlock($regexp, $pos) as $block) 60 $this->processEmphasisBlock($block); 61 } 62 protected function getEmphasisByBlock($regexp, $pos) 63 { 64 $block = []; 65 $blocks = []; 66 $breakPos = $this->text->indexOf("\x17", $pos); 67 \preg_match_all($regexp, $this->text, $matches, \PREG_OFFSET_CAPTURE, $pos); 68 foreach ($matches[0] as $m) 69 { 70 $matchPos = $m[1]; 71 $matchLen = \strlen($m[0]); 72 if ($matchPos > $breakPos) 73 { 74 $blocks[] = $block; 75 $block = []; 76 $breakPos = $this->text->indexOf("\x17", $matchPos); 77 } 78 if (!$this->ignoreEmphasis($matchPos, $matchLen)) 79 $block[] = [$matchPos, $matchLen]; 80 } 81 $blocks[] = $block; 82 return $blocks; 83 } 84 protected function ignoreEmphasis($matchPos, $matchLen) 85 { 86 return ($this->text->charAt($matchPos) === '_' && $matchLen === 1 && $this->text->isSurroundedByAlnum($matchPos, $matchLen)); 87 } 88 protected function openSpans($pos) 89 { 90 if ($this->remaining & 1) 91 $this->emPos = $pos - $this->remaining; 92 if ($this->remaining & 2) 93 $this->strongPos = $pos - $this->remaining; 94 } 95 protected function processEmphasisBlock(array $block) 96 { 97 $this->emPos = \null; 98 $this->strongPos = \null; 99 foreach ($block as $_aab3a45e) 100 { 101 list($matchPos, $matchLen) = $_aab3a45e; 102 $this->processEmphasisMatch($matchPos, $matchLen); 103 } 104 } 105 protected function processEmphasisMatch($matchPos, $matchLen) 106 { 107 $canOpen = !$this->text->isBeforeWhitespace($matchPos + $matchLen - 1); 108 $canClose = !$this->text->isAfterWhitespace($matchPos); 109 $closeLen = ($canClose) ? \min($matchLen, 3) : 0; 110 $this->closeEm = ($closeLen & 1) && isset($this->emPos); 111 $this->closeStrong = ($closeLen & 2) && isset($this->strongPos); 112 $this->emEndPos = $matchPos; 113 $this->strongEndPos = $matchPos; 114 $this->remaining = $matchLen; 115 $this->adjustStartingPositions(); 116 $this->adjustEndingPositions(); 117 $this->closeSpans(); 118 $this->remaining = ($canOpen) ? \min($this->remaining, 3) : 0; 119 $this->openSpans($matchPos + $matchLen); 120 } 121 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |