| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace League\HTMLToMarkdown\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use League\HTMLToMarkdown\ElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class CodeConverter implements ConverterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param ElementInterface $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 12 |  |     public function convert(ElementInterface $element) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 12 |  |         $language = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         // Checking for language class on the code block | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 12 |  |         $classes = $element->getAttribute('class'); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 12 |  |         if ($classes) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             // Since tags can have more than one class, we need to find the one that starts with 'language-' | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 3 |  |             $classes = explode(' ', $classes); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 3 |  |             foreach ($classes as $class) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 3 |  |                 if (strpos($class, 'language-') !== false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                     // Found one, save it as the selected language and stop looping over the classes. | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 3 |  |                     $language = str_replace('language-', '', $class); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 3 |  |                     break; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 2 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 12 |  |         $markdown = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 12 |  |         $code = html_entity_decode($element->getChildrenAsString()); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // In order to remove the code tags we need to search for them and, in the case of the opening tag | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         // use a regular expression to find the tag and the other attributes it might have | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 12 |  |         $code = preg_replace('/<code\b[^>]*>/', '', $code); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 12 |  |         $code = str_replace('</code>', '', $code); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         // Checking if it's a code block or span | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 12 |  |         if ($element->getParent()->getTagName() == 'pre') { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             // Code block detected, newlines will be added in parent | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 6 |  |             $markdown .= '```' . $language . "\n" . $code . "\n" . '```'; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 4 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             // One line of code, wrapping it on one backtick, removing new lines | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 9 |  |             $markdown .= '`' . preg_replace('/\r\n|\r|\n/', '', $code) . '`'; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 12 |  |         return $markdown; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 84 |  |     public function getSupportedTags() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 84 |  |         return array('code'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |