bearings 0.1.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.
Files changed (143) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +31 -0
  3. data/LICENSE +21 -0
  4. data/README.md +32 -0
  5. data/lib/bearings/checks/code_examples.rb +80 -0
  6. data/lib/bearings/checks/constant_names.rb +99 -0
  7. data/lib/bearings/checks/diagrams.rb +63 -0
  8. data/lib/bearings/checks/external_links/failure.rb +3 -0
  9. data/lib/bearings/checks/external_links.rb +53 -0
  10. data/lib/bearings/checks/images.rb +120 -0
  11. data/lib/bearings/checks/links.rb +230 -0
  12. data/lib/bearings/checks/manifest_matches_disk.rb +48 -0
  13. data/lib/bearings/checks/readme_links.rb +103 -0
  14. data/lib/bearings/document.rb +9 -0
  15. data/lib/bearings/guide.rb +28 -0
  16. data/lib/bearings/html_pages/diagram_failure.rb +3 -0
  17. data/lib/bearings/html_pages.rb +174 -0
  18. data/lib/bearings/images/dimensions.rb +82 -0
  19. data/lib/bearings/images.rb +83 -0
  20. data/lib/bearings/kramdown.rb +253 -0
  21. data/lib/bearings/llms_txt.rb +61 -0
  22. data/lib/bearings/manifest/checks.rb +3 -0
  23. data/lib/bearings/manifest/group.rb +3 -0
  24. data/lib/bearings/manifest/outputs.rb +3 -0
  25. data/lib/bearings/manifest/page.rb +3 -0
  26. data/lib/bearings/manifest/parser.rb +264 -0
  27. data/lib/bearings/manifest/redirect.rb +3 -0
  28. data/lib/bearings/manifest/site.rb +3 -0
  29. data/lib/bearings/manifest.rb +3 -0
  30. data/lib/bearings/markdown_copies.rb +141 -0
  31. data/lib/bearings/net_http.rb +47 -0
  32. data/lib/bearings/output_file.rb +3 -0
  33. data/lib/bearings/pipeline.rb +101 -0
  34. data/lib/bearings/problem.rb +3 -0
  35. data/lib/bearings/rake.rb +318 -0
  36. data/lib/bearings/rbs.rb +20 -0
  37. data/lib/bearings/redirects.rb +103 -0
  38. data/lib/bearings/repo_path.rb +19 -0
  39. data/lib/bearings/report.rb +40 -0
  40. data/lib/bearings/search_index.rb +47 -0
  41. data/lib/bearings/server.rb +115 -0
  42. data/lib/bearings/site/group.rb +3 -0
  43. data/lib/bearings/site/page.rb +27 -0
  44. data/lib/bearings/site.rb +135 -0
  45. data/lib/bearings/sitemap.rb +57 -0
  46. data/lib/bearings/version.rb +5 -0
  47. data/lib/bearings/watch.rb +31 -0
  48. data/lib/bearings/writer.rb +63 -0
  49. data/lib/bearings.rb +20 -0
  50. data/sig/_private/kramdown.rbs +33 -0
  51. data/sig/_private/psych.rbs +50 -0
  52. data/sig/_private/rake.rbs +12 -0
  53. data/sig/_private/socket.rbs +4 -0
  54. data/sig/generated/bearings/checks/code_examples.rbs +35 -0
  55. data/sig/generated/bearings/checks/constant_names.rbs +48 -0
  56. data/sig/generated/bearings/checks/diagrams.rbs +39 -0
  57. data/sig/generated/bearings/checks/external_links/failure.rbs +2 -0
  58. data/sig/generated/bearings/checks/external_links.rbs +29 -0
  59. data/sig/generated/bearings/checks/images.rbs +59 -0
  60. data/sig/generated/bearings/checks/links.rbs +123 -0
  61. data/sig/generated/bearings/checks/manifest_matches_disk.rbs +20 -0
  62. data/sig/generated/bearings/checks/readme_links.rbs +46 -0
  63. data/sig/generated/bearings/document.rbs +2 -0
  64. data/sig/generated/bearings/guide.rbs +15 -0
  65. data/sig/generated/bearings/html_pages/diagram_failure.rbs +2 -0
  66. data/sig/generated/bearings/html_pages.rbs +90 -0
  67. data/sig/generated/bearings/images/dimensions.rbs +56 -0
  68. data/sig/generated/bearings/images.rbs +46 -0
  69. data/sig/generated/bearings/kramdown.rbs +119 -0
  70. data/sig/generated/bearings/llms_txt.rbs +30 -0
  71. data/sig/generated/bearings/manifest/checks.rbs +2 -0
  72. data/sig/generated/bearings/manifest/group.rbs +2 -0
  73. data/sig/generated/bearings/manifest/outputs.rbs +2 -0
  74. data/sig/generated/bearings/manifest/page.rbs +2 -0
  75. data/sig/generated/bearings/manifest/parser.rbs +127 -0
  76. data/sig/generated/bearings/manifest/redirect.rbs +2 -0
  77. data/sig/generated/bearings/manifest/site.rbs +2 -0
  78. data/sig/generated/bearings/manifest.rbs +2 -0
  79. data/sig/generated/bearings/markdown_copies.rbs +69 -0
  80. data/sig/generated/bearings/net_http.rbs +24 -0
  81. data/sig/generated/bearings/output_file.rbs +2 -0
  82. data/sig/generated/bearings/pipeline.rbs +62 -0
  83. data/sig/generated/bearings/problem.rbs +2 -0
  84. data/sig/generated/bearings/rake.rbs +156 -0
  85. data/sig/generated/bearings/rbs.rbs +7 -0
  86. data/sig/generated/bearings/redirects.rbs +44 -0
  87. data/sig/generated/bearings/repo_path.rbs +10 -0
  88. data/sig/generated/bearings/report.rbs +25 -0
  89. data/sig/generated/bearings/search_index.rbs +44 -0
  90. data/sig/generated/bearings/server.rbs +55 -0
  91. data/sig/generated/bearings/site/group.rbs +2 -0
  92. data/sig/generated/bearings/site/page.rbs +7 -0
  93. data/sig/generated/bearings/site.rbs +48 -0
  94. data/sig/generated/bearings/sitemap.rbs +22 -0
  95. data/sig/generated/bearings/version.rbs +5 -0
  96. data/sig/generated/bearings/watch.rbs +19 -0
  97. data/sig/generated/bearings/writer.rbs +29 -0
  98. data/sig/generated/bearings.rbs +4 -0
  99. data/sig/manual/bearings/checks/code_examples.rbs +4 -0
  100. data/sig/manual/bearings/checks/constant_names.rbs +4 -0
  101. data/sig/manual/bearings/checks/diagrams.rbs +4 -0
  102. data/sig/manual/bearings/checks/external_links/failure.rbs +13 -0
  103. data/sig/manual/bearings/checks/external_links.rbs +8 -0
  104. data/sig/manual/bearings/checks/images.rbs +4 -0
  105. data/sig/manual/bearings/checks/links.rbs +4 -0
  106. data/sig/manual/bearings/checks/manifest_matches_disk.rbs +4 -0
  107. data/sig/manual/bearings/checks/readme_links.rbs +4 -0
  108. data/sig/manual/bearings/checks.rbs +3 -0
  109. data/sig/manual/bearings/document.rbs +109 -0
  110. data/sig/manual/bearings/guide.rbs +8 -0
  111. data/sig/manual/bearings/html_pages/diagram_failure.rbs +11 -0
  112. data/sig/manual/bearings/html_pages.rbs +8 -0
  113. data/sig/manual/bearings/images/dimensions.rbs +4 -0
  114. data/sig/manual/bearings/images.rbs +4 -0
  115. data/sig/manual/bearings/kramdown.rbs +4 -0
  116. data/sig/manual/bearings/llms_txt.rbs +4 -0
  117. data/sig/manual/bearings/manifest/checks.rbs +13 -0
  118. data/sig/manual/bearings/manifest/group.rbs +15 -0
  119. data/sig/manual/bearings/manifest/outputs.rbs +11 -0
  120. data/sig/manual/bearings/manifest/page.rbs +19 -0
  121. data/sig/manual/bearings/manifest/parser.rbs +4 -0
  122. data/sig/manual/bearings/manifest/redirect.rbs +15 -0
  123. data/sig/manual/bearings/manifest/site.rbs +21 -0
  124. data/sig/manual/bearings/manifest.rbs +21 -0
  125. data/sig/manual/bearings/markdown_copies.rbs +4 -0
  126. data/sig/manual/bearings/net_http.rbs +4 -0
  127. data/sig/manual/bearings/output_file.rbs +13 -0
  128. data/sig/manual/bearings/pipeline.rbs +4 -0
  129. data/sig/manual/bearings/problem.rbs +21 -0
  130. data/sig/manual/bearings/rake.rbs +4 -0
  131. data/sig/manual/bearings/rbs.rbs +4 -0
  132. data/sig/manual/bearings/redirects.rbs +4 -0
  133. data/sig/manual/bearings/repo_path.rbs +4 -0
  134. data/sig/manual/bearings/report.rbs +4 -0
  135. data/sig/manual/bearings/search_index.rbs +4 -0
  136. data/sig/manual/bearings/server.rbs +4 -0
  137. data/sig/manual/bearings/site/group.rbs +15 -0
  138. data/sig/manual/bearings/site/page.rbs +39 -0
  139. data/sig/manual/bearings/site.rbs +17 -0
  140. data/sig/manual/bearings/sitemap.rbs +4 -0
  141. data/sig/manual/bearings/watch.rbs +4 -0
  142. data/sig/manual/bearings/writer.rbs +4 -0
  143. metadata +242 -0
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi/escape'
4
+ require 'pathname'
5
+
6
+ module Bearings::Checks::Links
7
+ extend self
8
+
9
+ # @rbs!
10
+ # type reference = [String, String, String, Integer]
11
+ # type located = [String, Integer]
12
+
13
+ EXTERNAL = %r{#{Bearings::RepoPath::SCHEME}|\A//} #: Regexp
14
+ ATTRIBUTE = /\b(?:href|src)="([^"]+)"/ #: Regexp
15
+ CODE = %r{<code\b[^>]*>.*?</code>}m #: Regexp
16
+ ID = /\bid="([^"]+)"/ #: Regexp
17
+ LINK_HINT = 'point the link at a page or file the site writes' #: String
18
+ ANCHOR_HINT = "point the fragment at an id on that page, such as a heading's" #: String
19
+
20
+ # @rbs site: Bearings::Site
21
+ # @rbs files: Array[Bearings::OutputFile]
22
+ # @rbs sources: Hash[String, String]
23
+ # @rbs markdown: Bearings::_Markdown
24
+ # @rbs layout_file: String
25
+ # @rbs landing_file: String
26
+ # @rbs manifest_file: String
27
+ # @rbs return: Array[Bearings::Problem]
28
+ def call(site, files, sources:, markdown:, layout_file:, landing_file:, manifest_file:)
29
+ contents = files.to_h { |file| [file.path, file.content] }
30
+ texts = contents.select { |_, content| content.is_a?(String) } #: Hash[String, String]
31
+ guides = site.pages.to_h { |page| [page.source_path, page] }
32
+ references = [
33
+ *site.pages.flat_map { |page| in_page(page, texts, guides, sources.fetch(page.source_path, ''), layout_file) },
34
+ *site.pages.flat_map { |page| in_copy(page, texts, markdown) },
35
+ *in_html('index.html', texts).map { |href| ['index.html', href, landing_file, 1] },
36
+ *in_markdown('llms.txt', texts, markdown).map { |href, _| ['llms.txt', href, manifest_file, 1] }
37
+ ] #: Array[reference]
38
+ base = site.details.url&.delete_suffix('/')
39
+ images = Set.new(site.pages.flat_map { |page| images(page) })
40
+ references.filter_map { |reference| problem(reference, contents, texts, base, images) }.uniq
41
+ end
42
+
43
+ # @rbs document: Bearings::Document
44
+ # @rbs return: Array[located]
45
+ def written(document)
46
+ [
47
+ *document.links.map { |link| [link.href, link.line] },
48
+ *document.images.map { |image| [image.src, image.line] }
49
+ ]
50
+ end
51
+
52
+ # @rbs source: String
53
+ # @rbs document: Bearings::Document
54
+ # @rbs return: Array[located]
55
+ def raw(source, document)
56
+ # HTML shown as code is text in the guide, not a link.
57
+ shown = Set.new(Bearings::MarkdownCopies.fenced(document))
58
+ source.lines.each.with_index(1).flat_map do |line, number|
59
+ next [] if shown.include?(number)
60
+
61
+ attributes(line.gsub(Bearings::MarkdownCopies::CODE_SPAN, '')).map { |value| [value, number] }
62
+ end
63
+ end
64
+
65
+ # @rbs html: String
66
+ # @rbs return: Array[String]
67
+ def ids(html)
68
+ html.gsub(CODE, '').scan(ID).flatten
69
+ end
70
+
71
+ private
72
+
73
+ # @rbs page: Bearings::Site::Page
74
+ # @rbs texts: Hash[String, String]
75
+ # @rbs guides: Hash[String, Bearings::Site::Page]
76
+ # @rbs source: String
77
+ # @rbs layout_file: String
78
+ # @rbs return: Array[reference]
79
+ def in_page(page, texts, guides, source, layout_file)
80
+ from = Bearings::HtmlPages.path(page)
81
+ written_at = by_form(page, written(page.document), guides)
82
+ # The markdown parser lists no link written as raw HTML, so one is found in the guide's source.
83
+ raw_at = by_form(page, raw(source, page.document), guides)
84
+ in_html(from, texts).flat_map do |href|
85
+ lines = written_at.fetch(href, [])
86
+ next [unwritten(page, from, href, raw_at, layout_file)] if lines.empty?
87
+ # HtmlPages reports a guide's link to a markdown file that is not a guide, and leaves it as written.
88
+ next [] if Bearings::HtmlPages::WHOLE_GUIDE_LINK.match?(href)
89
+
90
+ lines.map { |line| [from, href, page.source_path, line] }
91
+ end
92
+ end
93
+
94
+ # @rbs page: Bearings::Site::Page
95
+ # @rbs found: Array[located]
96
+ # @rbs guides: Hash[String, Bearings::Site::Page]
97
+ # @rbs return: Hash[String, Array[Integer]]
98
+ def by_form(page, found, guides)
99
+ forms = found.flat_map do |value, line|
100
+ [value, *rewritten(page, value, guides)].map { |form| [form, line] }
101
+ end #: Array[located]
102
+ forms.group_by(&:first).transform_values { |pairs| pairs.map(&:last) }
103
+ end
104
+
105
+ # @rbs page: Bearings::Site::Page
106
+ # @rbs source: String
107
+ # @rbs guides: Hash[String, Bearings::Site::Page]
108
+ # @rbs return: Array[String]
109
+ def rewritten(page, source, guides)
110
+ path, hash, fragment = source.partition('#')
111
+ in_repo = Bearings::RepoPath.resolve(page.source_path, path)
112
+ return [] unless in_repo
113
+
114
+ # HtmlPages rewrites a guide's link to another guide before it reaches the page.
115
+ guide = guides.fetch(in_repo, nil)
116
+ guide ? ["#{guide.url}#{hash}#{fragment}"] : []
117
+ end
118
+
119
+ # @rbs page: Bearings::Site::Page
120
+ # @rbs from: String
121
+ # @rbs href: String
122
+ # @rbs raw_at: Hash[String, Array[Integer]]
123
+ # @rbs layout_file: String
124
+ # @rbs return: reference
125
+ def unwritten(page, from, href, raw_at, layout_file)
126
+ line = raw_at.fetch(href, []).first
127
+ line ? [from, href, page.source_path, line] : [from, href, layout_file, 1]
128
+ end
129
+
130
+ # @rbs page: Bearings::Site::Page
131
+ # @rbs return: Array[String]
132
+ def images(page)
133
+ page.document.images.filter_map do |image|
134
+ Bearings::RepoPath.resolve(page.source_path, image.src)&.then { |in_repo| Bearings::Images.url(in_repo) }
135
+ end
136
+ end
137
+
138
+ # @rbs page: Bearings::Site::Page
139
+ # @rbs texts: Hash[String, String]
140
+ # @rbs markdown: Bearings::_Markdown
141
+ # @rbs return: Array[reference]
142
+ def in_copy(page, texts, markdown)
143
+ from = Bearings::MarkdownCopies.path(page)
144
+ # A copy keeps its guide's lines, so a link's line in the copy is its line in the guide.
145
+ in_markdown(from, texts, markdown).filter_map do |href, line|
146
+ # HtmlPages reports a link to a markdown file that is not a guide, which the copy leaves as written.
147
+ [from, href, page.source_path, line] unless Bearings::HtmlPages::WHOLE_GUIDE_LINK.match?(href)
148
+ end
149
+ end
150
+
151
+ # @rbs path: String
152
+ # @rbs texts: Hash[String, String]
153
+ # @rbs return: Array[String]
154
+ def in_html(path, texts)
155
+ attributes(texts.fetch(path, '')).uniq
156
+ end
157
+
158
+ # @rbs html: String
159
+ # @rbs return: Array[String]
160
+ def attributes(html)
161
+ # HTML shown as code is text on the page, not a link.
162
+ found = html.gsub(CODE, '').scan(ATTRIBUTE).flatten #: Array[String]
163
+ found.map { |value| CGI.unescapeHTML(value) }
164
+ end
165
+
166
+ # @rbs path: String
167
+ # @rbs texts: Hash[String, String]
168
+ # @rbs markdown: Bearings::_Markdown
169
+ # @rbs return: Array[located]
170
+ def in_markdown(path, texts, markdown)
171
+ text = texts[path]
172
+ document = markdown.document(text) if text
173
+ document ? written(document) : []
174
+ end
175
+
176
+ # @rbs reference: reference
177
+ # @rbs contents: Hash[String, String | Pathname]
178
+ # @rbs texts: Hash[String, String]
179
+ # @rbs base: String?
180
+ # @rbs images: Set[String]
181
+ # @rbs return: Bearings::Problem?
182
+ def problem(reference, contents, texts, base, images)
183
+ from, href, file, line = reference
184
+ local = base && href.start_with?("#{base}/") ? href.delete_prefix(base) : href
185
+ return if EXTERNAL.match?(local)
186
+
187
+ address, _, fragment = local.partition('#')
188
+ path = address.partition('?').first
189
+ # Checks::Images reports a guide's image that is missing, and Images writes every one that is not.
190
+ return if images.include?(path)
191
+
192
+ target = target(from, path, contents)
193
+ unless target
194
+ return failed(file, line, 'link', "#{subject(from)} `#{href}`, which is not a file in the site", LINK_HINT)
195
+ end
196
+ # A fragment in markdown names a heading on GitHub, and a copied file's ids can't be read without opening it.
197
+ return if fragment.empty? || !from.end_with?('.html') || !target.end_with?('.html') || !texts.key?(target)
198
+ return if ids(texts.fetch(target)).include?(fragment)
199
+
200
+ failed(file, line, 'anchor', "links to `#{href}`, but #{target} has no id `#{fragment}`", ANCHOR_HINT)
201
+ end
202
+
203
+ # @rbs from: String
204
+ # @rbs path: String
205
+ # @rbs contents: Hash[String, String | Pathname]
206
+ # @rbs return: String?
207
+ def target(from, path, contents)
208
+ return from if path.empty?
209
+
210
+ resolved = Pathname("/#{from}").dirname.join(path).cleanpath
211
+ candidates = [resolved, resolved.join('index.html')].map { |file| file.to_s.delete_prefix('/') }
212
+ candidates.find { |file| contents.key?(file) }
213
+ end
214
+
215
+ # @rbs from: String
216
+ # @rbs return: String
217
+ def subject(from)
218
+ from.end_with?('.md') ? "its copy #{from} links to" : 'links to'
219
+ end
220
+
221
+ # @rbs file: String
222
+ # @rbs line: Integer
223
+ # @rbs check: String
224
+ # @rbs message: String
225
+ # @rbs hint: String
226
+ # @rbs return: Bearings::Problem
227
+ def failed(file, line, check, message, hint)
228
+ Bearings::Problem.new(check:, level: :error, file:, line:, message:, hint:)
229
+ end
230
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bearings::Checks::ManifestMatchesDisk
4
+ extend self
5
+
6
+ # @rbs manifest: Bearings::Manifest
7
+ # @rbs paths: Array[String]
8
+ # @rbs return: Array[Bearings::Problem]
9
+ def call(manifest:, paths:)
10
+ pages = manifest.groups.flat_map(&:pages)
11
+ unlisted(paths, manifest.watch, pages.map(&:source)) + undescribed(pages)
12
+ end
13
+
14
+ private
15
+
16
+ # @rbs paths: Array[String]
17
+ # @rbs watch: Array[String]
18
+ # @rbs sources: Array[String]
19
+ # @rbs return: Array[Bearings::Problem]
20
+ def unlisted(paths, watch, sources)
21
+ folders = watch.map { |folder| "#{folder.delete_suffix('/')}/" }
22
+ (paths - sources).select { |path| path.end_with?('.md') && path.start_with?(*folders) }.map do |path|
23
+ Bearings::Problem.new(
24
+ check: 'unlisted',
25
+ level: :error,
26
+ file: path,
27
+ line: 1,
28
+ message: 'is in a watched folder but no page in the manifest lists it',
29
+ hint: 'add a page with this `source` to the manifest, or move the file out of the watched folder'
30
+ )
31
+ end
32
+ end
33
+
34
+ # @rbs pages: Array[Bearings::Manifest::Page]
35
+ # @rbs return: Array[Bearings::Problem]
36
+ def undescribed(pages)
37
+ pages.select { |page| page.description.to_s.strip.empty? }.map do |page|
38
+ Bearings::Problem.new(
39
+ check: 'description',
40
+ level: :error,
41
+ file: page.source,
42
+ line: 1,
43
+ message: 'has no description in the manifest',
44
+ hint: 'add a one-line `description` to its page in the manifest'
45
+ )
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
5
+ module Bearings::Checks::ReadmeLinks
6
+ extend self
7
+
8
+ # @rbs!
9
+ # type anchors = [String, Array[String]]
10
+
11
+ FILE = 'README.md' #: String
12
+
13
+ # @rbs site: Bearings::Site
14
+ # @rbs files: Array[Bearings::OutputFile]
15
+ # @rbs readme: String
16
+ # @rbs paths: Array[String]
17
+ # @rbs markdown: singleton(Bearings::Kramdown)
18
+ # @rbs return: Array[Bearings::Problem]
19
+ def call(site, files, readme:, paths:, markdown:)
20
+ document = markdown.whole(readme)
21
+ texts = files.to_h { |file| [file.path, file.content] }.select { |_, content| content.is_a?(String) } #: Hash[String, String]
22
+ guides = site.pages.to_h do |page|
23
+ built = Bearings::HtmlPages.path(page)
24
+ [page.source_path, [built, Bearings::Checks::Links.ids(texts.fetch(built, ''))]]
25
+ end #: Hash[String, anchors]
26
+ own = [FILE, Bearings::Checks::Links.ids(document.body)] #: anchors
27
+ internal(document, readme).filter_map { |href, line| problem(href, line, paths, guides, own) }
28
+ end
29
+
30
+ # @rbs readme: String
31
+ # @rbs markdown: singleton(Bearings::Kramdown)
32
+ # @rbs return: Array[String]
33
+ def paths(readme, markdown:)
34
+ internal(markdown.whole(readme), readme).filter_map { |href, _| repo_path(href) }.uniq
35
+ end
36
+
37
+ private
38
+
39
+ # @rbs document: Bearings::Document
40
+ # @rbs readme: String
41
+ # @rbs return: Array[[String, Integer]]
42
+ def internal(document, readme)
43
+ found = [*Bearings::Checks::Links.written(document), *Bearings::Checks::Links.raw(readme, document)].uniq
44
+ found.reject { |href, _| Bearings::Checks::Links::EXTERNAL.match?(href) }
45
+ end
46
+
47
+ # @rbs href: String
48
+ # @rbs return: String?
49
+ def repo_path(href)
50
+ path = href.partition('#').first
51
+ Pathname(path).cleanpath.to_s unless path.empty?
52
+ end
53
+
54
+ # @rbs href: String
55
+ # @rbs line: Integer
56
+ # @rbs paths: Array[String]
57
+ # @rbs guides: Hash[String, anchors]
58
+ # @rbs own: anchors
59
+ # @rbs return: Bearings::Problem?
60
+ def problem(href, line, paths, guides, own)
61
+ file = repo_path(href)
62
+ if file && !paths.include?(file)
63
+ return failed(
64
+ line,
65
+ 'link',
66
+ "links to `#{href}`, which is not a file in the repo",
67
+ 'fix the path, or add the file'
68
+ )
69
+ end
70
+
71
+ fragment = href.partition('#').last
72
+ return if fragment.empty?
73
+
74
+ anchors = file ? guides.fetch(file, nil) : own
75
+ unless anchors
76
+ return failed(
77
+ line,
78
+ 'anchor',
79
+ "links to `#{href}`, an anchor in a file that is not a guide",
80
+ 'link to the file without the anchor, or to an anchor in a guide'
81
+ )
82
+ end
83
+
84
+ built, ids = anchors
85
+ return if ids.include?(fragment)
86
+
87
+ failed(
88
+ line,
89
+ 'anchor',
90
+ "links to `#{href}`, but #{built} has no id `#{fragment}`",
91
+ Bearings::Checks::Links::ANCHOR_HINT
92
+ )
93
+ end
94
+
95
+ # @rbs line: Integer
96
+ # @rbs check: String
97
+ # @rbs message: String
98
+ # @rbs hint: String
99
+ # @rbs return: Bearings::Problem
100
+ def failed(line, check, message, hint)
101
+ Bearings::Problem.new(check:, level: :error, file: FILE, line:, message:, hint:)
102
+ end
103
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bearings::Document = Data.define(:title, :headings, :links, :code_blocks, :code_spans, :images, :diagrams, :body)
4
+ Bearings::Document::Heading = Data.define(:level, :id, :line)
5
+ Bearings::Document::Link = Data.define(:href, :line)
6
+ Bearings::Document::CodeBlock = Data.define(:language, :code, :line)
7
+ Bearings::Document::CodeSpan = Data.define(:code, :line)
8
+ Bearings::Document::Image = Data.define(:src, :alt, :line)
9
+ Bearings::Document::Diagram = Data.define(:code, :line)
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bearings::Guide
4
+ extend self
5
+
6
+ # @rbs file: String
7
+ # @rbs source: String
8
+ # @rbs markdown: Bearings::_Markdown
9
+ # @rbs return: Bearings::Document | Bearings::Problem
10
+ def parse(file, source, markdown)
11
+ markdown.document(source) || untitled(file)
12
+ end
13
+
14
+ private
15
+
16
+ # @rbs file: String
17
+ # @rbs return: Bearings::Problem
18
+ def untitled(file)
19
+ Bearings::Problem.new(
20
+ check: 'title',
21
+ level: :error,
22
+ file:,
23
+ line: 1,
24
+ message: 'has no "# " title line',
25
+ hint: 'start the guide with a "# Title" line; it becomes the page title'
26
+ )
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bearings::HtmlPages::DiagramFailure = Data.define(:reason)
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi/escape'
4
+ require 'erb'
5
+ require 'pathname'
6
+
7
+ module Bearings::HtmlPages
8
+ extend self
9
+
10
+ # @rbs!
11
+ # type toc_entry = { id: String, title: String }
12
+ # type drawing = String | Bearings::HtmlPages::DiagramFailure
13
+
14
+ GUIDE_LINK = /(?<path>[^":]+\.md)(?<fragment>#[^"]*)?/ #: Regexp
15
+ GUIDE_HREF = /href="#{GUIDE_LINK}"/ #: Regexp
16
+ WHOLE_GUIDE_LINK = /\A#{GUIDE_LINK}\z/ #: Regexp
17
+ H2 = %r{<h2 id="([^"]+)"[^>]*>(.*?)</h2>}m #: Regexp
18
+ DIAGRAM = %r{<pre class="mermaid">(.*?)</pre>}m #: Regexp
19
+
20
+ # @rbs site: Bearings::Site
21
+ # @rbs layout: String
22
+ # @rbs landing: String
23
+ # @rbs assets: Hash[String, Pathname]
24
+ # @rbs images: Hash[String, String]
25
+ # @rbs diagram_renderer: Bearings::_DiagramRenderer?
26
+ # @rbs return: [Array[Bearings::OutputFile], Array[Bearings::Problem]]
27
+ def call(site, layout:, landing:, assets:, images:, diagram_renderer: nil)
28
+ by_source = site.pages.to_h { |page| [page.source_path, page] }
29
+ by_slug = site.pages.to_h { |page| [page.slug, page] }
30
+ template = ERB.new(layout, trim_mode: '-')
31
+ drawn = draw(site.pages, diagram_renderer)
32
+ files = [
33
+ landing_file(site, landing),
34
+ *site.pages.map { |page| page_file(page, site, template, by_source, by_slug, images, drawn) },
35
+ *assets.map { |path, source| Bearings::OutputFile.new(path:, content: source) }
36
+ ]
37
+ [files, site.pages.flat_map { |page| unknown_links(page, by_source) + undrawn(page, drawn) }]
38
+ end
39
+
40
+ # @rbs page: Bearings::Site::Page
41
+ # @rbs return: String
42
+ def path(page)
43
+ "#{page.url.delete_prefix('/')}index.html"
44
+ end
45
+
46
+ private
47
+
48
+ # @rbs pages: Array[Bearings::Site::Page]
49
+ # @rbs renderer: Bearings::_DiagramRenderer?
50
+ # @rbs return: Hash[String, drawing]
51
+ def draw(pages, renderer)
52
+ return {} unless renderer
53
+
54
+ pages.flat_map { |page| page.document.diagrams.map(&:code) }.uniq.to_h { |code| [code, renderer.render(code)] }
55
+ end
56
+
57
+ # @rbs site: Bearings::Site
58
+ # @rbs return: Hash[Symbol, untyped]
59
+ def shared(site)
60
+ { groups: site.groups, pages: site.pages, version: site.version, site: site.details }
61
+ end
62
+
63
+ # @rbs site: Bearings::Site
64
+ # @rbs landing: String
65
+ # @rbs return: Bearings::OutputFile
66
+ def landing_file(site, landing)
67
+ html = ERB.new(landing, trim_mode: '-').result_with_hash(shared(site))
68
+ Bearings::OutputFile.new(path: 'index.html', content: html)
69
+ end
70
+
71
+ # @rbs page: Bearings::Site::Page
72
+ # @rbs site: Bearings::Site
73
+ # @rbs layout: ERB
74
+ # @rbs by_source: Hash[String, Bearings::Site::Page]
75
+ # @rbs by_slug: Hash[String, Bearings::Site::Page]
76
+ # @rbs images: Hash[String, String]
77
+ # @rbs drawn: Hash[String, drawing]
78
+ # @rbs return: Bearings::OutputFile
79
+ def page_file(page, site, layout, by_source, by_slug, images, drawn)
80
+ body = with_drawings(Bearings::Images.html(rewrite_links(page, by_source), page.source_path, images), drawn)
81
+ html = layout.result_with_hash(
82
+ **shared(site),
83
+ page:,
84
+ body:,
85
+ toc: toc(body),
86
+ prev_page: neighbour(page.previous_slug, by_slug),
87
+ next_page: neighbour(page.next_slug, by_slug),
88
+ search_index_url: Bearings::SearchIndex::URL
89
+ )
90
+ Bearings::OutputFile.new(path: path(page), content: html)
91
+ end
92
+
93
+ # @rbs page: Bearings::Site::Page
94
+ # @rbs by_source: Hash[String, Bearings::Site::Page]
95
+ # @rbs return: String
96
+ def rewrite_links(page, by_source)
97
+ page.document.body.gsub(GUIDE_HREF) do |href|
98
+ match = Regexp.last_match #: MatchData
99
+ path = match['path'] #: String
100
+ target = by_source.fetch(resolve(page.source_path, path), nil)
101
+ target ? %(href="#{target.url}#{match['fragment']}") : href
102
+ end
103
+ end
104
+
105
+ # @rbs body: String
106
+ # @rbs drawn: Hash[String, drawing]
107
+ # @rbs return: String
108
+ def with_drawings(body, drawn)
109
+ body.gsub(DIAGRAM) do |pre|
110
+ source = Regexp.last_match(1) #: String
111
+ drawing = drawn[CGI.unescapeHTML(source)]
112
+ drawing.is_a?(String) ? drawing : pre
113
+ end
114
+ end
115
+
116
+ # @rbs body: String
117
+ # @rbs return: Array[toc_entry]
118
+ def toc(body)
119
+ headings = body.scan(H2) #: Array[[String, String]]
120
+ headings.map { |id, title| { id:, title: title.gsub(Bearings::SearchIndex::TAG, '') } }
121
+ end
122
+
123
+ # @rbs slug: String?
124
+ # @rbs by_slug: Hash[String, Bearings::Site::Page]
125
+ # @rbs return: Bearings::Site::Page?
126
+ def neighbour(slug, by_slug)
127
+ by_slug[slug] if slug
128
+ end
129
+
130
+ # @rbs page: Bearings::Site::Page
131
+ # @rbs by_source: Hash[String, Bearings::Site::Page]
132
+ # @rbs return: Array[Bearings::Problem]
133
+ def unknown_links(page, by_source)
134
+ page.document.links.filter_map do |link|
135
+ path = link.href[WHOLE_GUIDE_LINK, 'path']
136
+ next unless path && !by_source.key?(resolve(page.source_path, path))
137
+
138
+ Bearings::Problem.new(
139
+ check: 'link',
140
+ level: :error,
141
+ file: page.source_path,
142
+ line: link.line,
143
+ message: "links to `#{link.href}`, which is not a page",
144
+ hint: 'point the link at a guide the manifest lists, or add that guide to the manifest'
145
+ )
146
+ end
147
+ end
148
+
149
+ # @rbs page: Bearings::Site::Page
150
+ # @rbs drawn: Hash[String, drawing]
151
+ # @rbs return: Array[Bearings::Problem]
152
+ def undrawn(page, drawn)
153
+ page.document.diagrams.filter_map do |diagram|
154
+ failure = drawn[diagram.code]
155
+ next unless failure.is_a?(Bearings::HtmlPages::DiagramFailure)
156
+
157
+ Bearings::Problem.new(
158
+ check: 'diagram',
159
+ level: :error,
160
+ file: page.source_path,
161
+ line: diagram.line,
162
+ message: "has a diagram the renderer could not draw: #{failure.reason}",
163
+ hint: 'fix the Mermaid source, or remove the diagram'
164
+ )
165
+ end
166
+ end
167
+
168
+ # @rbs source: String
169
+ # @rbs path: String
170
+ # @rbs return: String
171
+ def resolve(source, path)
172
+ Pathname(source).dirname.join(path).cleanpath.to_s
173
+ end
174
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bearings::Images::Dimensions
4
+ extend self
5
+
6
+ PNG = "\x89PNG\r\n\x1A\n".b #: String
7
+ GIF = %w[GIF87a GIF89a].freeze #: Array[String]
8
+ JPEG = "\xFF\xD8".b #: String
9
+ # The start-of-frame markers, which hold the size; C4, C8 and CC fall in their range but mark other segments.
10
+ JPEG_FRAMES = ((0xC0..0xCF).to_a - [0xC4, 0xC8, 0xCC]).freeze #: Array[Integer]
11
+ SVG = /<svg\b[^>]*>/ #: Regexp
12
+ SVG_LENGTH = '\s*=\s*["\']\s*(\d+(?:\.\d+)?)(?:px)?\s*["\']' #: String
13
+ SVG_WIDTH = /\swidth#{SVG_LENGTH}/ #: Regexp
14
+ SVG_HEIGHT = /\sheight#{SVG_LENGTH}/ #: Regexp
15
+ SVG_VIEW_BOX = /\sviewBox\s*=\s*["']\s*[-\d.]+[\s,]+[-\d.]+[\s,]+(\d+(?:\.\d+)?)[\s,]+(\d+(?:\.\d+)?)\s*["']/ #: Regexp
16
+
17
+ # @rbs bytes: String
18
+ # @rbs return: [Integer, Integer]?
19
+ def call(bytes)
20
+ data = bytes.b
21
+ return png(data) if data.start_with?(PNG)
22
+ return gif(data) if data.start_with?(*GIF)
23
+ return jpeg(data, JPEG.bytesize) if data.start_with?(JPEG)
24
+
25
+ svg(data)
26
+ end
27
+
28
+ private
29
+
30
+ # @rbs data: String
31
+ # @rbs return: [Integer, Integer]?
32
+ def png(data)
33
+ pair(data, 'N2', 16) if data.bytesize >= 24
34
+ end
35
+
36
+ # @rbs data: String
37
+ # @rbs return: [Integer, Integer]?
38
+ def gif(data)
39
+ pair(data, 'v2', 6) if data.bytesize >= 10
40
+ end
41
+
42
+ # @rbs data: String
43
+ # @rbs at: Integer
44
+ # @rbs return: [Integer, Integer]?
45
+ def jpeg(data, at)
46
+ return if data.bytesize < at + 9
47
+
48
+ prefix, marker, length = data.unpack('C2n', offset: at) #: [Integer, Integer, Integer]
49
+ return unless prefix == 0xFF
50
+ return jpeg(data, at + 2 + length) unless JPEG_FRAMES.include?(marker)
51
+
52
+ height, width = pair(data, 'n2', at + 5)
53
+ [width, height]
54
+ end
55
+
56
+ # @rbs data: String
57
+ # @rbs format: String
58
+ # @rbs offset: Integer
59
+ # @rbs return: [Integer, Integer]
60
+ def pair(data, format, offset)
61
+ data.unpack(format, offset:) #: [Integer, Integer]
62
+ end
63
+
64
+ # @rbs data: String
65
+ # @rbs return: [Integer, Integer]?
66
+ def svg(data)
67
+ tag = data[SVG].to_s
68
+ width = tag[SVG_WIDTH, 1]
69
+ height = tag[SVG_HEIGHT, 1]
70
+ return pixels(width, height) if width && height
71
+
72
+ view_box = tag.match(SVG_VIEW_BOX)
73
+ pixels(view_box[1].to_s, view_box[2].to_s) if view_box
74
+ end
75
+
76
+ # @rbs width: String
77
+ # @rbs height: String
78
+ # @rbs return: [Integer, Integer]
79
+ def pixels(width, height)
80
+ [Float(width).round, Float(height).round]
81
+ end
82
+ end