powertom 0.2.2 → 0.2.4
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/README.md +18 -6
- data/lib/powertom/adapter/ruby_pptx_adapter.rb +26 -2
- data/lib/powertom/grid_value.rb +10 -0
- data/lib/powertom/render.rb +63 -5
- data/lib/powertom/table_loop.rb +101 -4
- data/lib/powertom/validator.rb +46 -0
- data/lib/powertom/version.rb +2 -2
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2bd5ae9cb5cc4d857cd9411e7839bc1b5c378f521b8e2cf6bb3c7a33aba20724
|
|
4
|
+
data.tar.gz: b1bb3a91a89ff12a080a5ccaf178a8fdcaeedc330abf46b79899795024c8b32a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6aa5f809ba569163acd9c9bbf1070af3c6712bd083d6fea006b055013d0193f04c100059a085a2516fdefd22a2b6642ddba38f593b0ce42a63f7b206b1bec35e
|
|
7
|
+
data.tar.gz: e93148568cd8eb0c13f679c77a0ca9c6b0be9597596077dc8c2b4ac167774c6067d5862ceddaa0467b076e58be36536fbbef5105d0ea5b02286212ad9a7fbb90
|
data/README.md
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
# PowerTom — PowerPoint template engine for Ruby
|
|
2
2
|
|
|
3
|
+
[](https://rubygems.org/gems/powertom)
|
|
4
|
+
|
|
3
5
|
> Designers keep the template in PowerPoint. Engineers pass a Ruby Hash (or a TOML file) and get a finished deck.
|
|
4
6
|
> Layout, fonts, colours, masters and theme stay under the designer's control; the code only scans tags, binds data and clones slides.
|
|
5
7
|
|
|
8
|
+
```ruby
|
|
9
|
+
require "powertom"
|
|
10
|
+
|
|
11
|
+
data = { title: "Final exams", classes: [{ name: "Grade 8 (3)", avg: 82.4 }] }
|
|
12
|
+
PowerTom.render("template.pptx", data: data).save("report.pptx")
|
|
13
|
+
```
|
|
14
|
+
|
|
6
15
|
<sub>Not affiliated with Microsoft or TomTom.</sub>
|
|
7
16
|
|
|
8
17
|
---
|
|
@@ -44,9 +53,6 @@ PowerTom fills the middle layer: a **template engine on top of [`ruby_pptx`](htt
|
|
|
44
53
|
```ruby
|
|
45
54
|
# Gemfile
|
|
46
55
|
gem "powertom"
|
|
47
|
-
|
|
48
|
-
# Chemin local tant que le gem n'est pas publié sur RubyGems
|
|
49
|
-
# gem "powertom", path: "/chemin/vers/PowerTom"
|
|
50
56
|
```
|
|
51
57
|
|
|
52
58
|
Requires Ruby >= 3.3; depends on `ruby_pptx ~> 0.2.0` (read/write) and `tomlrb ~> 3.0` (data files).
|
|
@@ -102,6 +108,8 @@ result = PowerTom.render("templates/report.pptx", data: {
|
|
|
102
108
|
|
|
103
109
|
## Template syntax
|
|
104
110
|
|
|
111
|
+
> Full tag reference: [docs/usage.md](https://github.com/ixepet/powertom/blob/main/docs/usage.md) — data shape, every tag form, table blocks, formatters, images and errors.
|
|
112
|
+
|
|
105
113
|
| Syntax | Where | Meaning |
|
|
106
114
|
| --- | --- | --- |
|
|
107
115
|
| `{{title}}` | body | Bind a field |
|
|
@@ -113,6 +121,7 @@ result = PowerTom.render("templates/report.pptx", data: {
|
|
|
113
121
|
| `{{#rows groups}} ... {{/rows}}` | table rows | Paired row block: clones all rows between the markers per item (up to 3 levels) |
|
|
114
122
|
| `{{#cols subjects}} ... {{/cols}}` | table row | Column band: one or more template columns; may be declared in the header or inside a row block |
|
|
115
123
|
| `{{#if groups[].flag}} ... {{/if}}` | table rows | Conditional row block inside a `#rows` block |
|
|
124
|
+
| `{{#if subjects[].show}} ... {{/if}}` | table header | Conditional columns inside a `#cols` band |
|
|
116
125
|
| `{{price:currency}}` | body | Formatter: ¥1,234.56 |
|
|
117
126
|
| `{{ratio:percent.1}}` | body | Formatter: 29.7% |
|
|
118
127
|
| `{{day:date.year_month}}` | body | Formatter: 2026年11月 |
|
|
@@ -122,11 +131,12 @@ result = PowerTom.render("templates/report.pptx", data: {
|
|
|
122
131
|
- **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.
|
|
123
132
|
- **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}}`).
|
|
124
133
|
- **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]`).
|
|
125
|
-
- **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
|
|
134
|
+
- **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.
|
|
126
135
|
- **Pictures**: name the shape or its alt text `powertom:image:cover` and pass a file path in the data. Replacement always fills the shape and crops from the centre.
|
|
127
136
|
- One directive per notes page, maximum.
|
|
128
137
|
- TOML dates are written unquoted (`report_date = 2026-01-20`), normalised to Ruby `Date`, and can be formatted with `{{report_date:date.cn}}`.
|
|
129
|
-
- Full guide for designers (Chinese): [docs/模板制作规范.md](docs/模板制作规范.md).
|
|
138
|
+
- Full guide for designers (Chinese): [docs/模板制作规范.md](https://github.com/ixepet/powertom/blob/main/docs/模板制作规范.md).
|
|
139
|
+
- Full usage reference (English): [docs/usage.md](https://github.com/ixepet/powertom/blob/main/docs/usage.md).
|
|
130
140
|
|
|
131
141
|
Built-in formatters: `currency`, `percent`, `number`, `date` (with `.year_month` / `.cn` / `.short`).
|
|
132
142
|
Register your own:
|
|
@@ -328,6 +338,8 @@ result = PowerTom.render("templates/report.pptx", data: {
|
|
|
328
338
|
|
|
329
339
|
## Syntaxe des modèles
|
|
330
340
|
|
|
341
|
+
> Référence complète : [docs/usage.md](https://github.com/ixepet/powertom/blob/main/docs/usage.md) — données, balises, blocs de tableaux, formateurs, images et erreurs.
|
|
342
|
+
|
|
331
343
|
| Syntaxe | Emplacement | Signification |
|
|
332
344
|
| --- | --- | --- |
|
|
333
345
|
| `{{title}}` | corps | Lier un champ |
|
|
@@ -352,7 +364,7 @@ result = PowerTom.render("templates/report.pptx", data: {
|
|
|
352
364
|
- **Images** : nommez la forme ou son texte de remplacement `powertom:image:cover` et fournissez un chemin de fichier dans les données. Le remplacement remplit toujours la forme et recadre depuis le centre.
|
|
353
365
|
- Une seule directive par page de notes, au maximum.
|
|
354
366
|
- Les dates TOML s'écrivent sans guillemets (`report_date = 2026-01-20`), sont normalisées en `Date` Ruby et peuvent être formatées avec `{{report_date:date.cn}}`.
|
|
355
|
-
- Guide complet pour les designers (en chinois) : [docs/模板制作规范.md](docs/模板制作规范.md).
|
|
367
|
+
- Guide complet pour les designers (en chinois) : [docs/模板制作规范.md](https://github.com/ixepet/powertom/blob/main/docs/模板制作规范.md).
|
|
356
368
|
|
|
357
369
|
Formateurs intégrés : `currency`, `percent`, `number`, `date` (avec `.year_month` / `.cn` / `.short`).
|
|
358
370
|
Ajoutez les vôtres :
|
|
@@ -200,11 +200,11 @@ module PowerTom
|
|
|
200
200
|
true
|
|
201
201
|
end
|
|
202
202
|
|
|
203
|
-
# 列带标记({{#cols}} / {{/cols}})在列克隆前移除,避免被复制到每一列
|
|
203
|
+
# 列带标记({{#cols}} / {{/cols}} / 列条件)在列克隆前移除,避免被复制到每一列
|
|
204
204
|
def strip_column_markers(table_element, row_index, first_col, last_col)
|
|
205
205
|
cells = table_element.tr_list[row_index].tc_list
|
|
206
206
|
(first_col..last_col).each do |c|
|
|
207
|
-
strip_tags_in_cell(cells[c], /\{\{\s*(?:#cols\b[^{}]*|\/cols)\s*\}\}/)
|
|
207
|
+
strip_tags_in_cell(cells[c], /\{\{\s*(?:#cols\b[^{}]*|\/cols|#if\b[^{}]*|\/if)\s*\}\}/)
|
|
208
208
|
end
|
|
209
209
|
true
|
|
210
210
|
end
|
|
@@ -274,6 +274,23 @@ module PowerTom
|
|
|
274
274
|
template_cells.each { |cell| tr.remove(cell) }
|
|
275
275
|
end
|
|
276
276
|
sync_table_height(table_element, frame_element)
|
|
277
|
+
sync_table_width(table_element, frame_element)
|
|
278
|
+
true
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# 按原始列索引删除若干列(从右到左),用于列级条件
|
|
282
|
+
def remove_table_columns(table_element, frame_element, indices)
|
|
283
|
+
return true if indices.empty?
|
|
284
|
+
|
|
285
|
+
grid = table_element.xpath("./a:tblGrid").first
|
|
286
|
+
grid_cols = grid.gridCol_list
|
|
287
|
+
indices.uniq.sort.reverse_each do |index|
|
|
288
|
+
grid.remove(grid_cols[index])
|
|
289
|
+
table_element.tr_list.each do |tr|
|
|
290
|
+
tr.remove(tr.tc_list[index])
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
sync_table_width(table_element, frame_element)
|
|
277
294
|
true
|
|
278
295
|
end
|
|
279
296
|
|
|
@@ -384,6 +401,13 @@ module PowerTom
|
|
|
384
401
|
ext&.set("cy", total.to_s)
|
|
385
402
|
end
|
|
386
403
|
|
|
404
|
+
# 表格实际宽度 = 各 gridCol 的 w 之和;写回 graphicFrame 的 a:ext/@cx
|
|
405
|
+
def sync_table_width(table_element, frame_element)
|
|
406
|
+
total = table_element.xpath("./a:tblGrid/a:gridCol").sum { |col| col.get("w").to_i }
|
|
407
|
+
ext = frame_element.xpath("./p:xfrm/a:ext").first
|
|
408
|
+
ext&.set("cx", total.to_s)
|
|
409
|
+
end
|
|
410
|
+
|
|
387
411
|
def collect_container_texts(entries, container, kind)
|
|
388
412
|
label = begin
|
|
389
413
|
name = container.part.name
|
data/lib/powertom/grid_value.rb
CHANGED
|
@@ -29,6 +29,16 @@ module PowerTom
|
|
|
29
29
|
value_paths.size == 1 ? value_paths.first : value_paths[template_index]
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# 列条件取值:优先当前列项,缺失时回退整页数据(支持 subjects[].show 完整路径)
|
|
33
|
+
def column_condition_value(path, col, band_path, data)
|
|
34
|
+
relative = path.start_with?("#{band_path}[].") ? path.delete_prefix("#{band_path}[].") : path
|
|
35
|
+
begin
|
|
36
|
+
PathResolver.resolve(col, relative)
|
|
37
|
+
rescue MissingDataError
|
|
38
|
+
PathResolver.resolve(data, relative)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
32
42
|
def cell_value(row, col, value_path)
|
|
33
43
|
key = column_key(col)
|
|
34
44
|
container = value_path ? PathResolver.resolve(row, value_path) : row
|
data/lib/powertom/render.rb
CHANGED
|
@@ -275,13 +275,17 @@ module PowerTom
|
|
|
275
275
|
ctx = { slide_no: table[:slide_no], shape: table[:shape] }
|
|
276
276
|
band = plan[:cols]
|
|
277
277
|
cols = []
|
|
278
|
+
column_plan = []
|
|
278
279
|
if band
|
|
279
280
|
cols = fetch_loop_array(binder, band[:path], table, ctx)
|
|
280
281
|
check_grid_columns!(cols, table, ctx)
|
|
282
|
+
visibility = column_visibility(band, cols)
|
|
283
|
+
column_plan = build_column_plan(band, cols, visibility)
|
|
281
284
|
# 先剥离标记,再复制列:否则标记会被复制到每一列
|
|
282
285
|
adapter.strip_column_markers(table[:element], band[:row], band[:first_col], band[:last_col])
|
|
283
286
|
adapter.expand_column_band(table[:element], table[:frame],
|
|
284
287
|
band[:first_col], band[:last_col], cols.size)
|
|
288
|
+
remove_hidden_columns(adapter, table, band, visibility)
|
|
285
289
|
end
|
|
286
290
|
|
|
287
291
|
# 行索引在列克隆后不变,先捕获顶层区块的开闭行元素
|
|
@@ -307,7 +311,59 @@ module PowerTom
|
|
|
307
311
|
end
|
|
308
312
|
adapter.refresh_table_height(table[:element], table[:frame])
|
|
309
313
|
|
|
310
|
-
{ kind: :blocks, records: records, cols: cols, band: band
|
|
314
|
+
{ kind: :blocks, records: records, cols: cols, band: band,
|
|
315
|
+
column_plan: column_plan }
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
# 列条件:按列项求值,返回 [item][template_column] 的可见矩阵
|
|
319
|
+
def column_visibility(band, cols)
|
|
320
|
+
width = band[:width]
|
|
321
|
+
return cols.map { Array.new(width, true) } if band[:ifs].nil? || band[:ifs].empty?
|
|
322
|
+
|
|
323
|
+
cols.map do |col|
|
|
324
|
+
values = band[:ifs].to_h do |node|
|
|
325
|
+
[node, column_condition_truthy?(node[:path], col, band)]
|
|
326
|
+
end
|
|
327
|
+
Array.new(width) do |template_index|
|
|
328
|
+
target = band[:first_col] + template_index
|
|
329
|
+
node = band[:ifs].find { |n| target.between?(n[:open_col], n[:close_col]) }
|
|
330
|
+
node.nil? || values[node]
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def column_condition_truthy?(path, col, band)
|
|
336
|
+
value = GridValue.column_condition_value(path, col, band[:path], @data)
|
|
337
|
+
!(value.nil? || value == false)
|
|
338
|
+
rescue MissingDataError => e
|
|
339
|
+
raise MissingDataError, "{{#if #{path}}} 无法求值:#{e.message}"
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# 展开后的列计划:每个输出列对应哪个列项、哪个模板列
|
|
343
|
+
def build_column_plan(band, cols, visibility)
|
|
344
|
+
plan = []
|
|
345
|
+
cols.each_with_index do |col, item_index|
|
|
346
|
+
band[:width].times do |template_index|
|
|
347
|
+
next unless visibility[item_index][template_index]
|
|
348
|
+
|
|
349
|
+
plan << { col: col, template_index: template_index }
|
|
350
|
+
end
|
|
351
|
+
end
|
|
352
|
+
plan
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def remove_hidden_columns(adapter, table, band, visibility)
|
|
356
|
+
hidden = []
|
|
357
|
+
visibility.each_with_index do |item, item_index|
|
|
358
|
+
item.each_with_index do |visible, template_index|
|
|
359
|
+
next if visible
|
|
360
|
+
|
|
361
|
+
hidden << band[:first_col] + item_index * band[:width] + template_index
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
return if hidden.empty?
|
|
365
|
+
|
|
366
|
+
adapter.remove_table_columns(table[:element], table[:frame], hidden)
|
|
311
367
|
end
|
|
312
368
|
|
|
313
369
|
# 子区块的数组依赖父项:按每个父项解析一次,供行计划与展开复用
|
|
@@ -510,11 +566,13 @@ module PowerTom
|
|
|
510
566
|
|
|
511
567
|
col_index = ctx[:col_index].to_i
|
|
512
568
|
start = band[:first_col]
|
|
513
|
-
|
|
514
|
-
|
|
569
|
+
return nil if col_index < start
|
|
570
|
+
|
|
571
|
+
entry = scope[:column_plan][col_index - start]
|
|
572
|
+
return nil unless entry
|
|
515
573
|
|
|
516
|
-
col =
|
|
517
|
-
template_index =
|
|
574
|
+
col = entry[:col]
|
|
575
|
+
template_index = entry[:template_index]
|
|
518
576
|
value_path = GridValue.band_value_path(band[:value_paths], template_index)
|
|
519
577
|
record = scope[:records][ctx[:row_index]]
|
|
520
578
|
row = record && record[:row_item]
|
data/lib/powertom/table_loop.rb
CHANGED
|
@@ -411,9 +411,11 @@ module PowerTom
|
|
|
411
411
|
end
|
|
412
412
|
|
|
413
413
|
def blocks_plan(rows, merged_rows)
|
|
414
|
-
row_tags, col_tags = collect_block_tags(rows)
|
|
415
|
-
blocks = parse_block_tree(rows, row_tags)
|
|
414
|
+
row_tags, col_tags, if_tags = collect_block_tags(rows)
|
|
416
415
|
band = parse_column_band(rows, row_tags, col_tags)
|
|
416
|
+
col_ifs, row_ifs = split_ifs(if_tags, band)
|
|
417
|
+
band[:ifs] = parse_column_ifs(rows, col_ifs, band) if band
|
|
418
|
+
blocks = parse_block_tree(rows, (row_tags + row_ifs).sort_by { |t| t[:row] })
|
|
417
419
|
if band && band_in_if?(blocks, band[:row])
|
|
418
420
|
raise TemplateSyntaxError, "表格第 #{band[:row] + 1} 行:列带不能写在条件区块内"
|
|
419
421
|
end
|
|
@@ -427,15 +429,18 @@ module PowerTom
|
|
|
427
429
|
def collect_block_tags(rows)
|
|
428
430
|
row_tags = []
|
|
429
431
|
col_tags = []
|
|
432
|
+
if_tags = []
|
|
430
433
|
rows.each_with_index do |cells, r|
|
|
431
434
|
cells.each_with_index do |text, c|
|
|
432
435
|
directive_tags(text).each do |tag|
|
|
433
436
|
raw = raw_of(tag)
|
|
434
437
|
item = { row: r, col: c, raw: raw, tag: tag }
|
|
435
|
-
if raw.start_with?("#rows", "/rows"
|
|
438
|
+
if raw.start_with?("#rows", "/rows")
|
|
436
439
|
row_tags << item
|
|
437
440
|
elsif raw.start_with?("#cols", "/cols")
|
|
438
441
|
col_tags << item
|
|
442
|
+
elsif raw.start_with?("#if", "/if")
|
|
443
|
+
if_tags << item
|
|
439
444
|
else
|
|
440
445
|
raise TemplateSyntaxError,
|
|
441
446
|
"表格第 #{r + 1} 行:区块模式不支持 #{tag}(只支持 #rows / #cols / #if 区块)"
|
|
@@ -443,7 +448,99 @@ module PowerTom
|
|
|
443
448
|
end
|
|
444
449
|
end
|
|
445
450
|
end
|
|
446
|
-
[row_tags, col_tags]
|
|
451
|
+
[row_tags, col_tags, if_tags]
|
|
452
|
+
end
|
|
453
|
+
|
|
454
|
+
# 列带表头行内、位于列带范围内的 #if 是列级条件;其余是行级条件
|
|
455
|
+
def split_ifs(tags, band)
|
|
456
|
+
return [[], tags] unless band
|
|
457
|
+
|
|
458
|
+
col_ifs = []
|
|
459
|
+
row_ifs = []
|
|
460
|
+
tags.each do |tag|
|
|
461
|
+
if tag[:row] == band[:row] && tag[:col].between?(band[:first_col], band[:last_col])
|
|
462
|
+
col_ifs << tag
|
|
463
|
+
else
|
|
464
|
+
row_ifs << tag
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
[col_ifs, row_ifs]
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
def parse_column_ifs(rows, tags, band)
|
|
471
|
+
return [] if tags.empty?
|
|
472
|
+
|
|
473
|
+
stack = []
|
|
474
|
+
result = []
|
|
475
|
+
tags.each do |item|
|
|
476
|
+
if item[:raw].start_with?("#if")
|
|
477
|
+
node = open_column_if(rows, item, band, stack)
|
|
478
|
+
stack.push(node)
|
|
479
|
+
else
|
|
480
|
+
close_column_if(rows, item, band, stack, result)
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
unless stack.empty?
|
|
484
|
+
node = stack.last
|
|
485
|
+
raise TemplateSyntaxError,
|
|
486
|
+
"表格第 #{band[:row] + 1} 行:列条件 {{#if #{node[:path]}}} 缺少 {{/if}}"
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
result.sort_by { |node| node[:open_col] }.each_cons(2) do |left, right|
|
|
490
|
+
next if right[:open_col] > left[:close_col]
|
|
491
|
+
|
|
492
|
+
raise TemplateSyntaxError,
|
|
493
|
+
"表格第 #{band[:row] + 1} 行:列条件范围重叠" \
|
|
494
|
+
"(第 #{left[:open_col] + 1}–#{left[:close_col] + 1} 列与 " \
|
|
495
|
+
"#{right[:open_col] + 1}–#{right[:close_col] + 1} 列)"
|
|
496
|
+
end
|
|
497
|
+
result
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
def open_column_if(rows, item, band, stack)
|
|
501
|
+
validate_column_if_marker!(rows, item, band)
|
|
502
|
+
path = item[:tag][IF_OPEN_RE, 1]
|
|
503
|
+
unless path
|
|
504
|
+
raise TemplateSyntaxError,
|
|
505
|
+
"表格第 #{band[:row] + 1} 行:#{item[:tag]} 列条件标记不合法,应为 {{#if 路径}}"
|
|
506
|
+
end
|
|
507
|
+
if path.include?("[]") && !path.start_with?("#{band[:path]}[]")
|
|
508
|
+
raise TemplateSyntaxError,
|
|
509
|
+
"表格第 #{band[:row] + 1} 行:{{#if #{path}}} 只能引用当前列数组 " \
|
|
510
|
+
"#{band[:path]}[] 或整页字段"
|
|
511
|
+
end
|
|
512
|
+
if stack.any?
|
|
513
|
+
raise TemplateSyntaxError, "表格第 #{band[:row] + 1} 行:列条件不能嵌套"
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
{ path: path, open_col: item[:col], close_col: nil }
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
def close_column_if(rows, item, band, stack, result)
|
|
520
|
+
validate_column_if_marker!(rows, item, band)
|
|
521
|
+
if stack.empty?
|
|
522
|
+
raise TemplateSyntaxError,
|
|
523
|
+
"表格第 #{band[:row] + 1} 行:{{/if}} 没有对应的列条件 {{#if}}"
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
node = stack.pop
|
|
527
|
+
if item[:col] < node[:open_col]
|
|
528
|
+
raise TemplateSyntaxError,
|
|
529
|
+
"表格第 #{band[:row] + 1} 行:{{/if}} 必须在列条件 {{#if}} 右侧"
|
|
530
|
+
end
|
|
531
|
+
node[:close_col] = item[:col]
|
|
532
|
+
result << node
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
def validate_column_if_marker!(rows, item, band)
|
|
536
|
+
line = "表格第 #{band[:row] + 1} 行"
|
|
537
|
+
unless item[:row] == band[:row] &&
|
|
538
|
+
item[:col].between?(band[:first_col], band[:last_col])
|
|
539
|
+
raise TemplateSyntaxError, "#{line}:列条件必须写在列带表头范围内"
|
|
540
|
+
end
|
|
541
|
+
return if rows[item[:row]][item[:col]].to_s.include?(item[:tag])
|
|
542
|
+
|
|
543
|
+
raise TemplateSyntaxError, "#{line}:#{item[:tag]} 列条件标记定位失败"
|
|
447
544
|
end
|
|
448
545
|
|
|
449
546
|
def parse_block_tree(rows, tags)
|
data/lib/powertom/validator.rb
CHANGED
|
@@ -162,6 +162,7 @@ module PowerTom
|
|
|
162
162
|
ctx = l[:context]
|
|
163
163
|
next if ctx[:table_shape] && failed_tables.include?([ctx[:slide_no], ctx[:table_shape]])
|
|
164
164
|
next if block_row_stacks.dig([ctx[:slide_no], ctx[:table_shape]], ctx[:row_index])
|
|
165
|
+
next if block_band_cell?(table_plans, ctx)
|
|
165
166
|
|
|
166
167
|
grid = grid_plan_for(table_plans, ctx)
|
|
167
168
|
if grid && grid_variable_path?(l[:path])
|
|
@@ -182,6 +183,7 @@ module PowerTom
|
|
|
182
183
|
next if l[:path].include?("[]")
|
|
183
184
|
next if block_row_stacks.dig([l[:context][:slide_no], l[:context][:table_shape]],
|
|
184
185
|
l[:context][:row_index])
|
|
186
|
+
next if block_band_cell?(table_plans, l[:context])
|
|
185
187
|
next if grid_plan_for(table_plans, l[:context]) && grid_variable_path?(l[:path])
|
|
186
188
|
|
|
187
189
|
overflow_check(l, directives[l[:context][:slide_no]], data, entries)
|
|
@@ -199,6 +201,10 @@ module PowerTom
|
|
|
199
201
|
used_roots << root_of(l[:path]) unless grid_variable_path?(l[:path])
|
|
200
202
|
next
|
|
201
203
|
end
|
|
204
|
+
if block_band_cell?(table_plans, l[:context])
|
|
205
|
+
used_roots << root_of(l[:path]) unless grid_variable_path?(l[:path])
|
|
206
|
+
next
|
|
207
|
+
end
|
|
202
208
|
next if grid_plan_for(table_plans, l[:context]) && grid_variable_path?(l[:path])
|
|
203
209
|
|
|
204
210
|
used_roots << root_of(l[:path])
|
|
@@ -447,6 +453,17 @@ module PowerTom
|
|
|
447
453
|
path == "col" || path.start_with?("col.")
|
|
448
454
|
end
|
|
449
455
|
|
|
456
|
+
# 该单元格是否位于区块表格的列带范围内(含不在行区块内的表头行)
|
|
457
|
+
def block_band_cell?(plans, context)
|
|
458
|
+
return false unless context[:table_shape]
|
|
459
|
+
|
|
460
|
+
plan = plans[[context[:slide_no], context[:table_shape]]]
|
|
461
|
+
return false unless plan && plan[:kind] == :blocks && plan[:cols]
|
|
462
|
+
|
|
463
|
+
band = plan[:cols]
|
|
464
|
+
context[:col_index].to_i.between?(band[:first_col], band[:last_col])
|
|
465
|
+
end
|
|
466
|
+
|
|
450
467
|
# ---- v0.2.0:行区块 + 列带对账 ----
|
|
451
468
|
|
|
452
469
|
def reconcile_block_tables(plans, labels_by_row, data, entries)
|
|
@@ -455,10 +472,39 @@ module PowerTom
|
|
|
455
472
|
|
|
456
473
|
ctx = { slide_no: slide_no, shape: shape }
|
|
457
474
|
cols = plan[:cols] ? (resolve_grid_array(plan[:cols][:path], ctx, data, entries) || []) : []
|
|
475
|
+
reconcile_column_ifs(plan[:cols], cols, ctx, data, entries)
|
|
476
|
+
reconcile_static_band_header(plan, cols, labels_by_row, ctx, data, entries)
|
|
458
477
|
reconcile_blocks(plan[:blocks], {}, nil, cols, plan[:cols], labels_by_row, ctx, data, entries)
|
|
459
478
|
end
|
|
460
479
|
end
|
|
461
480
|
|
|
481
|
+
# 列带表头若不在行区块内,其 {{col.*}} 也要单独对账
|
|
482
|
+
def reconcile_static_band_header(plan, cols, labels_by_row, ctx, data, entries)
|
|
483
|
+
band = plan[:cols]
|
|
484
|
+
return unless band
|
|
485
|
+
|
|
486
|
+
stacks = TableLoop.build_row_stacks(plan[:blocks])
|
|
487
|
+
return unless stacks[band[:row]].nil?
|
|
488
|
+
|
|
489
|
+
labels_by_row[[ctx[:slide_no], ctx[:shape], band[:row]]].each do |label|
|
|
490
|
+
reconcile_block_label(label, {}, nil, cols, band, "列带", data, entries)
|
|
491
|
+
end
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# 列条件:每个列项都要能取到条件字段
|
|
495
|
+
def reconcile_column_ifs(band, cols, ctx, data, entries)
|
|
496
|
+
return unless band && band[:ifs]&.any?
|
|
497
|
+
|
|
498
|
+
band[:ifs].each do |node|
|
|
499
|
+
cols.each_with_index do |col, index|
|
|
500
|
+
GridValue.column_condition_value(node[:path], col, band[:path], data)
|
|
501
|
+
rescue MissingDataError => e
|
|
502
|
+
entries << error_entry(ctx[:slide_no], ctx[:shape], node[:path],
|
|
503
|
+
"列条件第 #{index + 1} 项:#{e.message}")
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
end
|
|
507
|
+
|
|
462
508
|
# 与渲染同一套行计划:按区块逐项展开,再对每一行模板里的标签对账
|
|
463
509
|
def reconcile_blocks(blocks, parent_scope, parent_item, cols, band,
|
|
464
510
|
labels_by_row, ctx, data, entries)
|
data/lib/powertom/version.rb
CHANGED
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.2.
|
|
4
|
+
version: 0.2.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- PowerTom contributors
|
|
@@ -37,7 +37,12 @@ dependencies:
|
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '3.0'
|
|
40
|
-
description:
|
|
40
|
+
description: |-
|
|
41
|
+
PowerTom is a PowerPoint template engine for Ruby. Designers keep the template in PowerPoint; engineers pass a Ruby Hash or a TOML file and get a finished deck. Layout, fonts, colours, masters and theme stay under the designer's control.
|
|
42
|
+
|
|
43
|
+
PowerTom est un moteur de modèles PowerPoint pour Ruby. Les designers conservent le modèle dans PowerPoint ; les ingénieurs passent un Hash Ruby ou un fichier TOML et obtiennent un deck finalisé. La mise en page, les polices, les couleurs, les masques et le thème restent sous le contrôle du designer.
|
|
44
|
+
|
|
45
|
+
设计师在 PowerPoint 里维护模板,工程师传一个 Ruby Hash 或 TOML 文件,产出成品 deck。排版、字体、配色、母版和主题完全由设计工具掌控。
|
|
41
46
|
executables: []
|
|
42
47
|
extensions: []
|
|
43
48
|
extra_rdoc_files: []
|
|
@@ -64,6 +69,7 @@ licenses:
|
|
|
64
69
|
metadata:
|
|
65
70
|
source_code_uri: https://github.com/ixepet/powertom
|
|
66
71
|
bug_tracker_uri: https://github.com/ixepet/powertom/issues
|
|
72
|
+
documentation_uri: https://github.com/ixepet/powertom/blob/main/docs/usage.md
|
|
67
73
|
rdoc_options: []
|
|
68
74
|
require_paths:
|
|
69
75
|
- lib
|