powertom 0.2.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +447 -0
- data/lib/powertom/adapter/page_surgery.rb +229 -0
- data/lib/powertom/adapter/ruby_pptx_adapter.rb +403 -0
- data/lib/powertom/binder.rb +204 -0
- data/lib/powertom/data_loading.rb +45 -0
- data/lib/powertom/formatter.rb +114 -0
- data/lib/powertom/grid_value.rb +53 -0
- data/lib/powertom/path_resolver.rb +52 -0
- data/lib/powertom/render.rb +558 -0
- data/lib/powertom/run_merge.rb +26 -0
- data/lib/powertom/scanner.rb +146 -0
- data/lib/powertom/table_loop.rb +776 -0
- data/lib/powertom/validator.rb +697 -0
- data/lib/powertom/version.rb +6 -0
- data/lib/powertom.rb +63 -0
- metadata +84 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rexml/document"
|
|
4
|
+
require "zip"
|
|
5
|
+
|
|
6
|
+
module PowerTom
|
|
7
|
+
module Adapter
|
|
8
|
+
# 页面手术:按计划克隆/删除整页(§5.2)。
|
|
9
|
+
#
|
|
10
|
+
# ruby_pptx 0.2.0 尚无 duplicate / delete(Slides 只有 add / add_table_pages,
|
|
11
|
+
# 设计文档 §5.2 已核对),本类在适配层内基于 OOXML 包结构直接完成,
|
|
12
|
+
# XML 操作不外泄出适配层;待上游补齐能力后应收缩为对 ruby_pptx 的调用。
|
|
13
|
+
#
|
|
14
|
+
# 策略:输出页按顺序整体重新编号(slide1..slideM / notesSlide1..M),
|
|
15
|
+
# 彻底规避上游 next_slide_partname 以 sldIdLst.size+1 命名的撞名问题。
|
|
16
|
+
# 媒体 / 布局 / 母版 / tags 等部件被多个页面共享引用,无需复制。
|
|
17
|
+
class PageSurgery
|
|
18
|
+
SLIDE_CT = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"
|
|
19
|
+
NOTES_CT = "application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml"
|
|
20
|
+
|
|
21
|
+
# plan: [{source: 原页号, iteration: nil|k}] 按输出顺序(克隆页连续出现)。
|
|
22
|
+
# 返回 [{source:, iteration:}](与输出页一一对应,供渲染阶段做作用域绑定)。
|
|
23
|
+
def self.execute(template_path, out_path, plan)
|
|
24
|
+
new(template_path, out_path, plan).run
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(template_path, out_path, plan)
|
|
28
|
+
@template_path = template_path
|
|
29
|
+
@out_path = out_path
|
|
30
|
+
@plan = plan
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def run
|
|
34
|
+
parts = read_parts
|
|
35
|
+
order = slide_partnames_in_order(parts)
|
|
36
|
+
|
|
37
|
+
# 输出映射:每个输出页对应哪张原始页(含其 notes 部件)
|
|
38
|
+
outputs = @plan.map do |p|
|
|
39
|
+
src = order[p[:source] - 1]
|
|
40
|
+
raise Error, "页码越界:#{p[:source]}(共 #{order.size} 页)" unless src
|
|
41
|
+
{ src_slide: src, src_notes: notes_part_for(parts, p[:source]) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
rewritten = build_rewritten_parts(parts, outputs)
|
|
45
|
+
write_zip(parts, rewritten)
|
|
46
|
+
|
|
47
|
+
@plan.map { |p| { source: p[:source], iteration: p[:iteration] } }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def slide_name(index) = "slide#{index + 1}.xml"
|
|
53
|
+
|
|
54
|
+
def notes_name(index) = "notesSlide#{index + 1}.xml"
|
|
55
|
+
|
|
56
|
+
# ---- 读取 ----
|
|
57
|
+
|
|
58
|
+
def read_parts
|
|
59
|
+
parts = {}
|
|
60
|
+
Zip::File.open(@template_path) do |zip|
|
|
61
|
+
zip.entries.each do |entry|
|
|
62
|
+
next if entry.directory?
|
|
63
|
+
parts[entry.name] = entry.get_input_stream.read
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
parts
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def slide_partnames_in_order(parts)
|
|
70
|
+
pres = REXML::Document.new(parts.fetch("ppt/presentation.xml"))
|
|
71
|
+
rels = REXML::Document.new(parts.fetch("ppt/_rels/presentation.xml.rels"))
|
|
72
|
+
rid_target = {}
|
|
73
|
+
REXML::XPath.match(rels, "//Relationship").each do |rel|
|
|
74
|
+
target = rel.attributes["Target"].to_s
|
|
75
|
+
rid_target[rel.attributes["Id"]] = target if target.match?(%r{\Aslides/slide\d+\.xml\z})
|
|
76
|
+
end
|
|
77
|
+
REXML::XPath.match(pres, "//p:sldIdLst/p:sldId").map do |sld_id|
|
|
78
|
+
rid_attr = sld_id.attributes.get_attribute("r:id")
|
|
79
|
+
target = rid_target[rid_attr&.value]
|
|
80
|
+
raise Error, "presentation.xml 的 sldId 引用了未知关系" unless target
|
|
81
|
+
"ppt/#{target}"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def notes_part_for(parts, old_slide_no)
|
|
86
|
+
rels_name = "ppt/slides/_rels/slide#{old_slide_no}.xml.rels"
|
|
87
|
+
return nil unless parts[rels_name]
|
|
88
|
+
rel = REXML::XPath.match(REXML::Document.new(parts[rels_name]), "//Relationship").find do |r|
|
|
89
|
+
r.attributes["Type"].to_s.end_with?("/notesSlide")
|
|
90
|
+
end
|
|
91
|
+
return nil unless rel
|
|
92
|
+
"ppt/notesSlides/#{File.basename(rel.attributes['Target'])}"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# ---- 重写 ----
|
|
96
|
+
|
|
97
|
+
def build_rewritten_parts(parts, outputs)
|
|
98
|
+
count = outputs.size
|
|
99
|
+
new_rids = allocate_slide_rids(parts, count)
|
|
100
|
+
|
|
101
|
+
slide_parts = outputs.each_with_index.flat_map do |out, i|
|
|
102
|
+
{
|
|
103
|
+
"ppt/slides/#{slide_name(i)}" => parts.fetch(out[:src_slide]),
|
|
104
|
+
"ppt/slides/_rels/#{slide_name(i)}.rels" =>
|
|
105
|
+
rewrite_slide_rels(parts, out[:src_slide], out[:src_notes] ? notes_name(i) : nil)
|
|
106
|
+
}
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
notes_parts = outputs.each_with_index.filter_map do |out, i|
|
|
110
|
+
next nil unless out[:src_notes]
|
|
111
|
+
{
|
|
112
|
+
"ppt/notesSlides/#{notes_name(i)}" => parts.fetch(out[:src_notes]),
|
|
113
|
+
"ppt/notesSlides/_rels/#{notes_name(i)}.rels" =>
|
|
114
|
+
rewrite_notes_rels(parts, out[:src_notes], slide_name(i))
|
|
115
|
+
}
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
{
|
|
119
|
+
"ppt/presentation.xml" => rewrite_presentation(parts, count, new_rids),
|
|
120
|
+
"ppt/_rels/presentation.xml.rels" => rewrite_presentation_rels(parts, count, new_rids),
|
|
121
|
+
"[Content_Types].xml" => rewrite_content_types(parts, count),
|
|
122
|
+
"slide_parts" => slide_parts,
|
|
123
|
+
"notes_parts" => notes_parts
|
|
124
|
+
}
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# presentation.xml.rels 里非 slide 关系保留原 rId,新 slide 关系从 max+1 起分配
|
|
128
|
+
def allocate_slide_rids(parts, count)
|
|
129
|
+
rels = REXML::Document.new(parts.fetch("ppt/_rels/presentation.xml.rels"))
|
|
130
|
+
max = REXML::XPath.match(rels, "//Relationship")
|
|
131
|
+
.filter_map { |r| r.attributes["Id"].to_s[/\ArId(\d+)\z/, 1] }
|
|
132
|
+
.map(&:to_i).max || 0
|
|
133
|
+
(1..count).map { |i| "rId#{max + i}" }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def rewrite_presentation(parts, count, new_rids)
|
|
137
|
+
xml = parts.fetch("ppt/presentation.xml").dup.force_encoding(Encoding::UTF_8)
|
|
138
|
+
entries = (0...count).map { |i| %(<p:sldId id="#{256 + i}" r:id="#{new_rids[i]}"/>) }.join
|
|
139
|
+
unless xml.sub!(%r{<p:sldIdLst>.*?</p:sldIdLst>}m, "<p:sldIdLst>#{entries}</p:sldIdLst>")
|
|
140
|
+
raise Error, "presentation.xml 中未找到 sldIdLst"
|
|
141
|
+
end
|
|
142
|
+
xml.force_encoding(Encoding::BINARY)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def rewrite_presentation_rels(parts, count, new_rids)
|
|
146
|
+
xml = parts.fetch("ppt/_rels/presentation.xml.rels").dup.force_encoding(Encoding::UTF_8)
|
|
147
|
+
xml = xml.gsub(%r{<Relationship\b[^>]*Target="slides/slide\d+\.xml"[^>]*/>}, "")
|
|
148
|
+
new_rels = (0...count).map do |i|
|
|
149
|
+
%(<Relationship Id="#{new_rids[i]}" ) +
|
|
150
|
+
%(Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" ) +
|
|
151
|
+
%(Target="slides/#{slide_name(i)}"/>)
|
|
152
|
+
end
|
|
153
|
+
xml.sub!(%r{</Relationships>}, "#{new_rels.join}</Relationships>")
|
|
154
|
+
xml.force_encoding(Encoding::BINARY)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def rewrite_content_types(parts, count)
|
|
158
|
+
xml = parts.fetch("[Content_Types].xml").dup.force_encoding(Encoding::UTF_8)
|
|
159
|
+
xml = xml.gsub(%r{<Override [^>]*PartName="/ppt/slides/slide\d+\.xml"[^>]*/>}, "")
|
|
160
|
+
xml = xml.gsub(%r{<Override [^>]*PartName="/ppt/notesSlides/notesSlide\d+\.xml"[^>]*/>}, "")
|
|
161
|
+
overrides = (0...count).flat_map do |i|
|
|
162
|
+
[%(<Override PartName="/ppt/slides/slide#{i + 1}.xml" ContentType="#{SLIDE_CT}"/>),
|
|
163
|
+
%(<Override PartName="/ppt/notesSlides/notesSlide#{i + 1}.xml" ContentType="#{NOTES_CT}"/>)]
|
|
164
|
+
end
|
|
165
|
+
xml.sub!(%r{</Types>}, "#{overrides.join}</Types>")
|
|
166
|
+
xml.force_encoding(Encoding::BINARY)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# slide rels:notesSlide 指向重编号后的新 notesSlide;其余(媒体/布局/tags)原样共享
|
|
170
|
+
def rewrite_slide_rels(parts, src_slide_part, new_notes_name)
|
|
171
|
+
rels_name = "ppt/slides/_rels/#{File.basename(src_slide_part)}.rels"
|
|
172
|
+
xml = parts.fetch(rels_name).dup.force_encoding(Encoding::UTF_8)
|
|
173
|
+
if new_notes_name
|
|
174
|
+
xml = xml.gsub(%r{Target="\.\./notesSlides/notesSlide\d+\.xml"},
|
|
175
|
+
%(Target="../notesSlides/#{new_notes_name}"))
|
|
176
|
+
end
|
|
177
|
+
xml.force_encoding(Encoding::BINARY)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def rewrite_notes_rels(parts, src_notes_part, new_slide_name)
|
|
181
|
+
rels_name = "ppt/notesSlides/_rels/#{File.basename(src_notes_part)}.rels"
|
|
182
|
+
xml = parts.fetch(rels_name).dup.force_encoding(Encoding::UTF_8)
|
|
183
|
+
xml = xml.gsub(%r{Target="\.\./slides/slide\d+\.xml"}, %(Target="../slides/#{new_slide_name}"))
|
|
184
|
+
xml.force_encoding(Encoding::BINARY)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# ---- 写出 ----
|
|
188
|
+
|
|
189
|
+
# 这些部件不照抄:重编号后的 slides/notesSlides、重写后的 presentation 层
|
|
190
|
+
SKIP_PATTERNS = [
|
|
191
|
+
%r{\Appt/slides/slide\d+\.xml\z},
|
|
192
|
+
%r{\Appt/slides/_rels/slide\d+\.xml\.rels\z},
|
|
193
|
+
%r{\Appt/notesSlides/notesSlide\d+\.xml\z},
|
|
194
|
+
%r{\Appt/notesSlides/_rels/notesSlide\d+\.xml\.rels\z},
|
|
195
|
+
%r{\Appt/presentation\.xml\z},
|
|
196
|
+
%r{\Appt/_rels/presentation\.xml\.rels\z}
|
|
197
|
+
].freeze
|
|
198
|
+
|
|
199
|
+
def write_zip(parts, rewritten)
|
|
200
|
+
Zip::OutputStream.open(@out_path) do |zos|
|
|
201
|
+
zos.put_next_entry("[Content_Types].xml")
|
|
202
|
+
zos.write(rewritten.fetch("[Content_Types].xml"))
|
|
203
|
+
|
|
204
|
+
parts.each do |name, data|
|
|
205
|
+
next if name == "[Content_Types].xml"
|
|
206
|
+
next if SKIP_PATTERNS.any? { |re| name.match?(re) }
|
|
207
|
+
zos.put_next_entry(name)
|
|
208
|
+
zos.write(data)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
{ "ppt/presentation.xml" => "ppt/presentation.xml",
|
|
212
|
+
"ppt/_rels/presentation.xml.rels" => "ppt/_rels/presentation.xml.rels" }.each do |key, name|
|
|
213
|
+
zos.put_next_entry(name)
|
|
214
|
+
zos.write(rewritten.fetch(key))
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
%w[slide_parts notes_parts].each do |group|
|
|
218
|
+
rewritten.fetch(group).each do |entries|
|
|
219
|
+
entries.each do |name, data|
|
|
220
|
+
zos.put_next_entry(name)
|
|
221
|
+
zos.write(data)
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ruby_pptx"
|
|
4
|
+
|
|
5
|
+
module PowerTom
|
|
6
|
+
module Adapter
|
|
7
|
+
# 设计文档 §4:适配层是唯一 import ruby_pptx 的模块。
|
|
8
|
+
# 上游 0.x API 变动时,改动被封在这个文件里。
|
|
9
|
+
class RubyPptxAdapter
|
|
10
|
+
def open(path)
|
|
11
|
+
Pptx::Presentation.open(path)
|
|
12
|
+
rescue StandardError => e
|
|
13
|
+
raise InvalidTemplateError, "模板无法打开(#{e.class}: #{e.message})"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# 遍历全部幻灯片的文本段落(组合形状递归展开)。
|
|
17
|
+
# 返回 [{slide_no:, shape:, paragraph:, run_texts:}]
|
|
18
|
+
def paragraph_contexts(prs)
|
|
19
|
+
contexts = []
|
|
20
|
+
prs.slides.each_with_index do |slide, i|
|
|
21
|
+
slide_no = i + 1
|
|
22
|
+
each_text_shape(slide.shapes) do |shape|
|
|
23
|
+
shape.text_frame.paragraphs.each do |paragraph|
|
|
24
|
+
runs = paragraph.runs
|
|
25
|
+
next if runs.empty?
|
|
26
|
+
contexts << {
|
|
27
|
+
slide_no: slide_no,
|
|
28
|
+
shape: shape.name,
|
|
29
|
+
paragraph: paragraph,
|
|
30
|
+
run_texts: runs.map(&:text)
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
contexts
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# §5.1 写回:按下标设置每个 run 的文本。文本数组长度必须与现有 run 数一致,
|
|
39
|
+
# run 节点本身不动(保留 rPr 样式引用)。
|
|
40
|
+
def write_paragraph_runs(paragraph, texts)
|
|
41
|
+
runs = paragraph.runs
|
|
42
|
+
if texts.size != runs.size
|
|
43
|
+
raise Error, "写回失败:run 数量不匹配(现有 #{runs.size},传入 #{texts.size})"
|
|
44
|
+
end
|
|
45
|
+
runs.each_with_index { |run, i| run.text = texts[i] }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# 备注文本(§3.3:v0 不渲染备注,只用于发现其中的标签并记 warning)。
|
|
49
|
+
# 返回 [{slide_no:, text:}];无备注页返回空串。
|
|
50
|
+
def notes_texts(prs)
|
|
51
|
+
prs.slides.each_with_index.map do |slide, i|
|
|
52
|
+
{ slide_no: i + 1, text: slide.notes.to_s }
|
|
53
|
+
end
|
|
54
|
+
rescue StandardError
|
|
55
|
+
# 备注读取异常不影响正文渲染(v0 备注只做检查)
|
|
56
|
+
[]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# 图片占位上下文:形状 name 或 alt text 符合 powertom:image:字段名 约定(§5.3)
|
|
60
|
+
# 返回 [{slide_no:, shape:, field:, slide:, picture:}]
|
|
61
|
+
def picture_contexts(prs)
|
|
62
|
+
contexts = []
|
|
63
|
+
prs.slides.each_with_index do |slide, i|
|
|
64
|
+
walk_shapes(slide.shapes) do |shape|
|
|
65
|
+
next unless shape.is_a?(Pptx::Picture)
|
|
66
|
+
field = image_field(shape)
|
|
67
|
+
next unless field
|
|
68
|
+
contexts << { slide_no: i + 1, shape: shape.name, field: field,
|
|
69
|
+
slide: slide, picture: shape }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
contexts
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# §5.3 图片替换:get_or_add 新 image part(按内容 sha1 去重,扩展名/content type
|
|
76
|
+
# 由 ruby_pptx 按真实图片类型维护),repoint blip 引用,再按"填满形状、居中裁切"
|
|
77
|
+
# 设置 srcRect。SVG 等矢量图不做裁切。
|
|
78
|
+
def replace_image(slide, picture, image_path)
|
|
79
|
+
_image_part, r_id = slide.part.get_or_add_image_part(image_path)
|
|
80
|
+
pic_el = picture.element
|
|
81
|
+
pic_el.blip.embed = r_id
|
|
82
|
+
image = Pptx::Image.from_file(image_path)
|
|
83
|
+
unless image.vector?
|
|
84
|
+
view = [picture.width, picture.height]
|
|
85
|
+
if view[0] && view[1]
|
|
86
|
+
# crop_to_fit 只关心比例;Length 没有 to_f,传 EMU 裸值
|
|
87
|
+
native = image.native_size
|
|
88
|
+
pic_el.crop_to_fit([native[0].emu, native[1].emu],
|
|
89
|
+
[view[0].emu, view[1].emu])
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
true
|
|
93
|
+
rescue StandardError => e
|
|
94
|
+
raise PowerTom::Error,
|
|
95
|
+
"图片替换失败(#{File.basename(image_path.to_s)}):#{e.class} #{e.message}"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# 逐页形状名(含组合内、含无文本形状),用于模板结构自检。
|
|
99
|
+
# 返回 [{slide_no:, names: [String]}]
|
|
100
|
+
def shape_names_by_slide(prs)
|
|
101
|
+
prs.slides.each_with_index.map do |slide, i|
|
|
102
|
+
names = []
|
|
103
|
+
walk_shapes(slide.shapes) { |shape| names << shape.name }
|
|
104
|
+
{ slide_no: i + 1, names: names }
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# 母版与版式上的文本形状(用于发现写在母版/版式上的标签)。
|
|
109
|
+
# 返回 [{container:, shape:, text:}]
|
|
110
|
+
def container_texts(prs)
|
|
111
|
+
entries = []
|
|
112
|
+
prs.slide_masters.each do |master|
|
|
113
|
+
collect_container_texts(entries, master, "母版")
|
|
114
|
+
master.slide_layouts.each do |layout|
|
|
115
|
+
collect_container_texts(entries, layout, "版式")
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
entries
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# 表格结构(表格行循环用):逐页每张表的行与单元格文本,以及合并单元格分布
|
|
122
|
+
# 返回 [{slide_no:, shape:, columns:, element:, frame:, rows: [[cell_text, ...], ...],
|
|
123
|
+
# merged_rows: [bool, ...]}]
|
|
124
|
+
def table_contexts(prs)
|
|
125
|
+
list = []
|
|
126
|
+
prs.slides.each_with_index do |slide, i|
|
|
127
|
+
walk_shapes(slide.shapes) do |shape|
|
|
128
|
+
next unless shape.respond_to?(:table?) && shape.table?
|
|
129
|
+
|
|
130
|
+
table = shape.table
|
|
131
|
+
list << {
|
|
132
|
+
slide_no: i + 1,
|
|
133
|
+
shape: shape.name,
|
|
134
|
+
columns: table.columns.size,
|
|
135
|
+
element: table_element(shape),
|
|
136
|
+
frame: shape.element,
|
|
137
|
+
rows: table.rows.map { |row| row.cells.map { |cell| cell.text.to_s } },
|
|
138
|
+
merged_rows: table.rows.map do |row|
|
|
139
|
+
row.cells.any? { |cell| cell.merge_origin? || cell.spanned? }
|
|
140
|
+
end
|
|
141
|
+
}
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
list
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# 克隆第 row_index 行为 count 份(插在原位置),再删除模板行;
|
|
148
|
+
# 同步 graphicFrame 高度,避免行数变化后被外框裁剪。
|
|
149
|
+
def expand_table_row(table_element, frame_element, row_index, count)
|
|
150
|
+
template = table_element.tr_list[row_index]
|
|
151
|
+
count.times { template.add_previous_sibling(table_element.import(template)) }
|
|
152
|
+
table_element.remove(template)
|
|
153
|
+
sync_table_height(table_element, frame_element)
|
|
154
|
+
true
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# 两级嵌套展开:每个父项先输出一行父内容行,随后输出它的子内容行。
|
|
158
|
+
# child_counts: [每个父项的子数组长度]
|
|
159
|
+
# 模板中的父/子标记行与内容行在展开后全部删除。
|
|
160
|
+
def expand_table_rows_nested(table_element, frame_element, plan, child_counts)
|
|
161
|
+
trs = table_element.tr_list
|
|
162
|
+
parent_marker = trs[plan[:parent][:marker]]
|
|
163
|
+
parent_content = trs[plan[:parent][:content]]
|
|
164
|
+
child_marker = trs[plan[:child][:marker]]
|
|
165
|
+
child_content = trs[plan[:child][:content]]
|
|
166
|
+
|
|
167
|
+
child_counts.each do |count|
|
|
168
|
+
parent_marker.add_previous_sibling(table_element.import(parent_content))
|
|
169
|
+
count.times { parent_marker.add_previous_sibling(table_element.import(child_content)) }
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
[parent_marker, parent_content, child_marker, child_content].each do |tr|
|
|
173
|
+
table_element.remove(tr)
|
|
174
|
+
end
|
|
175
|
+
sync_table_height(table_element, frame_element)
|
|
176
|
+
true
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# 动态列(#grid):首列固定,第二列是列模板;按 column_count 克隆
|
|
180
|
+
# a:gridCol 与每一行的第二个 a:tc,再删除模板列。
|
|
181
|
+
def expand_grid_columns(table_element, frame_element, column_count)
|
|
182
|
+
grid = table_element.xpath("./a:tblGrid").first
|
|
183
|
+
template_col = grid.gridCol_list[1]
|
|
184
|
+
column_count.times { template_col.add_previous_sibling(grid.import(template_col)) }
|
|
185
|
+
grid.remove(template_col)
|
|
186
|
+
|
|
187
|
+
table_element.tr_list.each do |tr|
|
|
188
|
+
template_cell = tr.tc_list[1]
|
|
189
|
+
column_count.times { template_cell.add_previous_sibling(tr.import(template_cell)) }
|
|
190
|
+
tr.remove(template_cell)
|
|
191
|
+
end
|
|
192
|
+
sync_table_height(table_element, frame_element)
|
|
193
|
+
true
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# grid 标记只用于声明,渲染前从表头首格移除(保留同格的其他文字与首 run 样式)
|
|
197
|
+
def strip_grid_marker(table_element, row_index, column_index)
|
|
198
|
+
cell = table_element.tr_list[row_index].tc_list[column_index]
|
|
199
|
+
strip_tags_in_cell(cell, /\{\{\s*#grid\b[^{}]*\}\}/)
|
|
200
|
+
true
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# 列带标记({{#cols}} / {{/cols}})在列克隆前移除,避免被复制到每一列
|
|
204
|
+
def strip_column_markers(table_element, row_index, first_col, last_col)
|
|
205
|
+
cells = table_element.tr_list[row_index].tc_list
|
|
206
|
+
(first_col..last_col).each do |c|
|
|
207
|
+
strip_tags_in_cell(cells[c], /\{\{\s*(?:#cols\b[^{}]*|\/cols)\s*\}\}/)
|
|
208
|
+
end
|
|
209
|
+
true
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def table_row_element(table_element, row_index)
|
|
213
|
+
table_element.tr_list[row_index]
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# 把 [open_element, close_element] 之间的模板行整体克隆 count 份。
|
|
217
|
+
# 返回每份克隆的行元素数组(按输出顺序),并删除开闭标记与模板行。
|
|
218
|
+
def clone_table_row_range(table_element, open_element, close_element, count)
|
|
219
|
+
trs = table_element.tr_list
|
|
220
|
+
open_index = trs.index { |tr| tr.node == open_element.node }
|
|
221
|
+
close_index = trs.index { |tr| tr.node == close_element.node }
|
|
222
|
+
unless open_index && close_index && close_index > open_index
|
|
223
|
+
raise PowerTom::Error, "行区块标记定位失败(区块可能已被展开)"
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
templates = (open_index + 1...close_index).map { |i| trs[i] }
|
|
227
|
+
groups = Array.new(count) do
|
|
228
|
+
templates.map { |template| open_element.add_previous_sibling(table_element.import(template)) }
|
|
229
|
+
end
|
|
230
|
+
([open_element, close_element] + templates).each { |tr| table_element.remove(tr) }
|
|
231
|
+
groups
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# 条件区块:标记行总是删除;keep_content=false 时内容行一并删除
|
|
235
|
+
def prune_table_row_range(table_element, open_element, close_element, keep_content:)
|
|
236
|
+
trs = table_element.tr_list
|
|
237
|
+
open_index = trs.index { |tr| tr.node == open_element.node }
|
|
238
|
+
close_index = trs.index { |tr| tr.node == close_element.node }
|
|
239
|
+
unless open_index && close_index && close_index > open_index
|
|
240
|
+
raise PowerTom::Error, "条件区块标记定位失败(区块可能已被展开)"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
content = (open_index + 1...close_index).map { |i| trs[i] }
|
|
244
|
+
content.each { |tr| table_element.remove(tr) } unless keep_content
|
|
245
|
+
table_element.remove(open_element)
|
|
246
|
+
table_element.remove(close_element)
|
|
247
|
+
true
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# 区块展开/裁剪后统一刷新表格高度
|
|
251
|
+
def refresh_table_height(table_element, frame_element)
|
|
252
|
+
sync_table_height(table_element, frame_element)
|
|
253
|
+
true
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# 动态列带:模板列 [first_col..last_col] 按 count 克隆(表格网格 + 每一行)
|
|
257
|
+
def expand_column_band(table_element, frame_element, first_col, last_col, count)
|
|
258
|
+
grid = table_element.xpath("./a:tblGrid").first
|
|
259
|
+
grid_cols = grid.gridCol_list
|
|
260
|
+
template_cols = (first_col..last_col).map { |i| grid_cols[i] }
|
|
261
|
+
anchor = grid_cols[last_col]
|
|
262
|
+
count.times do
|
|
263
|
+
template_cols.each { |col| anchor.add_previous_sibling(grid.import(col)) }
|
|
264
|
+
end
|
|
265
|
+
template_cols.each { |col| grid.remove(col) }
|
|
266
|
+
|
|
267
|
+
table_element.tr_list.each do |tr|
|
|
268
|
+
cells = tr.tc_list
|
|
269
|
+
template_cells = (first_col..last_col).map { |i| cells[i] }
|
|
270
|
+
cell_anchor = cells[last_col]
|
|
271
|
+
count.times do
|
|
272
|
+
template_cells.each { |cell| cell_anchor.add_previous_sibling(tr.import(cell)) }
|
|
273
|
+
end
|
|
274
|
+
template_cells.each { |cell| tr.remove(cell) }
|
|
275
|
+
end
|
|
276
|
+
sync_table_height(table_element, frame_element)
|
|
277
|
+
true
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# 把单元格段落里的指定标签移除(保留同格其他文字与首 run 样式)
|
|
281
|
+
def strip_tags_in_cell(cell, pattern)
|
|
282
|
+
cell.xpath("./a:txBody/a:p").each do |paragraph|
|
|
283
|
+
runs = paragraph.xpath("./a:r")
|
|
284
|
+
next if runs.empty?
|
|
285
|
+
|
|
286
|
+
joined = runs.map { |run| run.xpath("./a:t").first&.text.to_s }.join
|
|
287
|
+
next unless joined.match?(pattern)
|
|
288
|
+
|
|
289
|
+
cleaned = joined.gsub(pattern, "").strip
|
|
290
|
+
runs.each_with_index do |run, i|
|
|
291
|
+
text_node = run.xpath("./a:t").first
|
|
292
|
+
text_node.text = i.zero? ? cleaned : "" if text_node
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
true
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# 表格单元格里的段落(展开行之后采集,用于绑定)
|
|
299
|
+
def table_cell_contexts(prs)
|
|
300
|
+
list = []
|
|
301
|
+
prs.slides.each_with_index do |slide, i|
|
|
302
|
+
walk_shapes(slide.shapes) do |shape|
|
|
303
|
+
next unless shape.respond_to?(:table?) && shape.table?
|
|
304
|
+
|
|
305
|
+
table = shape.table
|
|
306
|
+
table.rows.each_with_index do |row, r|
|
|
307
|
+
row.cells.each_with_index do |cell, c|
|
|
308
|
+
cell.text_frame.paragraphs.each do |paragraph|
|
|
309
|
+
runs = paragraph.runs
|
|
310
|
+
next if runs.empty?
|
|
311
|
+
|
|
312
|
+
list << {
|
|
313
|
+
slide_no: i + 1,
|
|
314
|
+
shape: "#{shape.name}[#{r + 1},#{c + 1}]",
|
|
315
|
+
table_shape: shape.name,
|
|
316
|
+
row_index: r,
|
|
317
|
+
col_index: c,
|
|
318
|
+
in_table: true,
|
|
319
|
+
paragraph: paragraph,
|
|
320
|
+
run_texts: runs.map(&:text)
|
|
321
|
+
}
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
list
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
# 按名称查找形状(含组合内);找不到返回 nil
|
|
331
|
+
def find_shape(prs, slide_no, name)
|
|
332
|
+
slide = prs.slides[slide_no - 1]
|
|
333
|
+
return nil unless slide
|
|
334
|
+
found = nil
|
|
335
|
+
walk_shapes(slide.shapes) { |shape| found ||= shape if shape.name == name }
|
|
336
|
+
found
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def save(prs, path)
|
|
340
|
+
prs.save(path)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
private
|
|
344
|
+
|
|
345
|
+
def walk_shapes(shapes, &block)
|
|
346
|
+
shapes.each do |shape|
|
|
347
|
+
if shape.is_a?(Pptx::GroupShape)
|
|
348
|
+
walk_shapes(shape.shapes, &block)
|
|
349
|
+
else
|
|
350
|
+
block.call(shape)
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
def each_text_shape(shapes, &block)
|
|
356
|
+
walk_shapes(shapes) { |shape| block.call(shape) if shape.text_frame? }
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
IMAGE_FIELD_PREFIX = "powertom:image:"
|
|
360
|
+
|
|
361
|
+
def image_field(shape)
|
|
362
|
+
name = shape.name.to_s
|
|
363
|
+
return name.delete_prefix(IMAGE_FIELD_PREFIX) if name.start_with?(IMAGE_FIELD_PREFIX)
|
|
364
|
+
descr = alt_text_of(shape)
|
|
365
|
+
return descr.delete_prefix(IMAGE_FIELD_PREFIX) if descr&.start_with?(IMAGE_FIELD_PREFIX)
|
|
366
|
+
nil
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def alt_text_of(shape)
|
|
370
|
+
cnv_pr = shape.element.xpath("./p:nvPicPr/p:cNvPr").first
|
|
371
|
+
cnv_pr&.get("descr")
|
|
372
|
+
rescue StandardError
|
|
373
|
+
nil
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
def table_element(shape)
|
|
377
|
+
shape.element.xpath("./a:graphic/a:graphicData/a:tbl").first
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
# 表格实际高度 = 各行 h 之和;写回 graphicFrame 的 a:ext/@cy
|
|
381
|
+
def sync_table_height(table_element, frame_element)
|
|
382
|
+
total = table_element.tr_list.sum { |tr| tr.get("h").to_i }
|
|
383
|
+
ext = frame_element.xpath("./p:xfrm/a:ext").first
|
|
384
|
+
ext&.set("cy", total.to_s)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def collect_container_texts(entries, container, kind)
|
|
388
|
+
label = begin
|
|
389
|
+
name = container.part.name
|
|
390
|
+
name.nil? || name.to_s.empty? ? kind : "#{kind}『#{name}』"
|
|
391
|
+
rescue StandardError
|
|
392
|
+
kind
|
|
393
|
+
end
|
|
394
|
+
walk_shapes(container.shapes) do |shape|
|
|
395
|
+
next unless shape.text_frame?
|
|
396
|
+
text = shape.text_frame.text.to_s
|
|
397
|
+
next unless text.include?("{{")
|
|
398
|
+
entries << { container: label, shape: shape.name, text: text }
|
|
399
|
+
end
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
end
|