生成完整的 draw.io XML Args: cells: 文本单元格列表 Returns: XML 字符串
(self, cells: list[TextCellData])
| 149 | return escaped |
| 150 | |
| 151 | def generate_xml(self, cells: list[TextCellData]) -> str: |
| 152 | """ |
| 153 | 生成完整的 draw.io XML |
| 154 | |
| 155 | Args: |
| 156 | cells: 文本单元格列表 |
| 157 | |
| 158 | Returns: |
| 159 | XML 字符串 |
| 160 | """ |
| 161 | # 创建根元素 |
| 162 | mxfile = ET.Element("mxfile") |
| 163 | mxfile.set("host", "app.diagrams.net") |
| 164 | mxfile.set("modified", "2024-01-01T00:00:00.000Z") |
| 165 | mxfile.set("agent", "OCR Vector Restore") |
| 166 | mxfile.set("version", "1.0.0") |
| 167 | mxfile.set("type", "device") |
| 168 | |
| 169 | # 创建 diagram |
| 170 | diagram = ET.SubElement(mxfile, "diagram") |
| 171 | diagram.set("name", self.diagram_name) |
| 172 | diagram.set("id", "diagram-1") |
| 173 | |
| 174 | # 创建 mxGraphModel |
| 175 | graph_model = ET.SubElement(diagram, "mxGraphModel") |
| 176 | graph_model.set("dx", "0") |
| 177 | graph_model.set("dy", "0") |
| 178 | graph_model.set("grid", "1") |
| 179 | graph_model.set("gridSize", "10") |
| 180 | graph_model.set("guides", "1") |
| 181 | graph_model.set("tooltips", "1") |
| 182 | graph_model.set("connect", "1") |
| 183 | graph_model.set("arrows", "1") |
| 184 | graph_model.set("fold", "1") |
| 185 | graph_model.set("page", "1") |
| 186 | graph_model.set("pageScale", "1") |
| 187 | graph_model.set("pageWidth", str(self.page_width)) |
| 188 | graph_model.set("pageHeight", str(self.page_height)) |
| 189 | graph_model.set("math", "1") # 启用数学公式渲染 |
| 190 | |
| 191 | # 创建 root |
| 192 | root = ET.SubElement(graph_model, "root") |
| 193 | |
| 194 | # 添加必需的基础单元格 |
| 195 | cell_0 = ET.SubElement(root, "mxCell") |
| 196 | cell_0.set("id", "0") |
| 197 | |
| 198 | cell_1 = ET.SubElement(root, "mxCell") |
| 199 | cell_1.set("id", "1") |
| 200 | cell_1.set("parent", "0") |
| 201 | |
| 202 | # 添加所有文本单元格 |
| 203 | for cell_data in cells: |
| 204 | self._add_text_cell(root, cell_data) |
| 205 | |
| 206 | # 格式化 XML |
| 207 | xml_string = ET.tostring(mxfile, encoding="unicode") |
| 208 | dom = minidom.parseString(xml_string) |
no test coverage detected