carve-hexapdf 0.1.7 → 0.2.0
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 +23 -1
- data/README.md +1 -1
- data/lib/carve/hexapdf/renderer.rb +166 -127
- data/lib/carve/hexapdf/version.rb +1 -1
- data/lib/carve/hexapdf.rb +53 -4
- metadata +7 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56039d75c83f496d9038debcc8a7a8ad9a03b331c1f5b6b289381a0d3f24fe4a
|
|
4
|
+
data.tar.gz: 812463d7437234c71d8c76c57a5b37691cefa2f30861ca1bebec5b85805ae176
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e4a51e9786486184cd1ee2e1ef33f015a8b35c30643af497a72507012ba4be61819706e93b49df1bc8515f1dadb09ee0f207198ab5d7157e4bf605bdff17825
|
|
7
|
+
data.tar.gz: 2bd43b01f76c7735ddb3e4a3d8b9b10e3997da632c3da4fa7e10e19705c4a5e1ce65d5689926fa758ebdcdc35d99e4002b9307adb6623e953f1fad714fda9c1f
|
data/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,29 @@ All notable changes to this project are documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [
|
|
8
|
+
## [0.2.0] - 2026-12-04
|
|
9
|
+
|
|
10
|
+
carve-hexapdf reads the Carve 0.2 tree and needs carve-lang 0.2. Navigation extensions now reach the PDF, and a `{.page-break}` thematic break starts a new page.
|
|
11
|
+
|
|
12
|
+
### Breaking
|
|
13
|
+
|
|
14
|
+
- Ported to Carve 0.2: the renderer reads the 0.2 tree (AST contract 2.0) and requires carve-lang `>= 0.2.0, < 0.3.0`. Captions are caption nodes on the block they describe (0.2 has no figure node), and divs draw their quoted title (#73).
|
|
15
|
+
- Ruby 3.2 or newer is required, as in carve-lang 0.2 (#87).
|
|
16
|
+
- Include warnings carry the engine's `code` (`include-not-found`, `include-outside-root`, `include-cycle`), and `on_includes:` dependencies carry `path`, `from`, `read` and `file` instead of `resolved` and `denial` (#73).
|
|
17
|
+
- `profile:` no longer changes the parse; a 0.2 profile restricts the HTML render (#73).
|
|
18
|
+
|
|
19
|
+
### Improvements
|
|
20
|
+
|
|
21
|
+
- `extensions:` selects the engine's standard extensions. The navigation extensions (autolink, heading numbers, permalinks, table of contents) are drawn from the render view (#77, #78).
|
|
22
|
+
- A top-level `{.page-break}` thematic break starts a new page instead of drawing a bar; a break on an empty page adds no blank page (#83).
|
|
23
|
+
- Named snippets are drawn as their value where they are used (#86).
|
|
24
|
+
|
|
25
|
+
### Release preparation
|
|
26
|
+
|
|
27
|
+
- The 0.2 port moved to main (#74), with engine readiness checks and interim binding pins (#75, #76, #79). The published `carve-lang` 0.2.0 gem replaces those interim pins.
|
|
28
|
+
- Removed the obsolete substitution node from the inline vocabulary (#82) and updated documentation after the branch switch (#84). The release changelog was completed against the merged work (#88).
|
|
29
|
+
|
|
30
|
+
**Full Changelog**: https://github.com/markup-carve/carve-hexapdf/compare/v0.1.7...v0.2.0
|
|
9
31
|
|
|
10
32
|
## [0.1.7] - 2026-11-12
|
|
11
33
|
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# carve-hexapdf
|
|
2
2
|
|
|
3
|
-
Ruby PDF renderer for Carve documents. It
|
|
3
|
+
Ruby PDF renderer for Carve 0.2 documents. It reads the 0.2 tree from the published `carve-lang` gem and uses the HexaPDF composition engine. See the [Carve documentation](https://markup-carve.github.io/carve/).
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -78,6 +78,9 @@ module Carve
|
|
|
78
78
|
# ---- block dispatch ------------------------------------------------
|
|
79
79
|
|
|
80
80
|
def block(node, target)
|
|
81
|
+
# 0.2 has no figure node: a caption belongs to the block it describes.
|
|
82
|
+
return captioned(node, target) if node[:caption] && !%w[table figure_group].include?(node[:type])
|
|
83
|
+
|
|
81
84
|
case node[:type]
|
|
82
85
|
# A definition is RELOCATED, not rendered in place - it belongs in the
|
|
83
86
|
# endnote section, which is what the HTML renderer does with it too.
|
|
@@ -92,16 +95,17 @@ module Carve
|
|
|
92
95
|
when "list" then list(node, target)
|
|
93
96
|
when "block_quote" then block_quote(node, target)
|
|
94
97
|
when "table" then table(node, target)
|
|
95
|
-
when "thematic_break" then thematic_break(target)
|
|
96
|
-
when "div" then
|
|
98
|
+
when "thematic_break" then thematic_break(node, target)
|
|
99
|
+
when "div" then div(node, target)
|
|
97
100
|
when "admonition" then admonition(node, target)
|
|
98
101
|
when "definition_list" then definition_list(node, target)
|
|
99
|
-
when "figure" then figure(node, target)
|
|
100
102
|
when "figure_group" then figure_group(node, target)
|
|
103
|
+
when "section" then Array(node[:children]).each { |ch| block(ch, target) }
|
|
104
|
+
when "include" then include_node(node, target)
|
|
101
105
|
when "block_image", "image" then image_block(node, target)
|
|
102
|
-
when "block_extension" then container_of(node
|
|
103
|
-
when "raw_block", "comment", "abbreviation_def"
|
|
104
|
-
# No meaningful PDF form - drop.
|
|
106
|
+
when "block_extension" then container_of(extension_content(node), target)
|
|
107
|
+
when "raw_block", "comment", "abbreviation_def", "snippet_definition"
|
|
108
|
+
# No meaningful PDF form - drop. A snippet definition renders nothing (C2-H-214).
|
|
105
109
|
else
|
|
106
110
|
if inline_children?(node[:children])
|
|
107
111
|
emit_paragraph(node[:children], target)
|
|
@@ -162,8 +166,12 @@ module Carve
|
|
|
162
166
|
end
|
|
163
167
|
|
|
164
168
|
def paragraph(node, target)
|
|
165
|
-
# A paragraph that is only a display-math node becomes a centered image.
|
|
166
169
|
children = Array(node[:children])
|
|
170
|
+
# An image paragraph (C2-A `role: "image"`) draws its images as blocks.
|
|
171
|
+
if node[:role] == "image" && children.all? { |ch| ch[:type] == "image" }
|
|
172
|
+
return children.each { |image| image_block(image, target) }
|
|
173
|
+
end
|
|
174
|
+
# A paragraph that is only a display-math node becomes a centered image.
|
|
167
175
|
if children.size == 1 && children.first[:type] == "math" && children.first[:display]
|
|
168
176
|
return display_math(children.first, target)
|
|
169
177
|
end
|
|
@@ -180,12 +188,12 @@ module Carve
|
|
|
180
188
|
end
|
|
181
189
|
|
|
182
190
|
def code_block(node, target)
|
|
183
|
-
lang = node[:
|
|
184
|
-
if (key = DIAGRAM_LANGS[lang]) && (bytes = call_renderer(key, node[:
|
|
191
|
+
lang = node[:syntax].to_s.downcase
|
|
192
|
+
if (key = DIAGRAM_LANGS[lang]) && (bytes = call_renderer(key, node[:value].to_s))
|
|
185
193
|
return image_bytes(bytes, target)
|
|
186
194
|
end
|
|
187
195
|
|
|
188
|
-
content = resolve_nbsp(node[:
|
|
196
|
+
content = resolve_nbsp(node[:value]).chomp
|
|
189
197
|
style = style_for("code.block", inherited_font: true, with_box: true)
|
|
190
198
|
box = style.delete(:box)
|
|
191
199
|
target.text(drawable_text(content, style[:font] || code_font), **style, box_style: box)
|
|
@@ -196,15 +204,16 @@ module Carve
|
|
|
196
204
|
# paragraph chain, and Composer#list rejects text keywords (:font,
|
|
197
205
|
# :font_size, ...) that the chain inherits from base - whitelist.
|
|
198
206
|
style = style_for("list").slice(:item_spacing, :content_indentation)
|
|
199
|
-
items = Array(node[:
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
207
|
+
items = Array(node[:children])
|
|
208
|
+
marker_info = node[:marker] || {}
|
|
209
|
+
ordered = marker_info[:kind] == "ordered"
|
|
210
|
+
task = items.any? { |it| it[:task] }
|
|
211
|
+
start = marker_info[:start] || 1
|
|
203
212
|
marker = if task
|
|
204
213
|
# Standard PDF fonts have no checkbox glyphs; draw an ASCII
|
|
205
214
|
# checkbox in the marker column so item text (and nested
|
|
206
215
|
# lists) align like any other list.
|
|
207
|
-
checked = items.map { |it| it[:
|
|
216
|
+
checked = items.map { |it| it[:task] && it[:task][:mark].to_s.strip != "" }
|
|
208
217
|
font = style_for("paragraph", inherited_font: true)[:font] || "Times"
|
|
209
218
|
->(doc, list_box, index) do
|
|
210
219
|
# A list split across pages continues in a box whose
|
|
@@ -232,9 +241,17 @@ module Carve
|
|
|
232
241
|
def block_quote(node, target)
|
|
233
242
|
target.container(style: style_for("quote", with_box: true)[:box]) do |cont|
|
|
234
243
|
Array(node[:children]).each { |ch| block(ch, cont) }
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# A div's quoted title (C2-B-120) is drawn before its body.
|
|
248
|
+
def div(node, target)
|
|
249
|
+
title = Array(node[:title])
|
|
250
|
+
return container_of(node[:children], target) if title.empty?
|
|
251
|
+
|
|
252
|
+
target.container(style: { margin: [0, 0, BLOCK_GAP] }) do |cont|
|
|
253
|
+
cont.formatted_text(inline_runs(title, bold: true), margin: [0, 0, 2])
|
|
254
|
+
Array(node[:children]).each { |ch| block(ch, cont) }
|
|
238
255
|
end
|
|
239
256
|
end
|
|
240
257
|
|
|
@@ -259,9 +276,9 @@ module Carve
|
|
|
259
276
|
def definition_list(node, target)
|
|
260
277
|
style = style_for("definition_list", with_box: true)
|
|
261
278
|
target.container(style: style[:box]) do |cont|
|
|
262
|
-
# `
|
|
279
|
+
# `children` is a flat run of definition_term / definition_description
|
|
263
280
|
# nodes, not term/definition pairs.
|
|
264
|
-
Array(node[:
|
|
281
|
+
Array(node[:children]).each do |item|
|
|
265
282
|
case item[:type]
|
|
266
283
|
when "definition_term"
|
|
267
284
|
cont.formatted_text(inline_runs(item[:children], bold: true), margin: [2, 0, 1])
|
|
@@ -274,23 +291,52 @@ module Carve
|
|
|
274
291
|
end
|
|
275
292
|
end
|
|
276
293
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
294
|
+
# A captioned block: the block as it stands, then its caption.
|
|
295
|
+
def captioned(node, target)
|
|
296
|
+
block(node.reject { |key, _| key == :caption }, target)
|
|
297
|
+
caption = caption_inlines(node[:caption])
|
|
298
|
+
return if caption.empty?
|
|
299
|
+
|
|
300
|
+
style = text_style("figure.caption")
|
|
301
|
+
target.formatted_text(inline_runs(caption, italic: true, font_family: style[:font]), **style)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# A caption node holds blocks; a caption line is drawn from their inlines.
|
|
305
|
+
def caption_inlines(caption)
|
|
306
|
+
return [] unless caption.is_a?(Hash)
|
|
307
|
+
|
|
308
|
+
Array(caption[:children]).each_with_index.flat_map do |blk, index|
|
|
309
|
+
lead = index.positive? ? [{ type: "text", value: " " }] : []
|
|
310
|
+
lead + (blk[:type] == "paragraph" ? Array(blk[:children]) : [])
|
|
283
311
|
end
|
|
284
312
|
end
|
|
285
313
|
|
|
314
|
+
# An expanded include stands for its blocks; an unexpanded one is the
|
|
315
|
+
# directive as text, as the HTML writer renders it (C2-N-070).
|
|
316
|
+
def include_node(node, target)
|
|
317
|
+
if node[:children]
|
|
318
|
+
Array(node[:children]).each { |ch| block(ch, target) }
|
|
319
|
+
else
|
|
320
|
+
emit_paragraph([{ type: "text", value: "{{ #{node[:path]} }}" }], target)
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# An extension this renderer does not implement prints the fallback it
|
|
325
|
+
# promised (C2-X-023), or its children when it has none.
|
|
326
|
+
def extension_content(node)
|
|
327
|
+
children = Array(node[:children])
|
|
328
|
+
children.empty? ? Array(node[:fallback]) : children
|
|
329
|
+
end
|
|
330
|
+
|
|
286
331
|
# A composite figure (PART 9 section 4c) is ONE float. Its panels, the
|
|
287
332
|
# stray content preserved between them and the group caption are laid out
|
|
288
333
|
# as a single box that a page break may not enter, so the caption cannot
|
|
289
334
|
# be stranded from the panels it numbers, nor a panel from its own
|
|
290
335
|
# caption.
|
|
291
336
|
#
|
|
292
|
-
# PANELS ARE THE
|
|
293
|
-
# other child is plain group content and is drawn IN
|
|
337
|
+
# PANELS ARE THE CAPTIONED CHILDREN (an image paragraph, a table), in
|
|
338
|
+
# source order; every other child is plain group content and is drawn IN
|
|
339
|
+
# PLACE between them.
|
|
294
340
|
# Nothing here re-attaches or drops it.
|
|
295
341
|
#
|
|
296
342
|
# THE GROUP CAPTION IS ALREADY NUMBERED when it arrives: the number is a
|
|
@@ -301,9 +347,10 @@ module Carve
|
|
|
301
347
|
style = style_for("figure.group", with_box: true)
|
|
302
348
|
panels = ::HexaPDF::Document::Layout::ChildrenCollector.collect(@layout) do |cont|
|
|
303
349
|
Array(node[:children]).each do |child|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
else
|
|
350
|
+
if child[:type] == "table" || child[:caption]
|
|
351
|
+
panel(child, cont)
|
|
352
|
+
else
|
|
353
|
+
block(child, cont)
|
|
307
354
|
end
|
|
308
355
|
end
|
|
309
356
|
end
|
|
@@ -313,7 +360,8 @@ module Carve
|
|
|
313
360
|
else
|
|
314
361
|
panels
|
|
315
362
|
end
|
|
316
|
-
|
|
363
|
+
caption = caption_inlines(node[:caption])
|
|
364
|
+
body += [group_caption_box(caption)] unless caption.empty?
|
|
317
365
|
emit_box(keep_together(body, style: style[:box]), target)
|
|
318
366
|
end
|
|
319
367
|
|
|
@@ -323,7 +371,7 @@ module Carve
|
|
|
323
371
|
# the panel wrapper adds nothing to it.
|
|
324
372
|
def panel(node, target)
|
|
325
373
|
children = ::HexaPDF::Document::Layout::ChildrenCollector.collect(@layout) do |cont|
|
|
326
|
-
node[:type] == "table" ? table(node, cont) :
|
|
374
|
+
node[:type] == "table" ? table(node, cont) : captioned(node, cont)
|
|
327
375
|
end
|
|
328
376
|
emit_box(keep_together(children), target)
|
|
329
377
|
end
|
|
@@ -395,23 +443,38 @@ module Carve
|
|
|
395
443
|
end
|
|
396
444
|
|
|
397
445
|
def display_math(node, target)
|
|
398
|
-
if (bytes = call_renderer(:math, node[:
|
|
446
|
+
if (bytes = call_renderer(:math, node[:value].to_s, true))
|
|
399
447
|
return image_bytes(bytes, target, align: :center)
|
|
400
448
|
end
|
|
401
449
|
|
|
402
450
|
style = style_for("math", with_box: true)
|
|
403
451
|
box = style.delete(:box)
|
|
404
452
|
style[:font] ||= code_font
|
|
405
|
-
target.text(drawable_text(node[:
|
|
453
|
+
target.text(drawable_text(node[:value].to_s, style[:font]), **style,
|
|
406
454
|
text_align: :center, box_style: box)
|
|
407
455
|
end
|
|
408
456
|
|
|
409
|
-
|
|
457
|
+
# `{.page-break}` on `***` is the page-break convention (carve#6793). Only
|
|
458
|
+
# the composer can start a page; inside a container the break draws
|
|
459
|
+
# nothing, and on a page with nothing on it yet it adds no blank page.
|
|
460
|
+
def thematic_break(node, target)
|
|
461
|
+
attrs = node[:attrs]
|
|
462
|
+
if attrs.is_a?(Hash) && Array(attrs[:classes]).map(&:to_s).include?("page-break")
|
|
463
|
+
target.new_page if target.respond_to?(:new_page) && !page_empty?(target)
|
|
464
|
+
return
|
|
465
|
+
end
|
|
466
|
+
|
|
410
467
|
style = style_for("thematic_break")
|
|
411
468
|
height = style.delete(:height)
|
|
412
469
|
target.box(:base, height: height, style: style)
|
|
413
470
|
end
|
|
414
471
|
|
|
472
|
+
# Nothing drawn yet: the frame cannot tell, since a float or an unmasked
|
|
473
|
+
# box draws without moving +y+ or cutting the frame shape.
|
|
474
|
+
def page_empty?(composer)
|
|
475
|
+
composer.canvas.contents.empty?
|
|
476
|
+
end
|
|
477
|
+
|
|
415
478
|
# Draw a rendered image ({bytes:, width:, height:} or raw bytes) as a
|
|
416
479
|
# block image. :width/:height are box constructor arguments in HexaPDF,
|
|
417
480
|
# not style properties.
|
|
@@ -430,13 +493,28 @@ module Carve
|
|
|
430
493
|
|
|
431
494
|
# ---- tables (with row/col spans) -----------------------------------
|
|
432
495
|
|
|
496
|
+
# The rows of a table and the number in its head group (C2-A table).
|
|
497
|
+
def table_rows(node)
|
|
498
|
+
rows = []
|
|
499
|
+
head = 0
|
|
500
|
+
Array(node[:children]).each do |group|
|
|
501
|
+
Array(group[:children]).each do |row|
|
|
502
|
+
next unless row[:type] == "table_row"
|
|
503
|
+
|
|
504
|
+
rows << row
|
|
505
|
+
head += 1 if group[:type] == "table_head"
|
|
506
|
+
end
|
|
507
|
+
end
|
|
508
|
+
[rows, head]
|
|
509
|
+
end
|
|
510
|
+
|
|
433
511
|
def table(node, target)
|
|
434
|
-
|
|
512
|
+
rows, head_rows = table_rows(node)
|
|
513
|
+
resolved = resolve_spans(rows)
|
|
435
514
|
return if resolved.empty?
|
|
436
515
|
|
|
437
|
-
# HexaPDF
|
|
438
|
-
header_count = resolved.first.
|
|
439
|
-
resolved.first.none? { |o| o[:row_span] > 1 } ? 1 : 0
|
|
516
|
+
# HexaPDF repeats one header row, and not one whose cell spans into the body.
|
|
517
|
+
header_count = head_rows.positive? && resolved.first.none? { |o| o[:row_span] > 1 } ? 1 : 0
|
|
440
518
|
table_style = style_for("table")
|
|
441
519
|
header_style = style_for("table.header")
|
|
442
520
|
columns = column_alignments(node)
|
|
@@ -444,7 +522,7 @@ module Carve
|
|
|
444
522
|
cell_boxes = resolved.map do |row|
|
|
445
523
|
row.map do |o|
|
|
446
524
|
cell_style = o[:header] ? table_style.merge(header_style) : table_style
|
|
447
|
-
runs = inline_runs(o[:cell]
|
|
525
|
+
runs = inline_runs(cell_inlines(o[:cell]), bold: o[:header],
|
|
448
526
|
font_family: cell_style[:font])
|
|
449
527
|
runs = [{ text: "" }] if runs.empty?
|
|
450
528
|
# :margin belongs to the table box, :cell_padding is our pseudo-prop.
|
|
@@ -466,10 +544,19 @@ module Carve
|
|
|
466
544
|
body = [[{ content: @layout.text_box("") }]] if body.nil? || body.empty?
|
|
467
545
|
|
|
468
546
|
target.table(body, header: header, margin: table_style[:margin])
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
547
|
+
caption = caption_inlines(node[:caption])
|
|
548
|
+
return if caption.empty?
|
|
549
|
+
|
|
550
|
+
style = text_style("table.caption")
|
|
551
|
+
target.formatted_text(inline_runs(caption, italic: true, font_family: style[:font]), **style)
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
# A cell holds cell paragraphs (or blocks); a table cell draws one run of
|
|
555
|
+
# their inlines.
|
|
556
|
+
def cell_inlines(cell)
|
|
557
|
+
Array(cell[:children]).each_with_index.flat_map do |blk, index|
|
|
558
|
+
lead = index.positive? ? [{ type: "text", value: " " }] : []
|
|
559
|
+
lead + (%w[cell_paragraph paragraph].include?(blk[:type]) ? Array(blk[:children]) : [blk])
|
|
473
560
|
end
|
|
474
561
|
end
|
|
475
562
|
|
|
@@ -477,16 +564,14 @@ module Carve
|
|
|
477
564
|
|
|
478
565
|
# A cell without its own alignment inherits its column's: the table's
|
|
479
566
|
# `columns` metadata, overridden by the head rows (the HTML `thead`), a
|
|
480
|
-
# later row winning. Any other cell's alignment is local. The head is
|
|
481
|
-
#
|
|
567
|
+
# later row winning. Any other cell's alignment is local. The head is the
|
|
568
|
+
# table's head group.
|
|
482
569
|
# Vertical alignment is not drawn: `text_valign` overflows a table cell.
|
|
483
570
|
def column_alignments(node)
|
|
484
|
-
columns = Array(node[:columns]).map { |c| (c || {})
|
|
485
|
-
rows =
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
head.each do |row|
|
|
489
|
-
Array(row[:cells]).each_with_index do |cell, col|
|
|
571
|
+
columns = Array(node[:columns]).map { |c| { align: (c || {})[:align] || (c || {})[:resolvedAlign] }.compact }
|
|
572
|
+
rows, head_rows = table_rows(node)
|
|
573
|
+
rows.first(head_rows).each do |row|
|
|
574
|
+
Array(row[:children]).each_with_index do |cell, col|
|
|
490
575
|
next if cell[:span]
|
|
491
576
|
|
|
492
577
|
(columns[col] ||= {})[:align] = cell[:align] if cell[:align]
|
|
@@ -495,12 +580,6 @@ module Carve
|
|
|
495
580
|
columns
|
|
496
581
|
end
|
|
497
582
|
|
|
498
|
-
# A `^` / `<` marker is not a header cell, but leaves its row in the head.
|
|
499
|
-
def header_row?(row)
|
|
500
|
-
content = Array(row[:cells]).reject { |cell| cell[:span] }
|
|
501
|
-
!content.empty? && content.all? { |cell| cell[:header] }
|
|
502
|
-
end
|
|
503
|
-
|
|
504
583
|
# Resolve Carve's explicit span markers (a `<` cell = merge left, a `^`
|
|
505
584
|
# cell = merge up) into per-cell col_span / row_span counts, returning
|
|
506
585
|
# rows of originator hashes {cell:, header:, col:, col_span:, row_span:} with
|
|
@@ -513,7 +592,7 @@ module Carve
|
|
|
513
592
|
emitted = []
|
|
514
593
|
last = nil
|
|
515
594
|
bumped = {} # originators already row-extended in THIS row (by object id)
|
|
516
|
-
Array(row[:
|
|
595
|
+
Array(row[:children]).each_with_index do |cell, col|
|
|
517
596
|
case cell[:span]
|
|
518
597
|
when "colspan"
|
|
519
598
|
owner = last || col_owner[col - 1]
|
|
@@ -566,8 +645,6 @@ module Carve
|
|
|
566
645
|
case node[:type]
|
|
567
646
|
when "text" then out << run(node[:value].to_s, ctx)
|
|
568
647
|
when "soft_break" then out << run(" ", ctx)
|
|
569
|
-
# A `+` continuation row join; the spec renders it as one space.
|
|
570
|
-
when "cell_wrap" then out << run(" ", ctx)
|
|
571
648
|
when "hard_break" then out << { text: "\n" }
|
|
572
649
|
# Each emphasis sort is its OWN node type. They used to be one
|
|
573
650
|
# `emphasis` node carrying a `kind`, so a profile could not deny bold
|
|
@@ -599,53 +676,39 @@ module Carve
|
|
|
599
676
|
# dropped. There is no wrapper in a PDF text run, so what is left is the
|
|
600
677
|
# content, unstyled: carve-js renders `A !`raw span` B` as `A raw span B`
|
|
601
678
|
# in plain text.
|
|
602
|
-
when "literal_inline" then out << run(node[:
|
|
679
|
+
when "literal_inline" then out << run(node[:value].to_s, ctx)
|
|
603
680
|
# The number in "Figure 1:". Without this the caption renders without it.
|
|
604
681
|
when "caption_number" then out << run(node[:n].to_s, ctx)
|
|
605
682
|
# `:name[content]` - the content is an inline array, and the extension's
|
|
606
683
|
# own presentation has no PDF form, so emit what it wraps. carve-js
|
|
607
684
|
# renders `:kbd[Ctrl]` as `Ctrl` in plain text.
|
|
608
685
|
when "inline_extension" then inline_extension(node, ctx, out)
|
|
609
|
-
when "mention" then out << run("@#{node[:
|
|
686
|
+
when "mention" then out << run("@#{node[:target]}", ctx)
|
|
610
687
|
when "tag" then out << run("##{node[:name]}", ctx)
|
|
611
688
|
# `footnote_ref` is `[^label]` and `inline_footnote` is `^[body]`.
|
|
612
689
|
# Both used to publish as `footnote`, which is the BLOCK definition's
|
|
613
690
|
# type - one identifier for three constructs, so nothing could tell them
|
|
614
691
|
# apart (carve-rb#19). The old name is still accepted: a tree stored by
|
|
615
692
|
# an older version can still be rendered.
|
|
616
|
-
when "footnote_ref", "inline_footnote"
|
|
693
|
+
when "footnote_ref", "inline_footnote"
|
|
617
694
|
number = register_footnote(node)
|
|
618
695
|
out << run("[#{number}]", ctx.merge(super: true)) if number
|
|
619
696
|
when "citation_group" then out << run(node[:raw].to_s, ctx)
|
|
620
|
-
when "abbreviation" then out << run(node[:
|
|
621
|
-
#
|
|
622
|
-
#
|
|
623
|
-
#
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
# in its source spelling rather than as a bare word that reads like
|
|
631
|
-
# prose.
|
|
632
|
-
when "heading_ref", "cross_ref"
|
|
633
|
-
if node[:href].to_s.empty?
|
|
634
|
-
out << run("</##{node[:target]}>", ctx)
|
|
697
|
+
when "abbreviation" then out << run(node[:key].to_s, ctx)
|
|
698
|
+
# A cross-reference (`</#id>`). A resolved one carries `href` and draws
|
|
699
|
+
# as a link over its key; an unresolved one has no `href` and is drawn
|
|
700
|
+
# in its source spelling, as the HTML writer renders it.
|
|
701
|
+
when "xref"
|
|
702
|
+
if node[:namespace] == "footnote"
|
|
703
|
+
number = register_footnote(node)
|
|
704
|
+
out << run("[#{number}]", ctx.merge(super: true)) if number
|
|
705
|
+
elsif node[:href].to_s.empty?
|
|
706
|
+
out << run("</##{node[:key]}>", ctx)
|
|
635
707
|
else
|
|
636
|
-
out << run(node[:
|
|
708
|
+
out << run(node[:key].to_s, ctx.merge(link: node[:href].to_s))
|
|
637
709
|
end
|
|
638
|
-
when "caption_number" then (out << run(node[:number].to_s, ctx) if node[:number])
|
|
639
710
|
when "insert" then emit_children(node, ctx.merge(underline: true), out)
|
|
640
711
|
when "delete" then emit_children(node, ctx.merge(strike: true), out)
|
|
641
|
-
# Both halves are inline arrays, and both reach the page: struck for the
|
|
642
|
-
# replaced run, underlined for the replacement, which is what the
|
|
643
|
-
# reference ANSI and HTML renderers show.
|
|
644
|
-
# carve-lang 0.1.3 and older publish the halves as `oldText`/`newText`
|
|
645
|
-
# strings; the gemspec floor still admits those engines.
|
|
646
|
-
when "substitution"
|
|
647
|
-
emit_half(node[:old], node[:oldText], ctx.merge(strike: true), out)
|
|
648
|
-
emit_half(node[:new], node[:newText], ctx.merge(underline: true), out)
|
|
649
712
|
when "ruby"
|
|
650
713
|
Array(node[:pairs]).each do |pair|
|
|
651
714
|
Array(pair[:base]).each { |part| emit_inline(part, ctx, out) }
|
|
@@ -654,12 +717,7 @@ module Carve
|
|
|
654
717
|
out << run(")", ctx)
|
|
655
718
|
end
|
|
656
719
|
when "smart_punctuation"
|
|
657
|
-
out << run(smart_punctuation_text(node), ctx, fallback: node[:
|
|
658
|
-
# A character the author escaped. The backslash is authoring syntax, so
|
|
659
|
-
# the page shows the character - but the node has no children, so
|
|
660
|
-
# without this arm it falls through to the branch that emits children
|
|
661
|
-
# and renders nothing at all (carve#350, carve#355).
|
|
662
|
-
when "escaped_text" then out << run(node[:value].to_s, ctx)
|
|
720
|
+
out << run(smart_punctuation_text(node), ctx, fallback: node[:source])
|
|
663
721
|
when "raw_inline", "critic_comment"
|
|
664
722
|
# No safe PDF form - drop.
|
|
665
723
|
else
|
|
@@ -703,21 +761,13 @@ module Carve
|
|
|
703
761
|
glyph = node[:glyph]
|
|
704
762
|
return glyph.to_s unless glyph.nil? || glyph.to_s.empty?
|
|
705
763
|
|
|
706
|
-
SMART_PUNCTUATION_GLYPHS.fetch(node[:kind].to_s) { node[:
|
|
764
|
+
SMART_PUNCTUATION_GLYPHS.fetch(node[:kind].to_s) { node[:source].to_s }
|
|
707
765
|
end
|
|
708
766
|
|
|
709
767
|
def emit_children(node, ctx, out)
|
|
710
768
|
Array(node[:children]).each { |c| emit_inline(c, ctx, out) }
|
|
711
769
|
end
|
|
712
770
|
|
|
713
|
-
def emit_half(inlines, text, ctx, out)
|
|
714
|
-
if inlines.nil? && text
|
|
715
|
-
out << run(text.to_s, ctx)
|
|
716
|
-
else
|
|
717
|
-
emit_children({ children: inlines }, ctx, out)
|
|
718
|
-
end
|
|
719
|
-
end
|
|
720
|
-
|
|
721
771
|
# Styling for one emphasis node.
|
|
722
772
|
#
|
|
723
773
|
# `/*both*/` is a single `strong` node carrying `boldItalic`, not a
|
|
@@ -744,15 +794,9 @@ module Carve
|
|
|
744
794
|
|
|
745
795
|
# Build one formatted-text run hash for +text+ under styling +ctx+.
|
|
746
796
|
# `:name[content]` - the extension's own presentation has no PDF form, so
|
|
747
|
-
# emit what it wraps
|
|
748
|
-
# hand-built one may hold a bare string, and `render_ast` is public, so
|
|
749
|
-
# take both rather than raising on the second.
|
|
797
|
+
# emit what it wraps, or the fallback it promised when it wraps nothing.
|
|
750
798
|
def inline_extension(node, ctx, out)
|
|
751
|
-
|
|
752
|
-
case content
|
|
753
|
-
when Array then emit_children({ children: content }, ctx, out)
|
|
754
|
-
when String then out << run(content, ctx)
|
|
755
|
-
end
|
|
799
|
+
emit_children({ children: extension_content(node) }, ctx, out)
|
|
756
800
|
end
|
|
757
801
|
|
|
758
802
|
# U+E000 STANDS FOR a no-break space the parser resolved - from an escaped
|
|
@@ -904,8 +948,8 @@ module Carve
|
|
|
904
948
|
def walk_footnote_defs(node, defs)
|
|
905
949
|
return unless node.is_a?(Hash)
|
|
906
950
|
|
|
907
|
-
if node[:type] == "footnote" && node[:
|
|
908
|
-
defs[node[:
|
|
951
|
+
if node[:type] == "footnote" && node[:key]
|
|
952
|
+
defs[node[:key].to_s] ||= Array(node[:children])
|
|
909
953
|
end
|
|
910
954
|
|
|
911
955
|
node.each_value do |value|
|
|
@@ -920,17 +964,12 @@ module Carve
|
|
|
920
964
|
# when there is nothing to number (unresolvable reference). Repeated
|
|
921
965
|
# references to the same definition share one number and one endnote.
|
|
922
966
|
def register_footnote(node)
|
|
923
|
-
if node[:
|
|
924
|
-
@footnotes << { number: @footnotes.size + 1, inline: node[:
|
|
967
|
+
if node[:type] == "inline_footnote"
|
|
968
|
+
@footnotes << { number: @footnotes.size + 1, inline: Array(node[:children]) }
|
|
925
969
|
return @footnotes.last[:number]
|
|
926
970
|
end
|
|
927
971
|
|
|
928
|
-
|
|
929
|
-
# still accepted, the same way the node-type arms above accept both
|
|
930
|
-
# vocabularies. Reading only `id` silently dropped the body: the marker
|
|
931
|
-
# still numbered itself from `node[:number]`, so the page kept `[2]`
|
|
932
|
-
# and lost "2. Referenced note body." entirely.
|
|
933
|
-
id = node[:label] || node[:id]
|
|
972
|
+
id = node[:key]&.to_s
|
|
934
973
|
if id && @footnote_defs.key?(id)
|
|
935
974
|
return @footnote_numbers[id] ||= begin
|
|
936
975
|
@footnotes << { number: @footnotes.size + 1, blocks: @footnote_defs[id] }
|
|
@@ -979,16 +1018,16 @@ module Carve
|
|
|
979
1018
|
end
|
|
980
1019
|
|
|
981
1020
|
def inline_math(node, ctx, out)
|
|
982
|
-
if (img = call_renderer(:math, node[:
|
|
1021
|
+
if (img = call_renderer(:math, node[:value].to_s, !!node[:display]))
|
|
983
1022
|
spec = { box: [:image, StringIO.new(img[:bytes])],
|
|
984
1023
|
height: img[:height] || 11, valign: :baseline }
|
|
985
1024
|
spec[:width] = img[:width] if img[:width]
|
|
986
1025
|
out << spec
|
|
987
1026
|
else
|
|
988
|
-
out << run(node[:
|
|
1027
|
+
out << run(node[:value].to_s, ctx.merge(code: true))
|
|
989
1028
|
end
|
|
990
1029
|
rescue StandardError
|
|
991
|
-
out << run(node[:
|
|
1030
|
+
out << run(node[:value].to_s, ctx.merge(code: true))
|
|
992
1031
|
end
|
|
993
1032
|
|
|
994
1033
|
def inline_image(node, ctx, out)
|
|
@@ -1046,7 +1085,7 @@ module Carve
|
|
|
1046
1085
|
text emphasis code link image span math raw_inline emoji autolink
|
|
1047
1086
|
cross_ref caption_number mention tag citation_group inline_extension
|
|
1048
1087
|
abbreviation footnote soft_break hard_break insert delete
|
|
1049
|
-
|
|
1088
|
+
critic_comment
|
|
1050
1089
|
].freeze
|
|
1051
1090
|
end
|
|
1052
1091
|
end
|
data/lib/carve/hexapdf.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "hexapdf"
|
|
4
4
|
require "carve"
|
|
5
|
+
require "json"
|
|
5
6
|
|
|
6
7
|
require_relative "hexapdf/version"
|
|
7
8
|
require_relative "hexapdf/style_map"
|
|
@@ -25,6 +26,9 @@ module Carve
|
|
|
25
26
|
# (MIT) only bridges to it; your use of HexaPDF is governed by HexaPDF's own
|
|
26
27
|
# terms.
|
|
27
28
|
module Hexapdf
|
|
29
|
+
# Raised when the installed engine does not produce the Carve 0.2 tree.
|
|
30
|
+
class EngineError < StandardError; end
|
|
31
|
+
|
|
28
32
|
class << self
|
|
29
33
|
# Render Carve +source+ to a PDF and return the document as a binary
|
|
30
34
|
# String.
|
|
@@ -80,7 +84,7 @@ module Carve
|
|
|
80
84
|
composer = ::HexaPDF::Composer.new(page_size: page_size, margin: margin)
|
|
81
85
|
Renderer.new(composer, base_font: base_font, code_font: code_font,
|
|
82
86
|
link_color: link_color, highlight_color: highlight_color,
|
|
83
|
-
styles: styles, renderers: renderers).render_document(ast)
|
|
87
|
+
styles: styles, renderers: renderers).render_document(expand_snippets(ast))
|
|
84
88
|
composer.write_to_string
|
|
85
89
|
end
|
|
86
90
|
|
|
@@ -109,7 +113,7 @@ module Carve
|
|
|
109
113
|
"pass include_root: and source_path:, or drop them"
|
|
110
114
|
end
|
|
111
115
|
|
|
112
|
-
return ::Carve.parse(source)
|
|
116
|
+
return require_tree_v2(::Carve.parse(source))
|
|
113
117
|
end
|
|
114
118
|
|
|
115
119
|
result = ::Carve.parse_with_includes(source, root: include_root,
|
|
@@ -117,7 +121,50 @@ module Carve
|
|
|
117
121
|
extensions: extensions,
|
|
118
122
|
profile: profile)
|
|
119
123
|
report_includes(result, on_includes)
|
|
120
|
-
result[:value]
|
|
124
|
+
tree = require_tree_v2(result[:value])
|
|
125
|
+
return tree if Array(extensions).empty?
|
|
126
|
+
|
|
127
|
+
render_view(tree, extensions)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# A snippet use has no children: its copy exists only in the render
|
|
131
|
+
# view (C2-N-091, C2-A-221), so a tree that holds one is drawn from there.
|
|
132
|
+
def expand_snippets(tree)
|
|
133
|
+
snippet_use?(tree) ? render_view(tree, nil) : tree
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def snippet_use?(node)
|
|
137
|
+
case node
|
|
138
|
+
when Hash then node[:type] == "snippet_use" || node.each_value.any? { |value| snippet_use?(value) }
|
|
139
|
+
when Array then node.any? { |value| snippet_use?(value) }
|
|
140
|
+
else false
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The view the HTML renderer would draw. Document transforms (heading
|
|
145
|
+
# numbers, autolink, the table of contents) edit that view only, never
|
|
146
|
+
# the parse tree (C2-X-053), so a host transform registered after them
|
|
147
|
+
# hands it back. The expanded tree goes in, so the drawn document is the
|
|
148
|
+
# one the include report describes.
|
|
149
|
+
def render_view(tree, extensions)
|
|
150
|
+
view = nil
|
|
151
|
+
capture = ->(document) { view = document }
|
|
152
|
+
::Carve.render_with_host(tree: tree,
|
|
153
|
+
options: { extensions: Array(extensions).map(&:to_s) },
|
|
154
|
+
transforms: [capture])
|
|
155
|
+
require_tree_v2(JSON.parse(JSON.generate(view), symbolize_names: true))
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# The renderer reads the 0.2 tree. carve-rb's 0.2 line still carries the gem
|
|
159
|
+
# version of the last 0.1 release, so the gemspec range cannot tell the
|
|
160
|
+
# two engines apart; the tree's own contract version can.
|
|
161
|
+
def require_tree_v2(tree)
|
|
162
|
+
version = tree.is_a?(Hash) ? tree[:astVersion].to_s : ""
|
|
163
|
+
return tree if version.start_with?("2.")
|
|
164
|
+
|
|
165
|
+
raise ::Carve::Hexapdf::EngineError,
|
|
166
|
+
"carve-hexapdf needs the Carve 0.2 engine (AST contract 2.x); the installed " \
|
|
167
|
+
"carve-lang #{::Carve::VERSION} produced #{version.empty? ? 'a 0.1 tree' : "contract #{version}"}"
|
|
121
168
|
end
|
|
122
169
|
|
|
123
170
|
# Hand the caller everything expansion found, or say what degraded.
|
|
@@ -134,9 +181,11 @@ module Carve
|
|
|
134
181
|
return
|
|
135
182
|
end
|
|
136
183
|
|
|
184
|
+
# A 0.2 diagnostic names its cause in `code` (C2-N-013) and the
|
|
185
|
+
# included file it points into in `file`, never a host path.
|
|
137
186
|
Array(result[:warnings]).each do |warning|
|
|
138
187
|
where = warning[:file] ? "#{warning[:file]}: " : ""
|
|
139
|
-
$stderr.puts "carve-hexapdf: #{where}#{warning[:
|
|
188
|
+
$stderr.puts "carve-hexapdf: #{where}#{warning[:code]}: #{warning[:message]}"
|
|
140
189
|
end
|
|
141
190
|
suppressed = result[:suppressedWarnings].to_i
|
|
142
191
|
return unless suppressed.positive?
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: carve-hexapdf
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- markup-carve
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: carve-lang
|
|
@@ -16,20 +16,20 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 0.
|
|
19
|
+
version: 0.2.0
|
|
20
20
|
- - "<"
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
|
-
version: 0.
|
|
22
|
+
version: 0.3.0
|
|
23
23
|
type: :runtime
|
|
24
24
|
prerelease: false
|
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
26
26
|
requirements:
|
|
27
27
|
- - ">="
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
|
-
version: 0.
|
|
29
|
+
version: 0.2.0
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 0.
|
|
32
|
+
version: 0.3.0
|
|
33
33
|
- !ruby/object:Gem::Dependency
|
|
34
34
|
name: hexapdf
|
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
106
106
|
requirements:
|
|
107
107
|
- - ">="
|
|
108
108
|
- !ruby/object:Gem::Version
|
|
109
|
-
version: 3.
|
|
109
|
+
version: '3.2'
|
|
110
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
111
|
requirements:
|
|
112
112
|
- - ">="
|