| 1 |  |  | #!/usr/bin/env python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | Pandoc filter for adding complex figures. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from panflute import ( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     Caption, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     Div, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     Doc, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     Element, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     Figure, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     Plain, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     convert_text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     debug, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     run_filter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | # pylint: disable=broad-exception-caught | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | def figure(elem: Element, doc: Doc) -> Figure | None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     Transform a div element into a figure element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     Arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     --------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     elem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         The pandoc element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     doc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         The pandoc document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     Figure | None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         Figure or None. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         doc.api_version >= (1, 23) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         and isinstance(elem, Div) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         and "figure" in elem.classes | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         and "caption" in elem.attributes | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     ): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             caption = convert_text(elem.attributes["caption"]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             del elem.attributes["caption"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             elem.classes.remove("figure") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             return Figure( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 *elem.content, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 caption=Caption(Plain(*caption[0].content)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 identifier=elem.identifier, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 classes=elem.classes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 attributes=elem.attributes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         except Exception as error:  # noqa: B902 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             debug(f"[WARNING] pandoc-figure: {error}") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | def prepare(doc: Doc) -> None: | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     Prepare the pandoc document. | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     Arguments | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     --------- | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     doc | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         The pandoc document | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     if doc.api_version < (1, 23): | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         debug( | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             f"[WARNING] pandoc-figure: pandoc api version " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             f"{'.'.join(str(value) for value in doc.api_version)} " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             "is not compatible" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | def main(doc: Doc | None = None) -> Doc: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     Convert the pandoc document. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     Arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     --------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     doc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         The pandoc document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     Doc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         The modified pandoc document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     return run_filter(figure, prepare=prepare, doc=doc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | if __name__ == "__main__": | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |     main() | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  |