powertom 0.2.5 → 0.2.7
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 +81 -0
- data/README.md +15 -240
- data/lib/powertom/adapter/page_surgery.rb +13 -5
- data/lib/powertom/adapter/ruby_pptx_adapter.rb +54 -12
- data/lib/powertom/binder.rb +10 -14
- data/lib/powertom/formatter.rb +5 -3
- data/lib/powertom/path_resolver.rb +4 -5
- data/lib/powertom/render.rb +38 -12
- data/lib/powertom/run_merge.rb +1 -0
- data/lib/powertom/scanner.rb +20 -15
- data/lib/powertom/table_loop.rb +36 -58
- data/lib/powertom/validator.rb +46 -23
- data/lib/powertom/version.rb +2 -2
- data/lib/powertom.rb +35 -13
- metadata +8 -7
data/lib/powertom/binder.rb
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "date"
|
|
4
|
-
require "set"
|
|
5
4
|
require_relative "path_resolver"
|
|
6
5
|
require_relative "formatter"
|
|
7
6
|
|
|
@@ -34,9 +33,11 @@ module PowerTom
|
|
|
34
33
|
# §2.2:nil 渲染为空字符串,formatter 不参与(避免 currency 之类的报错)
|
|
35
34
|
return "" if value.nil?
|
|
36
35
|
return Formatter.apply(value, spec) if spec
|
|
36
|
+
|
|
37
37
|
to_text(value, path)
|
|
38
38
|
rescue MissingDataError => e
|
|
39
39
|
raise if @strict
|
|
40
|
+
|
|
40
41
|
@warnings << "#{e.message},已填空字符串"
|
|
41
42
|
""
|
|
42
43
|
end
|
|
@@ -53,6 +54,7 @@ module PowerTom
|
|
|
53
54
|
end
|
|
54
55
|
rescue MissingDataError => e
|
|
55
56
|
raise if @strict
|
|
57
|
+
|
|
56
58
|
@warnings << "#{e.message}(#{purpose})"
|
|
57
59
|
nil
|
|
58
60
|
end
|
|
@@ -77,11 +79,9 @@ module PowerTom
|
|
|
77
79
|
end
|
|
78
80
|
|
|
79
81
|
def resolve(path, context)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
raise MissingDataError, "#{describe(context)}:{{#{path}}} #{e.message}"
|
|
84
|
-
end
|
|
82
|
+
PathResolver.resolve(context[:scope] || @data, path)
|
|
83
|
+
rescue MissingDataError => e
|
|
84
|
+
raise MissingDataError, "#{describe(context)}:{{#{path}}} #{e.message}"
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
# 表格行标签:{{数组[].字段}} / {{父数组[].子数组[].字段}} 相对当前行作用域取值。
|
|
@@ -89,9 +89,7 @@ module PowerTom
|
|
|
89
89
|
# #grid 作用域另见 resolve_grid_label({{row.x}} / {{col.y}} / {{cell}})。
|
|
90
90
|
def resolve_label(path, context)
|
|
91
91
|
return resolve_grid_label(path, context) if context[:grid_scope]
|
|
92
|
-
if context[:row_item] && (path == "row" || path.start_with?("row."))
|
|
93
|
-
return resolve_row_item_label(path, context)
|
|
94
|
-
end
|
|
92
|
+
return resolve_row_item_label(path, context) if context[:row_item] && (path == "row" || path.start_with?("row."))
|
|
95
93
|
return resolve(path, context) unless path.include?("[]")
|
|
96
94
|
|
|
97
95
|
resolve_row_label(path, context)
|
|
@@ -149,11 +147,9 @@ module PowerTom
|
|
|
149
147
|
end
|
|
150
148
|
|
|
151
149
|
def resolve_grid_item(item, remainder, path, context)
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
raise MissingDataError, "#{describe(context)}:{{#{path}}} #{e.message}"
|
|
156
|
-
end
|
|
150
|
+
PathResolver.resolve(item, remainder)
|
|
151
|
+
rescue MissingDataError => e
|
|
152
|
+
raise MissingDataError, "#{describe(context)}:{{#{path}}} #{e.message}"
|
|
157
153
|
end
|
|
158
154
|
|
|
159
155
|
def resolve_row_label(path, context)
|
data/lib/powertom/formatter.rb
CHANGED
|
@@ -13,6 +13,7 @@ module PowerTom
|
|
|
13
13
|
def register(name, &block)
|
|
14
14
|
raise ArgumentError, "formatter 名不能为空" if name.nil? || name.to_s.empty?
|
|
15
15
|
raise ArgumentError, "formatter 需要接收 (value, arg) 的 block" unless block
|
|
16
|
+
|
|
16
17
|
registry[name.to_s] = block
|
|
17
18
|
name.to_s
|
|
18
19
|
end
|
|
@@ -32,6 +33,7 @@ module PowerTom
|
|
|
32
33
|
# spec:"currency" / "percent.1" / "date.year_month";nil → nil(无格式化)
|
|
33
34
|
def apply(value, spec)
|
|
34
35
|
return nil if spec.nil?
|
|
36
|
+
|
|
35
37
|
name, arg = parse_spec(spec)
|
|
36
38
|
proc = registry[name]
|
|
37
39
|
unless proc
|
|
@@ -60,6 +62,7 @@ module PowerTom
|
|
|
60
62
|
|
|
61
63
|
def numeric!(value, name)
|
|
62
64
|
return value if value.is_a?(Numeric)
|
|
65
|
+
|
|
63
66
|
raise PowerTom::Error, "formatter '#{name}' 要求数字,得到 #{value.class}"
|
|
64
67
|
end
|
|
65
68
|
|
|
@@ -101,9 +104,8 @@ module PowerTom
|
|
|
101
104
|
}.freeze
|
|
102
105
|
|
|
103
106
|
register("date") do |value, arg|
|
|
104
|
-
unless value.respond_to?(:strftime)
|
|
105
|
-
|
|
106
|
-
end
|
|
107
|
+
raise PowerTom::Error, "formatter 'date' 要求 Date/Time,得到 #{value.class}" unless value.respond_to?(:strftime)
|
|
108
|
+
|
|
107
109
|
pattern = DATE_PATTERNS.fetch(arg) do
|
|
108
110
|
raise PowerTom::Error,
|
|
109
111
|
"formatter 'date' 不支持 '#{arg}'(可用:nil, #{DATE_PATTERNS.keys.compact.join(', ')})"
|
|
@@ -7,7 +7,7 @@ module PowerTom
|
|
|
7
7
|
# 抛出的 MissingDataError 只含原因短语(如"数据里没有字段 'x'"),
|
|
8
8
|
# 页码/形状/标签等定位信息由调用方补充。
|
|
9
9
|
module PathResolver
|
|
10
|
-
SEGMENT_RE = /\A([A-Za-z0-9_]+)(?:\[(\d+)\])?\z
|
|
10
|
+
SEGMENT_RE = /\A([A-Za-z0-9_]+)(?:\[(\d+)\])?\z/
|
|
11
11
|
|
|
12
12
|
module_function
|
|
13
13
|
|
|
@@ -42,10 +42,9 @@ module PowerTom
|
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
def fetch_index(current, index,
|
|
46
|
-
unless current.is_a?(Array) && index < current.size
|
|
47
|
-
|
|
48
|
-
end
|
|
45
|
+
def fetch_index(current, index, _path)
|
|
46
|
+
raise MissingDataError, "下标 [#{index}] 越界或不在数组上" unless current.is_a?(Array) && index < current.size
|
|
47
|
+
|
|
49
48
|
current[index]
|
|
50
49
|
end
|
|
51
50
|
end
|
data/lib/powertom/render.rb
CHANGED
|
@@ -24,7 +24,9 @@ module PowerTom
|
|
|
24
24
|
Render.new(template_path, data: DataLoading.coerce(data), strict: strict).call
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
# Result of {PowerTom.render}: the edited presentation plus render warnings.
|
|
27
28
|
class RenderResult
|
|
29
|
+
# @return [Array<String>] non-fatal warnings (unused fields, fallbacks, limitations).
|
|
28
30
|
attr_reader :warnings
|
|
29
31
|
|
|
30
32
|
def initialize(adapter, prs, warnings, temp_path: nil)
|
|
@@ -34,13 +36,19 @@ module PowerTom
|
|
|
34
36
|
@temp_path = temp_path
|
|
35
37
|
end
|
|
36
38
|
|
|
39
|
+
# Save the rendered deck.
|
|
40
|
+
#
|
|
41
|
+
# @param path [String] destination +.pptx+ path.
|
|
42
|
+
# @return [String] the destination path.
|
|
37
43
|
def save(path)
|
|
38
44
|
@adapter.save(@prs, path)
|
|
39
45
|
FileUtils.remove_entry(@temp_path, true) if @temp_path
|
|
40
46
|
path
|
|
41
47
|
end
|
|
42
48
|
|
|
43
|
-
#
|
|
49
|
+
# Number of slides in the rendered deck (after cloning/removal).
|
|
50
|
+
#
|
|
51
|
+
# @return [Integer]
|
|
44
52
|
def slide_count
|
|
45
53
|
@prs.slides.size
|
|
46
54
|
end
|
|
@@ -102,7 +110,8 @@ module PowerTom
|
|
|
102
110
|
adapter.write_paragraph_runs(ctx[:paragraph], new_texts) if new_texts
|
|
103
111
|
end
|
|
104
112
|
|
|
105
|
-
replace_images(adapter, work_prs, page_scopes, binder)
|
|
113
|
+
replace_images(adapter, work_prs, page_scopes, binder, warnings)
|
|
114
|
+
report_chart_tags(adapter, work_prs, warnings)
|
|
106
115
|
|
|
107
116
|
report_unused_fields(binder, warnings)
|
|
108
117
|
report_notes_labels(notes, warnings)
|
|
@@ -149,7 +158,7 @@ module PowerTom
|
|
|
149
158
|
end
|
|
150
159
|
|
|
151
160
|
# 结构手术走字节级包操作:模板原文件 → 手术后的临时文件 → 重新打开绑定
|
|
152
|
-
def run_surgery(
|
|
161
|
+
def run_surgery(_adapter, pages)
|
|
153
162
|
tmp = Tempfile.create(["powertom_surgery", ".pptx"])
|
|
154
163
|
tmp.close
|
|
155
164
|
@work_path = tmp.path
|
|
@@ -159,7 +168,7 @@ module PowerTom
|
|
|
159
168
|
# §5.3 图片替换:powertom:image:字段名 占位 → 数据里的图片文件。
|
|
160
169
|
# 与文本一样作用域感知:#each 克隆页各自取本项的图片;缺字段(strict: false)
|
|
161
170
|
# 时保留占位图并记 warning。
|
|
162
|
-
def replace_images(adapter, prs, page_scopes, binder)
|
|
171
|
+
def replace_images(adapter, prs, page_scopes, binder, warnings)
|
|
163
172
|
adapter.picture_contexts(prs).each do |pctx|
|
|
164
173
|
page = page_scopes[pctx[:slide_no] - 1]
|
|
165
174
|
ctx = { slide_no: pctx[:slide_no], shape: pctx[:shape],
|
|
@@ -175,10 +184,29 @@ module PowerTom
|
|
|
175
184
|
raise PowerTom::Error,
|
|
176
185
|
"第 #{pctx[:slide_no]} 页 图片字段 '#{pctx[:field]}' 的文件不存在:#{value}"
|
|
177
186
|
end
|
|
187
|
+
warn_image_aspect(adapter, pctx, value, warnings)
|
|
178
188
|
adapter.replace_image(pctx[:slide], pctx[:picture], value)
|
|
179
189
|
end
|
|
180
190
|
end
|
|
181
191
|
|
|
192
|
+
# 固定策略是"填满形状、居中裁切";比例差异大时提前提示设计师
|
|
193
|
+
def warn_image_aspect(adapter, pctx, image_path, warnings)
|
|
194
|
+
mismatch = adapter.image_aspect_mismatch(pctx[:picture], image_path)
|
|
195
|
+
return unless mismatch
|
|
196
|
+
|
|
197
|
+
warnings << "第 #{pctx[:slide_no]} 页 图片 '#{pctx[:field]}':" \
|
|
198
|
+
"图片比例 #{format('%.2f', mismatch[:image_ratio])} 与占位形状 " \
|
|
199
|
+
"#{format('%.2f', mismatch[:shape_ratio])} 差异较大,将居中裁切"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# 图表不绑定数据:发现图表里残留标签时温和告警,不修改文件
|
|
203
|
+
def report_chart_tags(adapter, prs, warnings)
|
|
204
|
+
adapter.chart_tags(prs).each do |chart|
|
|
205
|
+
warnings << "第 #{chart[:slide_no]} 页 图表'#{chart[:shape]}'含标签 " \
|
|
206
|
+
"#{chart[:tags].join('、')}:v0 不绑定图表数据,已原样保留"
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
182
210
|
# 表格循环:单级行 / 两级嵌套行 / 动态列;页级 #each 与表格循环不叠加
|
|
183
211
|
def expand_table_rows(adapter, prs, page_scopes, binder)
|
|
184
212
|
scopes = {}
|
|
@@ -358,7 +386,7 @@ module PowerTom
|
|
|
358
386
|
item.each_with_index do |visible, template_index|
|
|
359
387
|
next if visible
|
|
360
388
|
|
|
361
|
-
hidden << band[:first_col] + item_index * band[:width] + template_index
|
|
389
|
+
hidden << (band[:first_col] + (item_index * band[:width]) + template_index)
|
|
362
390
|
end
|
|
363
391
|
end
|
|
364
392
|
return if hidden.empty?
|
|
@@ -439,7 +467,7 @@ module PowerTom
|
|
|
439
467
|
while row < block[:close]
|
|
440
468
|
if_node = block[:ifs].find { |node| node[:open] == row }
|
|
441
469
|
if if_node
|
|
442
|
-
(if_node[:open] + 1...if_node[:close]).each { records << record } if if_values[if_node]
|
|
470
|
+
((if_node[:open] + 1)...if_node[:close]).each { records << record } if if_values[if_node]
|
|
443
471
|
row = if_node[:close] + 1
|
|
444
472
|
next
|
|
445
473
|
end
|
|
@@ -530,9 +558,7 @@ module PowerTom
|
|
|
530
558
|
return nil unless row
|
|
531
559
|
|
|
532
560
|
result = { scope[:parent_prefix] => scope[:parents][row[:parent]] }
|
|
533
|
-
if row[:child]
|
|
534
|
-
result[scope[:child_prefix]] = scope[:children][row[:parent]][row[:child]]
|
|
535
|
-
end
|
|
561
|
+
result[scope[:child_prefix]] = scope[:children][row[:parent]][row[:child]] if row[:child]
|
|
536
562
|
result
|
|
537
563
|
end
|
|
538
564
|
end
|
|
@@ -554,9 +580,7 @@ module PowerTom
|
|
|
554
580
|
grid = {}
|
|
555
581
|
grid[:col] = col if col
|
|
556
582
|
grid[:row] = row if row
|
|
557
|
-
if row && col
|
|
558
|
-
grid[:cell] = -> { GridValue.cell_value(row, col, scope[:value_path]) }
|
|
559
|
-
end
|
|
583
|
+
grid[:cell] = -> { GridValue.cell_value(row, col, scope[:value_path]) } if row && col
|
|
560
584
|
grid.empty? ? nil : grid
|
|
561
585
|
end
|
|
562
586
|
|
|
@@ -599,6 +623,7 @@ module PowerTom
|
|
|
599
623
|
# §2.3 数据有模板未使用的字段 → warning(v0 只追踪顶层字段)
|
|
600
624
|
def report_unused_fields(binder, warnings)
|
|
601
625
|
return unless @data.is_a?(Hash)
|
|
626
|
+
|
|
602
627
|
unused = @data.keys.map(&:to_s) - binder.used_root_keys.to_a
|
|
603
628
|
unused.each { |key| warnings << "数据字段 '#{key}' 未被模板使用" }
|
|
604
629
|
end
|
|
@@ -608,6 +633,7 @@ module PowerTom
|
|
|
608
633
|
notes.each do |note|
|
|
609
634
|
Scanner.scan_notes(note[:text], note[:slide_no]).each do |found|
|
|
610
635
|
next if found[:tag] =~ /\{\{\s*#/
|
|
636
|
+
|
|
611
637
|
warnings << "第 #{found[:slide_no]} 页备注含标签 #{found[:tag]},v0 不渲染备注,已忽略"
|
|
612
638
|
end
|
|
613
639
|
end
|
data/lib/powertom/run_merge.rb
CHANGED
|
@@ -13,6 +13,7 @@ module PowerTom
|
|
|
13
13
|
# 返回新的 run 文本数组;段落里没有可替换标签时返回 nil(调用方跳过写入)
|
|
14
14
|
def merge(run_texts, resolver)
|
|
15
15
|
return nil if run_texts.nil? || run_texts.empty?
|
|
16
|
+
|
|
16
17
|
joined = run_texts.join
|
|
17
18
|
return nil unless joined.include?("{{")
|
|
18
19
|
return nil unless joined.match?(PowerTom::Scanner::LABEL_RE)
|
data/lib/powertom/scanner.rb
CHANGED
|
@@ -6,16 +6,20 @@ module PowerTom
|
|
|
6
6
|
# - 页级指令 {{#each}} / {{#if}} 只允许写在幻灯片备注(§3.3),出现在正文 = 语法错误
|
|
7
7
|
module Scanner
|
|
8
8
|
# 宽松的"疑似标签"正则,用于语法校验兜底:任何 {{...}} 都必须能通过严格校验
|
|
9
|
-
TAG_RE = /\{\{[^{}]*\}\}
|
|
9
|
+
TAG_RE = /\{\{[^{}]*\}\}/
|
|
10
10
|
|
|
11
11
|
# 严格的文本标签正则:点路径与下标 + 可选 formatter 规格(§3.4)
|
|
12
|
-
LABEL_RE =
|
|
12
|
+
LABEL_RE = /
|
|
13
|
+
\{\{\s*([A-Za-z0-9_\[\]]+(?:\.[A-Za-z0-9_\[\]]+)*)
|
|
14
|
+
(?::([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)*))?
|
|
15
|
+
\s*\}\}
|
|
16
|
+
/x
|
|
13
17
|
|
|
14
18
|
# 合法的路径段:字段名或 字段名[下标]
|
|
15
|
-
SEGMENT_RE = /\A[A-Za-z0-9_]+(?:\[\d+\])?\z
|
|
19
|
+
SEGMENT_RE = /\A[A-Za-z0-9_]+(?:\[\d+\])?\z/
|
|
16
20
|
|
|
17
21
|
# formatter 规格:词 + 可选 .参数(percent.1 / date.year_month)
|
|
18
|
-
FORMAT_SPEC_RE = /\A[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)*\z
|
|
22
|
+
FORMAT_SPEC_RE = /\A[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)*\z/
|
|
19
23
|
|
|
20
24
|
module_function
|
|
21
25
|
|
|
@@ -42,6 +46,7 @@ module PowerTom
|
|
|
42
46
|
# 备注里的标签:v0 不渲染备注,发现即记 warning(§3.3)
|
|
43
47
|
def scan_notes(notes_text, slide_no)
|
|
44
48
|
return [] unless notes_text
|
|
49
|
+
|
|
45
50
|
notes_text.scan(TAG_RE).map { |tag| { tag: tag, slide_no: slide_no } }
|
|
46
51
|
end
|
|
47
52
|
|
|
@@ -69,7 +74,7 @@ module PowerTom
|
|
|
69
74
|
end
|
|
70
75
|
|
|
71
76
|
raw = directives_in_notes.first[/\{\{\s*#\s*([^{}]*?)\s*\}\}/, 1].to_s
|
|
72
|
-
m = raw.match(/\A(each|if)\s+([A-Za-z0-9_\[\]
|
|
77
|
+
m = raw.match(/\A(each|if)\s+([A-Za-z0-9_\[\].]+)\z/)
|
|
73
78
|
unless m
|
|
74
79
|
raise TemplateSyntaxError,
|
|
75
80
|
"第 #{slide_no} 页备注指令 {{##{raw}}} 不合法,只支持 {{#each 路径}} 与 {{#if 路径}}"
|
|
@@ -80,7 +85,7 @@ module PowerTom
|
|
|
80
85
|
def validate!(raw, tag, ctx)
|
|
81
86
|
where = "第 #{ctx[:slide_no]} 页 形状'#{ctx[:shape]}'"
|
|
82
87
|
if raw.start_with?("#") || raw.start_with?("/")
|
|
83
|
-
if ctx[:in_table] && raw.match?(
|
|
88
|
+
if ctx[:in_table] && raw.match?(%r{\A#(rows|grid|cols|if)\b|\A/(rows|cols|if)\z})
|
|
84
89
|
validate_table_directive!(raw, tag, where)
|
|
85
90
|
return
|
|
86
91
|
end
|
|
@@ -89,18 +94,18 @@ module PowerTom
|
|
|
89
94
|
end
|
|
90
95
|
path, spec = raw.split(":", 2)
|
|
91
96
|
validate_path!(path, tag, where, ctx)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
97
|
+
return unless spec && !spec.match?(FORMAT_SPEC_RE)
|
|
98
|
+
|
|
99
|
+
raise TemplateSyntaxError,
|
|
100
|
+
"#{where}:#{tag} 的 formatter 写法不合法,应为 {{路径:名字}} 或 {{路径:名字.参数}}"
|
|
96
101
|
end
|
|
97
102
|
|
|
98
103
|
# 表格指令:{{#rows 路径}} / {{#grid ...}} / {{#cols ...}} / {{/rows}} / {{/cols}}。
|
|
99
104
|
# 这里只做轻量语法校验,结构(层级/位置/作用域)由 TableLoop 校验。
|
|
100
|
-
GRID_MARKER_RE = /\A#grid\s+.+\z
|
|
101
|
-
COLS_MARKER_RE = /\A#cols\s+[A-Za-z0-9_
|
|
102
|
-
IF_MARKER_RE = /\A#if\s+[A-Za-z0-9_\[\]
|
|
103
|
-
CLOSE_MARKER_RE =
|
|
105
|
+
GRID_MARKER_RE = /\A#grid\s+.+\z/
|
|
106
|
+
COLS_MARKER_RE = /\A#cols\s+[A-Za-z0-9_.]+(?:\s+value:\s*[A-Za-z0-9_.]+(?:\s*,\s*[A-Za-z0-9_.]+)*)?\z/
|
|
107
|
+
IF_MARKER_RE = /\A#if\s+[A-Za-z0-9_\[\].]+\z/
|
|
108
|
+
CLOSE_MARKER_RE = %r{\A/(rows|cols|if)\z}
|
|
104
109
|
|
|
105
110
|
def validate_table_directive!(raw, tag, where)
|
|
106
111
|
case raw
|
|
@@ -117,7 +122,7 @@ module PowerTom
|
|
|
117
122
|
|
|
118
123
|
# 表格行标记 {{#rows 路径}}:只校验路径写法,结构与层级由 TableLoop 校验
|
|
119
124
|
def validate_row_marker!(raw, tag, where)
|
|
120
|
-
unless raw.match?(/\A#rows\s+[A-Za-z0-9_\[\]
|
|
125
|
+
unless raw.match?(/\A#rows\s+[A-Za-z0-9_\[\].]+\z/)
|
|
121
126
|
raise TemplateSyntaxError,
|
|
122
127
|
"#{where}:#{tag} 行标记不合法,应为 {{#rows 路径}}(路径只支持点路径与 [])"
|
|
123
128
|
end
|
data/lib/powertom/table_loop.rb
CHANGED
|
@@ -5,7 +5,7 @@ require_relative "scanner"
|
|
|
5
5
|
module PowerTom
|
|
6
6
|
# 表格行/列循环的纯逻辑部分:根据表格单元格文本生成展开计划。
|
|
7
7
|
#
|
|
8
|
-
# 三种语法(设计见 docs
|
|
8
|
+
# 三种语法(设计见 docs/table-loop-design.md):
|
|
9
9
|
# L0 {{数组[].字段}} 单级行循环(一张表一个循环行)
|
|
10
10
|
# L1 {{#rows 路径}} 标记行 两级嵌套:父行 → 子行
|
|
11
11
|
# L2 {{#grid rows: .. cols: ..}} 动态列:首列固定,列按 cols 克隆
|
|
@@ -13,11 +13,15 @@ module PowerTom
|
|
|
13
13
|
# plan 返回 nil 表示该表没有循环;结构错误抛 TemplateSyntaxError。
|
|
14
14
|
module TableLoop
|
|
15
15
|
# 行标记必须整格只有这一条标签
|
|
16
|
-
MARKER_RE = /\A\{\{\s*#rows\s+([A-Za-z0-9_\[\]
|
|
16
|
+
MARKER_RE = /\A\{\{\s*#rows\s+([A-Za-z0-9_\[\].]+)\s*\}\}\z/
|
|
17
17
|
|
|
18
18
|
# v0.2.x 区块语法:配对行区块 + 列带(可写在行区块内,支持多列模板)
|
|
19
|
-
COLS_OPEN_RE =
|
|
20
|
-
|
|
19
|
+
COLS_OPEN_RE = /
|
|
20
|
+
\A\{\{\s*\#cols\s+([A-Za-z0-9_.]+)
|
|
21
|
+
(?:\s+value:\s*([A-Za-z0-9_.]+(?:\s*,\s*[A-Za-z0-9_.]+)*))?
|
|
22
|
+
\s*\}\}\z
|
|
23
|
+
/x
|
|
24
|
+
IF_OPEN_RE = /\A\{\{\s*#if\s+([A-Za-z0-9_\[\].]+)\s*\}\}\z/
|
|
21
25
|
MAX_BLOCK_DEPTH = 3
|
|
22
26
|
|
|
23
27
|
module_function
|
|
@@ -134,17 +138,13 @@ module PowerTom
|
|
|
134
138
|
|
|
135
139
|
col = tags.first[:col]
|
|
136
140
|
tag = tags.first[:tag]
|
|
137
|
-
unless col.zero? && cells[col].to_s.strip == tag
|
|
138
|
-
raise TemplateSyntaxError, "#{line}:#{tag} 行标记必须独占首列单元格"
|
|
139
|
-
end
|
|
141
|
+
raise TemplateSyntaxError, "#{line}:#{tag} 行标记必须独占首列单元格" unless col.zero? && cells[col].to_s.strip == tag
|
|
140
142
|
if cells.each_with_index.any? { |text, c| c != col && !text.to_s.strip.empty? }
|
|
141
143
|
raise TemplateSyntaxError, "#{line}:#{tag} 行标记必须独占一行,其余单元格请留空"
|
|
142
144
|
end
|
|
143
145
|
|
|
144
146
|
path = tag[MARKER_RE, 1]
|
|
145
|
-
unless path
|
|
146
|
-
raise TemplateSyntaxError, "#{line}:#{tag} 行标记不合法,应为 {{#rows 路径}}"
|
|
147
|
-
end
|
|
147
|
+
raise TemplateSyntaxError, "#{line}:#{tag} 行标记不合法,应为 {{#rows 路径}}" unless path
|
|
148
148
|
|
|
149
149
|
check_marker_path!(path, line, tag)
|
|
150
150
|
end
|
|
@@ -281,9 +281,7 @@ module PowerTom
|
|
|
281
281
|
|
|
282
282
|
def validate_grid_row!(index, tags)
|
|
283
283
|
line = "表格第 #{index + 1} 行"
|
|
284
|
-
if tags.size > 1
|
|
285
|
-
raise TemplateSyntaxError, "#{line}包含 #{tags.size} 条 grid 标记,一行只允许一条"
|
|
286
|
-
end
|
|
284
|
+
raise TemplateSyntaxError, "#{line}包含 #{tags.size} 条 grid 标记,一行只允许一条" if tags.size > 1
|
|
287
285
|
return if tags.first[:col].zero?
|
|
288
286
|
|
|
289
287
|
raise TemplateSyntaxError, "#{line}:#{tags.first[:tag]} grid 标记必须放在表头行首列"
|
|
@@ -297,7 +295,7 @@ module PowerTom
|
|
|
297
295
|
args = {}
|
|
298
296
|
pos = 0
|
|
299
297
|
while pos < body.length
|
|
300
|
-
m = body.match(/\G\s*(rows|cols|value)\s*:\s*([A-Za-z0-9_
|
|
298
|
+
m = body.match(/\G\s*(rows|cols|value)\s*:\s*([A-Za-z0-9_.]+)\s*/, pos)
|
|
301
299
|
unless m
|
|
302
300
|
raise TemplateSyntaxError,
|
|
303
301
|
"#{line}:#{tag} grid 标记写法不合法," \
|
|
@@ -330,9 +328,7 @@ module PowerTom
|
|
|
330
328
|
raise TemplateSyntaxError,
|
|
331
329
|
"#{line}:grid 表只支持两列模板(首列行标题 + 第二列数据模板),当前 #{columns} 列"
|
|
332
330
|
end
|
|
333
|
-
if marker[:row] + 1 >= rows.size
|
|
334
|
-
raise TemplateSyntaxError, "#{line}:grid 标记后缺少内容行"
|
|
335
|
-
end
|
|
331
|
+
raise TemplateSyntaxError, "#{line}:grid 标记后缺少内容行" if marker[:row] + 1 >= rows.size
|
|
336
332
|
|
|
337
333
|
content = marker[:row] + 1
|
|
338
334
|
if merged_rows.any?
|
|
@@ -416,9 +412,8 @@ module PowerTom
|
|
|
416
412
|
col_ifs, row_ifs = split_ifs(if_tags, band)
|
|
417
413
|
band[:ifs] = parse_column_ifs(rows, col_ifs, band) if band
|
|
418
414
|
blocks = parse_block_tree(rows, (row_tags + row_ifs).sort_by { |t| t[:row] })
|
|
419
|
-
if band && band_in_if?(blocks, band[:row])
|
|
420
|
-
|
|
421
|
-
end
|
|
415
|
+
raise TemplateSyntaxError, "表格第 #{band[:row] + 1} 行:列带不能写在条件区块内" if band && band_in_if?(blocks, band[:row])
|
|
416
|
+
|
|
422
417
|
check_block_ifs!(blocks)
|
|
423
418
|
check_block_merges!(merged_rows, blocks, band)
|
|
424
419
|
stacks = build_row_stacks(blocks)
|
|
@@ -509,9 +504,7 @@ module PowerTom
|
|
|
509
504
|
"表格第 #{band[:row] + 1} 行:{{#if #{path}}} 只能引用当前列数组 " \
|
|
510
505
|
"#{band[:path]}[] 或整页字段"
|
|
511
506
|
end
|
|
512
|
-
if stack.any?
|
|
513
|
-
raise TemplateSyntaxError, "表格第 #{band[:row] + 1} 行:列条件不能嵌套"
|
|
514
|
-
end
|
|
507
|
+
raise TemplateSyntaxError, "表格第 #{band[:row] + 1} 行:列条件不能嵌套" if stack.any?
|
|
515
508
|
|
|
516
509
|
{ path: path, open_col: item[:col], close_col: nil }
|
|
517
510
|
end
|
|
@@ -555,7 +548,7 @@ module PowerTom
|
|
|
555
548
|
"表格第 #{item[:row] + 1} 行:条件区块内不能放行区块(请把条件写在外层)"
|
|
556
549
|
end
|
|
557
550
|
stack.push(open_block(rows, item, stack))
|
|
558
|
-
when
|
|
551
|
+
when %r{\A/rows}
|
|
559
552
|
if if_stack.any?
|
|
560
553
|
raise TemplateSyntaxError,
|
|
561
554
|
"表格第 #{item[:row] + 1} 行:条件区块必须完整包含在行区块内"
|
|
@@ -587,12 +580,8 @@ module PowerTom
|
|
|
587
580
|
raise TemplateSyntaxError,
|
|
588
581
|
"表格第 #{item[:row] + 1} 行:#{item[:tag]} 条件标记不合法,应为 {{#if 路径}}"
|
|
589
582
|
end
|
|
590
|
-
if stack.empty?
|
|
591
|
-
|
|
592
|
-
end
|
|
593
|
-
if if_stack.any?
|
|
594
|
-
raise TemplateSyntaxError, "表格第 #{item[:row] + 1} 行:条件区块不能嵌套(请合并条件)"
|
|
595
|
-
end
|
|
583
|
+
raise TemplateSyntaxError, "表格第 #{item[:row] + 1} 行:条件区块必须写在行区块内" if stack.empty?
|
|
584
|
+
raise TemplateSyntaxError, "表格第 #{item[:row] + 1} 行:条件区块不能嵌套(请合并条件)" if if_stack.any?
|
|
596
585
|
|
|
597
586
|
if_stack.push({ path: path, open: item[:row], close: nil })
|
|
598
587
|
end
|
|
@@ -670,21 +659,22 @@ module PowerTom
|
|
|
670
659
|
if cells.each_with_index.any? { |text, c| c != item[:col] && !text.to_s.strip.empty? }
|
|
671
660
|
raise TemplateSyntaxError, "#{line}:#{item[:tag]} 区块标记必须独占整行,其余单元格请留空"
|
|
672
661
|
end
|
|
662
|
+
|
|
673
663
|
case kind
|
|
674
664
|
when "#rows"
|
|
675
665
|
return if item[:tag].match?(MARKER_RE)
|
|
676
666
|
when "/rows"
|
|
677
|
-
return if item[:raw]
|
|
667
|
+
return if item[:raw] == "/rows"
|
|
678
668
|
when "#if"
|
|
679
669
|
return if item[:tag].match?(IF_OPEN_RE)
|
|
680
670
|
else
|
|
681
|
-
return if item[:raw]
|
|
671
|
+
return if item[:raw] == "/if"
|
|
682
672
|
end
|
|
683
673
|
|
|
684
674
|
raise TemplateSyntaxError, "#{line}:#{item[:tag]} 区块标记不合法"
|
|
685
675
|
end
|
|
686
676
|
|
|
687
|
-
def parse_column_band(
|
|
677
|
+
def parse_column_band(_rows, row_tags, tags)
|
|
688
678
|
return nil if tags.empty?
|
|
689
679
|
|
|
690
680
|
opens = tags.select { |t| t[:raw].start_with?("#cols") }
|
|
@@ -701,18 +691,10 @@ module PowerTom
|
|
|
701
691
|
open = opens.first
|
|
702
692
|
close = closes.first
|
|
703
693
|
line = "表格第 #{open[:row] + 1} 行"
|
|
704
|
-
unless open[:row] == close[:row]
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
raise TemplateSyntaxError, "#{line}:列带不能写在行区块标记行上"
|
|
709
|
-
end
|
|
710
|
-
if open[:col] < 1
|
|
711
|
-
raise TemplateSyntaxError, "#{line}:列带必须写在数据列(首列之外)"
|
|
712
|
-
end
|
|
713
|
-
if close[:col] < open[:col]
|
|
714
|
-
raise TemplateSyntaxError, "#{line}:{{/cols}} 必须在 {{#cols}} 右侧"
|
|
715
|
-
end
|
|
694
|
+
raise TemplateSyntaxError, "#{line}:{{#cols}} 与 {{/cols}} 必须写在同一行" unless open[:row] == close[:row]
|
|
695
|
+
raise TemplateSyntaxError, "#{line}:列带不能写在行区块标记行上" if row_tags.any? { |t| t[:row] == open[:row] }
|
|
696
|
+
raise TemplateSyntaxError, "#{line}:列带必须写在数据列(首列之外)" if open[:col] < 1
|
|
697
|
+
raise TemplateSyntaxError, "#{line}:{{/cols}} 必须在 {{#cols}} 右侧" if close[:col] < open[:col]
|
|
716
698
|
|
|
717
699
|
m = open[:tag].match(COLS_OPEN_RE)
|
|
718
700
|
unless m
|
|
@@ -732,9 +714,7 @@ module PowerTom
|
|
|
732
714
|
def check_block_merges!(merged_rows, blocks, band)
|
|
733
715
|
if band
|
|
734
716
|
row = merged_rows.index(true)
|
|
735
|
-
if row
|
|
736
|
-
raise TemplateSyntaxError, "表格第 #{row + 1} 行:列带循环不支持合并单元格,请先拆分"
|
|
737
|
-
end
|
|
717
|
+
raise TemplateSyntaxError, "表格第 #{row + 1} 行:列带循环不支持合并单元格,请先拆分" if row
|
|
738
718
|
end
|
|
739
719
|
|
|
740
720
|
stacks = build_row_stacks(blocks)
|
|
@@ -814,15 +794,13 @@ module PowerTom
|
|
|
814
794
|
return
|
|
815
795
|
end
|
|
816
796
|
if path == "col" || path.start_with?("col.")
|
|
817
|
-
unless in_band
|
|
818
|
-
|
|
819
|
-
end
|
|
797
|
+
raise TemplateSyntaxError, "#{line}:{{#{path}}} 只能写在列带单元格里" unless in_band
|
|
798
|
+
|
|
820
799
|
return
|
|
821
800
|
end
|
|
822
801
|
if path == "row" || path.start_with?("row.")
|
|
823
|
-
if stack.empty?
|
|
824
|
-
|
|
825
|
-
end
|
|
802
|
+
raise TemplateSyntaxError, "#{line}:{{#{path}}} 只能写在行区块内容行里" if stack.empty?
|
|
803
|
+
|
|
826
804
|
return
|
|
827
805
|
end
|
|
828
806
|
if band_decl_row && in_band
|
|
@@ -837,10 +815,10 @@ module PowerTom
|
|
|
837
815
|
raise TemplateSyntaxError,
|
|
838
816
|
"#{line}:{{#{path}}} 不在当前行区块作用域内"
|
|
839
817
|
end
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
818
|
+
return unless path.scan("[]").size > match[:prefix].scan("[]").size
|
|
819
|
+
|
|
820
|
+
raise TemplateSyntaxError,
|
|
821
|
+
"#{line}:{{#{path}}} 引用了未展开的下一级数组(当前最内层 #{match[:prefix]})"
|
|
844
822
|
end
|
|
845
823
|
|
|
846
824
|
# ---- 文本工具 ----
|