| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the league/commonmark-ext-autolink package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Colin O'Dell <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace League\CommonMark\Ext\Autolink; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use League\CommonMark\Block\Element\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use League\CommonMark\DocumentProcessorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use League\CommonMark\Inline\Element\Link; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use League\CommonMark\Inline\Element\Text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class EmailAutolinkProcessor implements DocumentProcessorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const REGEX = '/([A-Za-z0-9.\-_+]+@[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_.]+)/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param Document $document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 93 |  |     public function processDocument(Document $document) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 93 |  |         $walker = $document->walker(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 93 |  |         while ($event = $walker->next()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 93 |  |             if ($event->getNode() instanceof Text) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 93 |  |                 self::processAutolinks($event->getNode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 93 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 39 | 93 |  |     private static function processAutolinks(Text $node) | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 41 | 93 |  |         $contents = \preg_split(self::REGEX, $node->getContent(), -1, PREG_SPLIT_DELIM_CAPTURE); | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 43 | 93 |  |         if (\count($contents) === 1) { | 
            
                                                        
            
                                    
            
            
                | 44 | 69 |  |             return; | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 | 24 |  |         $leftovers = ''; | 
            
                                                        
            
                                    
            
            
                | 48 | 24 |  |         foreach ($contents as $i => $content) { | 
            
                                                        
            
                                    
            
            
                | 49 | 24 |  |             if ($i % 2 === 0) { | 
            
                                                        
            
                                    
            
            
                | 50 | 24 |  |                 $text = $leftovers.$content; | 
            
                                                        
            
                                    
            
            
                | 51 | 24 |  |                 if ($text !== '') { | 
            
                                                        
            
                                    
            
            
                | 52 | 12 |  |                     $node->insertBefore(new Text($leftovers . $content)); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 | 24 |  |                 $leftovers = ''; | 
            
                                                        
            
                                    
            
            
                | 56 | 24 |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             // Does the URL end with punctuation that should be stripped? | 
            
                                                        
            
                                    
            
            
                | 60 | 24 |  |             if (\substr($content, -1) === '.') { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |                 // Add the punctuation later | 
            
                                                        
            
                                    
            
            
                | 62 | 3 |  |                 $content = \substr($content, 0, -1); | 
            
                                                        
            
                                    
            
            
                | 63 | 3 |  |                 $leftovers = '.'; | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             // The last character cannot be - or _ | 
            
                                                        
            
                                    
            
            
                | 67 | 24 |  |             if (\in_array(\substr($content, -1), ['-', '_'])) { | 
            
                                                        
            
                                    
            
            
                | 68 | 6 |  |                 $node->insertBefore(new Text($content . $leftovers)); | 
            
                                                        
            
                                    
            
            
                | 69 | 6 |  |                 $leftovers = ''; | 
            
                                                        
            
                                    
            
            
                | 70 | 6 |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 73 | 18 |  |             $node->insertBefore(new Link('mailto:' . $content, $content)); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 76 | 24 |  |         $node->detach(); | 
            
                                                        
            
                                    
            
            
                | 77 | 24 |  |     } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |