Repository navigation
Expand file tree
/
Copy pathxmind_parser.py
More file actions
135 lines (119 loc) · 4.84 KB
/
Copy pathxmind_parser.py
File metadata and controls
135 lines (119 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import json
import os
import tempfile
import uuid
import zipfile
from pathlib import Path
from models import Node, Style
def _normalize_color(c: str | None) -> str | None:
"""Normalize color to 6-char hex (#RRGGBB), stripping alpha if present."""
if not c:
return None
c = c.strip()
if c.startswith("#") and len(c) == 9: # #RRGGBBAA
return c[:7]
return c
def _xmind_color(c: str | None) -> str | None:
"""Convert 6-char hex to XMind 8-char hex (#RRGGBBFF)."""
if not c:
return None
c = c.strip()
if c.startswith("#") and len(c) == 7:
return c + "FF"
return c
_SHAPE_PREFIX = "org.xmind.topicShape."
_BRANCH_PREFIX = "org.xmind.branchConnection."
def _xmind_node_to_node(x: dict) -> Node:
style = None
if "style" in x:
props = x["style"].get("properties", {})
raw_shape = props.get("shape-class", "")
raw_branch_style = props.get("line-class", "")
fw = props.get("fo:font-weight", "")
td = props.get("fo:text-decoration", "")
style = Style(
bg=_normalize_color(props.get("svg:fill")),
fg=_normalize_color(props.get("fo:color")),
branch=_normalize_color(props.get("line-color")),
shape=raw_shape[len(_SHAPE_PREFIX):] if raw_shape.startswith(_SHAPE_PREFIX) else (raw_shape or None),
branch_style=raw_branch_style[len(_BRANCH_PREFIX):] if raw_branch_style.startswith(_BRANCH_PREFIX) else (raw_branch_style or None),
branch_pattern=props.get("line-pattern") or None,
bold=True if fw in ("700", "bold") else (False if fw in ("400", "normal") else None),
italic=True if props.get("fo:font-style") == "italic" else None,
strikethrough=True if "line-through" in td else None,
font_size=props.get("fo:font-size") or None,
border_color=_normalize_color(props.get("border-line-color")),
border_width=props.get("border-line-width") or None,
border_pattern=props.get("border-line-pattern") or None,
)
if not any(v is not None for v in style.__dict__.values()):
style = None
children_raw = x.get("children", {}).get("attached", [])
return Node(
id=x["id"],
title=x["title"],
style=style,
children=[_xmind_node_to_node(c) for c in children_raw],
)
def read_xmind(path: Path | str) -> Node:
with zipfile.ZipFile(path, "r") as zf:
content = json.loads(zf.read("content.json"))
return _xmind_node_to_node(content[0]["rootTopic"])
def _node_to_xmind(node: Node) -> dict:
x = {"id": node.id, "title": node.title, "children": {"attached": []}}
if node.style:
props = {}
s = node.style
if s.bg:
props["svg:fill"] = _xmind_color(s.bg)
if s.fg:
props["fo:color"] = _xmind_color(s.fg)
if s.branch:
props["line-color"] = _xmind_color(s.branch)
if s.shape:
props["shape-class"] = _SHAPE_PREFIX + s.shape
if s.branch_style:
props["line-class"] = _BRANCH_PREFIX + s.branch_style
if s.branch_pattern:
props["line-pattern"] = s.branch_pattern
if s.bold is not None:
props["fo:font-weight"] = "700" if s.bold else "400"
if s.italic:
props["fo:font-style"] = "italic"
if s.strikethrough:
props["fo:text-decoration"] = "line-through"
if s.font_size:
props["fo:font-size"] = s.font_size
if s.border_color:
props["border-line-color"] = _xmind_color(s.border_color)
if s.border_width:
props["border-line-width"] = s.border_width
if s.border_pattern:
props["border-line-pattern"] = s.border_pattern
if props:
x["style"] = {"id": str(uuid.uuid4()), "properties": props}
x["children"]["attached"] = [_node_to_xmind(c) for c in node.children]
return x
_METADATA = json.dumps({
"dataStructureVersion": "3",
"creator": {"name": "xmind-mcp", "version": "1.0.0"},
"layoutEngineVersion": "5",
}, ensure_ascii=False)
def write_xmind(path: Path | str, root: Node) -> None:
path = Path(path)
content = [{"id": "sheet-1", "rootTopic": _node_to_xmind(root)}]
manifest = json.dumps(
{"file-entries": {"content.json": {}, "metadata.json": {}}},
ensure_ascii=False,
)
tmp_fd, tmp_path = tempfile.mkstemp(dir=path.parent, suffix=".xmind.tmp")
try:
with os.fdopen(tmp_fd, "wb") as f:
with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zf:
zf.writestr("content.json", json.dumps(content, ensure_ascii=False))
zf.writestr("metadata.json", _METADATA)
zf.writestr("manifest.json", manifest)
os.replace(tmp_path, path)
except Exception:
os.unlink(tmp_path)
raise