@@ -74,10 +74,14 @@ def render_drawio_diagrams(self, output_content, page):
7474 "html.parser" ,
7575 )
7676 else :
77- diagram_page = diagram ["alt" ]
77+ diagram_page = ""
78+
7879 # Use page attribute instead of alt if it is set
79- if "page" in diagram :
80- diagram_page = diagram ["page" ]
80+ if diagram .has_attr ("page" ):
81+ diagram_page = diagram .get ("page" )
82+ else :
83+ diagram_page = diagram .get ("alt" )
84+
8185 mxgraph = BeautifulSoup (
8286 DrawioPlugin .substitute_with_file (
8387 diagram_config , path , diagram ["src" ], diagram_page
@@ -96,48 +100,46 @@ def substitute_with_url(config: Dict, url: str) -> str:
96100 return SUB_TEMPLATE .substitute (config = escape (json .dumps (config )))
97101
98102 @staticmethod
99- def substitute_with_file (config : Dict , path : Path , src : str , alt : str ) -> str :
103+ def substitute_with_file (config : Dict , path : Path , src : str , page : str ) -> str :
100104 try :
101105 diagram_xml = etree .parse (path .joinpath (src ).resolve ())
102- except Exception :
106+ except Exception as e :
103107 LOGGER .error (
104- f"Error: Provided diagram file '{ src } ' on path '{ path } ' is not a valid diagram "
108+ f"Error: Could not parse diagram file '{ src } ' on path '{ path } ': { e } "
105109 )
106- diagram_xml = etree .fromstring ("<invalid/>" )
110+ config ["xml" ] = ""
111+ return SUB_TEMPLATE .substitute (config = escape (json .dumps (config )))
107112
108- diagram = DrawioPlugin .parse_diagram (diagram_xml , alt )
113+ diagram = DrawioPlugin .parse_diagram (diagram_xml , page )
109114 config ["xml" ] = diagram
110-
111115 return SUB_TEMPLATE .substitute (config = escape (json .dumps (config )))
112116
113117 @staticmethod
114- def parse_diagram (data , alt , src = "" , path = None ) -> str :
115- if alt is None or len (alt ) == 0 :
116- return etree .tostring (data , encoding = str )
118+ def parse_diagram (data , page_name , src = "" , path = None ) -> str :
119+ mxfile_nodes = data .xpath ("//mxfile" )
117120
118- try :
119- mxfile = data .xpath ("//mxfile" )[0 ]
121+ if not mxfile_nodes :
122+ if data .xpath ("//*[local-name()='svg']" ) is not None :
123+ return etree .tostring (data , encoding = str )
124+ return ""
120125
121- # try to parse for a specific page by using the alt attribute
122- page = mxfile .xpath (f"//diagram[@name='{ alt } ']" )
126+ mxfile = mxfile_nodes [0 ]
123127
124- if len (page ) == 1 :
125- parser = etree .XMLParser ()
126- result = parser .makeelement (mxfile .tag , mxfile .attrib )
128+ if not page_name :
129+ return etree .tostring (mxfile , encoding = str )
127130
128- result . append ( page [ 0 ] )
129- return etree . tostring ( result , encoding = str )
130- else :
131- LOGGER . warning (
132- f"Warning: Found { len (page ) } results for page name ' { alt } ' for diagram ' { src } ' on path ' { path } '"
133- )
131+ page = mxfile . xpath ( f"//diagram[@name=' { page_name } ']" )
132+ if len ( page ) == 1 :
133+ parser = etree . XMLParser ()
134+ result = parser . makeelement ( mxfile . tag , mxfile . attrib )
135+ result . append (page [ 0 ])
136+ return etree . tostring ( result , encoding = str )
134137
135- return etree .tostring (mxfile , encoding = str )
136- except Exception :
137- LOGGER .error (
138- f"Error: Could not properly parse page name '{ alt } ' for diagram '{ src } ' on path '{ path } '"
139- )
140- return ""
138+ LOGGER .warning (
139+ f"Warning: Found { len (page )} results for page name '{ page_name } ' "
140+ f"falling back to first page."
141+ )
142+ return etree .tostring (mxfile , encoding = str )
141143
142144 def on_config (self , config : base .Config ):
143145 """Load embedded files"""
0 commit comments