ratatui_ruby 1.2.2 → 1.3.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 (269) hide show
  1. checksums.yaml +4 -4
  2. data/.builds/ruby-3.2.yml +54 -0
  3. data/.builds/ruby-3.3.yml +54 -0
  4. data/.builds/ruby-3.4.yml +54 -0
  5. data/.builds/ruby-4.0.0.yml +54 -0
  6. data/.pre-commit-config.yaml +16 -0
  7. data/.rubocop.yml +10 -0
  8. data/AGENTS.md +147 -0
  9. data/CHANGELOG.md +771 -0
  10. data/README.md +187 -0
  11. data/README.rdoc +302 -0
  12. data/Rakefile +11 -0
  13. data/Steepfile +50 -0
  14. data/doc/concepts/application_architecture.md +321 -0
  15. data/doc/concepts/application_testing.md +193 -0
  16. data/doc/concepts/async.md +190 -0
  17. data/doc/concepts/custom_widgets.md +247 -0
  18. data/doc/concepts/debugging.md +401 -0
  19. data/doc/concepts/event_handling.md +162 -0
  20. data/doc/concepts/interactive_design.md +146 -0
  21. data/doc/contributors/auditing/parity.md +239 -0
  22. data/doc/contributors/design/ruby_frontend.md +448 -0
  23. data/doc/contributors/design/rust_backend.md +434 -0
  24. data/doc/contributors/design.md +11 -0
  25. data/doc/contributors/developing_examples.md +400 -0
  26. data/doc/contributors/documentation_style.md +121 -0
  27. data/doc/contributors/index.md +21 -0
  28. data/doc/contributors/releasing.md +215 -0
  29. data/doc/contributors/todo/align/api_completeness_audit-finished.md +381 -0
  30. data/doc/contributors/todo/align/api_completeness_audit-unfinished.md +200 -0
  31. data/doc/contributors/todo/align/term.md +351 -0
  32. data/doc/contributors/todo/align/terminal.md +647 -0
  33. data/doc/contributors/todo/future_work.md +169 -0
  34. data/doc/contributors/upstream_requests/paragraph_span_rects.md +259 -0
  35. data/doc/contributors/upstream_requests/tab_rects.md +173 -0
  36. data/doc/contributors/upstream_requests/title_rects.md +132 -0
  37. data/doc/custom.css +22 -0
  38. data/doc/getting_started/quickstart.md +291 -0
  39. data/doc/getting_started/why.md +93 -0
  40. data/doc/images/app_all_events.png +0 -0
  41. data/doc/images/app_cli_rich_moments.gif +0 -0
  42. data/doc/images/app_color_picker.png +0 -0
  43. data/doc/images/app_debugging_showcase.gif +0 -0
  44. data/doc/images/app_debugging_showcase.png +0 -0
  45. data/doc/images/app_external_editor.gif +0 -0
  46. data/doc/images/app_login_form.png +0 -0
  47. data/doc/images/app_stateful_interaction.png +0 -0
  48. data/doc/images/verify_quickstart_dsl.png +0 -0
  49. data/doc/images/verify_quickstart_layout.png +0 -0
  50. data/doc/images/verify_quickstart_lifecycle.png +0 -0
  51. data/doc/images/verify_readme_usage.png +0 -0
  52. data/doc/images/widget_barchart.png +0 -0
  53. data/doc/images/widget_block.png +0 -0
  54. data/doc/images/widget_box.png +0 -0
  55. data/doc/images/widget_calendar.png +0 -0
  56. data/doc/images/widget_canvas.png +0 -0
  57. data/doc/images/widget_cell.png +0 -0
  58. data/doc/images/widget_center.png +0 -0
  59. data/doc/images/widget_chart.png +0 -0
  60. data/doc/images/widget_gauge.png +0 -0
  61. data/doc/images/widget_layout_split.png +0 -0
  62. data/doc/images/widget_line_gauge.png +0 -0
  63. data/doc/images/widget_list.png +0 -0
  64. data/doc/images/widget_map.png +0 -0
  65. data/doc/images/widget_overlay.png +0 -0
  66. data/doc/images/widget_popup.png +0 -0
  67. data/doc/images/widget_ratatui_logo.png +0 -0
  68. data/doc/images/widget_ratatui_mascot.png +0 -0
  69. data/doc/images/widget_rect.png +0 -0
  70. data/doc/images/widget_render.png +0 -0
  71. data/doc/images/widget_rich_text.png +0 -0
  72. data/doc/images/widget_scroll_text.png +0 -0
  73. data/doc/images/widget_scrollbar.png +0 -0
  74. data/doc/images/widget_sparkline.png +0 -0
  75. data/doc/images/widget_style_colors.png +0 -0
  76. data/doc/images/widget_table.png +0 -0
  77. data/doc/images/widget_tabs.png +0 -0
  78. data/doc/images/widget_text_width.png +0 -0
  79. data/doc/index.md +34 -0
  80. data/doc/troubleshooting/async.md +4 -0
  81. data/doc/troubleshooting/terminal_limitations.md +131 -0
  82. data/doc/troubleshooting/tui_output.md +197 -0
  83. data/examples/app_all_events/README.md +114 -0
  84. data/examples/app_all_events/app.rb +98 -0
  85. data/examples/app_all_events/model/app_model.rb +159 -0
  86. data/examples/app_all_events/model/event_color_cycle.rb +43 -0
  87. data/examples/app_all_events/model/event_entry.rb +94 -0
  88. data/examples/app_all_events/model/msg.rb +39 -0
  89. data/examples/app_all_events/model/timestamp.rb +56 -0
  90. data/examples/app_all_events/update.rb +75 -0
  91. data/examples/app_all_events/view/app_view.rb +80 -0
  92. data/examples/app_all_events/view/controls_view.rb +54 -0
  93. data/examples/app_all_events/view/counts_view.rb +61 -0
  94. data/examples/app_all_events/view/live_view.rb +72 -0
  95. data/examples/app_all_events/view/log_view.rb +57 -0
  96. data/examples/app_all_events/view.rb +9 -0
  97. data/examples/app_cli_rich_moments/README.md +81 -0
  98. data/examples/app_cli_rich_moments/app.rb +189 -0
  99. data/examples/app_color_picker/README.md +156 -0
  100. data/examples/app_color_picker/app.rb +76 -0
  101. data/examples/app_color_picker/clipboard.rb +86 -0
  102. data/examples/app_color_picker/color.rb +193 -0
  103. data/examples/app_color_picker/controls.rb +92 -0
  104. data/examples/app_color_picker/copy_dialog.rb +168 -0
  105. data/examples/app_color_picker/export_pane.rb +128 -0
  106. data/examples/app_color_picker/harmony.rb +58 -0
  107. data/examples/app_color_picker/input.rb +176 -0
  108. data/examples/app_color_picker/main_container.rb +180 -0
  109. data/examples/app_color_picker/palette.rb +111 -0
  110. data/examples/app_debugging_showcase/README.md +119 -0
  111. data/examples/app_debugging_showcase/app.rb +318 -0
  112. data/examples/app_external_editor/README.md +62 -0
  113. data/examples/app_external_editor/app.rb +344 -0
  114. data/examples/app_login_form/README.md +58 -0
  115. data/examples/app_login_form/app.rb +109 -0
  116. data/examples/app_stateful_interaction/README.md +35 -0
  117. data/examples/app_stateful_interaction/app.rb +328 -0
  118. data/examples/timeout_demo.rb +45 -0
  119. data/examples/verify_quickstart_dsl/README.md +55 -0
  120. data/examples/verify_quickstart_dsl/app.rb +49 -0
  121. data/examples/verify_quickstart_layout/README.md +77 -0
  122. data/examples/verify_quickstart_layout/app.rb +73 -0
  123. data/examples/verify_quickstart_lifecycle/README.md +68 -0
  124. data/examples/verify_quickstart_lifecycle/app.rb +62 -0
  125. data/examples/verify_readme_usage/README.md +49 -0
  126. data/examples/verify_readme_usage/app.rb +42 -0
  127. data/examples/verify_website_managed/README.md +48 -0
  128. data/examples/verify_website_managed/app.rb +36 -0
  129. data/examples/verify_website_menu/README.md +60 -0
  130. data/examples/verify_website_menu/app.rb +84 -0
  131. data/examples/verify_website_spinner/README.md +44 -0
  132. data/examples/verify_website_spinner/app.rb +34 -0
  133. data/examples/widget_barchart/README.md +58 -0
  134. data/examples/widget_barchart/app.rb +240 -0
  135. data/examples/widget_block/README.md +44 -0
  136. data/examples/widget_block/app.rb +258 -0
  137. data/examples/widget_box/README.md +54 -0
  138. data/examples/widget_box/app.rb +255 -0
  139. data/examples/widget_calendar/README.md +48 -0
  140. data/examples/widget_calendar/app.rb +115 -0
  141. data/examples/widget_canvas/README.md +31 -0
  142. data/examples/widget_canvas/app.rb +130 -0
  143. data/examples/widget_cell/README.md +45 -0
  144. data/examples/widget_cell/app.rb +112 -0
  145. data/examples/widget_center/README.md +33 -0
  146. data/examples/widget_center/app.rb +118 -0
  147. data/examples/widget_chart/README.md +50 -0
  148. data/examples/widget_chart/app.rb +220 -0
  149. data/examples/widget_gauge/README.md +50 -0
  150. data/examples/widget_gauge/app.rb +229 -0
  151. data/examples/widget_layout_split/README.md +53 -0
  152. data/examples/widget_layout_split/app.rb +260 -0
  153. data/examples/widget_line_gauge/README.md +50 -0
  154. data/examples/widget_line_gauge/app.rb +219 -0
  155. data/examples/widget_list/README.md +58 -0
  156. data/examples/widget_list/app.rb +382 -0
  157. data/examples/widget_map/README.md +48 -0
  158. data/examples/widget_map/app.rb +95 -0
  159. data/examples/widget_overlay/README.md +45 -0
  160. data/examples/widget_overlay/app.rb +250 -0
  161. data/examples/widget_popup/README.md +45 -0
  162. data/examples/widget_popup/app.rb +106 -0
  163. data/examples/widget_ratatui_logo/README.md +43 -0
  164. data/examples/widget_ratatui_logo/app.rb +104 -0
  165. data/examples/widget_ratatui_mascot/README.md +43 -0
  166. data/examples/widget_ratatui_mascot/app.rb +95 -0
  167. data/examples/widget_rect/README.md +53 -0
  168. data/examples/widget_rect/app.rb +222 -0
  169. data/examples/widget_render/README.md +46 -0
  170. data/examples/widget_render/app.rb +186 -0
  171. data/examples/widget_render/app.rbs +41 -0
  172. data/examples/widget_rich_text/README.md +44 -0
  173. data/examples/widget_rich_text/app.rb +193 -0
  174. data/examples/widget_scroll_text/README.md +46 -0
  175. data/examples/widget_scroll_text/app.rb +109 -0
  176. data/examples/widget_scrollbar/README.md +46 -0
  177. data/examples/widget_scrollbar/app.rb +155 -0
  178. data/examples/widget_sparkline/README.md +51 -0
  179. data/examples/widget_sparkline/app.rb +277 -0
  180. data/examples/widget_style_colors/README.md +43 -0
  181. data/examples/widget_style_colors/app.rb +83 -0
  182. data/examples/widget_table/README.md +57 -0
  183. data/examples/widget_table/app.rb +285 -0
  184. data/examples/widget_tabs/README.md +50 -0
  185. data/examples/widget_tabs/app.rb +183 -0
  186. data/examples/widget_text_width/README.md +44 -0
  187. data/examples/widget_text_width/app.rb +117 -0
  188. data/ext/ratatui_ruby/Cargo.lock +1 -2
  189. data/ext/ratatui_ruby/Cargo.toml +1 -2
  190. data/ext/ratatui_ruby/src/events.rs +18 -157
  191. data/lib/ratatui_ruby/event/focus_gained.rb +50 -0
  192. data/lib/ratatui_ruby/event/focus_lost.rb +51 -0
  193. data/lib/ratatui_ruby/event/key/dwim.rb +301 -0
  194. data/lib/ratatui_ruby/event/key/modifier.rb +2 -0
  195. data/lib/ratatui_ruby/event/key.rb +9 -0
  196. data/lib/ratatui_ruby/event/mouse.rb +33 -0
  197. data/lib/ratatui_ruby/event/paste.rb +25 -0
  198. data/lib/ratatui_ruby/event/resize.rb +65 -0
  199. data/lib/ratatui_ruby/version.rb +1 -1
  200. data/migrate_to_buffer.rb +145 -0
  201. data/mise.toml +8 -0
  202. data/sig/ratatui_ruby/event.rbs +97 -0
  203. data/tasks/autodoc/examples.rb +87 -0
  204. data/tasks/autodoc/member.rb +58 -0
  205. data/tasks/autodoc/name.rb +21 -0
  206. data/tasks/autodoc.rake +21 -0
  207. data/tasks/bump/bump_workflow.rb +49 -0
  208. data/tasks/bump/cargo_lockfile.rb +21 -0
  209. data/tasks/bump/changelog.rb +104 -0
  210. data/tasks/bump/header.rb +32 -0
  211. data/tasks/bump/history.rb +32 -0
  212. data/tasks/bump/links.rb +69 -0
  213. data/tasks/bump/manifest.rb +33 -0
  214. data/tasks/bump/patch_release.rb +19 -0
  215. data/tasks/bump/release_branch.rb +17 -0
  216. data/tasks/bump/release_from_trunk.rb +49 -0
  217. data/tasks/bump/repository.rb +54 -0
  218. data/tasks/bump/ruby_gem.rb +29 -0
  219. data/tasks/bump/sem_ver.rb +44 -0
  220. data/tasks/bump/unreleased_section.rb +73 -0
  221. data/tasks/bump.rake +61 -0
  222. data/tasks/doc/documentation.rb +59 -0
  223. data/tasks/doc/link/file_url.rb +30 -0
  224. data/tasks/doc/link/relative_path.rb +61 -0
  225. data/tasks/doc/link/web_url.rb +55 -0
  226. data/tasks/doc/link.rb +52 -0
  227. data/tasks/doc/link_audit.rb +116 -0
  228. data/tasks/doc/problem.rb +40 -0
  229. data/tasks/doc/source_file.rb +93 -0
  230. data/tasks/doc.rake +905 -0
  231. data/tasks/example_viewer.html.erb +172 -0
  232. data/tasks/extension.rake +14 -0
  233. data/tasks/license/headers_md.rb +223 -0
  234. data/tasks/license/headers_rb.rb +210 -0
  235. data/tasks/license/license_utils.rb +130 -0
  236. data/tasks/license/snippets_md.rb +315 -0
  237. data/tasks/license/snippets_rdoc.rb +150 -0
  238. data/tasks/license.rake +91 -0
  239. data/tasks/lint.rake +170 -0
  240. data/tasks/rbs_predicates/predicate_catalog.rb +52 -0
  241. data/tasks/rbs_predicates/predicate_tests.rb +124 -0
  242. data/tasks/rbs_predicates/rbs_signature.rb +63 -0
  243. data/tasks/rbs_predicates.rake +31 -0
  244. data/tasks/rdoc_config.rb +29 -0
  245. data/tasks/resources/build.yml.erb +60 -0
  246. data/tasks/resources/index.html.erb +141 -0
  247. data/tasks/resources/rubies.yml +7 -0
  248. data/tasks/sourcehut.rake +122 -0
  249. data/tasks/steep.rake +11 -0
  250. data/tasks/terminal_preview/app_screenshot.rb +45 -0
  251. data/tasks/terminal_preview/crash_report.rb +54 -0
  252. data/tasks/terminal_preview/example_app.rb +27 -0
  253. data/tasks/terminal_preview/launcher_script.rb +48 -0
  254. data/tasks/terminal_preview/preview_collection.rb +60 -0
  255. data/tasks/terminal_preview/preview_timing.rb +24 -0
  256. data/tasks/terminal_preview/safety_confirmation.rb +58 -0
  257. data/tasks/terminal_preview/saved_screenshot.rb +56 -0
  258. data/tasks/terminal_preview/system_appearance.rb +13 -0
  259. data/tasks/terminal_preview/terminal_window.rb +138 -0
  260. data/tasks/terminal_preview/window_id.rb +16 -0
  261. data/tasks/terminal_preview.rake +30 -0
  262. data/tasks/test.rake +36 -0
  263. data/tasks/website/index_page.rb +30 -0
  264. data/tasks/website/version.rb +122 -0
  265. data/tasks/website/version_menu.rb +68 -0
  266. data/tasks/website/versioned_documentation.rb +83 -0
  267. data/tasks/website/website.rb +53 -0
  268. data/tasks/website.rake +28 -0
  269. metadata +256 -1
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ require "pathname"
9
+ require_relative "source_file"
10
+
11
+ # All documentation files in the project.
12
+ #
13
+ # Projects contain many files. Only some are documentation: Ruby source with
14
+ # RDoc, Markdown guides, RDoc text files. Finding them manually is tedious.
15
+ # Missing files means missing broken links.
16
+ #
17
+ # Documentation enumerates all relevant files. It excludes vendor and temp
18
+ # directories. It wraps each path in a SourceFile for link extraction.
19
+ #
20
+ # === Example
21
+ #
22
+ # docs = Documentation.new("/project/root")
23
+ # docs.each do |file|
24
+ # file.links.each { |link| puts link.raw }
25
+ # end
26
+ # docs.count # => 392
27
+ #
28
+ class Documentation
29
+ include Enumerable
30
+
31
+ # File extensions to scan for links.
32
+ EXTENSIONS = %w[rb md rdoc].freeze
33
+
34
+ # Directories to skip.
35
+ EXCLUDES = %w[/vendor/ /tmp/].freeze
36
+
37
+ # Creates a new Documentation collection.
38
+ #
39
+ # [root] Project root directory to scan.
40
+ def initialize(root)
41
+ @root = Pathname.new(root)
42
+ end
43
+
44
+ # Yields each SourceFile in the project.
45
+ def each(&)
46
+ files.each(&)
47
+ end
48
+
49
+ private def files # :nodoc:
50
+ @files ||= EXTENSIONS.flat_map { |ext| glob(ext) }
51
+ end
52
+
53
+ private def glob(extension) # :nodoc:
54
+ Dir.glob(@root.join("**/*.#{extension}")).filter_map do |path|
55
+ next if EXCLUDES.any? { |exclude| path.include?(exclude) }
56
+ SourceFile.new(path, @root)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ require_relative "../problem"
9
+
10
+ # A <tt>file://</tt> URL in documentation.
11
+ #
12
+ # IDEs generate these links for local file access. They work on your machine.
13
+ # They break for everyone else. Published docs cannot contain local paths.
14
+ #
15
+ # FileUrl always returns a Problem. There is no valid use case for
16
+ # <tt>file://</tt> URLs in published documentation.
17
+ #
18
+ # === Example
19
+ #
20
+ # link = FileUrl.new("file:///path/to/file.rb", 10, source_file)
21
+ # link.problem(root) # => Problem (always)
22
+ #
23
+ class FileUrl < Link
24
+ # Returns a Problem. <tt>file://</tt> URLs never work in published docs.
25
+ #
26
+ # [_root] Unused. Present for interface compatibility.
27
+ def problem(_root)
28
+ Problem.new(self, "file:// URLs won't work when published")
29
+ end
30
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ require "pathname"
9
+
10
+ require_relative "../problem"
11
+
12
+ # A relative path to a local file.
13
+ #
14
+ # Documentation references sibling files: images, other docs, source code.
15
+ # Files get renamed. Directories restructure. These paths silently break.
16
+ #
17
+ # RelativePath resolves the path against the project root and checks if the
18
+ # target exists. It also handles RDoc's HTML naming convention (converting
19
+ # <tt>foo_rb.html</tt> back to <tt>foo.rb</tt> for validation).
20
+ #
21
+ # === Example
22
+ #
23
+ # link = RelativePath.new("../images/diagram.png", 20, source_file)
24
+ # link.problem(root) # => nil (file exists) or Problem (missing)
25
+ #
26
+ class RelativePath < Link
27
+ # Returns a Problem if the target file does not exist. Returns <tt>nil</tt>
28
+ # if the path resolves to an existing file or directory.
29
+ #
30
+ # [root] Project root directory for resolving absolute paths.
31
+ def problem(root)
32
+ root = Pathname.new(root)
33
+ resolved = resolve(root)
34
+ return nil unless resolved
35
+
36
+ Problem.new(self, "File not found: #{resolved.relative_path_from(root)}") unless exists?(resolved)
37
+ end
38
+
39
+ private def exists?(resolved) # :nodoc:
40
+ return true if resolved.exist?
41
+
42
+ # RDoc generates foo_rb.html from foo.rb
43
+ if raw.end_with?("_rb.html")
44
+ source = Pathname.new(resolved.to_s.sub(/_rb\.html$/, ".rb"))
45
+ return true if source.exist?
46
+ end
47
+
48
+ resolved.directory? if raw.end_with?("/")
49
+ end
50
+
51
+ private def resolve(root) # :nodoc:
52
+ path = raw.split("#").first&.split("?")&.first
53
+ return nil if path.nil? || path.empty? || path.length < 4
54
+
55
+ if path.start_with?("/")
56
+ root.join(path.delete_prefix("/"))
57
+ else
58
+ source_file.path.dirname.join(path)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ require "net/http"
9
+ require "uri"
10
+
11
+ require_relative "../problem"
12
+
13
+ # An HTTP or HTTPS URL in documentation.
14
+ #
15
+ # External links rot. Servers go offline. Pages move. A link that worked last
16
+ # year may 404 today. Manual verification is slow and error-prone.
17
+ #
18
+ # WebUrl checks reachability by making a HEAD request. It returns a Problem if
19
+ # the server responds with an error or is unreachable.
20
+ #
21
+ # === Example
22
+ #
23
+ # link = WebUrl.new("https://example.com", 15, source_file)
24
+ # link.web? # => true
25
+ # link.problem(root) # => nil (site is up) or Problem (site is down)
26
+ #
27
+ class WebUrl < Link
28
+ # Whether this link points to a web URL. Always <tt>true</tt> for WebUrl.
29
+ def web?
30
+ true
31
+ end
32
+
33
+ # Returns a Problem if the URL is unreachable. Returns <tt>nil</tt> if the
34
+ # server responds with a 2xx or 3xx status.
35
+ #
36
+ # [_root] Unused. Present for interface compatibility.
37
+ def problem(_root)
38
+ Problem.new(self, "URL returned error or is unreachable") unless reachable?
39
+ end
40
+
41
+ private def reachable? # :nodoc:
42
+ uri = URI.parse(raw)
43
+ return false unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
44
+
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.use_ssl = uri.scheme == "https"
47
+ http.open_timeout = 5
48
+ http.read_timeout = 5
49
+
50
+ response = http.request_head(uri.request_uri)
51
+ response.code.to_i < 400
52
+ rescue
53
+ false
54
+ end
55
+ end
data/tasks/doc/link.rb ADDED
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ # Base class for links found in documentation.
9
+ #
10
+ # Documentation contains links: URLs, file paths, images. These links break.
11
+ # Pages move. Files get renamed. Servers go offline. Manual checking is tedious.
12
+ #
13
+ # Link is the base type. It holds the raw text, line number, and source file.
14
+ # Subclasses (FileUrl, WebUrl, RelativePath) implement <tt>problem</tt> to
15
+ # detect specific breakage.
16
+ #
17
+ # Use <tt>Link.build</tt> to create the correct subclass based on the raw text.
18
+ #
19
+ # === Example
20
+ #
21
+ # link = Link.build("https://example.com", 42, source_file)
22
+ # link.web? # => true
23
+ # link.problem(root) # => nil or Problem
24
+ #
25
+ class Link < Data.define(:raw, :line, :source_file)
26
+ # Creates the appropriate Link subclass based on the raw text.
27
+ #
28
+ # [raw] The raw link text extracted from the file.
29
+ # [line] Line number where the link appears.
30
+ # [source_file] The SourceFile containing this link.
31
+ def self.build(raw, line, source_file)
32
+ if raw.start_with?("file://")
33
+ FileUrl.new(raw, line, source_file)
34
+ elsif raw.start_with?("https://", "http://")
35
+ WebUrl.new(raw, line, source_file)
36
+ else
37
+ RelativePath.new(raw, line, source_file)
38
+ end
39
+ end
40
+
41
+ # Whether this link points to a web URL.
42
+ #
43
+ # WebUrl overrides this to return <tt>true</tt>. Other link types return
44
+ # <tt>false</tt>. The audit uses this to decide whether to skip verification.
45
+ def web?
46
+ false
47
+ end
48
+ end
49
+
50
+ require_relative "link/file_url"
51
+ require_relative "link/web_url"
52
+ require_relative "link/relative_path"
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ require_relative "documentation"
9
+
10
+ # Results of auditing documentation for broken links.
11
+ #
12
+ # Documentation breaks silently. Links rot. Files move. Paths change. Manual
13
+ # checking is tedious and error-prone. Automated checking catches problems
14
+ # before users do.
15
+ #
16
+ # LinkAudit scans all documentation, checks each link, and collects problems.
17
+ # It separates web URLs (slow to verify) from local paths (fast to check).
18
+ # The results render as a human-readable report via <tt>to_s</tt>.
19
+ #
20
+ # === Example
21
+ #
22
+ # audit = LinkAudit.new("/project/root", verify_web: false)
23
+ # puts audit # prints the full report
24
+ # exit 1 unless audit.success?
25
+ #
26
+ class LinkAudit
27
+ # Problems found during the audit.
28
+ attr_reader :problems
29
+
30
+ # Web URLs that were not verified (when <tt>verify_web: false</tt>).
31
+ attr_reader :unverified
32
+
33
+ # Creates a new LinkAudit and runs the scan immediately.
34
+ #
35
+ # [root] Project root directory to scan.
36
+ # [verify_web] Whether to make HTTP requests to verify web URLs.
37
+ def initialize(root, verify_web: false)
38
+ @root = root
39
+ @docs = Documentation.new(root)
40
+ @verify_web = verify_web
41
+ @problems = []
42
+ @unverified = []
43
+ @checked = 0
44
+ @web_checked = 0
45
+ scan
46
+ end
47
+
48
+ # Whether the audit passed with no problems.
49
+ def success?
50
+ @problems.empty?
51
+ end
52
+
53
+ # Returns the audit report as a String.
54
+ def to_s
55
+ lines = []
56
+ lines << ""
57
+ lines << "Scanning for broken links in #{@root}..."
58
+ lines << ""
59
+
60
+ if @verify_web
61
+ lines << "Checked #{@checked} links in #{@docs.count} files (#{@web_checked} web URLs probed)."
62
+ else
63
+ lines << "Checked #{@checked} links in #{@docs.count} files (URL verification skipped)."
64
+ end
65
+ lines << ""
66
+
67
+ unless @unverified.empty?
68
+ lines << "📋 Unverified HTTP/HTTPS URLs (#{@unverified.size}):"
69
+ lines << ""
70
+ @unverified.uniq(&:raw).each { |link| lines << " #{link.raw}" }
71
+ lines << ""
72
+ end
73
+
74
+ if @problems.empty?
75
+ lines << "✅ No broken links found!"
76
+ else
77
+ lines << "❌ Found #{@problems.size} broken link(s):"
78
+ lines << ""
79
+
80
+ @problems.group_by(&:file).each do |path, problems|
81
+ lines << "#{path}:"
82
+ problems.each do |problem|
83
+ lines << " L#{problem.line || '?'}: #{problem.raw}"
84
+ lines << " → #{problem.reason}"
85
+ end
86
+ lines << ""
87
+ end
88
+ end
89
+
90
+ lines.join("\n")
91
+ end
92
+
93
+ private def scan # :nodoc:
94
+ @docs.each do |file|
95
+ file.links.each do |link|
96
+ @checked += 1
97
+ check(link)
98
+ end
99
+ end
100
+ end
101
+
102
+ private def check(link) # :nodoc:
103
+ if link.web?
104
+ if @verify_web
105
+ @web_checked += 1
106
+ problem = link.problem(@root)
107
+ @problems << problem if problem
108
+ else
109
+ @unverified << link
110
+ end
111
+ else
112
+ problem = link.problem(@root)
113
+ @problems << problem if problem
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ # Represents a broken link.
9
+ #
10
+ # Link checking produces results. Some links are fine. Some are broken. You
11
+ # need to report the broken ones with context: where they were found and why
12
+ # they failed.
13
+ #
14
+ # Problem holds the link and the reason it failed. It delegates to the link
15
+ # for file, line, and raw text. Use it to build human-readable error reports.
16
+ #
17
+ # === Example
18
+ #
19
+ # problem = Problem.new(link, "File not found: missing.md")
20
+ # problem.file # => "doc/guide.md"
21
+ # problem.line # => 42
22
+ # problem.raw # => "missing.md"
23
+ # problem.reason # => "File not found: missing.md"
24
+ #
25
+ class Problem < Data.define(:link, :reason)
26
+ # Relative path to the file containing the broken link.
27
+ def file
28
+ link.source_file.relative_path
29
+ end
30
+
31
+ # Line number where the broken link appears.
32
+ def line
33
+ link.line
34
+ end
35
+
36
+ # The raw link text that failed validation.
37
+ def raw
38
+ link.raw
39
+ end
40
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ #--
4
+ # SPDX-FileCopyrightText: 2026 Kerrick Long <me@kerricklong.com>
5
+ # SPDX-License-Identifier: AGPL-3.0-or-later
6
+ #++
7
+
8
+ require "pathname"
9
+ require_relative "link"
10
+
11
+ # A file that may contain links.
12
+ #
13
+ # Documentation lives in many files: Ruby source with RDoc comments, Markdown
14
+ # guides, RDoc text files. Each file may reference images, other docs, or
15
+ # external URLs. Extracting these links by hand is tedious.
16
+ #
17
+ # SourceFile scans its content with regex patterns. It creates the appropriate
18
+ # Link subclass for each match. It tracks line numbers for error reporting.
19
+ #
20
+ # === Example
21
+ #
22
+ # file = SourceFile.new("/path/to/guide.md", "/project/root")
23
+ # file.links.each do |link|
24
+ # puts "#{link.raw} at line #{link.line}"
25
+ # end
26
+ #
27
+ class SourceFile
28
+ # Regex patterns for extracting links from documentation.
29
+ PATTERNS = {
30
+ rdoc_image_link: /\{rdoc-image:([^}]+)\}\[link:([^\]]+)\]/,
31
+ rdoc_link: /\{[^}]*\}\[link:([^\]]+)\]/,
32
+ rdoc_image: /\{rdoc-image:([^}]+)\}/,
33
+ markdown_link: /\[(?:[^\]]*)\]\((?!mailto:|#)([^)\s]+)\)/,
34
+ markdown_image: /!\[[^\]]*\]\(([^)\s]+)\)/,
35
+ html_src: /src=["']([^"']+)["']/,
36
+ html_href: /href=["'](?!mailto:|#)([^"']+)["']/,
37
+ }.freeze
38
+
39
+ # The Pathname to this file.
40
+ attr_reader :path
41
+
42
+ # Creates a new SourceFile.
43
+ #
44
+ # [path] Path to the file (String or Pathname).
45
+ # [root] Project root directory for computing relative paths.
46
+ def initialize(path, root)
47
+ @path = Pathname.new(path)
48
+ @root = Pathname.new(root)
49
+ end
50
+
51
+ # Extracts all links from the file content. Returns an Array of Link objects.
52
+ def links
53
+ found = []
54
+
55
+ PATTERNS.each_value do |pattern|
56
+ content.scan(pattern) do |matches|
57
+ matches = [matches] unless matches.is_a?(Array)
58
+ matches.each do |match|
59
+ next if match.nil? || match.empty?
60
+ next unless looks_like_link?(match)
61
+ found << Link.build(match, line_for(match), self)
62
+ end
63
+ end
64
+ end
65
+
66
+ found
67
+ end
68
+
69
+ # The file path relative to the project root.
70
+ def relative_path
71
+ @path.relative_path_from(@root).to_s
72
+ end
73
+
74
+ private def content # :nodoc:
75
+ @content ||= @path.read(mode: "rb").force_encoding("UTF-8").scrub("")
76
+ end
77
+
78
+ private def line_for(link_text) # :nodoc:
79
+ line_num = 1
80
+ content.each_line do |line|
81
+ return line_num if line.include?(link_text)
82
+ line_num += 1
83
+ end
84
+ nil
85
+ end
86
+
87
+ private def looks_like_link?(text) # :nodoc:
88
+ # Skip regex patterns and other false positives
89
+ return false if text.match?(/[\[\]^*+?]/) # Contains regex special chars
90
+ return false if text.length < 4
91
+ true
92
+ end
93
+ end