| 
                    1
                 | 
                                    
                                                     | 
                
                 | 
                <?php  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    2
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    3
                 | 
                                    
                                                     | 
                
                 | 
                /*  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    4
                 | 
                                    
                                                     | 
                
                 | 
                 * This file is part of the league/commonmark package.  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    5
                 | 
                                    
                                                     | 
                
                 | 
                 *  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    6
                 | 
                                    
                                                     | 
                
                 | 
                 * (c) Colin O'Dell <[email protected]>  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    7
                 | 
                                    
                                                     | 
                
                 | 
                 * (c) Rezo Zero / Ambroise Maupate  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    8
                 | 
                                    
                                                     | 
                
                 | 
                 *  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    9
                 | 
                                    
                                                     | 
                
                 | 
                 * For the full copyright and license information, please view the LICENSE  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    10
                 | 
                                    
                                                     | 
                
                 | 
                 * file that was distributed with this source code.  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    11
                 | 
                                    
                                                     | 
                
                 | 
                 */  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    12
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    13
                 | 
                                    
                                                     | 
                
                 | 
                declare(strict_types=1);  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    14
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    15
                 | 
                                    
                                                     | 
                
                 | 
                namespace League\CommonMark\Extension\Footnote\Event;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    16
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    17
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Event\DocumentParsedEvent;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    18
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Extension\Footnote\Node\Footnote;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    19
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Extension\Footnote\Node\FootnoteBackref;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    20
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Extension\Footnote\Node\FootnoteRef;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    21
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Node\Block\Paragraph;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    22
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Node\Inline\Text;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    23
                 | 
                                    
                                                     | 
                
                 | 
                use League\CommonMark\Reference\Reference;  | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    24
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                                                                            
                            
            
                                    
            
            
                | 
                    25
                 | 
                                    
                                                     | 
                
                 | 
                final class AnonymousFootnotesListener  | 
            
            
                                                                                                            
                                                                
            
                                    
            
            
                | 
                    26
                 | 
                                    
                                                     | 
                
                 | 
                { | 
            
            
                                                        
            
                                    
            
            
                | 
                    27
                 | 
                                    
                             42                          | 
                
                 | 
                    public function onDocumentParsed(DocumentParsedEvent $event): void  | 
            
            
                                                        
            
                                    
            
            
                | 
                    28
                 | 
                                    
                                                     | 
                
                 | 
                    { | 
            
            
                                                        
            
                                    
            
            
                | 
                    29
                 | 
                                    
                             42                          | 
                
                 | 
                        $document = $event->getDocument();  | 
            
            
                                                        
            
                                    
            
            
                | 
                    30
                 | 
                                    
                             42                          | 
                
                 | 
                        $walker   = $document->walker();  | 
            
            
                                                        
            
                                    
            
            
                | 
                    31
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                        
            
                                    
            
            
                | 
                    32
                 | 
                                    
                             42                          | 
                
                 | 
                        while ($event = $walker->next()) { | 
            
            
                                                        
            
                                    
            
            
                | 
                    33
                 | 
                                    
                             42                          | 
                
                 | 
                            if (! $event->isEntering()) { | 
            
            
                                                        
            
                                    
            
            
                | 
                    34
                 | 
                                    
                             42                          | 
                
                 | 
                                continue;  | 
            
            
                                                        
            
                                    
            
            
                | 
                    35
                 | 
                                    
                                                     | 
                
                 | 
                            }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    36
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                        
            
                                    
            
            
                | 
                    37
                 | 
                                    
                             42                          | 
                
                 | 
                            $node = $event->getNode();  | 
            
            
                                                        
            
                                    
            
            
                | 
                    38
                 | 
                                    
                             42                          | 
                
                 | 
                            if (! $node instanceof FootnoteRef || ($text = $node->getContent()) === null) { | 
            
            
                                                        
            
                                    
            
            
                | 
                    39
                 | 
                                    
                             42                          | 
                
                 | 
                                continue;  | 
            
            
                                                        
            
                                    
            
            
                | 
                    40
                 | 
                                    
                                                     | 
                
                 | 
                            }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    41
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                        
            
                                    
            
            
                | 
                    42
                 | 
                                    
                                                     | 
                
                 | 
                            // Anonymous footnote needs to create a footnote from its content  | 
            
            
                                                        
            
                                    
            
            
                | 
                    43
                 | 
                                    
                             18                          | 
                
                 | 
                            $existingReference = $node->getReference();  | 
            
            
                                                        
            
                                    
            
            
                | 
                    44
                 | 
                                    
                             18                          | 
                
                 | 
                            $newReference      = new Reference(  | 
            
            
                                                        
            
                                    
            
            
                | 
                    45
                 | 
                                    
                             18                          | 
                
                 | 
                                $existingReference->getLabel(),  | 
            
            
                                                        
            
                                    
            
            
                | 
                    46
                 | 
                                    
                             18                          | 
                
                 | 
                                '#fnref:' . $existingReference->getLabel(),  | 
            
            
                                                        
            
                                    
            
            
                | 
                    47
                 | 
                                    
                             18                          | 
                
                 | 
                                $existingReference->getTitle()  | 
            
            
                                                        
            
                                    
            
            
                | 
                    48
                 | 
                                    
                                                     | 
                
                 | 
                            );  | 
            
            
                                                        
            
                                    
            
            
                | 
                    49
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                        
            
                                    
            
            
                | 
                    50
                 | 
                                    
                             18                          | 
                
                 | 
                            $paragraph = new Paragraph();  | 
            
            
                                                        
            
                                    
            
            
                | 
                    51
                 | 
                                    
                             18                          | 
                
                 | 
                            $paragraph->appendChild(new Text($text));  | 
            
            
                                                        
            
                                    
            
            
                | 
                    52
                 | 
                                    
                             18                          | 
                
                 | 
                            $paragraph->appendChild(new FootnoteBackref($newReference));  | 
            
            
                                                        
            
                                    
            
            
                | 
                    53
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                        
            
                                    
            
            
                | 
                    54
                 | 
                                    
                             18                          | 
                
                 | 
                            $footnote = new Footnote($newReference);  | 
            
            
                                                        
            
                                    
            
            
                | 
                    55
                 | 
                                    
                             18                          | 
                
                 | 
                            $footnote->appendChild($paragraph);  | 
            
            
                                                        
            
                                    
            
            
                | 
                    56
                 | 
                                    
                                                     | 
                
                 | 
                 | 
            
            
                                                        
            
                                    
            
            
                | 
                    57
                 | 
                                    
                             18                          | 
                
                 | 
                            $document->appendChild($footnote);  | 
            
            
                                                        
            
                                    
            
            
                | 
                    58
                 | 
                                    
                                                     | 
                
                 | 
                        }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    59
                 | 
                                    
                             42                          | 
                
                 | 
                    }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    60
                 | 
                                    
                                                     | 
                
                 | 
                }  | 
            
            
                                                        
            
                                    
            
            
                | 
                    61
                 | 
                                    
                                                     | 
                
                 | 
                 |