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/render/ods.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Render a :class:`~odforge.ir.Spreadsheet` into a native ``.ods`` file via odfdo.
|
|
2
|
+
|
|
3
|
+
Single responsibility: this module only knows how to turn a ``Spreadsheet`` and
|
|
4
|
+
its sheets/rows/formulas into an OpenDocument spreadsheet. Document-type dispatch
|
|
5
|
+
lives in :mod:`odforge.render`.
|
|
6
|
+
|
|
7
|
+
Cell semantics:
|
|
8
|
+
|
|
9
|
+
* Numbers (``int``/``float``) become numeric cells (``office:value-type="float"``
|
|
10
|
+
with an ``office:value``) — never string cells.
|
|
11
|
+
* Strings become text cells.
|
|
12
|
+
* Each :class:`~odforge.ir.FormulaSpec` sets ``table:formula`` verbatim on the
|
|
13
|
+
target cell (the spec guarantees the ``of:=`` OpenFormula prefix); the value
|
|
14
|
+
type is left for LibreOffice to recompute on open.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import re
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
from odfdo import Cell, Document, Row, Style, Table
|
|
23
|
+
|
|
24
|
+
from odforge.ir import FormulaSpec, Sheet, Spreadsheet
|
|
25
|
+
|
|
26
|
+
# ---------------------------------------------------------------------------
|
|
27
|
+
# Fonts / styles (mirrors the .odt renderer's -asian CJK pattern)
|
|
28
|
+
# ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
FONT_NAME = "Noto Sans TC"
|
|
31
|
+
# svg:font-family lists the primary CJK face plus a Windows fallback.
|
|
32
|
+
FONT_FAMILY = '"Noto Sans TC", "微軟正黑體"'
|
|
33
|
+
|
|
34
|
+
_HEADER_STYLE = "ODForge_SheetHeader"
|
|
35
|
+
|
|
36
|
+
# The font-name must be set across Western/Asian/Complex scripts so the CJK
|
|
37
|
+
# face actually applies to Chinese text in LibreOffice.
|
|
38
|
+
_FONT_PROPS = {
|
|
39
|
+
"style:font-name": FONT_NAME,
|
|
40
|
+
"style:font-name-asian": FONT_NAME,
|
|
41
|
+
"style:font-name-complex": FONT_NAME,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# Bold across the same three scripts (Western fo: + Asian/Complex variants).
|
|
45
|
+
_BOLD_PROPS = {
|
|
46
|
+
"fo:font-weight": "bold",
|
|
47
|
+
"style:font-weight-asian": "bold",
|
|
48
|
+
"style:font-weight-complex": "bold",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
# ``!!`` and friends must be rejected; a well-formed ref is column letters
|
|
52
|
+
# followed by a 1-based row number (e.g. "B5", "AA12").
|
|
53
|
+
_CELL_REF_RE = re.compile(r"^[A-Za-z]+[0-9]+$")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _register_styles(doc: Document) -> None:
|
|
57
|
+
"""Insert the font-face declaration and the bold CJK header cell style."""
|
|
58
|
+
font_face = Style(
|
|
59
|
+
"font-face",
|
|
60
|
+
name=FONT_NAME,
|
|
61
|
+
font_name=FONT_NAME,
|
|
62
|
+
font_family=FONT_FAMILY,
|
|
63
|
+
font_family_generic="swiss",
|
|
64
|
+
font_pitch="variable",
|
|
65
|
+
)
|
|
66
|
+
doc.insert_style(font_face)
|
|
67
|
+
doc.insert_style(font_face.clone, default=True)
|
|
68
|
+
|
|
69
|
+
# Header cells: bold + CJK font (family="table-cell" carries text-properties).
|
|
70
|
+
header = Style(
|
|
71
|
+
family="table-cell",
|
|
72
|
+
name=_HEADER_STYLE,
|
|
73
|
+
display_name="ODForge Sheet Header",
|
|
74
|
+
)
|
|
75
|
+
header.set_properties({**_FONT_PROPS, **_BOLD_PROPS}, area="text")
|
|
76
|
+
doc.insert_style(header)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _validate_cell_ref(ref: str) -> None:
|
|
80
|
+
"""Raise ``ValueError`` (with the offending ref) for a malformed cell ref.
|
|
81
|
+
|
|
82
|
+
Accepts column letters + a 1-based row number (single-letter A-Z at minimum,
|
|
83
|
+
multi-letter AA+ also supported). Anything else is out of range.
|
|
84
|
+
"""
|
|
85
|
+
if not _CELL_REF_RE.match(ref):
|
|
86
|
+
raise ValueError(f"invalid cell reference: {ref!r}")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _cell_for_value(value: object) -> Cell:
|
|
90
|
+
"""Build a cell: numeric for int/float (never a string), text otherwise.
|
|
91
|
+
|
|
92
|
+
``None`` becomes an empty cell (no value, no text) that still occupies its
|
|
93
|
+
position, keeping later cells in their correct columns — typical for
|
|
94
|
+
formula-target cells the LLM leaves null. ``bool`` is excluded from the
|
|
95
|
+
numeric path (it is an ``int`` subclass) so a boolean never silently
|
|
96
|
+
becomes a float.
|
|
97
|
+
"""
|
|
98
|
+
if value is None:
|
|
99
|
+
return Cell()
|
|
100
|
+
if isinstance(value, bool):
|
|
101
|
+
return Cell(str(value))
|
|
102
|
+
if isinstance(value, (int, float)):
|
|
103
|
+
return Cell(value, cell_type="float")
|
|
104
|
+
return Cell(str(value))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _build_table(sheet: Sheet) -> Table:
|
|
108
|
+
"""Build one ``table:table`` for a sheet: bold header row then data rows."""
|
|
109
|
+
table = Table(sheet.name)
|
|
110
|
+
|
|
111
|
+
header = Row()
|
|
112
|
+
for column in sheet.columns:
|
|
113
|
+
header.append_cell(Cell(str(column), style=_HEADER_STYLE))
|
|
114
|
+
table.append_row(header)
|
|
115
|
+
|
|
116
|
+
for row in sheet.rows:
|
|
117
|
+
odf_row = Row()
|
|
118
|
+
for value in row:
|
|
119
|
+
odf_row.append_cell(_cell_for_value(value))
|
|
120
|
+
table.append_row(odf_row)
|
|
121
|
+
|
|
122
|
+
_apply_formulas(table, sheet.formulas)
|
|
123
|
+
return table
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _apply_formulas(table: Table, formulas: list[FormulaSpec]) -> None:
|
|
127
|
+
"""Set ``table:formula`` verbatim on each formula's target cell."""
|
|
128
|
+
for spec in formulas:
|
|
129
|
+
_validate_cell_ref(spec.cell)
|
|
130
|
+
cell = table.get_cell(spec.cell)
|
|
131
|
+
cell.formula = spec.formula
|
|
132
|
+
table.set_cell(spec.cell, cell)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def render_ods(s: Spreadsheet, out_path: Path) -> Path:
|
|
136
|
+
"""Render spreadsheet ``s`` to a native ``.ods`` file at ``out_path``."""
|
|
137
|
+
out_path = Path(out_path)
|
|
138
|
+
|
|
139
|
+
document = Document("spreadsheet")
|
|
140
|
+
body = document.body
|
|
141
|
+
# A fresh spreadsheet ships with one empty default table — drop it so the
|
|
142
|
+
# sheet count matches the IR exactly.
|
|
143
|
+
for table in body.get_tables():
|
|
144
|
+
table.delete()
|
|
145
|
+
|
|
146
|
+
_register_styles(document)
|
|
147
|
+
|
|
148
|
+
document.meta.set_title(s.title)
|
|
149
|
+
document.meta.set_generator("ODForge")
|
|
150
|
+
|
|
151
|
+
for sheet in s.sheets:
|
|
152
|
+
body.append(_build_table(sheet))
|
|
153
|
+
|
|
154
|
+
document.save(str(out_path))
|
|
155
|
+
return out_path
|
odforge/render/odt.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""Render a :class:`~odforge.ir.TextDoc` into a native ``.odt`` file via odfdo.
|
|
2
|
+
|
|
3
|
+
Single responsibility: this module only knows how to turn a ``TextDoc`` and its
|
|
4
|
+
blocks into an OpenDocument text document. The document-type dispatch lives in
|
|
5
|
+
:mod:`odforge.render`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Callable
|
|
12
|
+
|
|
13
|
+
from odfdo import TOC, Document, Header, List, ListItem, Paragraph, Style, Table
|
|
14
|
+
|
|
15
|
+
from odforge.ir import (
|
|
16
|
+
HeadingBlock,
|
|
17
|
+
ListBlock,
|
|
18
|
+
PageBreakBlock,
|
|
19
|
+
ParagraphBlock,
|
|
20
|
+
TableBlock,
|
|
21
|
+
TextDoc,
|
|
22
|
+
TocBlock,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
# ---------------------------------------------------------------------------
|
|
26
|
+
# Named styles (deep-blue headings, quote, note, table header, page break)
|
|
27
|
+
# ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
FONT_NAME = "Noto Sans TC"
|
|
30
|
+
# svg:font-family lists the primary CJK face plus a Windows fallback.
|
|
31
|
+
FONT_FAMILY = '"Noto Sans TC", "微軟正黑體"'
|
|
32
|
+
|
|
33
|
+
HEADING_COLOR = "#1A3C6E"
|
|
34
|
+
NOTE_COLOR = "#666666"
|
|
35
|
+
|
|
36
|
+
# level -> (style name, font size)
|
|
37
|
+
_HEADING_STYLES = {
|
|
38
|
+
1: ("ODForge_Heading_1", "18pt"),
|
|
39
|
+
2: ("ODForge_Heading_2", "15pt"),
|
|
40
|
+
3: ("ODForge_Heading_3", "13pt"),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_BODY_STYLE = "ODForge_Body"
|
|
44
|
+
_PARAGRAPH_STYLES = {
|
|
45
|
+
"body": _BODY_STYLE,
|
|
46
|
+
"quote": "ODForge_Quote",
|
|
47
|
+
"note": "ODForge_Note",
|
|
48
|
+
}
|
|
49
|
+
_TABLE_HEADER_STYLE = "ODForge_TableHeader"
|
|
50
|
+
_PAGE_BREAK_STYLE = "ODForge_PageBreak"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# The font-name must be set across Western/Asian/Complex scripts so the CJK
|
|
54
|
+
# face actually applies to Chinese text in LibreOffice.
|
|
55
|
+
_FONT_PROPS = {
|
|
56
|
+
"style:font-name": FONT_NAME,
|
|
57
|
+
"style:font-name-asian": FONT_NAME,
|
|
58
|
+
"style:font-name-complex": FONT_NAME,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _register_styles(doc: Document) -> None:
|
|
63
|
+
"""Insert every named style (and the font-face declaration) into ``doc``."""
|
|
64
|
+
# Font-face declaration: needed both in content.xml (automatic styles) and
|
|
65
|
+
# styles.xml (common styles) so any style referencing the font resolves.
|
|
66
|
+
font_face = Style(
|
|
67
|
+
"font-face",
|
|
68
|
+
name=FONT_NAME,
|
|
69
|
+
font_name=FONT_NAME,
|
|
70
|
+
font_family=FONT_FAMILY,
|
|
71
|
+
font_family_generic="swiss",
|
|
72
|
+
font_pitch="variable",
|
|
73
|
+
)
|
|
74
|
+
doc.insert_style(font_face)
|
|
75
|
+
doc.insert_style(font_face.clone, default=True)
|
|
76
|
+
|
|
77
|
+
# Body paragraph style: sets the CJK font for ordinary text.
|
|
78
|
+
body = Style(family="paragraph", name=_BODY_STYLE, display_name="ODForge Body")
|
|
79
|
+
body.set_properties(dict(_FONT_PROPS), area="text")
|
|
80
|
+
doc.insert_style(body)
|
|
81
|
+
|
|
82
|
+
# Heading 1-3: deep blue, bold, decreasing size, CJK font.
|
|
83
|
+
for _level, (name, size) in _HEADING_STYLES.items():
|
|
84
|
+
heading = Style(
|
|
85
|
+
family="paragraph",
|
|
86
|
+
name=name,
|
|
87
|
+
display_name=name.replace("_", " "),
|
|
88
|
+
parent_style="Heading",
|
|
89
|
+
area="text",
|
|
90
|
+
bold=True,
|
|
91
|
+
color=HEADING_COLOR,
|
|
92
|
+
)
|
|
93
|
+
heading.set_properties(
|
|
94
|
+
{**_FONT_PROPS, "fo:font-size": size}, area="text"
|
|
95
|
+
)
|
|
96
|
+
doc.insert_style(heading)
|
|
97
|
+
|
|
98
|
+
# Quote: 1cm left indent + italic.
|
|
99
|
+
quote = Style(
|
|
100
|
+
family="paragraph",
|
|
101
|
+
name=_PARAGRAPH_STYLES["quote"],
|
|
102
|
+
display_name="ODForge Quote",
|
|
103
|
+
area="text",
|
|
104
|
+
italic=True,
|
|
105
|
+
)
|
|
106
|
+
quote.set_properties(dict(_FONT_PROPS), area="text")
|
|
107
|
+
quote.set_properties({"fo:margin-left": "1cm"})
|
|
108
|
+
doc.insert_style(quote)
|
|
109
|
+
|
|
110
|
+
# Note: grey text.
|
|
111
|
+
note = Style(
|
|
112
|
+
family="paragraph",
|
|
113
|
+
name=_PARAGRAPH_STYLES["note"],
|
|
114
|
+
display_name="ODForge Note",
|
|
115
|
+
area="text",
|
|
116
|
+
color=NOTE_COLOR,
|
|
117
|
+
)
|
|
118
|
+
note.set_properties(dict(_FONT_PROPS), area="text")
|
|
119
|
+
doc.insert_style(note)
|
|
120
|
+
|
|
121
|
+
# Table header: bold cells.
|
|
122
|
+
table_header = Style(
|
|
123
|
+
family="paragraph",
|
|
124
|
+
name=_TABLE_HEADER_STYLE,
|
|
125
|
+
display_name="ODForge Table Header",
|
|
126
|
+
area="text",
|
|
127
|
+
bold=True,
|
|
128
|
+
)
|
|
129
|
+
table_header.set_properties(dict(_FONT_PROPS), area="text")
|
|
130
|
+
doc.insert_style(table_header)
|
|
131
|
+
|
|
132
|
+
# Numbered / bullet list styles (distinguish ordered vs unordered lists).
|
|
133
|
+
numbered = Style(family="list", name="ODForge_Numbered")
|
|
134
|
+
numbered.set_level_style(1, num_format="1", suffix=".")
|
|
135
|
+
doc.insert_style(numbered)
|
|
136
|
+
|
|
137
|
+
bulleted = Style(family="list", name="ODForge_Bulleted")
|
|
138
|
+
bulleted.set_level_style(1, bullet_char="•")
|
|
139
|
+
doc.insert_style(bulleted)
|
|
140
|
+
|
|
141
|
+
# Page break: empty paragraph carrying fo:break-before="page".
|
|
142
|
+
page_break = Style(family="paragraph", name=_PAGE_BREAK_STYLE)
|
|
143
|
+
page_break.set_properties({"fo:break-before": "page"})
|
|
144
|
+
doc.insert_style(page_break)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ---------------------------------------------------------------------------
|
|
148
|
+
# Block handlers (dict dispatch on block.kind — no if/elif chain)
|
|
149
|
+
# ---------------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _render_heading(body, block: HeadingBlock) -> None:
|
|
153
|
+
style_name, _ = _HEADING_STYLES.get(block.level, _HEADING_STYLES[3])
|
|
154
|
+
body.append(Header(block.level, block.text, style=style_name))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _render_paragraph(body, block: ParagraphBlock) -> None:
|
|
158
|
+
style_name = _PARAGRAPH_STYLES.get(block.style, _BODY_STYLE)
|
|
159
|
+
body.append(Paragraph(block.text, style=style_name))
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _render_list(body, block: ListBlock) -> None:
|
|
163
|
+
style_name = "ODForge_Numbered" if block.ordered else "ODForge_Bulleted"
|
|
164
|
+
odf_list = List(style=style_name)
|
|
165
|
+
for item in block.items:
|
|
166
|
+
odf_list.append(ListItem(item))
|
|
167
|
+
body.append(odf_list)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _render_table(body, block: TableBlock) -> None:
|
|
171
|
+
table = Table("Table")
|
|
172
|
+
table.set_values([block.header, *block.rows])
|
|
173
|
+
# Bold the header row: style the paragraph inside each header cell.
|
|
174
|
+
header_row = table.get_row(0)
|
|
175
|
+
for cell in header_row.get_cells():
|
|
176
|
+
for paragraph in cell.get_elements("text:p"):
|
|
177
|
+
paragraph.style = _TABLE_HEADER_STYLE
|
|
178
|
+
table.set_row(0, header_row)
|
|
179
|
+
body.append(table)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _render_toc(body, block: TocBlock) -> None:
|
|
183
|
+
body.append(TOC())
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _render_pagebreak(body, block: PageBreakBlock) -> None:
|
|
187
|
+
body.append(Paragraph("", style=_PAGE_BREAK_STYLE))
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
_HANDLERS: dict[str, Callable] = {
|
|
191
|
+
"heading": _render_heading,
|
|
192
|
+
"paragraph": _render_paragraph,
|
|
193
|
+
"list": _render_list,
|
|
194
|
+
"table": _render_table,
|
|
195
|
+
"toc": _render_toc,
|
|
196
|
+
"pagebreak": _render_pagebreak,
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# ---------------------------------------------------------------------------
|
|
201
|
+
# Entry point
|
|
202
|
+
# ---------------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def render_odt(doc: TextDoc, out_path: Path) -> Path:
|
|
206
|
+
"""Render ``doc`` to a native ``.odt`` file at ``out_path`` and return it."""
|
|
207
|
+
out_path = Path(out_path)
|
|
208
|
+
|
|
209
|
+
document = Document("text")
|
|
210
|
+
body = document.body
|
|
211
|
+
body.clear()
|
|
212
|
+
|
|
213
|
+
_register_styles(document)
|
|
214
|
+
|
|
215
|
+
document.meta.set_title(doc.title)
|
|
216
|
+
document.meta.set_generator("ODForge")
|
|
217
|
+
document.language = doc.lang
|
|
218
|
+
|
|
219
|
+
for block in doc.blocks:
|
|
220
|
+
_HANDLERS[block.kind](body, block)
|
|
221
|
+
|
|
222
|
+
document.save(str(out_path))
|
|
223
|
+
return out_path
|