coradoc-docx 0.1.5 → 0.1.6
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 87447c69606c8f969627babd02d48f6c78dbb9abf77cfe76ecd4411f1f2c5cc5
|
|
4
|
+
data.tar.gz: b8e6deafc0b9fa2229190983d565e54d053bb362131fdf638b948aef916e5c86
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4691ea74df3c8156b35b298ebf5052face11fdebb28332f784fca1239f15c5d76c63065b18727e3b51c05681f15fff38d7565ebdd4b339cdcf7c7e934826e1ad
|
|
7
|
+
data.tar.gz: 282b7ee691b69dd278f1d4ada56e785b83a681edd73f2bd356782883527e3598fff33a5257d020469a5bcf85e128136d05178be6eea759c9a6c1b4ce72af9ee0
|
|
@@ -91,7 +91,8 @@ module Coradoc
|
|
|
91
91
|
|
|
92
92
|
FrontmatterCoreProperties.apply(doc, FrontmatterCoreProperties.extract(element))
|
|
93
93
|
|
|
94
|
-
transform_children(element.children, doc)
|
|
94
|
+
footnotes = transform_children(element.children, doc)
|
|
95
|
+
attach_footnotes(footnotes, doc)
|
|
95
96
|
|
|
96
97
|
doc.build
|
|
97
98
|
end
|
|
@@ -145,14 +146,19 @@ module Coradoc
|
|
|
145
146
|
elements.map { |e| transform(e) }
|
|
146
147
|
end
|
|
147
148
|
|
|
148
|
-
# Helper to transform children into builder calls
|
|
149
|
+
# Helper to transform children into builder calls. Returns the
|
|
150
|
+
# CoreModel::Footnote definitions encountered (including ones
|
|
151
|
+
# nested in sections) so the caller can attach a footnotes part.
|
|
149
152
|
def transform_children(children, builder)
|
|
150
|
-
return unless children
|
|
153
|
+
return [] unless children
|
|
151
154
|
|
|
155
|
+
footnotes = []
|
|
152
156
|
children.each do |child|
|
|
153
157
|
case child
|
|
158
|
+
when Coradoc::CoreModel::Footnote
|
|
159
|
+
footnotes << child
|
|
154
160
|
when Coradoc::CoreModel::StructuralElement
|
|
155
|
-
add_section_to_builder(child, builder)
|
|
161
|
+
footnotes.concat(add_section_to_builder(child, builder))
|
|
156
162
|
when Coradoc::CoreModel::AnnotationBlock
|
|
157
163
|
add_annotation_to_builder(child, builder)
|
|
158
164
|
when Coradoc::CoreModel::Block
|
|
@@ -165,6 +171,7 @@ module Coradoc
|
|
|
165
171
|
add_image_to_builder(child, builder)
|
|
166
172
|
end
|
|
167
173
|
end
|
|
174
|
+
footnotes
|
|
168
175
|
end
|
|
169
176
|
|
|
170
177
|
def add_section_to_builder(element, builder)
|
|
@@ -203,6 +210,8 @@ module Coradoc
|
|
|
203
210
|
p << child
|
|
204
211
|
when Coradoc::CoreModel::InlineElement
|
|
205
212
|
p << build_inline_text(child)
|
|
213
|
+
when Coradoc::CoreModel::FootnoteReference
|
|
214
|
+
p << build_ooxml_footnote_reference(child)
|
|
206
215
|
end
|
|
207
216
|
end
|
|
208
217
|
end
|
|
@@ -285,6 +294,8 @@ module Coradoc
|
|
|
285
294
|
para.runs << run
|
|
286
295
|
when Coradoc::CoreModel::InlineElement
|
|
287
296
|
para.runs << build_ooxml_run(child)
|
|
297
|
+
when Coradoc::CoreModel::FootnoteReference
|
|
298
|
+
para.runs << build_ooxml_footnote_reference(child)
|
|
288
299
|
end
|
|
289
300
|
end
|
|
290
301
|
else
|
|
@@ -461,6 +472,29 @@ module Coradoc
|
|
|
461
472
|
run
|
|
462
473
|
end
|
|
463
474
|
|
|
475
|
+
# Write CoreModel::Footnote definitions as a w:footnotes part on
|
|
476
|
+
# the DocumentRoot. Reference runs carry the core ids verbatim,
|
|
477
|
+
# keeping w:footnoteReference/@w:id aligned with w:footnote/@w:id.
|
|
478
|
+
def attach_footnotes(footnotes, builder)
|
|
479
|
+
return if Array(footnotes).empty?
|
|
480
|
+
|
|
481
|
+
part = Uniword::Wordprocessingml::Footnotes.new
|
|
482
|
+
footnotes.each do |fn|
|
|
483
|
+
entry = Uniword::Wordprocessingml::Footnote.new(id: fn.id.to_s)
|
|
484
|
+
entry.paragraphs << footnote_content_paragraph(fn)
|
|
485
|
+
part.footnote_entries << entry
|
|
486
|
+
end
|
|
487
|
+
builder.model.footnotes = part
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
def footnote_content_paragraph(fn)
|
|
491
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
492
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
493
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: fn.content.to_s)
|
|
494
|
+
para.runs << run
|
|
495
|
+
para
|
|
496
|
+
end
|
|
497
|
+
|
|
464
498
|
# Build OOXML footnote content as a paragraph placeholder
|
|
465
499
|
def build_ooxml_footnote(footnote)
|
|
466
500
|
para = Uniword::Wordprocessingml::Paragraph.new
|
|
@@ -49,6 +49,11 @@ module Coradoc
|
|
|
49
49
|
doc_title = extract_document_title(document, context)
|
|
50
50
|
children = transform_elements(body, context)
|
|
51
51
|
|
|
52
|
+
# Footnote definitions ride along as document-level children so
|
|
53
|
+
# writers can emit [^id]: definition blocks after the body
|
|
54
|
+
definitions = footnote_definitions(document)
|
|
55
|
+
children.concat(definitions) if definitions.any?
|
|
56
|
+
|
|
52
57
|
# If the first child is an H1 matching the doc title, skip the
|
|
53
58
|
# duplicate — the document title already captures it
|
|
54
59
|
if doc_title && children.first.is_a?(Coradoc::CoreModel::StructuralElement) &&
|
|
@@ -220,6 +225,17 @@ module Coradoc
|
|
|
220
225
|
end
|
|
221
226
|
end
|
|
222
227
|
|
|
228
|
+
# Footnote entries from the Uniword footnotes part. User notes
|
|
229
|
+
# only: separator/continuationSeparator entries carry w:type and
|
|
230
|
+
# no prose, so they never become definitions.
|
|
231
|
+
def footnote_entries(document)
|
|
232
|
+
part = document.footnotes
|
|
233
|
+
return [] unless defined?(Uniword::Wordprocessingml::Footnotes) &&
|
|
234
|
+
part.is_a?(Uniword::Wordprocessingml::Footnotes)
|
|
235
|
+
|
|
236
|
+
footnote_collection(part).reject(&:type)
|
|
237
|
+
end
|
|
238
|
+
|
|
223
239
|
def collect_footnotes(document)
|
|
224
240
|
footnotes = {}
|
|
225
241
|
|
|
@@ -231,22 +247,34 @@ module Coradoc
|
|
|
231
247
|
end
|
|
232
248
|
end
|
|
233
249
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
# uniword >= 1.7 exposes +footnote_entries+ (Footnote
|
|
237
|
-
# collection); older shapes carried +footnotes+.
|
|
238
|
-
entries = footnote_collection(document.footnotes)
|
|
239
|
-
entries.each do |fn|
|
|
240
|
-
id = fn.id&.to_s
|
|
241
|
-
next unless id
|
|
242
|
-
|
|
243
|
-
footnotes[id] = Array(fn.paragraphs || [])
|
|
244
|
-
end
|
|
250
|
+
footnote_entries(document).each do |fn|
|
|
251
|
+
footnotes[fn.id&.to_s] = Array(fn.paragraphs || [])
|
|
245
252
|
end
|
|
246
253
|
|
|
247
254
|
footnotes
|
|
248
255
|
end
|
|
249
256
|
|
|
257
|
+
# Build CoreModel::Footnote definitions from the footnotes part.
|
|
258
|
+
# Content is the entry's paragraph run text (plain text; inline
|
|
259
|
+
# formatting inside footnotes is out of scope for the reference).
|
|
260
|
+
def footnote_definitions(document)
|
|
261
|
+
footnote_entries(document).filter_map do |fn|
|
|
262
|
+
id = fn.id&.to_s
|
|
263
|
+
next unless id
|
|
264
|
+
|
|
265
|
+
text = footnote_text(fn)
|
|
266
|
+
next if text.nil? || text.strip.empty?
|
|
267
|
+
|
|
268
|
+
CoreModel::Footnote.new(id: id, content: text)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def footnote_text(entry)
|
|
273
|
+
Array(entry.paragraphs).map do |para|
|
|
274
|
+
Array(para.runs).map { |r| Array(r.text).map(&:to_s).join }.join
|
|
275
|
+
end.join("\n")
|
|
276
|
+
end
|
|
277
|
+
|
|
250
278
|
def extract_document_title(document, context)
|
|
251
279
|
body = document.body
|
|
252
280
|
return nil unless body
|
data/lib/coradoc/docx/version.rb
CHANGED