odforge 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- odforge/__init__.py +1 -0
- odforge/benchmark.py +287 -0
- odforge/check.py +416 -0
- odforge/cli.py +1027 -0
- odforge/components.py +270 -0
- odforge/critic.py +1260 -0
- odforge/docfonts.py +104 -0
- odforge/editor_api.py +33 -0
- odforge/editor_launch.py +134 -0
- odforge/editor_routes.py +131 -0
- odforge/extract.py +478 -0
- odforge/feedback.py +86 -0
- odforge/fontembed.py +166 -0
- odforge/ir.py +1181 -0
- odforge/layoutcheck.py +602 -0
- odforge/llm.py +1865 -0
- odforge/localguard.py +42 -0
- odforge/mcp_server.py +552 -0
- odforge/media.py +352 -0
- odforge/odpedit.py +1219 -0
- odforge/package.py +114 -0
- odforge/pagefacts.py +388 -0
- odforge/preview.py +104 -0
- odforge/references.py +153 -0
- odforge/render/__init__.py +40 -0
- odforge/render/odp.py +5687 -0
- odforge/render/ods.py +155 -0
- odforge/render/odt.py +223 -0
- odforge/scene.py +657 -0
- odforge/setup_agents.py +274 -0
- odforge/skills/odforge-design/SKILL.md +298 -0
- odforge/skills/odforge-design/themes/academic.md +52 -0
- odforge/skills/odforge-design/themes/clay.md +53 -0
- odforge/skills/odforge-design/themes/color-system.md +59 -0
- odforge/skills/odforge-design/themes/crimson.md +53 -0
- odforge/skills/odforge-design/themes/dark.md +53 -0
- odforge/skills/odforge-design/themes/forest.md +54 -0
- odforge/skills/odforge-design/themes/gold.md +53 -0
- odforge/skills/odforge-design/themes/minimal.md +53 -0
- odforge/skills/odforge-design/themes/navy.md +56 -0
- odforge/skills/odforge-design/themes/plum.md +53 -0
- odforge/skills/odforge-design/themes/sky.md +53 -0
- odforge/skills/odforge-design/themes/slate.md +53 -0
- odforge/skills/odforge-design/themes/teal.md +53 -0
- odforge/skills/odforge-design/themes/violet.md +56 -0
- odforge/templates.py +250 -0
- odforge/textmetrics.py +616 -0
- odforge/textutil.py +24 -0
- odforge/themes.py +886 -0
- odforge/validate.py +403 -0
- odforge/watch.py +266 -0
- odforge/webapi.py +2617 -0
- odforge/webui/assets/editor-CjpYXM1a.css +1 -0
- odforge/webui/assets/editor-DPToJGio.js +3 -0
- odforge/webui/assets/jsx-runtime-C7KMcVI_.js +8 -0
- odforge/webui/assets/main-BLOs7Hnk.js +7 -0
- odforge/webui/assets/main-D21yFVE6.css +1 -0
- odforge/webui/editor.html +14 -0
- odforge/webui/index.html +25 -0
- odforge/xmlsafe.py +46 -0
- odforge/zipguard.py +91 -0
- odforge-0.3.0.dist-info/METADATA +95 -0
- odforge-0.3.0.dist-info/RECORD +66 -0
- odforge-0.3.0.dist-info/WHEEL +4 -0
- odforge-0.3.0.dist-info/entry_points.txt +3 -0
- odforge-0.3.0.dist-info/licenses/LICENSE +21 -0
odforge/components.py
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""Ready-made slide parts for the editor's 元素 panel, styled like the deck they go into.
|
|
2
|
+
|
|
3
|
+
ODForge decks are built from a small vocabulary — cards, numbered steps, big
|
|
4
|
+
metrics, quote and takeaway bands, kickers, timeline nodes. A person editing a
|
|
5
|
+
deck by hand should be able to add one that looks like the ones already there,
|
|
6
|
+
so each part takes its colours and fonts from the document itself
|
|
7
|
+
(:func:`infer_theme`), and reuses the deck's own card style when it has one.
|
|
8
|
+
|
|
9
|
+
Every part is a list of plain element specs; :mod:`odforge.odpedit` turns them
|
|
10
|
+
into ODF inside a ``draw:g`` so the part moves as one.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import colorsys
|
|
16
|
+
from collections import Counter
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from typing import Any, Dict, List, Optional
|
|
19
|
+
|
|
20
|
+
from lxml import etree
|
|
21
|
+
|
|
22
|
+
from .scene import NS, Styles, cm, drawables, q
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class Theme:
|
|
27
|
+
ink: str
|
|
28
|
+
muted: str
|
|
29
|
+
accent: str
|
|
30
|
+
display_font: Optional[str]
|
|
31
|
+
body_font: Optional[str]
|
|
32
|
+
card_style: Optional[str]
|
|
33
|
+
card_radius: float
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _rgb(color: str):
|
|
37
|
+
color = color.lstrip("#")
|
|
38
|
+
if len(color) != 6:
|
|
39
|
+
return None
|
|
40
|
+
try:
|
|
41
|
+
return tuple(int(color[i : i + 2], 16) / 255 for i in (0, 2, 4))
|
|
42
|
+
except ValueError:
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _hls(color: str):
|
|
47
|
+
rgb = _rgb(color)
|
|
48
|
+
return colorsys.rgb_to_hls(*rgb) if rgb else None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def infer_theme(content: etree._Element, styles_root: etree._Element) -> Theme:
|
|
52
|
+
styles = Styles((content, styles_root))
|
|
53
|
+
text_colors: Counter[str] = Counter()
|
|
54
|
+
fills: Counter[str] = Counter()
|
|
55
|
+
sizes: Dict[str, float] = {}
|
|
56
|
+
fonts: Counter[str] = Counter()
|
|
57
|
+
for root in (content, styles_root):
|
|
58
|
+
for props in root.iter(q("style", "text-properties")):
|
|
59
|
+
color = props.get(q("fo", "color"))
|
|
60
|
+
if color:
|
|
61
|
+
text_colors[color.upper()] += 1
|
|
62
|
+
font = props.get(q("style", "font-name"))
|
|
63
|
+
if font:
|
|
64
|
+
fonts[font] += 1
|
|
65
|
+
size = props.get(q("fo", "font-size"), "")
|
|
66
|
+
if size.endswith("pt"):
|
|
67
|
+
sizes[font] = max(sizes.get(font, 0), float(size[:-2]))
|
|
68
|
+
for props in root.iter(q("style", "graphic-properties")):
|
|
69
|
+
if props.get(q("draw", "fill")) == "solid" and props.get(q("draw", "fill-color")):
|
|
70
|
+
fills[props.get(q("draw", "fill-color")).upper()] += 1
|
|
71
|
+
|
|
72
|
+
def lightness(c: str) -> float:
|
|
73
|
+
hls = _hls(c)
|
|
74
|
+
return hls[1] if hls else 0.5
|
|
75
|
+
|
|
76
|
+
def saturation(c: str) -> float:
|
|
77
|
+
hls = _hls(c)
|
|
78
|
+
return hls[2] if hls else 0.0
|
|
79
|
+
|
|
80
|
+
darks = [c for c, _ in text_colors.most_common() if lightness(c) < 0.3]
|
|
81
|
+
mids = [
|
|
82
|
+
c
|
|
83
|
+
for c, _ in text_colors.most_common()
|
|
84
|
+
if 0.3 <= lightness(c) < 0.65 and saturation(c) < 0.3
|
|
85
|
+
]
|
|
86
|
+
chroma = [
|
|
87
|
+
c
|
|
88
|
+
for c, _ in (text_colors + fills).most_common()
|
|
89
|
+
if saturation(c) >= 0.3 and 0.2 < lightness(c) < 0.75
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
display = max(sizes, key=sizes.get) if sizes else None
|
|
93
|
+
body = fonts.most_common(1)[0][0] if fonts else None
|
|
94
|
+
|
|
95
|
+
card_style, radius = None, 0.12
|
|
96
|
+
for page in content.iter(q("draw", "page")):
|
|
97
|
+
for el in drawables(page):
|
|
98
|
+
if el.tag != q("draw", "rect"):
|
|
99
|
+
continue
|
|
100
|
+
if cm(el.get(q("svg", "width"))) < 3 or cm(el.get(q("svg", "height"))) < 2:
|
|
101
|
+
continue
|
|
102
|
+
name = el.get(q("draw", "style-name"))
|
|
103
|
+
props = styles.props("graphic", name, "graphic-properties")
|
|
104
|
+
if props.get("draw:stroke", "none") != "none":
|
|
105
|
+
card_style = name
|
|
106
|
+
radius = cm(el.get(q("draw", "corner-radius")), 0.0)
|
|
107
|
+
break
|
|
108
|
+
if card_style:
|
|
109
|
+
break
|
|
110
|
+
|
|
111
|
+
return Theme(
|
|
112
|
+
ink=darks[0] if darks else "#1F2733",
|
|
113
|
+
muted=mids[0] if mids else "#5B6470",
|
|
114
|
+
accent=chroma[0] if chroma else "#1F3F99",
|
|
115
|
+
display_font=display,
|
|
116
|
+
body_font=body,
|
|
117
|
+
card_style=card_style,
|
|
118
|
+
card_radius=radius,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# A spec: {"tag": "rect"|"ellipse"|"line"|"text", box..., "fill"/"stroke"/"text"/"style"...}
|
|
123
|
+
Spec = Dict[str, Any]
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _text(
|
|
127
|
+
x,
|
|
128
|
+
y,
|
|
129
|
+
w,
|
|
130
|
+
h,
|
|
131
|
+
text,
|
|
132
|
+
size,
|
|
133
|
+
color,
|
|
134
|
+
*,
|
|
135
|
+
weight=400,
|
|
136
|
+
font=None,
|
|
137
|
+
align="left",
|
|
138
|
+
italic=False,
|
|
139
|
+
spacing=None,
|
|
140
|
+
valign="top",
|
|
141
|
+
) -> Spec:
|
|
142
|
+
return {
|
|
143
|
+
"tag": "text",
|
|
144
|
+
"x": x,
|
|
145
|
+
"y": y,
|
|
146
|
+
"w": w,
|
|
147
|
+
"h": h,
|
|
148
|
+
"text": text,
|
|
149
|
+
"size": size,
|
|
150
|
+
"color": color,
|
|
151
|
+
"weight": weight,
|
|
152
|
+
"font": font,
|
|
153
|
+
"align": align,
|
|
154
|
+
"italic": italic,
|
|
155
|
+
"spacing": spacing,
|
|
156
|
+
"valign": valign,
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def build(kind: str, x: float, y: float, t: Theme) -> List[Spec]:
|
|
161
|
+
"""The element specs of one part, top-left at (x, y)."""
|
|
162
|
+
display, body = t.display_font or t.body_font, t.body_font
|
|
163
|
+
if kind == "heading":
|
|
164
|
+
return [_text(x, y, 16, 1.4, "標題", 28, t.ink, weight=700, font=display)]
|
|
165
|
+
if kind == "body":
|
|
166
|
+
return [_text(x, y, 12, 1.2, "內文,雙擊即可修改。", 16, t.ink, font=body)]
|
|
167
|
+
if kind == "kicker":
|
|
168
|
+
return [
|
|
169
|
+
_text(x, y, 8, 0.7, "01 章節標籤", 13, t.accent, weight=700, font=body, spacing=0.08)
|
|
170
|
+
]
|
|
171
|
+
if kind == "card":
|
|
172
|
+
card = {"tag": "rect", "x": x, "y": y, "w": 6, "h": 4.4, "radius": t.card_radius}
|
|
173
|
+
if t.card_style:
|
|
174
|
+
card["style"] = t.card_style
|
|
175
|
+
else:
|
|
176
|
+
card.update(fill="#FFFFFF", stroke="#C9CDD6")
|
|
177
|
+
return [
|
|
178
|
+
card,
|
|
179
|
+
_text(x + 0.35, y + 0.3, 5.3, 0.6, "標籤", 13, t.accent, weight=700, font=body),
|
|
180
|
+
_text(x + 0.35, y + 1.0, 5.3, 1.0, "卡片標題", 17, t.ink, weight=700, font=display),
|
|
181
|
+
_text(x + 0.35, y + 2.1, 5.3, 1.9, "說明文字,雙擊即可修改。", 12, t.muted, font=body),
|
|
182
|
+
]
|
|
183
|
+
if kind == "step":
|
|
184
|
+
return [
|
|
185
|
+
{
|
|
186
|
+
"tag": "ellipse",
|
|
187
|
+
"x": x,
|
|
188
|
+
"y": y,
|
|
189
|
+
"w": 0.9,
|
|
190
|
+
"h": 0.9,
|
|
191
|
+
"fill": t.accent,
|
|
192
|
+
"label": _text(
|
|
193
|
+
0, 0, 0, 0, "1", 14, "#FFFFFF", weight=700, font=body, align="center"
|
|
194
|
+
),
|
|
195
|
+
},
|
|
196
|
+
_text(
|
|
197
|
+
x + 1.2, y, 6, 0.9, "步驟標題", 16, t.ink, weight=700, font=display, valign="middle"
|
|
198
|
+
),
|
|
199
|
+
_text(x + 1.2, y + 1.0, 6, 1.2, "這一步要做什麼。", 12, t.muted, font=body),
|
|
200
|
+
]
|
|
201
|
+
if kind == "metric":
|
|
202
|
+
return [
|
|
203
|
+
_text(x, y, 6, 1.8, "87%", 40, t.accent, weight=700, font=display),
|
|
204
|
+
_text(x, y + 1.9, 6, 0.8, "指標說明", 12, t.muted, font=body),
|
|
205
|
+
]
|
|
206
|
+
if kind == "quote":
|
|
207
|
+
return [
|
|
208
|
+
{"tag": "rect", "x": x, "y": y, "w": 0.15, "h": 2.2, "fill": t.accent},
|
|
209
|
+
_text(
|
|
210
|
+
x + 0.45, y, 11, 1.5, "「引述的一句話。」", 20, t.ink, font=display, italic=False
|
|
211
|
+
),
|
|
212
|
+
_text(x + 0.45, y + 1.6, 11, 0.6, "— 出處", 12, t.muted, font=body),
|
|
213
|
+
]
|
|
214
|
+
if kind == "takeaway":
|
|
215
|
+
return [
|
|
216
|
+
{
|
|
217
|
+
"tag": "rect",
|
|
218
|
+
"x": x,
|
|
219
|
+
"y": y,
|
|
220
|
+
"w": 20,
|
|
221
|
+
"h": 1.35,
|
|
222
|
+
"fill": "#FFFFFF",
|
|
223
|
+
"stroke": t.accent,
|
|
224
|
+
"radius": t.card_radius,
|
|
225
|
+
},
|
|
226
|
+
{"tag": "rect", "x": x, "y": y, "w": 0.2, "h": 1.35, "fill": t.accent},
|
|
227
|
+
_text(
|
|
228
|
+
x + 0.45,
|
|
229
|
+
y,
|
|
230
|
+
19.3,
|
|
231
|
+
1.35,
|
|
232
|
+
"這一頁的結論。",
|
|
233
|
+
18,
|
|
234
|
+
t.ink,
|
|
235
|
+
weight=700,
|
|
236
|
+
font=display,
|
|
237
|
+
valign="middle",
|
|
238
|
+
),
|
|
239
|
+
]
|
|
240
|
+
if kind == "timeline":
|
|
241
|
+
return [
|
|
242
|
+
{"tag": "ellipse", "x": x, "y": y + 0.1, "w": 0.5, "h": 0.5, "fill": t.accent},
|
|
243
|
+
_text(x + 0.8, y, 5, 0.7, "2026 年", 13, t.accent, weight=700, font=body),
|
|
244
|
+
_text(x + 0.8, y + 0.75, 5, 1.0, "事件說明", 12, t.muted, font=body),
|
|
245
|
+
]
|
|
246
|
+
if kind == "divider":
|
|
247
|
+
return [{"tag": "line", "x1": x, "y1": y, "x2": x + 10, "y2": y, "stroke": "#A3A7AB"}]
|
|
248
|
+
if kind == "ellipse":
|
|
249
|
+
return [{"tag": "ellipse", "x": x, "y": y, "w": 3, "h": 3, "fill": t.accent}]
|
|
250
|
+
if kind == "rect":
|
|
251
|
+
return [{"tag": "rect", "x": x, "y": y, "w": 6, "h": 3.5, "fill": "#E8ECF4"}]
|
|
252
|
+
raise ValueError(kind)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
KINDS = (
|
|
256
|
+
"heading",
|
|
257
|
+
"body",
|
|
258
|
+
"kicker",
|
|
259
|
+
"card",
|
|
260
|
+
"step",
|
|
261
|
+
"metric",
|
|
262
|
+
"quote",
|
|
263
|
+
"takeaway",
|
|
264
|
+
"timeline",
|
|
265
|
+
"divider",
|
|
266
|
+
"rect",
|
|
267
|
+
"ellipse",
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
__all__ = ["KINDS", "NS", "Theme", "build", "infer_theme"]
|