suma 0.5.33 → 0.5.34

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: bd2e4a074e8d3b0ab67fd577a72084f05b6727c472b27d8372b8f22d0c98851c
4
- data.tar.gz: fd67ff08c1e9607ec9e24ed66100dda235bfea0805b42427c83199313a868632
3
+ metadata.gz: e638f1579d2a576dbe73f8b5947f82a2b3b964200b3c534f7340c385334a9b95
4
+ data.tar.gz: f2b91f20892348bcbab0b39b1d22f7a82c857c0f969a917e8ea28fcaa4f1a378
5
5
  SHA512:
6
- metadata.gz: 3017ab40298248f60f38a32b25842535a47d72c36603de5c6c49b55d1a269341c497752e06c5f2bb372d0830155a49ecda7429310f0ffc44865ea8792398ab7b
7
- data.tar.gz: 011e091b656afb963cd9c12156588a1fbf90a40ca08071c91ce58b20df3b8ba71464ccc075ca9d2486d263ac20e2400d5ac8b942d97e9d043566d2b3feee85f5
6
+ metadata.gz: 574fa8226308db543725ae05f768a69a6d4a23fd6774719d8cd27518501011f09e23e7e0cad27f33d3b60b5513ef1f77a4a7e0e2567da44380e777757263d076
7
+ data.tar.gz: 82c8d8d61115dadeb71dfaea93d0215c26df680b336f2f24f538738bb8173860f593ef1fde0ab2e2e5ead2851552b4f0f204e64d9d3cb134c5de1547121243f5
@@ -30,6 +30,19 @@ module Suma
30
30
  result.broken_count.zero?
31
31
  end
32
32
 
33
+ desc "resolve-fragments SITE_DIR",
34
+ "Assembly-time cross-page fragment resolution: repoint " \
35
+ "same-page fragment hrefs whose target renders on exactly " \
36
+ "one other page of the stitched site. Ambiguous matches are " \
37
+ "left untouched."
38
+ def resolve_fragments(site_dir)
39
+ result = Suma::Delivery::CrossPageFragments.new(site_dir).repair
40
+ Utils.log "Fragments: #{result[:own]} own-page fine, " \
41
+ "#{result[:remapped]} remapped cross-page, " \
42
+ "#{result[:ambiguous]} ambiguous left, " \
43
+ "#{result[:unresolved]} unresolved"
44
+ end
45
+
33
46
  desc "stitch BUNDLES_DIR",
34
47
  "Merge per-part bundles (each bundle.json + site/) into one " \
