By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

Update markdown implementation to fix image alt text

roundabout,
created on Monday, 22 April 2024, 16:55:52 (1713804952), received on Wednesday, 31 July 2024, 06:54:45 (1722408885)
Author identity: vlad <vlad.muntoiu@gmail.com>

014a07573ca4b87aa40e46304732a2e4283f448f

markdown.py

@@ -433,6 +433,7 @@ def make_html(ast):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            tag["href"] = i.destination
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        if i.tag_name == "img":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            tag["src"] = i.destination
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        tag["alt"] = " ".join(i.content)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        if i.tag_name == "pre":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            tag["data-language"] = i.language
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        if i.classes:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -440,23 +441,19 @@ def make_html(ast):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        try:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            if isinstance(i.content, list):
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                tag.append(make_html(i.content))
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        elif i.content:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        elif i.content and i.tag_name != "img":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                tag.string = i.content
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        if i.tag_name == "img":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            tag.string = ""
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        except AttributeError as exc:
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            # print(i)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            print(exc, file=sys.stderr)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        ...
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        soup.append(tag)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                return soup
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        if __name__ == '__main__':
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            with open("/home/vlad/roundabout/doc/changelog/0.1.0 (2024-03-31).md") as file:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # Generate an AST from a markdown file
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                ast = tokenise(file.read())
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # for i in ast:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                #     print(repr(i))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                # Now convert the AST to HTML
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                print(make_html(ast).prettify(formatter=beautifulsoup.formatter.HTMLFormatter(indent=4)))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def markdown2html(markdown):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            print(tokenise(markdown))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return make_html(tokenise(markdown))