powertom 0.4.1 → 0.4.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +100 -0
- data/README.md +89 -17
- data/lib/powertom/adapter/ruby_pptx_adapter.rb +115 -116
- data/lib/powertom/data_loading.rb +2 -1
- data/lib/powertom/render.rb +22 -3
- data/lib/powertom/tasks.rb +24 -0
- data/lib/powertom/validator.rb +41 -2
- data/lib/powertom/version.rb +3 -2
- data/lib/powertom.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6bd2e39f7b105bf4424506b980efb7829a78f5c9a9c5bc7dc028b6d8329da59d
|
|
4
|
+
data.tar.gz: '080c9e508b021486c01c53137ccb93e91abc00c619f70524c77f3697bd01ee67'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 73adacddfa6123ce1681d1cf62cfb8229e9b17d1312f693b54e457541e350ec5789a8c830d387725cb8872bb156ad1a288a73c629aa0021cf09307d8ec323a98
|
|
7
|
+
data.tar.gz: f3429d5adf7f58f761573bc44a88f95b5a2175fa8282211f322ff9c1c8428750b8dd4bb60a59eafe8b0053aa3fb4e449d84b99ff2393506746925bf9fe265882
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,75 @@ All notable changes to this project are documented here.
|
|
|
4
4
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
5
5
|
and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.4.2] - 2026-11-18
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- `lib/powertom/tasks.rb`: the documented `powertom:check` Rake task now ships
|
|
14
|
+
with the gem. Add `require "powertom/tasks"` to a project's Rakefile once and
|
|
15
|
+
`bundle exec rake powertom:check TEMPLATE=... DATA=...` works after
|
|
16
|
+
`gem install` / `bundle install`; the task's usage messages are English, like
|
|
17
|
+
every other user-facing message.
|
|
18
|
+
- README FAQ covering the first dogfooding round: the `#each` page context is
|
|
19
|
+
the current item (`{{headline}}`, not `{{scenario.headline}}` — and there is
|
|
20
|
+
no root fallback), how to load the Rake task, and how to pass JSON/YAML as a
|
|
21
|
+
parsed Hash. A warning box next to the syntax table makes the scope rule hard
|
|
22
|
+
to miss.
|
|
23
|
+
- L3 official validation layer: a minimal .NET tool
|
|
24
|
+
(`tools/openxml_validator`, OpenXmlValidator / DocumentFormat.OpenXml)
|
|
25
|
+
plus an `L3 OpenXML validation` workflow — ubuntu renders the L3 deck set
|
|
26
|
+
(`tools/build_l3_decks.rb`: every supported chart family, `#each`-cloned
|
|
27
|
+
combo pages, the mini golden render), a windows-latest job runs the
|
|
28
|
+
validator against the decks and the committed mini template. First run
|
|
29
|
+
surfaced two upstream writer defects (negative axis ids, illegal radar
|
|
30
|
+
`c:smooth` — both fixed below); the rerun validates all four decks clean.
|
|
31
|
+
The workflow is a **hard gate**: any schema-level finding fails the build;
|
|
32
|
+
a daily schedule guards against silent upstream drift.
|
|
33
|
+
- Picture placeholders support `; mode=contain`, which scales the image to fit
|
|
34
|
+
inside the frame and centres it without cropping. `fill` remains the default.
|
|
35
|
+
Margins show whatever is behind the picture; no background fill is added.
|
|
36
|
+
- `PowerTom.check` reports tags on slide masters/layouts as errors. They never
|
|
37
|
+
take effect at render time, so the public validation path no longer leaves
|
|
38
|
+
them silent.
|
|
39
|
+
|
|
40
|
+
### Changed
|
|
41
|
+
|
|
42
|
+
- Scatter and bubble binding now rides upstream `Chart#replace_data`:
|
|
43
|
+
ruby_pptx 0.3.0 added `XyChartData` / `BubbleChartData` support (it rewrites
|
|
44
|
+
each series' `c:tx`/`c:xVal`/`c:yVal`/`c:bubbleSize` in schema order and
|
|
45
|
+
keeps the formatting) and raises `Pptx::Error` for a data kind the chart
|
|
46
|
+
cannot hold. The adapter's own XY rewriter is gone; the regenerated workbook
|
|
47
|
+
still receives the theme minor font.
|
|
48
|
+
- The `ruby_pptx` floor is now `>= 0.3.0` (was `0.2.0`). On 0.2.x the binding
|
|
49
|
+
layer could only write category-chart data into scatter/bubble series, which
|
|
50
|
+
produces a chart PowerPoint has to repair.
|
|
51
|
+
- Releases are published with `rubygems/release-gem` instead of the manual
|
|
52
|
+
`configure-rubygems-credentials` + `gem push` pair. The action runs
|
|
53
|
+
`rake release` and signs the gem with Sigstore, so RubyGems shows
|
|
54
|
+
"Provenance: Built and signed on GitHub Actions" for each version, linking
|
|
55
|
+
the commit and workflow that built it. The Rakefile now loads
|
|
56
|
+
`bundler/gem_tasks`.
|
|
57
|
+
- Documented the JSON/YAML adapter pattern in `docs/usage.md`; no built-in
|
|
58
|
+
loader is added. The existing `.toml`-only error no longer says "v0" and
|
|
59
|
+
points to the Hash adapter.
|
|
60
|
+
|
|
61
|
+
### Fixed
|
|
62
|
+
|
|
63
|
+
- `powertom:check` existed only in the repository's `rakelib/`, which the
|
|
64
|
+
gemspec never packaged, so the checker command in the README failed with
|
|
65
|
+
"Don't know how to build task" for installed users. The task moved to
|
|
66
|
+
`lib/powertom/tasks.rb` (shipped by `Dir["lib/**/*.rb"]`); `powertom:lint`
|
|
67
|
+
and `fixtures:*` stay repository-only.
|
|
68
|
+
- `docs/usage.md` said `#each` labels resolve "against the current item first",
|
|
69
|
+
implying a fallback to the root data. There is no fallback; the wording now
|
|
70
|
+
says so.
|
|
71
|
+
- Pie chart binding now rejects data with more than one series. PowerPoint
|
|
72
|
+
draws only the first pie series, so accepting extra series silently lost
|
|
73
|
+
data; `PowerTom.check` and `render` both report a located error instead.
|
|
74
|
+
Doughnut charts still accept multiple series as concentric rings.
|
|
75
|
+
|
|
7
76
|
## [0.4.1] - 2026-11-17
|
|
8
77
|
|
|
9
78
|
### Added
|
|
@@ -31,6 +100,15 @@ and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
|
|
|
31
100
|
|
|
32
101
|
### Fixed
|
|
33
102
|
|
|
103
|
+
- Chart schema normalization on save (L3 first-run findings): upstream chart
|
|
104
|
+
writers use negative axis id constants (`VAL_AX_ID = "-2112772216"`) where
|
|
105
|
+
OOXML requires unsigned ints, and `RadarChartWriter` writes a `c:smooth`
|
|
106
|
+
into radar series although that element does not exist in the radar ser
|
|
107
|
+
schema. LibreOffice and PowerPoint tolerate both, but `OpenXmlValidator`
|
|
108
|
+
rejects them (6 findings per axis chart, 2 per radar series). Saving now
|
|
109
|
+
normalizes every chart in the package: negative `c:axId`/`c:crossAx`
|
|
110
|
+
values are converted to their absolute value (references stay self-consistent, and within the int32 bound) and illegal
|
|
111
|
+
radar `c:smooth` nodes are removed.
|
|
34
112
|
- Chart binding font consistency: the regenerated embedded workbook had no
|
|
35
113
|
`xl/styles.xml` (except for date categories, where the base font was
|
|
36
114
|
hardcoded to Calibri), so PowerPoint's "Edit Data" fell back to the local
|
|
@@ -88,6 +166,28 @@ and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
|
|
|
88
166
|
- The mini golden sample now includes a chart page (categories 3→2
|
|
89
167
|
reduction, two series) exercised in the public CI fidelity job.
|
|
90
168
|
|
|
169
|
+
## [0.3.2] - 2026-11-17
|
|
170
|
+
|
|
171
|
+
> Bugfix-only release cut from `release/v0.3.2`; main carries the same fixes
|
|
172
|
+
> inside 0.4.0.
|
|
173
|
+
|
|
174
|
+
### Added
|
|
175
|
+
|
|
176
|
+
- OOXML structural lint (`tools/ooxml_lint.rb`, L1): detects dangling
|
|
177
|
+
`[Content_Types].xml` overrides, dangling relationship targets, duplicate
|
|
178
|
+
zip entries and broken `sldId` references — the defects PowerPoint repairs
|
|
179
|
+
on open while LibreOffice silently tolerates them. CLI exits non-zero on
|
|
180
|
+
findings; the mini golden and page-surgery specs assert their outputs lint
|
|
181
|
+
clean, so the structural layer runs in CI.
|
|
182
|
+
|
|
183
|
+
### Fixed
|
|
184
|
+
|
|
185
|
+
- `PageSurgery` no longer declares a `notesSlide` content-type override for
|
|
186
|
+
output pages whose source page has no notes. The surgery output is now a
|
|
187
|
+
valid OPC package on its own (final rendered decks were already clean —
|
|
188
|
+
ruby_pptx regenerates `[Content_Types].xml` on save — but the intermediate
|
|
189
|
+
file used to carry dangling overrides).
|
|
190
|
+
|
|
91
191
|
## [0.3.1] - 2026-11-17
|
|
92
192
|
|
|
93
193
|
### Added
|
data/README.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/assets/logo-dark.png">
|
|
4
|
+
<img src="docs/assets/logo-light.png" alt="PowerTom — Ruby PowerPoint template engine" width="360">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
1
8
|
# PowerTom — PowerPoint template engine for Ruby
|
|
2
9
|
|
|
3
10
|
[](https://rubygems.org/gems/powertom)
|
|
@@ -34,7 +41,8 @@ Designer Engineer
|
|
|
34
41
|
PowerTom.render("template.pptx", data: "report.toml")
|
|
35
42
|
▼
|
|
36
43
|
Finished deck: tags bound, pages cloned/removed,
|
|
37
|
-
pictures
|
|
44
|
+
pictures and charts bound — opens in PowerPoint
|
|
45
|
+
without repair.
|
|
38
46
|
```
|
|
39
47
|
|
|
40
48
|
## Why another one
|
|
@@ -55,7 +63,7 @@ PowerTom fills the middle layer: a **template engine on top of [`ruby_pptx`](htt
|
|
|
55
63
|
gem "powertom"
|
|
56
64
|
```
|
|
57
65
|
|
|
58
|
-
Requires Ruby >= 3.3; depends on `ruby_pptx
|
|
66
|
+
Requires Ruby >= 3.3; depends on `ruby_pptx >= 0.3.0, < 1.0` (read/write) and `tomlrb ~> 3.0` (data files).
|
|
59
67
|
|
|
60
68
|
## Quick start
|
|
61
69
|
|
|
@@ -88,7 +96,7 @@ detail = "Ready automatically every Sunday"
|
|
|
88
96
|
```ruby
|
|
89
97
|
require "powertom"
|
|
90
98
|
|
|
91
|
-
# Read the template, bind tags, clone/remove pages, replace images
|
|
99
|
+
# Read the template, bind tags, clone/remove pages, replace images and charts
|
|
92
100
|
result = PowerTom.render("templates/report.pptx", data: "data/report.toml")
|
|
93
101
|
result.save("out/report.pptx")
|
|
94
102
|
result.warnings.each { |w| warn w } # unused fields, fallback values…
|
|
@@ -127,12 +135,20 @@ result = PowerTom.render("templates/report.pptx", data: {
|
|
|
127
135
|
| `{{#each scenarios}}` | notes | Clone the page per array item (empty array removes the group) |
|
|
128
136
|
| `{{#if show_compare}}` | notes | Remove the page when falsy |
|
|
129
137
|
|
|
138
|
+
> [!WARNING]
|
|
139
|
+
> **Scope inside `{{#each}}`.** An `{{#each scenarios}}` page renders once per
|
|
140
|
+
> item, and every tag on that page resolves against the **item** — there is no
|
|
141
|
+
> fallback to the root data. Write `{{headline}}`, not `{{scenario.headline}}`:
|
|
142
|
+
> a prefix that does not exist on the item raises `MissingDataError`. A value
|
|
143
|
+
> that lives only at the top level is not visible on that page; put it on a
|
|
144
|
+
> separate page or include it in each item.
|
|
145
|
+
|
|
130
146
|
- **Split runs are handled**: if PowerPoint stores `{{title}}` as several runs, the engine merges the paragraph and replaces correctly; the result inherits the first run's style.
|
|
131
147
|
- **Nested table rows**: `{{#rows groups}}` occupies a whole row (first cell only), and the next row is the parent content row; `{{#rows groups[].items}}` followed by its own content row adds one nesting level. Two levels maximum; parent and child tags use full paths (`{{groups[].name}}`, `{{groups[].items[].name}}`).
|
|
132
148
|
- **Dynamic columns**: put `{{#grid rows: distribution cols: bands value: counts}}` in the header row's first cell. The header row's second cell is the column template (`{{col.label}}`), and the next row is the row template (`{{row.name}}` in the first cell, `{{cell}}` in the data columns). Exactly two columns; `value:` is optional (defaults to `row[col.key]`).
|
|
133
149
|
- **Row blocks (v0.2.x)**: `{{#rows groups}} ... {{/rows}}` each occupy a whole row; everything between them is one record template and is cloned per item. Up to 3 nested levels; `{{row.name}}` refers to the current record. A `{{#cols ...}}` band may be declared in the header or directly inside a row block, and may span several template columns; `value: scores,rates` maps one field to each template column (a single field is shared by all). A `{{#if ...}} ... {{/if}}` block inside a row block removes its rows when the condition is falsy; inside a `#cols` band it removes the wrapped columns per column item.
|
|
134
|
-
- **Pictures**: name the shape or its alt text `powertom:image:cover` and pass a file path in the data. Replacement
|
|
135
|
-
- **Charts**: name the chart shape or its alt text `powertom:chart:sales` and pass `{ categories, series, number_format }` in the data; the whole data set is replaced and series formatting is inherited from the template.
|
|
150
|
+
- **Pictures**: name the shape or its alt text `powertom:image:cover` and pass a file path in the data. Replacement fills the shape and crops from the centre by default; append `; mode=contain` (`powertom:image:logo; mode=contain`) to fit the whole image inside the frame instead.
|
|
151
|
+
- **Charts**: name the chart shape or its alt text `powertom:chart:sales` and pass `{ categories, series, number_format }` in the data; the whole data set is replaced and series formatting is inherited from the template. Supported: bar/column, line, area, pie (exactly one series), doughnut and **radar** (including the filled variant — `radarStyle` and radial-axis scaling such as a fixed maximum are preserved). Combo charts and scatter/bubble charts use a different contract — see Limitations.
|
|
136
152
|
- One directive per notes page, maximum.
|
|
137
153
|
- TOML dates are written unquoted (`report_date = 2026-01-20`), normalised to Ruby `Date`, and can be formatted with `{{report_date:date.cn}}`.
|
|
138
154
|
- Full guide for designers: [docs/template-guide.md](https://github.com/ixepet/powertom/blob/main/docs/template-guide.md).
|
|
@@ -151,6 +167,14 @@ PowerTom.register_formatter("rmb") { |value, _arg| "#{value} RMB" }
|
|
|
151
167
|
|
|
152
168
|
Most tools treat validation as a side effect of logging. PowerTom makes it a first-class step: bad templates fail in CI before anything is rendered.
|
|
153
169
|
|
|
170
|
+
Add the task to your project's Rakefile once (the gem ships it):
|
|
171
|
+
|
|
172
|
+
```ruby
|
|
173
|
+
require "powertom/tasks"
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Then run:
|
|
177
|
+
|
|
154
178
|
```bash
|
|
155
179
|
bundle exec rake powertom:check TEMPLATE=templates/report.pptx DATA=data/sample.toml
|
|
156
180
|
```
|
|
@@ -178,7 +202,7 @@ result.report # the human-readable report shown above
|
|
|
178
202
|
result.exit_code # 0 or 1 — usable in CI
|
|
179
203
|
```
|
|
180
204
|
|
|
181
|
-
Checks: missing fields (including each item of an `#each` array), unregistered formatters, directive syntax, more than one directive per page, picture fields and missing files, chart data shape, placeholder overflow warnings, unused data fields, leftover tags in notes.
|
|
205
|
+
Checks: missing fields (including each item of an `#each` array), unregistered formatters, directive syntax, more than one directive per page, picture fields and placeholder options, missing files, chart kind and data shape, tags on masters/layouts (they never take effect), placeholder overflow warnings, unused data fields, leftover tags in notes.
|
|
182
206
|
|
|
183
207
|
## Error model
|
|
184
208
|
|
|
@@ -196,13 +220,54 @@ Output is **all or nothing**: every check runs before anything is written to dis
|
|
|
196
220
|
| Limitation | Notes |
|
|
197
221
|
| --- | --- |
|
|
198
222
|
| Tables support row loops, paired row blocks, nesting and dynamic columns | `{{classes[].name}}` clones one row; `{{#rows ...}} ... {{/rows}}` clones multi-row records (3 levels); `{{#grid ...}}` / `{{#cols ...}}` clone columns; merged cells inside loops are rejected |
|
|
199
|
-
| Charts: bar/column/line/area/pie/doughnut, radar, combo and scatter/bubble are data-bound | Name the chart shape `powertom:chart:<field>`; combo/scatter/bubble bind in document order and require exactly the template's series count (scatter/bubble carry `points: [{x, y(, size)}]` per series instead of shared categories); text tags inside charts are left as-is and reported as warnings |
|
|
223
|
+
| Charts: bar/column/line/area/pie/doughnut, radar, combo and scatter/bubble are data-bound | Name the chart shape `powertom:chart:<field>`; pie charts accept exactly one series (use doughnut for multiple rings); combo/scatter/bubble bind in document order and require exactly the template's series count (scatter/bubble carry `points: [{x, y(, size)}]` per series instead of shared categories); text tags inside charts are left as-is and reported as warnings |
|
|
200
224
|
| Notes are not rendered | Notes are only read for page directives; ordinary tags in notes produce a warning |
|
|
201
|
-
| Tags on masters/layouts are
|
|
225
|
+
| Tags on masters/layouts are rejected | Only shapes on slides are processed; `PowerTom.check` reports these tags as errors because they never take effect |
|
|
202
226
|
| No PPT → PDF | Use LibreOffice headless |
|
|
203
227
|
| `.pptx` only | No legacy 97-2003 binary format |
|
|
204
|
-
| TOML or Hash only | No YAML / JSON loader |
|
|
205
|
-
|
|
|
228
|
+
| TOML or Hash only | No YAML / JSON loader; load them yourself and pass a Hash (see the FAQ below) |
|
|
229
|
+
| Pictures default to fill + centre-crop | Append `; mode=contain` to `powertom:image:<field>` to fit the image inside the frame without cropping; the aspect-ratio warning only applies to fill mode |
|
|
230
|
+
|
|
231
|
+
## FAQ
|
|
232
|
+
|
|
233
|
+
**Why does `{{scenario.headline}}` fail inside `{{#each scenarios}}`?**
|
|
234
|
+
|
|
235
|
+
The page context becomes the current item, as in Handlebars or Liquid. Every
|
|
236
|
+
tag on that page resolves against the item, with no fallback to the root
|
|
237
|
+
document. Write `{{headline}}`; a prefix that does not exist on the item raises
|
|
238
|
+
`MissingDataError`. Values that exist only at the top level need their own page
|
|
239
|
+
or must be repeated in each item.
|
|
240
|
+
|
|
241
|
+
**`rake powertom:check` says "Don't know how to build task".**
|
|
242
|
+
|
|
243
|
+
The task has to be loaded by your project. Add this once to the Rakefile of the
|
|
244
|
+
app that uses the gem (0.4.2+):
|
|
245
|
+
|
|
246
|
+
```ruby
|
|
247
|
+
require "powertom/tasks"
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Then `bundle exec rake powertom:check TEMPLATE=... DATA=...` works. The
|
|
251
|
+
repository-only `powertom:lint` and `fixtures:*` tasks are not part of the gem.
|
|
252
|
+
|
|
253
|
+
**Can I pass JSON or YAML instead of TOML?**
|
|
254
|
+
|
|
255
|
+
The built-in loader accepts a `Hash` or a `.toml` path. Parse other formats
|
|
256
|
+
yourself and pass the resulting Hash — string and symbol keys both work:
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
require "json"
|
|
260
|
+
PowerTom.render("template.pptx", data: JSON.parse(File.read("data.json")))
|
|
261
|
+
|
|
262
|
+
require "yaml"
|
|
263
|
+
PowerTom.render("template.pptx",
|
|
264
|
+
data: YAML.safe_load_file("data.yaml",
|
|
265
|
+
permitted_classes: [Date, Time],
|
|
266
|
+
aliases: true))
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
JSON/YAML have no native date type: convert date-like strings to `Date` or
|
|
270
|
+
`Time` yourself if a `date` formatter is used.
|
|
206
271
|
|
|
207
272
|
## Security
|
|
208
273
|
|
|
@@ -229,9 +294,14 @@ Two fixture tiers cover the end-to-end paths:
|
|
|
229
294
|
- **Committed mini golden sample** (`spec/fixtures/mini/template.pptx`): split-run
|
|
230
295
|
merging, multi-tag paragraphs, page cloning/removal, picture replacement,
|
|
231
296
|
chart data binding and LibreOffice fidelity run in every CI build
|
|
232
|
-
(`spec/powertom/mini_golden_spec.rb`).
|
|
233
|
-
|
|
234
|
-
|
|
297
|
+
(`spec/powertom/mini_golden_spec.rb`). Regenerate it with
|
|
298
|
+
`bundle exec ruby tools/build_mini_golden.rb` if the template needs new coverage.
|
|
299
|
+
- **L3 official validation** (`.github/workflows/l3.yml`): the rendered deck set
|
|
300
|
+
(`tools/build_l3_decks.rb`) is checked with Microsoft's OpenXmlValidator on a
|
|
301
|
+
Windows runner (`tools/openxml_validator`) — the closest machine-checkable
|
|
302
|
+
approximation of PowerPoint's own tolerance. A **hard gate**: any
|
|
303
|
+
schema-level finding fails the build. Also runs on a daily schedule to catch
|
|
304
|
+
upstream drift.
|
|
235
305
|
- **Private golden sample**: the larger real-world deck used by the HaiMate
|
|
236
306
|
examples (`spec/fixtures/hai_mate/template.pptx`) is not committed, because of
|
|
237
307
|
size and ownership; only its structural snapshot `spec/fixtures/hai_mate/manifest.toml` is. With the original template at hand:
|
|
@@ -252,10 +322,12 @@ git tag -a v0.3.0 -m "v0.3.0"
|
|
|
252
322
|
git push origin v0.3.0
|
|
253
323
|
```
|
|
254
324
|
|
|
255
|
-
`.github/workflows/release.yml`
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
and
|
|
325
|
+
`.github/workflows/release.yml` runs `rubygems/release-gem` (trusted publishing
|
|
326
|
+
over the GitHub OIDC token), so no manual `gem push` or OTP is needed. The
|
|
327
|
+
action also signs the gem with Sigstore, so each RubyGems version shows
|
|
328
|
+
"Provenance: Built and signed on GitHub Actions" with the commit and workflow
|
|
329
|
+
that built it. Configure the trusted publisher on RubyGems with workflow
|
|
330
|
+
`release.yml` and no environment.
|
|
259
331
|
|
|
260
332
|
## License
|
|
261
333
|
|
|
@@ -50,6 +50,48 @@ module PowerTom
|
|
|
50
50
|
runs.each_with_index { |run, i| run.text = texts[i] }
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
+
# §6 保存:先做图表 schema 归一化(补偿上游 writer 缺陷),再写盘
|
|
54
|
+
def save(prs, path)
|
|
55
|
+
normalize_chart_quirks(prs)
|
|
56
|
+
prs.save(path)
|
|
57
|
+
path
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# 上游图表 writer 的两个缺陷(L3 OpenXmlValidator 首跑取证,ruby_pptx
|
|
61
|
+
# 0.3.0 仍未修复):
|
|
62
|
+
# 1. 轴 id 常量是负数(如 VAL_AX_ID="-2112772216"),而 axId 的 schema
|
|
63
|
+
# 约束是 MaxInclusive 2147483647(ST_DrawingElementId 实际按非负有界
|
|
64
|
+
# int32 校验,不是 uint32)—— LibreOffice/PowerPoint 宽容,官方校验器
|
|
65
|
+
# 拒绝;把全部 c:axId/c:crossAx 的负值取绝对值(上游负数都大于
|
|
66
|
+
# -2^31,abs 后合法且图表内引用自洽)。
|
|
67
|
+
# 2. RadarChartWriter 给每个 ser 写 c:smooth,但 radar ser 的 schema
|
|
68
|
+
# 没有该子元素 —— 直接移除。
|
|
69
|
+
#
|
|
70
|
+
# 这是适配层的兼容护城河:架构不能假设底层库永远输出正确,上游修复前
|
|
71
|
+
# 不得删除。上游发布修复版本后,先升级并用 L3 验证,再按版本门控
|
|
72
|
+
# (如 Gem::Version.new(Pptx::VERSION) < Gem::Version.new("<fixed>")),
|
|
73
|
+
# 最后才在后续大版本里标记 Deprecated 并移除。
|
|
74
|
+
def normalize_chart_quirks(prs)
|
|
75
|
+
prs.slides.each do |slide|
|
|
76
|
+
walk_shapes(slide.shapes) do |shape|
|
|
77
|
+
next unless shape.respond_to?(:chart?) && shape.chart?
|
|
78
|
+
|
|
79
|
+
normalize_chart_element(shape.chart.element)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def normalize_chart_element(chart_el)
|
|
85
|
+
chart_el.xpath(".//c:axId | .//c:crossAx").each do |el|
|
|
86
|
+
val = el.get("val").to_i
|
|
87
|
+
el.set("val", val.abs.to_s) if val.negative?
|
|
88
|
+
end
|
|
89
|
+
chart_el.xpath(".//c:radarChart//c:ser/c:smooth").each do |smooth|
|
|
90
|
+
smooth.parent.remove(smooth)
|
|
91
|
+
end
|
|
92
|
+
chart_el
|
|
93
|
+
end
|
|
94
|
+
|
|
53
95
|
# 备注文本(§3.3:v0 不渲染备注,只用于发现其中的标签并记 warning)。
|
|
54
96
|
# 返回 [{slide_no:, text:}];无备注页返回空串。
|
|
55
97
|
def notes_texts(prs)
|
|
@@ -62,17 +104,21 @@ module PowerTom
|
|
|
62
104
|
end
|
|
63
105
|
|
|
64
106
|
# 图片占位上下文:形状 name 或 alt text 符合 powertom:image:字段名 约定(§5.3)
|
|
65
|
-
# 返回 [{slide_no:, shape:, field:,
|
|
107
|
+
# 返回 [{slide_no:, shape:, field:, mode:, mode_error:, placeholder:,
|
|
108
|
+
# slide:, picture:}]
|
|
109
|
+
# 占位语法:powertom:image:<field>[; mode=fill|contain],默认 fill
|
|
66
110
|
def picture_contexts(prs)
|
|
67
111
|
contexts = []
|
|
68
112
|
prs.slides.each_with_index do |slide, i|
|
|
69
113
|
walk_shapes(slide.shapes) do |shape|
|
|
70
114
|
next unless shape.is_a?(Pptx::Picture)
|
|
71
115
|
|
|
72
|
-
|
|
73
|
-
next unless
|
|
116
|
+
spec = image_spec(shape)
|
|
117
|
+
next unless spec
|
|
74
118
|
|
|
75
|
-
contexts << { slide_no: i + 1, shape: shape.name, field: field,
|
|
119
|
+
contexts << { slide_no: i + 1, shape: shape.name, field: spec[:field],
|
|
120
|
+
mode: spec[:mode], mode_error: spec[:error],
|
|
121
|
+
placeholder: "#{IMAGE_FIELD_PREFIX}#{spec[:raw]}",
|
|
76
122
|
slide: slide, picture: shape }
|
|
77
123
|
end
|
|
78
124
|
end
|
|
@@ -182,8 +228,7 @@ module PowerTom
|
|
|
182
228
|
end
|
|
183
229
|
|
|
184
230
|
# 全部 plot 的系列总数(绑定数据必须恰好提供这么多系列:
|
|
185
|
-
# combo 数量不匹配会让上游 trim/clone 静默增删 plot
|
|
186
|
-
# scatter/bubble 的适配层重写同样按数量守卫)
|
|
231
|
+
# combo 数量不匹配会让上游 trim/clone 静默增删 plot 破坏结构)
|
|
187
232
|
def chart_series_count(chart)
|
|
188
233
|
chart.element.chart.plotArea.plot_elements.sum { |plot| plot.ser_list.size }
|
|
189
234
|
end
|
|
@@ -210,6 +255,9 @@ module PowerTom
|
|
|
210
255
|
# 把整个数据集写入该图表占位。克隆页共享同一 chart part(spike 实测
|
|
211
256
|
# shared=true),绑定前为该形状深拷贝独立 part,再 replace_data;
|
|
212
257
|
# value: {categories:, series: [{name:, values:}], number_format:}
|
|
258
|
+
# 散点/气泡:{series: [{name:, points: [{x:, y:(, size:)}]}]},
|
|
259
|
+
# ruby_pptx 0.3.0 起上游 replace_data 直接支持 XyChartData/BubbleChartData
|
|
260
|
+
# (重写 c:tx/c:xVal/c:yVal/c:bubbleSize 并重建内嵌 workbook)
|
|
213
261
|
def replace_chart_data(prs, shape, slide, value)
|
|
214
262
|
chart = shape.chart
|
|
215
263
|
if chart_part_shared?(prs, chart.part)
|
|
@@ -220,9 +268,7 @@ module PowerTom
|
|
|
220
268
|
|
|
221
269
|
kind = chart_kind(chart)
|
|
222
270
|
if %i[XY BUBBLE].include?(kind)
|
|
223
|
-
|
|
224
|
-
rewrite_xy_series(chart, xy)
|
|
225
|
-
chart.part.workbook.replace_with(xy.xlsx_blob)
|
|
271
|
+
chart.replace_data(build_xy_chart_data(value, bubble: kind == :BUBBLE))
|
|
226
272
|
else
|
|
227
273
|
data = Pptx::ChartData.new(number_format: part_value(value, "number_format") || "General")
|
|
228
274
|
data.categories = part_value(value, "categories")
|
|
@@ -237,9 +283,7 @@ module PowerTom
|
|
|
237
283
|
"chart data binding failed (#{shape.name}): #{e.class} #{e.message}"
|
|
238
284
|
end
|
|
239
285
|
|
|
240
|
-
# 散点/气泡数据:{series: [{name:, points: [{x:, y:(, size:)}]}]}
|
|
241
|
-
# 上游 replace_data 不能重绑 XY(SeriesFragments 只产 tx/cat/val,
|
|
242
|
-
# spike2 取证),适配层自行重写 ser 的 xVal/yVal/bubbleSize。
|
|
286
|
+
# 散点/气泡数据:{series: [{name:, points: [{x:, y:(, size:)}]}]}
|
|
243
287
|
def build_xy_chart_data(value, bubble:)
|
|
244
288
|
xy = (bubble ? Pptx::BubbleChartData : Pptx::XyChartData)
|
|
245
289
|
.new(number_format: part_value(value, "number_format") || "General")
|
|
@@ -256,95 +300,6 @@ module PowerTom
|
|
|
256
300
|
xy
|
|
257
301
|
end
|
|
258
302
|
|
|
259
|
-
# 与上游 rewrite_series 同构:remove 后 insert,元素定位交给
|
|
260
|
-
# CT_Series 的 zero_or_one 宏(TAG_SEQ 保证 schema 顺序)
|
|
261
|
-
def rewrite_xy_series(chart, xy)
|
|
262
|
-
sers = chart.element.chart.plotArea.series_elements
|
|
263
|
-
if sers.size != xy.size
|
|
264
|
-
raise PowerTom::Error,
|
|
265
|
-
"chart template draws #{chart_distribution_label(chart)} " \
|
|
266
|
-
"(#{sers.size} series in total), got #{xy.size} series"
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
sers.zip(xy.series.to_a) do |ser, series|
|
|
270
|
-
ser.remove_tx
|
|
271
|
-
ser.remove_cat
|
|
272
|
-
ser.remove_val
|
|
273
|
-
ser.remove_xVal
|
|
274
|
-
ser.remove_yVal
|
|
275
|
-
ser.remove_bubbleSize
|
|
276
|
-
|
|
277
|
-
tx, x_val, y_val, bubble_size = xy_fragments(xy, series)
|
|
278
|
-
ser.insert_tx(ser.build_from_xml(tx))
|
|
279
|
-
ser.insert_xVal(ser.build_from_xml(x_val))
|
|
280
|
-
ser.insert_yVal(ser.build_from_xml(y_val))
|
|
281
|
-
ser.insert_bubbleSize(ser.build_from_xml(bubble_size)) if bubble_size
|
|
282
|
-
end
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
def xy_fragments(xy_data, series)
|
|
286
|
-
number_format = series.number_format || xy_data.number_format
|
|
287
|
-
tx = xy_namespaced(xy_tx_fragment(series))
|
|
288
|
-
x_val = xy_namespaced(xy_num_ref_fragment("xVal", series.x_values_ref, series.x_values, number_format))
|
|
289
|
-
y_val = xy_namespaced(xy_num_ref_fragment("yVal", series.y_values_ref, series.y_values, number_format))
|
|
290
|
-
bubble_size = if xy_data.bubble?
|
|
291
|
-
xy_namespaced(xy_num_ref_fragment("bubbleSize", series.bubble_sizes_ref,
|
|
292
|
-
series.bubble_sizes, number_format))
|
|
293
|
-
end
|
|
294
|
-
[tx, x_val, y_val, bubble_size]
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
# 与上游 SeriesFragments#namespaced 一致:独立解析的片段需要自带
|
|
298
|
-
# 命名空间声明
|
|
299
|
-
def xy_namespaced(fragment)
|
|
300
|
-
fragment.sub(/\A<(c:\w+)/) { |tag| "#{tag} #{Pptx::Oxml::Ns.nsdecls('c', 'a', 'r')}" }
|
|
301
|
-
end
|
|
302
|
-
|
|
303
|
-
def xy_tx_fragment(series)
|
|
304
|
-
<<~XML
|
|
305
|
-
<c:tx>
|
|
306
|
-
<c:strRef>
|
|
307
|
-
<c:f>#{series.name_ref}</c:f>
|
|
308
|
-
<c:strCache>
|
|
309
|
-
<c:ptCount val="1"/>
|
|
310
|
-
<c:pt idx="0"><c:v>#{xy_escape(series.name)}</c:v></c:pt>
|
|
311
|
-
</c:strCache>
|
|
312
|
-
</c:strRef>
|
|
313
|
-
</c:tx>
|
|
314
|
-
XML
|
|
315
|
-
end
|
|
316
|
-
|
|
317
|
-
# 与上游 num_ref_xml 同构:引用 + 缓存;nil 值省点但 ptCount 照计(空档)
|
|
318
|
-
def xy_num_ref_fragment(tag, reference, values, number_format)
|
|
319
|
-
points = values.each_with_index.filter_map do |value, index|
|
|
320
|
-
next if value.nil?
|
|
321
|
-
|
|
322
|
-
%(<c:pt idx="#{index}"><c:v>#{xy_format_number(value)}</c:v></c:pt>)
|
|
323
|
-
end.join
|
|
324
|
-
<<~XML
|
|
325
|
-
<c:#{tag}>
|
|
326
|
-
<c:numRef>
|
|
327
|
-
<c:f>#{reference}</c:f>
|
|
328
|
-
<c:numCache>
|
|
329
|
-
<c:formatCode>#{number_format}</c:formatCode>
|
|
330
|
-
<c:ptCount val="#{values.size}"/>
|
|
331
|
-
#{points}
|
|
332
|
-
</c:numCache>
|
|
333
|
-
</c:numRef>
|
|
334
|
-
</c:#{tag}>
|
|
335
|
-
XML
|
|
336
|
-
end
|
|
337
|
-
|
|
338
|
-
def xy_format_number(value)
|
|
339
|
-
return value.to_s unless value.is_a?(Float)
|
|
340
|
-
|
|
341
|
-
value == value.truncate ? value.to_i.to_s : value.to_s
|
|
342
|
-
end
|
|
343
|
-
|
|
344
|
-
def xy_escape(value)
|
|
345
|
-
value.to_s.gsub("&", "&").gsub("<", "<").gsub(">", ">")
|
|
346
|
-
end
|
|
347
|
-
|
|
348
303
|
# 绑定会重建内嵌 workbook,上游 writer 只在含日期分类时才写 styles.xml
|
|
349
304
|
# 且把基础字体写死为 Calibri(workbook_writer);其余情况根本没有
|
|
350
305
|
# styles.xml,Excel 打开"编辑数据"时回退到本地默认字体,与演示文稿
|
|
@@ -505,14 +460,17 @@ module PowerTom
|
|
|
505
460
|
end
|
|
506
461
|
|
|
507
462
|
# §5.3 图片替换:get_or_add 新 image part(按内容 sha1 去重,扩展名/content type
|
|
508
|
-
# 由 ruby_pptx 按真实图片类型维护),repoint blip
|
|
509
|
-
#
|
|
510
|
-
|
|
463
|
+
# 由 ruby_pptx 按真实图片类型维护),repoint blip 引用。
|
|
464
|
+
# fill:填满形状、居中裁切;contain:等比缩放到形状内、居中留边。
|
|
465
|
+
# SVG 等矢量图没有原生像素尺寸,contain 保持原几何,fill 不做裁切。
|
|
466
|
+
def replace_image(slide, picture, image_path, mode: :fill)
|
|
511
467
|
_image_part, r_id = slide.part.get_or_add_image_part(image_path)
|
|
512
468
|
pic_el = picture.element
|
|
513
469
|
pic_el.blip.embed = r_id
|
|
514
470
|
image = Pptx::Image.from_file(image_path)
|
|
515
|
-
|
|
471
|
+
if mode == :contain
|
|
472
|
+
fit_image_inside(picture, image)
|
|
473
|
+
elsif !image.vector?
|
|
516
474
|
view = [picture.width, picture.height]
|
|
517
475
|
if view[0] && view[1]
|
|
518
476
|
# crop_to_fit 只关心比例;Length 没有 to_f,传 EMU 裸值
|
|
@@ -527,6 +485,32 @@ module PowerTom
|
|
|
527
485
|
"picture replacement failed (#{File.basename(image_path.to_s)}): #{e.class} #{e.message}"
|
|
528
486
|
end
|
|
529
487
|
|
|
488
|
+
# contain:按 min(宽比, 高比) 缩放,居中写入 picture 的 xfrm,并清掉旧裁切。
|
|
489
|
+
# 边距露出的是图片后面的内容(幻灯片背景或其它形状),这里不合成背景填充。
|
|
490
|
+
def fit_image_inside(picture, image)
|
|
491
|
+
return if image.vector?
|
|
492
|
+
|
|
493
|
+
native = image.native_size
|
|
494
|
+
left = picture.left&.emu
|
|
495
|
+
top = picture.top&.emu
|
|
496
|
+
width = picture.width&.emu
|
|
497
|
+
height = picture.height&.emu
|
|
498
|
+
return unless [left, top, width, height].none?(&:nil?)
|
|
499
|
+
return unless width.positive? && height.positive?
|
|
500
|
+
|
|
501
|
+
image_width = native[0].emu
|
|
502
|
+
image_height = native[1].emu
|
|
503
|
+
scale = [width.to_f / image_width, height.to_f / image_height].min
|
|
504
|
+
fitted_width = (image_width * scale).round
|
|
505
|
+
fitted_height = (image_height * scale).round
|
|
506
|
+
|
|
507
|
+
picture.left = Pptx::Length.emu(left + ((width - fitted_width) / 2))
|
|
508
|
+
picture.top = Pptx::Length.emu(top + ((height - fitted_height) / 2))
|
|
509
|
+
picture.width = Pptx::Length.emu(fitted_width)
|
|
510
|
+
picture.height = Pptx::Length.emu(fitted_height)
|
|
511
|
+
picture.element.xpath("./p:blipFill").first&.remove_all("a:srcRect")
|
|
512
|
+
end
|
|
513
|
+
|
|
530
514
|
# 逐页形状名(含组合内、含无文本形状),用于模板结构自检。
|
|
531
515
|
# 返回 [{slide_no:, names: [String]}]
|
|
532
516
|
def shape_names_by_slide(prs)
|
|
@@ -788,10 +772,6 @@ module PowerTom
|
|
|
788
772
|
found
|
|
789
773
|
end
|
|
790
774
|
|
|
791
|
-
def save(prs, path)
|
|
792
|
-
prs.save(path)
|
|
793
|
-
end
|
|
794
|
-
|
|
795
775
|
private
|
|
796
776
|
|
|
797
777
|
def walk_shapes(shapes, &block)
|
|
@@ -809,15 +789,34 @@ module PowerTom
|
|
|
809
789
|
end
|
|
810
790
|
|
|
811
791
|
IMAGE_FIELD_PREFIX = "powertom:image:"
|
|
792
|
+
IMAGE_MODES = %w[fill contain].freeze
|
|
812
793
|
|
|
813
|
-
|
|
794
|
+
# 从形状 name / alt text 解析图片占位;不是图片占位时返回 nil
|
|
795
|
+
def image_spec(shape)
|
|
814
796
|
name = shape.name.to_s
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
797
|
+
raw = if name.start_with?(IMAGE_FIELD_PREFIX)
|
|
798
|
+
name.delete_prefix(IMAGE_FIELD_PREFIX)
|
|
799
|
+
else
|
|
800
|
+
descr = alt_text_of(shape)
|
|
801
|
+
descr.delete_prefix(IMAGE_FIELD_PREFIX) if descr&.start_with?(IMAGE_FIELD_PREFIX)
|
|
802
|
+
end
|
|
803
|
+
raw && parse_image_spec(raw)
|
|
804
|
+
end
|
|
805
|
+
|
|
806
|
+
# field[; mode=fill|contain];默认 fill,未知选项保留为 mode_error
|
|
807
|
+
def parse_image_spec(raw)
|
|
808
|
+
field, *options = raw.split(";").map(&:strip)
|
|
809
|
+
mode = :fill
|
|
810
|
+
error = field.empty? ? "picture placeholder has no field name" : nil
|
|
811
|
+
options.each do |option|
|
|
812
|
+
key, value = option.split("=", 2).map(&:strip)
|
|
813
|
+
if key == "mode" && IMAGE_MODES.include?(value)
|
|
814
|
+
mode = value.to_sym
|
|
815
|
+
else
|
|
816
|
+
error ||= "unsupported picture option '#{option}'; supported: mode=fill|contain"
|
|
817
|
+
end
|
|
818
|
+
end
|
|
819
|
+
{ field: field, mode: mode, error: error, raw: raw.strip }
|
|
821
820
|
end
|
|
822
821
|
|
|
823
822
|
def alt_text_of(shape)
|
|
@@ -15,7 +15,8 @@ module PowerTom
|
|
|
15
15
|
|
|
16
16
|
unless data.end_with?(".toml")
|
|
17
17
|
raise InvalidTemplateError,
|
|
18
|
-
"data file #{data} is not .toml:
|
|
18
|
+
"data file #{data} is not .toml: the built-in loader only supports .toml; " \
|
|
19
|
+
"load JSON/YAML yourself and pass a Hash"
|
|
19
20
|
end
|
|
20
21
|
raise InvalidTemplateError, "data file not found: #{data}" unless File.file?(data)
|
|
21
22
|
|
data/lib/powertom/render.rb
CHANGED
|
@@ -189,6 +189,12 @@ module PowerTom
|
|
|
189
189
|
# 时保留占位图并记 warning。
|
|
190
190
|
def replace_images(adapter, prs, page_scopes, binder, warnings)
|
|
191
191
|
adapter.picture_contexts(prs).each do |pctx|
|
|
192
|
+
if pctx[:mode_error]
|
|
193
|
+
raise PowerTom::Error,
|
|
194
|
+
"slide #{pctx[:slide_no]} picture '#{pctx[:shape]}' field '#{pctx[:field]}': " \
|
|
195
|
+
"#{pctx[:mode_error]}"
|
|
196
|
+
end
|
|
197
|
+
|
|
192
198
|
page = page_scopes[pctx[:slide_no] - 1]
|
|
193
199
|
ctx = { slide_no: pctx[:slide_no], shape: pctx[:shape],
|
|
194
200
|
scope: page[:scope], scope_root: page[:scope_root] }
|
|
@@ -203,8 +209,8 @@ module PowerTom
|
|
|
203
209
|
raise PowerTom::Error,
|
|
204
210
|
"slide #{pctx[:slide_no]} picture field '#{pctx[:field]}' file does not exist: #{value}"
|
|
205
211
|
end
|
|
206
|
-
warn_image_aspect(adapter, pctx, value, warnings)
|
|
207
|
-
adapter.replace_image(pctx[:slide], pctx[:picture], value)
|
|
212
|
+
warn_image_aspect(adapter, pctx, value, warnings) if pctx[:mode] == :fill
|
|
213
|
+
adapter.replace_image(pctx[:slide], pctx[:picture], value, mode: pctx[:mode])
|
|
208
214
|
end
|
|
209
215
|
end
|
|
210
216
|
|
|
@@ -240,10 +246,11 @@ module PowerTom
|
|
|
240
246
|
raise PowerTom::Error,
|
|
241
247
|
"slide #{cctx[:slide_no]} chart '#{cctx[:shape]}' field '#{cctx[:field]}': " \
|
|
242
248
|
"#{label} charts are not supported for data binding " \
|
|
243
|
-
"(supported: column/bar, line, area, pie, doughnut, combo, scatter, bubble)"
|
|
249
|
+
"(supported: column/bar, line, area, pie, doughnut, radar, combo, scatter, bubble)"
|
|
244
250
|
end
|
|
245
251
|
validate_chart_data!(value, cctx)
|
|
246
252
|
validate_chart_series_count!(value, cctx) if cctx[:kind] == :combo
|
|
253
|
+
validate_pie_series_count!(value, cctx) if cctx[:kind] == :PIE
|
|
247
254
|
end
|
|
248
255
|
adapter.replace_chart_data(prs, cctx[:chart_shape], cctx[:slide], value)
|
|
249
256
|
end
|
|
@@ -308,6 +315,18 @@ module PowerTom
|
|
|
308
315
|
"order, got #{count}"
|
|
309
316
|
end
|
|
310
317
|
|
|
318
|
+
# 饼图只绘制第一个系列:多余系列会被静默丢弃,必须在写入前拒绝。
|
|
319
|
+
# 圆环图支持多系列(同心环),不受此守卫影响。
|
|
320
|
+
def validate_pie_series_count!(value, cctx)
|
|
321
|
+
series = chart_field_value(value, "series")
|
|
322
|
+
return unless series.is_a?(Array) && series.size > 1
|
|
323
|
+
|
|
324
|
+
raise PowerTom::Error,
|
|
325
|
+
"slide #{cctx[:slide_no]} chart '#{cctx[:shape]}' field '#{cctx[:field]}': " \
|
|
326
|
+
"a pie chart plots exactly 1 series, got #{series.size}; " \
|
|
327
|
+
"use a doughnut chart for multiple rings"
|
|
328
|
+
end
|
|
329
|
+
|
|
311
330
|
# 散点/气泡数据形状:{series: [{name:, points: [{x:, y:(, size:)}]}]}。
|
|
312
331
|
# 每个系列自带坐标对,没有共享 categories;误传 categories 视为形状错误
|
|
313
332
|
def validate_xy_chart_data!(value, cctx)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rake"
|
|
4
|
+
require_relative "../powertom"
|
|
5
|
+
|
|
6
|
+
# 模板校验任务(§7)。gem 用户在自己的 Rakefile 里 require 本文件:
|
|
7
|
+
# require "powertom/tasks"
|
|
8
|
+
# 仓库根目录的 Rakefile 也只 require 本文件,保证 powertom:check 只有一处定义。
|
|
9
|
+
namespace :powertom do
|
|
10
|
+
desc "Validate a template against sample data (errors block rendering)"
|
|
11
|
+
task :check do
|
|
12
|
+
template = ENV.fetch("TEMPLATE", nil)
|
|
13
|
+
if template.nil? || template.empty?
|
|
14
|
+
abort "usage: rake powertom:check TEMPLATE=/path/to/template.pptx [DATA=/path/to/sample.toml]"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
data = ENV.fetch("DATA", nil)
|
|
18
|
+
data = nil if data.nil? || data.empty?
|
|
19
|
+
|
|
20
|
+
result = PowerTom.check(template, data: data)
|
|
21
|
+
puts result.report
|
|
22
|
+
exit result.exit_code
|
|
23
|
+
end
|
|
24
|
+
end
|
data/lib/powertom/validator.rb
CHANGED
|
@@ -76,10 +76,11 @@ module PowerTom
|
|
|
76
76
|
# tables: [{slide_no:, shape:, run_texts:, in_table:}](表格单元格段落)
|
|
77
77
|
# table_structures: [{slide_no:, shape:, rows:, merged_rows:}](表格结构,校验行循环)
|
|
78
78
|
# notes: [{slide_no:, text:}]
|
|
79
|
+
# container_texts: [{container:, shape:, text:}](母版/版式上的文本)
|
|
79
80
|
# pictures: [{slide_no:, shape:, field:}](powertom:image 占位)
|
|
80
81
|
# data: Hash 或 nil(nil → 跳过字段对账,只做语法与结构检查)
|
|
81
82
|
def check(contexts:, notes:, data:, pictures: [], charts: [], tables: [], table_structures: [],
|
|
82
|
-
chart_placeholders: [], template_name: "template.pptx")
|
|
83
|
+
chart_placeholders: [], container_texts: [], template_name: "template.pptx")
|
|
83
84
|
entries = []
|
|
84
85
|
used_roots = Set.new
|
|
85
86
|
|
|
@@ -165,7 +166,26 @@ module PowerTom
|
|
|
165
166
|
entries << error_entry(
|
|
166
167
|
cctx[:slide_no], cctx[:shape], cctx[:field],
|
|
167
168
|
"#{kind_label(kind)} charts are not supported for data binding " \
|
|
168
|
-
"(supported: column/bar, line, area, pie, doughnut, combo, scatter, bubble)"
|
|
169
|
+
"(supported: column/bar, line, area, pie, doughnut, radar, combo, scatter, bubble)"
|
|
170
|
+
)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# 2.7 图片占位选项:mode=fill|contain;非法选项不依赖样例数据先报错
|
|
174
|
+
pictures.each do |pctx|
|
|
175
|
+
next unless pctx[:mode_error]
|
|
176
|
+
|
|
177
|
+
entries << error_entry(pctx[:slide_no], pctx[:shape], pctx[:field], pctx[:mode_error])
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# 2.8 母版/版式标签:运行时不会绑定,提前报 error(不依赖样例数据)
|
|
181
|
+
container_texts.each do |entry|
|
|
182
|
+
tags = entry[:text].to_s.scan(Scanner::TAG_RE).uniq
|
|
183
|
+
next if tags.empty?
|
|
184
|
+
|
|
185
|
+
entries << error_entry(
|
|
186
|
+
nil, nil, nil,
|
|
187
|
+
"#{entry[:container]} shape '#{entry[:shape]}' contains tags #{tags.join(', ')}: " \
|
|
188
|
+
"tags on masters/layouts never take effect"
|
|
169
189
|
)
|
|
170
190
|
end
|
|
171
191
|
|
|
@@ -383,6 +403,8 @@ module PowerTom
|
|
|
383
403
|
# 图片字段对账(§5.3):与文本标签同一套 #each/普通 语义;额外检查
|
|
384
404
|
# 值必须是 String 且文件真实存在(设计师漏传图是最常见的交付事故)
|
|
385
405
|
def reconcile_picture(pctx, directive, data, entries)
|
|
406
|
+
return if pctx[:mode_error] # 占位选项错误已在模板属性检查中报告
|
|
407
|
+
|
|
386
408
|
path = pctx[:field]
|
|
387
409
|
ctx = { slide_no: pctx[:slide_no], shape: pctx[:shape] }
|
|
388
410
|
if directive && directive[:kind] == :each
|
|
@@ -457,6 +479,7 @@ module PowerTom
|
|
|
457
479
|
next
|
|
458
480
|
end
|
|
459
481
|
check_chart_value(value, ctx, path, entries, k)
|
|
482
|
+
check_pie_series_count(value, ctx, path, entries, k) if cctx[:kind] == :PIE
|
|
460
483
|
check_chart_series_count(value, cctx, entries, k)
|
|
461
484
|
end
|
|
462
485
|
else
|
|
@@ -471,6 +494,7 @@ module PowerTom
|
|
|
471
494
|
else
|
|
472
495
|
check_chart_value(value, ctx, path, entries)
|
|
473
496
|
end
|
|
497
|
+
check_pie_series_count(value, ctx, path, entries) if cctx[:kind] == :PIE
|
|
474
498
|
check_chart_series_count(value, cctx, entries)
|
|
475
499
|
end
|
|
476
500
|
end
|
|
@@ -496,6 +520,21 @@ module PowerTom
|
|
|
496
520
|
)
|
|
497
521
|
end
|
|
498
522
|
|
|
523
|
+
# 饼图只绘制第一个系列:多余系列会被静默丢弃,check 阶段直接报 error
|
|
524
|
+
def check_pie_series_count(value, ctx, path, entries, item_index = nil)
|
|
525
|
+
return unless value.is_a?(Hash)
|
|
526
|
+
|
|
527
|
+
series = value.key?(:series) ? value[:series] : value["series"]
|
|
528
|
+
return unless series.is_a?(Array) && series.size > 1
|
|
529
|
+
|
|
530
|
+
where = item_index ? "item #{item_index + 1}: " : ""
|
|
531
|
+
entries << error_entry(
|
|
532
|
+
ctx[:slide_no], ctx[:shape], path,
|
|
533
|
+
"#{where}a pie chart plots exactly 1 series, got #{series.size}; " \
|
|
534
|
+
"use a doughnut chart for multiple rings"
|
|
535
|
+
)
|
|
536
|
+
end
|
|
537
|
+
|
|
499
538
|
# 散点/气泡数据形状校验(check 侧,与 render 的 validate_xy_chart_data!
|
|
500
539
|
# 同一规则):误传 categories、points 非坐标对、x/y/size 非数字都报 error
|
|
501
540
|
def check_xy_chart_value(value, ctx, path, entries, cctx)
|
data/lib/powertom/version.rb
CHANGED
data/lib/powertom.rb
CHANGED
|
@@ -75,6 +75,7 @@ module PowerTom
|
|
|
75
75
|
tables: adapter.table_cell_contexts(prs),
|
|
76
76
|
table_structures: adapter.table_contexts(prs),
|
|
77
77
|
notes: adapter.notes_texts(prs),
|
|
78
|
+
container_texts: adapter.container_texts(prs),
|
|
78
79
|
pictures: adapter.picture_contexts(prs),
|
|
79
80
|
charts: adapter.chart_tags(prs),
|
|
80
81
|
chart_placeholders: adapter.chart_contexts(prs),
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: powertom
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- PowerTom contributors
|
|
@@ -15,7 +15,7 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.
|
|
18
|
+
version: 0.3.0
|
|
19
19
|
- - "<"
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
21
|
version: '1.0'
|
|
@@ -25,7 +25,7 @@ dependencies:
|
|
|
25
25
|
requirements:
|
|
26
26
|
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
|
-
version: 0.
|
|
28
|
+
version: 0.3.0
|
|
29
29
|
- - "<"
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
31
|
version: '1.0'
|
|
@@ -68,6 +68,7 @@ files:
|
|
|
68
68
|
- lib/powertom/run_merge.rb
|
|
69
69
|
- lib/powertom/scanner.rb
|
|
70
70
|
- lib/powertom/table_loop.rb
|
|
71
|
+
- lib/powertom/tasks.rb
|
|
71
72
|
- lib/powertom/validator.rb
|
|
72
73
|
- lib/powertom/version.rb
|
|
73
74
|
homepage: https://github.com/ixepet/powertom
|