35
48
  "_site/, resolving every unresolved-eref span through the " \
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "leptris"
5
+
6
+ module Suma
7
+ module Delivery
8
+ # Assembly-time cross-page fragment resolution (the delivery
9
+ # correctness model's final pass): per-document rendering writes
10
+ # term-fragment and svgmap-hotspot hrefs page-locally, and the
11
+ # unioned site holds their targets on other pages. Two resolution
12
+ # rules, both requiring a unique site-wide match:
13
+ #
14
+ # 1. the fragment id exists on exactly one other page — the href
15
+ # repoints at that page;
16
+ # 2. the fragment names a declaration whose rendered anchor carries
17
+ # the stamped form `<scope>_ISO_…` with scope ending in
18
+ # `.<fragment>` (or equal to it) — the href repoints at the page
19
+ # rendering that anchor, at the full stamped id.
20
+ #
21
+ # Ambiguous matches (two or more candidate pages/anchors) stay
22
+ # untouched: guessing a target would be wrong exactly like an
23
+ # unresolved one.
24
+ class CrossPageFragments
25
+ SCOPE_PART = /[A-Za-z0-9-]+_[0-9]+/
26
+ # `<scope>_ISO_<doc>_<year>` — the scope captures as `schema` or
27
+ # `schema.entity`.
28
+ SCOPED_ID_RE = /\A([a-z][a-z0-9_]*(?:\.[a-z0-9_.]+)?)_ISO_#{SCOPE_PART}/
29
+
30
+ Result = Struct.new(:own, :remapped, :ambiguous, :unresolved,
31
+ keyword_init: true)
32
+
33
+ def initialize(site_dir)
34
+ @site_dir = Pathname.new(site_dir)
35
+ end
36
+
37
+ def repair
38
+ pages = html_pages
39
+ id_index = index_ids(pages)
40
+ counts = Hash.new(0)
41
+ pages.each do |path, doc|
42
+ remap_page(doc, path, id_index, counts)
43
+ path.write(doc.to_html)
44
+ end
45
+ Result.new(**counts.slice(:own, :remapped, :ambiguous, :unresolved))
46
+ end
47
+
48
+ private
49
+
50
+ def remap_page(doc, path, id_index, counts)
51
+ doc.css("a[href^='#']").each do |a|
52
+ status, href = resolve(a["href"].to_s.delete_prefix("#"),
53
+ path, id_index)
54
+ status = :unresolved if status.nil?
55
+ counts[status] += 1
56
+ a["href"] = href if href
57
+ end
58
+ end
59
+
60
+ # [status, href-or-nil]: :own = the target id is on the citing
61
+ # page (fine as written); :ambiguous = several candidates;
62
+ # :unresolved = no candidate; :remapped carries the new href.
63
+ def resolve(frag, path, id_index)
64
+ exact = id_index[frag]
65
+ if exact
66
+ uniq = exact[:pages].uniq
67
+ return [:own] if uniq == [path]
68
+ return [:ambiguous] if uniq.size > 1
69
+
70
+ return [:remapped, cross_href(path, uniq.first, frag)]
71
+ end
72
+
73
+ scoped_resolve(frag, path, id_index)
74
+ end
75
+
76
+ def scoped_resolve(frag, path, id_index)
77
+ candidates = matching_scopes(frag, path, id_index)
78
+ return [:ambiguous] if candidates.size > 1
79
+
80
+ id, pages = candidates.first
81
+ return [:unresolved, nil] unless id
82
+
83
+ [:remapped, cross_href(path, pages.first, id)]
84
+ end
85
+
86
+ # [id, pages] pairs for stamped anchors whose scope names the
87
+ # fragment, rendered on some page other than the citing one.
88
+ def matching_scopes(frag, path, id_index)
89
+ id_index.filter_map do |id, entry|
90
+ pages = entry[:pages].uniq
91
+ next if pages == [path]
92
+
93
+ scope = scope_of(id)
94
+ next unless scope == frag || scope.end_with?(".#{frag}")
95
+
96
+ [id, pages]
97
+ end
98
+ end
99
+
100
+ def scope_of(id)
101
+ SCOPED_ID_RE.match(id)&.[](1)
102
+ end
103
+
104
+ def cross_href(path, page, fragment)
105
+ "#{relative_href(path, page)}##{fragment}"
106
+ end
107
+
108
+ def relative_href(from, to)
109
+ from.dirname.join(to.basename).relative_path_from(from.dirname).to_s
110
+ end
111
+
112
+ def html_pages
113
+ @html_pages ||= @site_dir.glob("**/*.html")
114
+ .reject { |p| p.basename.to_s.end_with?(".err.html") }
115
+ .to_h { |p| [p, Leptris::XML.parse_html(p.read)] }
116
+ end
117
+
118
+ # fragment id → { pages: [path, …] }. A plain hash: reads of
119
+ # unknown ids must not insert (matching_scopes iterates the index
120
+ # afterwards).
121
+ def index_ids(pages)
122
+ index = {}
123
+ pages.each do |path, doc|
124
+ doc.css("[id]").each do |el|
125
+ next if (id = el["id"]).nil? || id.empty?
126
+
127
+ (index[id] ||= { pages: [] })[:pages] << path
128
+ end
129
+ end
130
+ index
131
+ end
132
+ end
133
+ end
134
+ end
data/lib/suma/delivery.rb CHANGED
@@ -5,6 +5,7 @@ module Suma
5
5
  # per-part sites and resolve their unresolved cross-document references.
6
6
  module Delivery
7
7
  autoload :FrameNav, "suma/delivery/frame_nav"
8
+ autoload :CrossPageFragments, "suma/delivery/cross_page_fragments"
8
9
  autoload :Pack, "suma/delivery/pack"
9
10
  autoload :Plan, "suma/delivery/plan"
10
11
  autoload :SiteVerifier, "suma/delivery/site_verifier"
data/lib/suma/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Suma
4
- VERSION = "0.5.33"
4
+ VERSION = "0.5.34"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: suma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.33
4
+ version: 0.5.34
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -242,6 +242,7 @@ files:
242
242
  - lib/suma/collection_manifest.rb
243
243
  - lib/suma/corpus_graph.rb
244
244
  - lib/suma/delivery.rb
245
+ - lib/suma/delivery/cross_page_fragments.rb
245
246
  - lib/suma/delivery/frame_nav.rb
246
247
  - lib/suma/delivery/pack.rb
247
248
  - lib/suma/delivery/plan.rb