apex-ruby 1.0.17 → 1.0.20

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 (207) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -3
  3. data/ext/apex_ext/apex_ext.c +260 -91
  4. data/ext/apex_ext/apex_src/CHANGELOG.md +440 -0
  5. data/ext/apex_ext/apex_src/CMakeLists.txt +15 -1
  6. data/ext/apex_ext/apex_src/Formula/apex.rb +7 -2
  7. data/ext/apex_ext/apex_src/Package.swift +15 -4
  8. data/ext/apex_ext/apex_src/README.md +94 -35
  9. data/ext/apex_ext/apex_src/VERSION +1 -1
  10. data/ext/apex_ext/apex_src/apex-debug.log +4 -0
  11. data/ext/apex_ext/apex_src/apex.code-workspace +38 -0
  12. data/ext/apex_ext/apex_src/apex.taskpaper +12 -0
  13. data/ext/apex_ext/apex_src/apex_deflist_debug.log +25 -0
  14. data/ext/apex_ext/apex_src/buildnotes.md +1435 -0
  15. data/ext/apex_ext/apex_src/cli/main.c +262 -33
  16. data/ext/apex_ext/apex_src/debug.log +7 -0
  17. data/ext/apex_ext/apex_src/docs/CITATIONS_EXAMPLES.md +274 -0
  18. data/ext/apex_ext/apex_src/docs/CITATIONS_IMPLEMENTATION_PLAN.md +749 -0
  19. data/ext/apex_ext/apex_src/docs/CITATIONS_PLAN.md +744 -0
  20. data/ext/apex_ext/apex_src/docs/INDEX_IMPLEMENTATION_ANALYSIS.md +382 -0
  21. data/ext/apex_ext/apex_src/docs/plans/2025-02-27-man-page-output-implementation.md +351 -0
  22. data/ext/apex_ext/apex_src/docs/superpowers/plans/2026-07-12-toc-output-format.md +745 -0
  23. data/ext/apex_ext/apex_src/docs/superpowers/plans/2026-07-18-bear-image-attributes.md +829 -0
  24. data/ext/apex_ext/apex_src/docs/superpowers/plans/2026-07-19-apex-node-npm.md +1148 -0
  25. data/ext/apex_ext/apex_src/docs/superpowers/specs/2026-07-12-toc-output-format-design.md +161 -0
  26. data/ext/apex_ext/apex_src/docs/superpowers/specs/2026-07-18-bear-image-attributes-design.md +184 -0
  27. data/ext/apex_ext/apex_src/docs/superpowers/specs/2026-07-19-apex-node-npm-design.md +211 -0
  28. data/ext/apex_ext/apex_src/documentation/app-settings-table.md +67 -0
  29. data/ext/apex_ext/apex_src/documentation/app-transformed/Citations.md +340 -0
  30. data/ext/apex_ext/apex_src/documentation/app-transformed/Credits.md +11 -0
  31. data/ext/apex_ext/apex_src/documentation/app-transformed/Header-IDs.md +234 -0
  32. data/ext/apex_ext/apex_src/documentation/app-transformed/Inline-Attribute-Lists.md +516 -0
  33. data/ext/apex_ext/apex_src/documentation/app-transformed/Metadata-Transforms.md +461 -0
  34. data/ext/apex_ext/apex_src/documentation/app-transformed/Modes.md +193 -0
  35. data/ext/apex_ext/apex_src/documentation/app-transformed/Multi-File-Documents.md +201 -0
  36. data/ext/apex_ext/apex_src/documentation/app-transformed/Plugins.md +678 -0
  37. data/ext/apex_ext/apex_src/documentation/app-transformed/Syntax.md +1367 -0
  38. data/ext/apex_ext/apex_src/documentation/docsets/Apex Command Line Options.cheatsheet.txt +6 -0
  39. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/C-API.html +774 -300
  40. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Callouts.html +1194 -0
  41. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Citations.html +416 -216
  42. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Command-Line-Options.html +3544 -1094
  43. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Configuration.html +371 -97
  44. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Credits.html +153 -23
  45. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Examples.html +358 -124
  46. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Filters.html +1656 -0
  47. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Generating-Man-Pages.html +1226 -0
  48. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Getting-Started.html +201 -56
  49. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Header-IDs.html +279 -109
  50. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Home.html +200 -53
  51. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Indices.html +2327 -0
  52. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Inline-Attribute-Lists.html +690 -271
  53. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Installation.html +201 -55
  54. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Limitations-and-Roadmap.html +238 -68
  55. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Metadata-Transforms.html +421 -221
  56. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Modes.html +340 -78
  57. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Multi-File-Documents.html +251 -86
  58. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Multi-Format-Images.html +1362 -0
  59. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Pandoc-Integration.html +277 -90
  60. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Plugins.html +673 -346
  61. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Quarto-Mode.html +1405 -0
  62. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Rendering-Markdown-In-Terminal.html +1902 -0
  63. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Syntax.html +1939 -1698
  64. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Tables.html +1781 -0
  65. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Troubleshooting.html +315 -140
  66. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Usage.html +307 -113
  67. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Using-Apex-with-Jekyll.html +1132 -0
  68. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Writing-Tests.html +1754 -0
  69. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/Documents/Xcode-Integration.html +872 -376
  70. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/docSet.dsidx +0 -0
  71. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/optimizedIndex.dsidx +0 -0
  72. data/ext/apex_ext/apex_src/documentation/html/apex-app-docs.html +8746 -0
  73. data/ext/apex_ext/apex_src/documentation/shared_styles.css +20 -15
  74. data/ext/apex_ext/apex_src/documentation/transform_for_app.md +55 -0
  75. data/ext/apex_ext/apex_src/escaping-repro.md +103 -0
  76. data/ext/apex_ext/apex_src/examples/quick-actions/Markdown to RTF.workflow/Contents/Info.plist +8 -0
  77. data/ext/apex_ext/apex_src/examples/quick-actions/Markdown to RTF.workflow/Contents/QuickLook/Preview.png +0 -0
  78. data/ext/apex_ext/apex_src/examples/quick-actions/Markdown to RTF.workflow/Contents/document.wflow +547 -0
  79. data/ext/apex_ext/apex_src/examples/quick-actions/Markdown-to-RTF.workflow.zip +0 -0
  80. data/ext/apex_ext/apex_src/examples/quick-actions/README.md +33 -0
  81. data/ext/apex_ext/apex_src/include/apex/apex.h +92 -6
  82. data/ext/apex_ext/apex_src/include/apex/ast_rtf.h +23 -0
  83. data/ext/apex_ext/apex_src/include/apex/ast_terminal.h +6 -0
  84. data/ext/apex_ext/apex_src/include/apex/module.modulemap +5 -0
  85. data/ext/apex_ext/apex_src/include/apex/plugins.h +105 -0
  86. data/ext/apex_ext/apex_src/include/apexc.h +1 -0
  87. data/ext/apex_ext/apex_src/man/apex.1 +82 -78
  88. data/ext/apex_ext/apex_src/man/apex.1.md +81 -4
  89. data/ext/apex_ext/apex_src/objc/Apex.swift +73 -0
  90. data/ext/apex_ext/apex_src/objc/ApexPlugins.swift +79 -0
  91. data/ext/apex_ext/apex_src/objc/NSString+Apex.h +83 -5
  92. data/ext/apex_ext/apex_src/objc/NSString+Apex.m +244 -1
  93. data/ext/apex_ext/apex_src/objc/NSString+ApexPlugins.h +76 -0
  94. data/ext/apex_ext/apex_src/objc/NSString+ApexPlugins.m +169 -0
  95. data/ext/apex_ext/apex_src/output.html +87 -0
  96. data/ext/apex_ext/apex_src/plans/quarto-mode.md +442 -0
  97. data/ext/apex_ext/apex_src/src/_README.md +85 -32
  98. data/ext/apex_ext/apex_src/src/apex.c +770 -200
  99. data/ext/apex_ext/apex_src/src/ast_rtf.c +848 -0
  100. data/ext/apex_ext/apex_src/src/ast_terminal.c +184 -7
  101. data/ext/apex_ext/apex_src/src/extensions/abbreviations.c +10 -3
  102. data/ext/apex_ext/apex_src/src/extensions/advanced_tables.c +7 -11
  103. data/ext/apex_ext/apex_src/src/extensions/bear_image_attrs.c +467 -0
  104. data/ext/apex_ext/apex_src/src/extensions/bear_image_attrs.h +27 -0
  105. data/ext/apex_ext/apex_src/src/extensions/callouts.c +400 -4
  106. data/ext/apex_ext/apex_src/src/extensions/callouts.h +15 -5
  107. data/ext/apex_ext/apex_src/src/extensions/citations.c +26 -5
  108. data/ext/apex_ext/apex_src/src/extensions/code_fence_attrs.c +632 -0
  109. data/ext/apex_ext/apex_src/src/extensions/code_fence_attrs.h +22 -0
  110. data/ext/apex_ext/apex_src/src/extensions/concordance.c +632 -0
  111. data/ext/apex_ext/apex_src/src/extensions/definition_list.c +129 -15
  112. data/ext/apex_ext/apex_src/src/extensions/grid_tables.c +1050 -121
  113. data/ext/apex_ext/apex_src/src/extensions/html_markdown.c +569 -16
  114. data/ext/apex_ext/apex_src/src/extensions/ial.c +502 -52
  115. data/ext/apex_ext/apex_src/src/extensions/ial.h +2 -1
  116. data/ext/apex_ext/apex_src/src/extensions/includes.c +532 -33
  117. data/ext/apex_ext/apex_src/src/extensions/includes.h +7 -1
  118. data/ext/apex_ext/apex_src/src/extensions/index.c +1427 -140
  119. data/ext/apex_ext/apex_src/src/extensions/index.h +28 -0
  120. data/ext/apex_ext/apex_src/src/extensions/metadata.c +200 -161
  121. data/ext/apex_ext/apex_src/src/extensions/quarto_diagrams.c +251 -0
  122. data/ext/apex_ext/apex_src/src/extensions/quarto_diagrams.h +22 -0
  123. data/ext/apex_ext/apex_src/src/extensions/quarto_lists.c +624 -0
  124. data/ext/apex_ext/apex_src/src/extensions/quarto_lists.h +35 -0
  125. data/ext/apex_ext/apex_src/src/extensions/quarto_polish.c +264 -0
  126. data/ext/apex_ext/apex_src/src/extensions/quarto_polish.h +23 -0
  127. data/ext/apex_ext/apex_src/src/extensions/quarto_shortcodes.c +307 -0
  128. data/ext/apex_ext/apex_src/src/extensions/quarto_shortcodes.h +21 -0
  129. data/ext/apex_ext/apex_src/src/extensions/raw_content.c +351 -0
  130. data/ext/apex_ext/apex_src/src/extensions/raw_content.h +20 -0
  131. data/ext/apex_ext/apex_src/src/extensions/relaxed_tables.c +117 -3
  132. data/ext/apex_ext/apex_src/src/extensions/special_markers.c +53 -0
  133. data/ext/apex_ext/apex_src/src/extensions/syntax_highlight.c +23 -1
  134. data/ext/apex_ext/apex_src/src/extensions/table_html_postprocess.c +83 -92
  135. data/ext/apex_ext/apex_src/src/extensions/toc.c +446 -25
  136. data/ext/apex_ext/apex_src/src/extensions/toc.h +26 -1
  137. data/ext/apex_ext/apex_src/src/extensions/wiki_links.c +28 -0
  138. data/ext/apex_ext/apex_src/src/html_renderer.c +387 -17
  139. data/ext/apex_ext/apex_src/src/plugin_catalog.c +517 -0
  140. data/ext/apex_ext/apex_src/src/plugins_remote.c +88 -14
  141. data/ext/apex_ext/apex_src/src/plugins_remote.h +34 -0
  142. data/ext/apex_ext/apex_src/src/pretty_html.c +6 -1
  143. data/ext/apex_ext/apex_src/tests/fixtures/comprehensive_test.md +21 -0
  144. data/ext/apex_ext/apex_src/tests/fixtures/images/bear_image_attributes.md +10 -0
  145. data/ext/apex_ext/apex_src/tests/fixtures/includes/sections.md +19 -0
  146. data/ext/apex_ext/apex_src/tests/fixtures/includes/sections.txt +11 -0
  147. data/ext/apex_ext/apex_src/tests/fixtures/minimal_pb.md +2 -0
  148. data/ext/apex_ext/apex_src/tests/fixtures/obsidian-callouts.md +46 -0
  149. data/ext/apex_ext/apex_src/tests/fixtures/pagebreak_html_repro.md +5 -0
  150. data/ext/apex_ext/apex_src/tests/fixtures/python-callouts.md +47 -0
  151. data/ext/apex_ext/apex_src/tests/fixtures/quarto/callouts.md +3 -0
  152. data/ext/apex_ext/apex_src/tests/fixtures/quarto/code-attrs.md +3 -0
  153. data/ext/apex_ext/apex_src/tests/fixtures/quarto/diagrams.md +8 -0
  154. data/ext/apex_ext/apex_src/tests/fixtures/quarto/lists.md +19 -0
  155. data/ext/apex_ext/apex_src/tests/fixtures/quarto/raw-content.md +5 -0
  156. data/ext/apex_ext/apex_src/tests/fixtures/quarto/shortcodes.md +7 -0
  157. data/ext/apex_ext/apex_src/tests/fixtures/quarto/smoke.md +81 -0
  158. data/ext/apex_ext/apex_src/tests/fixtures/quarto/spans.md +5 -0
  159. data/ext/apex_ext/apex_src/tests/fixtures/quarto-callouts.md +47 -0
  160. data/ext/apex_ext/apex_src/tests/fixtures/speed.md +192 -0
  161. data/ext/apex_ext/apex_src/tests/fixtures/tables/grid-tables.md +37 -0
  162. data/ext/apex_ext/apex_src/tests/paginate_cli_test.sh +16 -0
  163. data/ext/apex_ext/apex_src/tests/test_bear_image_attrs.c +301 -0
  164. data/ext/apex_ext/apex_src/tests/test_concordance.tsv +4 -0
  165. data/ext/apex_ext/apex_src/tests/test_escaping_repro.c +82 -0
  166. data/ext/apex_ext/apex_src/tests/test_extensions.c +821 -1
  167. data/ext/apex_ext/apex_src/tests/test_ial.c +7 -0
  168. data/ext/apex_ext/apex_src/tests/test_marked_integration.c +25 -0
  169. data/ext/apex_ext/apex_src/tests/test_metadata.c +129 -0
  170. data/ext/apex_ext/apex_src/tests/test_output.c +568 -2
  171. data/ext/apex_ext/apex_src/tests/test_runner.c +11 -0
  172. data/ext/apex_ext/apex_src/tests/test_tables.c +420 -0
  173. data/ext/apex_ext/apex_src/vendor/cmark-gfm/CMakeLists.txt +1 -1
  174. data/ext/apex_ext/apex_src/vendor/cmark-gfm/extensions/autolink.c +77 -1
  175. data/ext/apex_ext/apex_src/vendor/cmark-gfm/extensions/table.c +135 -5
  176. data/ext/apex_ext/apex_src/vendor/cmark-gfm/src/blocks.c +56 -1
  177. data/ext/apex_ext/apex_src/vendor/cmark-gfm/src/config.h +38 -0
  178. data/ext/apex_ext/apex_src/vendor/cmark-gfm/src/main.c +1 -1
  179. data/ext/apex_ext/apex_src/vendor/cmark-gfm/test/__pycache__/cmark.cpython-314.pyc +0 -0
  180. data/ext/apex_ext/apex_src/vendor/cmark-gfm/test/__pycache__/normalize.cpython-314.pyc +0 -0
  181. data/ext/apex_ext/apex_src/vendor/cmark-gfm/test/extensions.txt +69 -0
  182. data/ext/apex_ext/apex_src/xcode_build/CMakeScripts/ReRunCMake.make +74 -0
  183. data/ext/apex_ext/apex_src/xcode_build/CTestTestfile.cmake +22 -0
  184. data/ext/apex_ext/apex_src/xcode_build/Debug/Apex.framework/Versions/A/Apex +0 -0
  185. data/ext/apex_ext/apex_src/xcode_build/Debug/Apex.framework/Versions/A/Resources/Info.plist +44 -0
  186. data/ext/apex_ext/apex_src/xcode_build/Debug/Apex.framework/Versions/A/_CodeSignature/CodeResources +128 -0
  187. data/ext/apex_ext/apex_src/xcode_build/apex.xcodeproj/project.pbxproj +5118 -0
  188. data/ext/apex_ext/apex_src/xcode_build/apex.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +8 -0
  189. data/ext/apex_ext/apex_src/xcode_build/apex.xcodeproj/project.xcworkspace/xcuserdata/ttscoff.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  190. data/ext/apex_ext/apex_src/xcode_build/apex.xcodeproj/project.xcworkspace/xcuserdata/ttscoff.xcuserdatad/WorkspaceSettings.xcsettings +8 -0
  191. data/ext/apex_ext/apex_src/xcode_build/apex.xcodeproj/xcuserdata/ttscoff.xcuserdatad/xcschemes/xcschememanagement.plist +99 -0
  192. data/ext/apex_ext/apex_src/xcode_build/cmake_install.cmake +452 -0
  193. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/CMakeScripts/ReRunCMake.make +52 -0
  194. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/cmake_install.cmake +55 -0
  195. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/cmark-gfm.xcodeproj/project.pbxproj +2455 -0
  196. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/cmark-gfm.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings +8 -0
  197. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/extensions/cmake_install.cmake +171 -0
  198. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/man/cmake_install.cmake +55 -0
  199. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/src/cmake_install.cmake +220 -0
  200. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/src/cmark-gfm_export.h +43 -0
  201. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/src/cmark-gfm_version.h +7 -0
  202. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/src/config.h +76 -0
  203. data/ext/apex_ext/apex_src/xcode_build/vendor/cmark-gfm/src/libcmark-gfm.pc +10 -0
  204. data/ext/apex_ext/extconf.rb +35 -0
  205. data/lib/apex/version.rb +2 -2
  206. metadata +117 -2
  207. data/ext/apex_ext/apex_src/documentation/docsets/Apex.docset/Contents/Resources/tempOptimizedIndex.dsidx +0 -0
@@ -0,0 +1,829 @@
1
+ # Bear Image Attribute Comments Implementation Plan
2
+
3
+ > **For agentic workers:** REQUIRED SUB-SKILL: Use
4
+ > superpowers:subagent-driven-development (recommended) or
5
+ > superpowers:executing-plans to implement this plan task-by-task. Steps use
6
+ > checkbox (`- [ ]`) syntax for tracking.
7
+
8
+ **Goal:** Render allowlisted Bear JSON image metadata on inline and reference
9
+ images while preserving the source comment and existing HTML safety.
10
+
11
+ **Architecture:** Add a private, dependency-free parser for adjacent Bear
12
+ comments, then feed its normalized key/value results into the existing image
13
+ attribute preprocessor. Inline and per-use attributes use indexed image
14
+ entries; definition attributes use the existing reference expansion path.
15
+
16
+ **Tech Stack:** C99, cmark-gfm, Apex image attribute preprocessing, CMake,
17
+ and the existing C test runner.
18
+
19
+ ## Global Constraints
20
+
21
+ - Recognize metadata in CommonMark, GFM, MultiMarkdown, and Unified-family
22
+ modes, but not Kramdown.
23
+ - Accept only same-line comments separated from the image or definition by
24
+ zero or more spaces or tabs.
25
+ - Preserve the exact comment when raw HTML is allowed.
26
+ - Keep existing safe-HTML behavior, including `<!-- raw HTML omitted -->`.
27
+ - Allow only `width`, `height`, `style`, `class`, `id`, `rel`, and `title`.
28
+ - Accept numbers or strings for `width` and `height`; other allowed values
29
+ must be strings.
30
+ - Ignore unsupported keys and unsupported value types without applying them.
31
+ - Reject a malformed or non-object JSON payload as a whole.
32
+ - Do not add a JSON dependency or modify vendored cmark-gfm.
33
+ - Definition metadata is the baseline; per-use metadata wins for one image.
34
+ - Per-use metadata supports only explicit `![alt][id]` references.
35
+ - Definition metadata applies to full, collapsed, and shortcut image refs.
36
+ - Comments after reference definitions render exactly once.
37
+
38
+ ---
39
+
40
+ ## File Structure
41
+
42
+ - Create `src/extensions/bear_image_attrs.h`: private parser data types and
43
+ function declarations.
44
+ - Create `src/extensions/bear_image_attrs.c`: bounded JSON comment parser,
45
+ allowlist, scalar conversion, and memory cleanup.
46
+ - Modify `src/extensions/ial.c`: merge parsed Bear values into inline,
47
+ reference-definition, and per-use image attribute entries.
48
+ - Modify `src/apex.c`: run image preprocessing in CommonMark and GFM.
49
+ - Modify `CMakeLists.txt`: compile the parser and focused test suite.
50
+ - Create `tests/test_bear_image_attrs.c`: parser and end-to-end behavior.
51
+ - Modify `tests/test_runner.c`: register the focused test suite.
52
+ - Create `tests/fixtures/images/bear_image_attributes.md`: representative
53
+ inline and reference syntax.
54
+
55
+ ### Task 1: Private Bear Comment Parser
56
+
57
+ **Files:**
58
+
59
+ - Create: `src/extensions/bear_image_attrs.h`
60
+ - Create: `src/extensions/bear_image_attrs.c`
61
+ - Modify: `CMakeLists.txt:109-157,343-365`
62
+ - Create: `tests/test_bear_image_attrs.c`
63
+ - Modify: `tests/test_runner.c:27-87,101-163`
64
+
65
+ **Interfaces:**
66
+
67
+ - Produces:
68
+ `bool apex_parse_bear_image_comment(const char *, const char *,
69
+ const char **, apex_bear_image_attrs *)`
70
+ - Produces:
71
+ `void apex_free_bear_image_attrs(apex_bear_image_attrs *)`
72
+ - Consumes: no Apex parser internals and no external JSON library.
73
+
74
+ - [ ] **Step 1: Add failing parser tests and register the suite**
75
+
76
+ Create the test suite with direct parser assertions:
77
+
78
+ ```c
79
+ #include "test_helpers.h"
80
+ #include "extensions/bear_image_attrs.h"
81
+
82
+ #include <string.h>
83
+
84
+ static const char *bear_value(const apex_bear_image_attrs *attrs,
85
+ const char *key) {
86
+ for (size_t i = 0; i < attrs->count; i++) {
87
+ if (strcmp(attrs->items[i].key, key) == 0) {
88
+ return attrs->items[i].value;
89
+ }
90
+ }
91
+ return NULL;
92
+ }
93
+
94
+ void test_bear_image_attributes(void) {
95
+ int failures = suite_start();
96
+ print_suite_title("Bear Image Attribute Tests", false, true);
97
+
98
+ const char *input = "<!-- {\"width\":259,\"title\":\"A & B\"} -->";
99
+ const char *end = input + strlen(input);
100
+ const char *comment_end = NULL;
101
+ apex_bear_image_attrs attrs = {0};
102
+
103
+ bool ok = apex_parse_bear_image_comment(
104
+ input, end, &comment_end, &attrs);
105
+ test_result(ok, "Bear parser accepts a flat JSON object");
106
+ assert_option_string(
107
+ bear_value(&attrs, "width"), "259", "Numeric width is normalized");
108
+ assert_option_string(
109
+ bear_value(&attrs, "title"), "A & B", "String value is decoded");
110
+ test_result(comment_end == end, "Parser returns the comment end");
111
+ apex_free_bear_image_attrs(&attrs);
112
+
113
+ const char *unsafe =
114
+ "<!-- {\"width\":259,\"onclick\":\"alert(1)\","
115
+ "\"data-x\":\"bad\"} -->";
116
+ end = unsafe + strlen(unsafe);
117
+ attrs = (apex_bear_image_attrs){0};
118
+ ok = apex_parse_bear_image_comment(
119
+ unsafe, end, &comment_end, &attrs);
120
+ test_result(ok, "Unsupported keys do not invalidate metadata");
121
+ test_result(
122
+ attrs.count == 1, "Only allowlisted attributes are returned");
123
+ apex_free_bear_image_attrs(&attrs);
124
+
125
+ const char *nested =
126
+ "<!-- {\"width\":259,\"title\":{\"nested\":true}} -->";
127
+ end = nested + strlen(nested);
128
+ attrs = (apex_bear_image_attrs){0};
129
+ ok = apex_parse_bear_image_comment(
130
+ nested, end, &comment_end, &attrs);
131
+ test_result(ok, "Unsupported nested values are skipped");
132
+ test_result(attrs.count == 1, "Supported sibling value is retained");
133
+ apex_free_bear_image_attrs(&attrs);
134
+
135
+ const char *bad = "<!-- {\"width\":259,} -->";
136
+ end = bad + strlen(bad);
137
+ attrs = (apex_bear_image_attrs){0};
138
+ test_result(
139
+ !apex_parse_bear_image_comment(
140
+ bad, end, &comment_end, &attrs),
141
+ "Malformed JSON is rejected");
142
+
143
+ bool had_failures = suite_end(failures);
144
+ print_suite_title("Bear Image Attribute Tests", had_failures, false);
145
+ }
146
+ ```
147
+
148
+ Add `tests/test_bear_image_attrs.c` to the `apex_test_runner` sources in
149
+ `CMakeLists.txt`, declare `test_bear_image_attributes()` in
150
+ `tests/test_runner.c`, and register:
151
+
152
+ ```c
153
+ { "bear_image_attributes", test_bear_image_attributes },
154
+ ```
155
+
156
+ - [ ] **Step 2: Run the focused test and verify it fails**
157
+
158
+ Run:
159
+
160
+ ```bash
161
+ cmake -S . -B build
162
+ cmake --build build --target apex_test_runner
163
+ ./build/apex_test_runner bear_image_attributes
164
+ ```
165
+
166
+ Expected: compilation fails because `extensions/bear_image_attrs.h` does not
167
+ exist.
168
+
169
+ - [ ] **Step 3: Define the private parser interface**
170
+
171
+ Create `src/extensions/bear_image_attrs.h`:
172
+
173
+ ```c
174
+ #ifndef APEX_BEAR_IMAGE_ATTRS_H
175
+ #define APEX_BEAR_IMAGE_ATTRS_H
176
+
177
+ #include <stdbool.h>
178
+ #include <stddef.h>
179
+
180
+ #define APEX_BEAR_IMAGE_ATTR_CAPACITY 7
181
+
182
+ typedef struct {
183
+ char *key;
184
+ char *value;
185
+ } apex_bear_image_attr;
186
+
187
+ typedef struct {
188
+ apex_bear_image_attr items[APEX_BEAR_IMAGE_ATTR_CAPACITY];
189
+ size_t count;
190
+ } apex_bear_image_attrs;
191
+
192
+ bool apex_parse_bear_image_comment(
193
+ const char *comment_start,
194
+ const char *line_end,
195
+ const char **comment_end,
196
+ apex_bear_image_attrs *attrs);
197
+
198
+ void apex_free_bear_image_attrs(apex_bear_image_attrs *attrs);
199
+
200
+ #endif
201
+ ```
202
+
203
+ - [ ] **Step 4: Implement strict flat-object parsing**
204
+
205
+ Create `src/extensions/bear_image_attrs.c`. Implement these private helpers:
206
+
207
+ ```c
208
+ static void skip_json_ws(const char **cursor, const char *end);
209
+ static bool parse_json_string(
210
+ const char **cursor, const char *end, char **value);
211
+ static bool parse_json_number(
212
+ const char **cursor, const char *end, char **value);
213
+ static bool skip_json_value(
214
+ const char **cursor, const char *end, unsigned depth);
215
+ static bool is_allowed_key(const char *key);
216
+ static bool key_accepts_number(const char *key);
217
+ static bool set_attr(
218
+ apex_bear_image_attrs *attrs, char *key, char *value);
219
+ ```
220
+
221
+ The implementation must:
222
+
223
+ 1. Require `<!--`, find `-->` before `line_end`, and trim JSON whitespace.
224
+ 2. Require one complete object and no non-whitespace after its closing `}`.
225
+ 3. Decode JSON strings, including standard escapes and UTF-16 `\uXXXX`
226
+ surrogate pairs converted to UTF-8.
227
+ 4. Validate the full JSON number grammar before copying its source spelling.
228
+ 5. Recursively validate and skip unsupported arrays and objects with a
229
+ maximum nesting depth of 32.
230
+ 6. Validate and skip `true`, `false`, and `null`.
231
+ 7. Store only allowlisted scalar values of an accepted type.
232
+ 8. Replace an earlier duplicate allowed key so the final JSON member wins.
233
+ 9. Free partial results and return false on malformed JSON or allocation
234
+ failure.
235
+ 10. Set `comment_end` one byte after `-->` only on success.
236
+
237
+ Add `src/extensions/bear_image_attrs.c` after `src/extensions/ial.c` in
238
+ `APEX_LIB_SOURCES`.
239
+
240
+ - [ ] **Step 5: Run the parser tests**
241
+
242
+ Run:
243
+
244
+ ```bash
245
+ cmake --build build --target apex_test_runner
246
+ ./build/apex_test_runner bear_image_attributes
247
+ ```
248
+
249
+ Expected: all parser assertions pass.
250
+
251
+ - [ ] **Step 6: Commit the parser**
252
+
253
+ ```bash
254
+ git add CMakeLists.txt src/extensions/bear_image_attrs.c \
255
+ src/extensions/bear_image_attrs.h tests/test_bear_image_attrs.c \
256
+ tests/test_runner.c
257
+ git commit -m "$(cat <<'EOF'
258
+ Add Bear image metadata parser
259
+
260
+ Parse and validate adjacent flat JSON metadata without adding a dependency.
261
+ EOF
262
+ )"
263
+ ```
264
+
265
+ ### Task 2: Inline Images and Mode Integration
266
+
267
+ **Files:**
268
+
269
+ - Modify: `src/extensions/ial.c:604-800,2653-3015`
270
+ - Modify: `src/apex.c:5065-5082`
271
+ - Modify: `tests/test_bear_image_attrs.c`
272
+
273
+ **Interfaces:**
274
+
275
+ - Consumes: `apex_parse_bear_image_comment()` from Task 1.
276
+ - Produces: Bear attributes merged into indexed inline `image_attr_entry`
277
+ values.
278
+
279
+ - [ ] **Step 1: Add failing inline and mode tests**
280
+
281
+ Append a renderer helper and inline assertions:
282
+
283
+ ```c
284
+ static char *render_bear(
285
+ const char *markdown, apex_mode_t mode, bool unsafe) {
286
+ apex_options opts = apex_options_for_mode(mode);
287
+ opts.unsafe = unsafe;
288
+ return apex_markdown_to_html(markdown, strlen(markdown), &opts);
289
+ }
290
+
291
+ static void test_bear_inline_modes(void) {
292
+ const char *md =
293
+ "![](emperor-1.jpg)<!-- {\"width\":259} -->";
294
+ const apex_mode_t modes[] = {
295
+ APEX_MODE_COMMONMARK,
296
+ APEX_MODE_GFM,
297
+ APEX_MODE_MULTIMARKDOWN,
298
+ APEX_MODE_UNIFIED
299
+ };
300
+
301
+ for (size_t i = 0; i < sizeof(modes) / sizeof(modes[0]); i++) {
302
+ char *html = render_bear(md, modes[i], true);
303
+ assert_contains(html, "width=\"259\"", "Bear width is applied");
304
+ assert_contains(
305
+ html,
306
+ "<!-- {\"width\":259} -->",
307
+ "Bear comment is preserved");
308
+ apex_free_string(html);
309
+ }
310
+
311
+ char *safe = render_bear(md, APEX_MODE_COMMONMARK, false);
312
+ assert_contains(safe, "width=\"259\"", "Safe mode applies Bear width");
313
+ assert_contains(
314
+ safe, "raw HTML omitted", "Safe mode keeps raw HTML policy");
315
+ apex_free_string(safe);
316
+
317
+ char *spaced = render_bear(
318
+ "![](x.jpg) \t<!-- {\"width\":\"50%\",\"height\":\"20px\"} -->",
319
+ APEX_MODE_UNIFIED,
320
+ true);
321
+ assert_contains(
322
+ spaced, "height=\"20\"", "Pixel height uses an HTML attribute");
323
+ assert_contains(
324
+ spaced, "style=\"width: 50%\"", "Percent width uses style");
325
+ apex_free_string(spaced);
326
+ }
327
+ ```
328
+
329
+ Call `test_bear_inline_modes()` from `test_bear_image_attributes()`.
330
+
331
+ - [ ] **Step 2: Run the suite and verify behavior is missing**
332
+
333
+ Run:
334
+
335
+ ```bash
336
+ cmake --build build --target apex_test_runner
337
+ ./build/apex_test_runner bear_image_attributes
338
+ ```
339
+
340
+ Expected: parser tests pass; renderer assertions fail because Bear metadata
341
+ is not attached and preprocessing does not run in CommonMark or GFM.
342
+
343
+ - [ ] **Step 3: Add Bear-to-Apex attribute conversion**
344
+
345
+ Include `bear_image_attrs.h` from `ial.c` and add:
346
+
347
+ ```c
348
+ static apex_attributes *attributes_from_bear(
349
+ const apex_bear_image_attrs *bear) {
350
+ apex_attributes *attrs = create_attributes();
351
+ if (!attrs) return NULL;
352
+
353
+ for (size_t i = 0; i < bear->count; i++) {
354
+ add_attribute(
355
+ attrs, bear->items[i].key, bear->items[i].value);
356
+ }
357
+ return attrs;
358
+ }
359
+ ```
360
+
361
+ Add a small detector that skips only spaces and tabs, calls the parser, and
362
+ returns both parsed attributes and the end of the comment:
363
+
364
+ ```c
365
+ static apex_attributes *parse_adjacent_bear_attrs(
366
+ const char *after_construct,
367
+ const char *line_end,
368
+ const char **comment_start,
369
+ const char **comment_end);
370
+ ```
371
+
372
+ It must leave `comment_start` null when no valid Bear object is present.
373
+
374
+ - [ ] **Step 4: Merge Bear values after inline IAL values**
375
+
376
+ In the inline-image branch of `apex_preprocess_image_attributes()`, call
377
+ `parse_adjacent_bear_attrs()` after the closing parenthesis and optional IAL.
378
+ Merge with `merge_attributes(existing, bear)` in source order. Do not advance
379
+ the main read cursor over spaces or the comment, so cmark receives them
380
+ byte-for-byte.
381
+
382
+ Set `do_image_attrs` to:
383
+
384
+ ```c
385
+ bool do_image_attrs =
386
+ apex_mode_is_unified_family(mode) ||
387
+ mode == APEX_MODE_MULTIMARKDOWN ||
388
+ mode == APEX_MODE_GFM ||
389
+ mode == APEX_MODE_COMMONMARK;
390
+ ```
391
+
392
+ In `src/apex.c`, extend the preprocessing gate:
393
+
394
+ ```c
395
+ if (apex_mode_is_unified_family(options->mode) ||
396
+ options->mode == APEX_MODE_MULTIMARKDOWN ||
397
+ options->mode == APEX_MODE_KRAMDOWN ||
398
+ options->mode == APEX_MODE_GFM ||
399
+ options->mode == APEX_MODE_COMMONMARK) {
400
+ ```
401
+
402
+ Kramdown still enters the preprocessor for URL encoding, but
403
+ `do_image_attrs` remains false for that mode.
404
+
405
+ - [ ] **Step 5: Prevent Bear string values from injecting HTML**
406
+
407
+ Before adding Bear values to `apex_attributes`, encode `&`, `"`, `<`, and
408
+ `>` as `&amp;`, `&quot;`, `&lt;`, and `&gt;`. Keep width and height
409
+ classification based on the decoded value before encoding.
410
+
411
+ Add this assertion:
412
+
413
+ ```c
414
+ char *escaped = render_bear(
415
+ "![](x.jpg)<!-- {\"title\":\"x\\\" onerror=\\\"boom\"} -->",
416
+ APEX_MODE_UNIFIED,
417
+ true);
418
+ assert_contains(
419
+ escaped,
420
+ "title=\"x&quot; onerror=&quot;boom\"",
421
+ "Bear values are HTML attribute escaped");
422
+ assert_not_contains(
423
+ escaped, " onerror=\"boom\"", "Bear values cannot inject attributes");
424
+ apex_free_string(escaped);
425
+ ```
426
+
427
+ - [ ] **Step 6: Run focused and existing image tests**
428
+
429
+ Run:
430
+
431
+ ```bash
432
+ cmake --build build --target apex_test_runner
433
+ ./build/apex_test_runner bear_image_attributes
434
+ ./build/apex_test_runner image_width_height_conversion
435
+ ./build/apex_test_runner multimarkdown_image_attributes
436
+ ```
437
+
438
+ Expected: all three suites pass.
439
+
440
+ - [ ] **Step 7: Commit inline support**
441
+
442
+ ```bash
443
+ git add src/apex.c src/extensions/ial.c tests/test_bear_image_attrs.c
444
+ git commit -m "$(cat <<'EOF'
445
+ Render Bear attributes on inline images
446
+
447
+ @new **Bear image attributes** Apply safe JSON metadata while preserving its comment.
448
+ EOF
449
+ )"
450
+ ```
451
+
452
+ ### Task 3: Reference-Definition Metadata
453
+
454
+ **Files:**
455
+
456
+ - Modify: `src/extensions/ial.c:2551-2561,3076-3594,3775-3917`
457
+ - Modify: `tests/test_bear_image_attrs.c`
458
+ - Create: `tests/fixtures/images/bear_image_attributes.md`
459
+
460
+ **Interfaces:**
461
+
462
+ - Consumes: Task 1 parser and Task 2 conversion helper.
463
+ - Produces: normalized reference entries whose attributes expand into every
464
+ matching full, collapsed, or shortcut image reference.
465
+
466
+ - [ ] **Step 1: Add failing definition tests**
467
+
468
+ Add:
469
+
470
+ ```c
471
+ static void test_bear_reference_definitions(void) {
472
+ const char *md =
473
+ "![One][Emperor]\n\n"
474
+ "![Emperor][]\n\n"
475
+ "![Emperor]\n\n"
476
+ "[ emperor ]: emperor.jpg "
477
+ "<!-- {\"width\":259,\"title\":\"Ruler\"} -->";
478
+ char *html = render_bear(md, APEX_MODE_UNIFIED, true);
479
+
480
+ const char *cursor = html;
481
+ int widths = 0;
482
+ while ((cursor = strstr(cursor, "width=\"259\"")) != NULL) {
483
+ widths++;
484
+ cursor += strlen("width=\"259\"");
485
+ }
486
+ test_result(widths == 3, "Definition metadata applies to every use");
487
+ assert_contains(
488
+ html,
489
+ "<!-- {\"width\":259,\"title\":\"Ruler\"} -->",
490
+ "Definition comment is preserved once");
491
+
492
+ cursor = html;
493
+ int comments = 0;
494
+ while ((cursor = strstr(cursor, "<!-- {\"width\":259")) != NULL) {
495
+ comments++;
496
+ cursor++;
497
+ }
498
+ test_result(comments == 1, "Definition comment renders exactly once");
499
+ apex_free_string(html);
500
+ }
501
+ ```
502
+
503
+ Use reference labels whose casing and internal whitespace differ between
504
+ definition and use in an additional assertion.
505
+
506
+ - [ ] **Step 2: Run the suite and verify it fails**
507
+
508
+ Run:
509
+
510
+ ```bash
511
+ cmake --build build --target apex_test_runner
512
+ ./build/apex_test_runner bear_image_attributes
513
+ ```
514
+
515
+ Expected: definition metadata is not applied, and cmark consumes or rejects
516
+ the trailing comment as part of the definition.
517
+
518
+ - [ ] **Step 3: Reuse normalized reference matching**
519
+
520
+ Move `reference_id_matches()` above `find_image_attr_by_ref()` or add a
521
+ forward declaration. Change reference lookup from `strcmp()` to:
522
+
523
+ ```c
524
+ if (entry->ref_name &&
525
+ reference_id_matches(
526
+ entry->ref_name, ref_name, strlen(ref_name))) {
527
+ return entry;
528
+ }
529
+ ```
530
+
531
+ Use the same matcher for full, collapsed, and shortcut expansion.
532
+
533
+ - [ ] **Step 4: Parse definition comments before attribute tails**
534
+
535
+ In the reference-definition branch:
536
+
537
+ 1. Find an adjacent Bear comment after URL, title, or IAL processing.
538
+ 2. Use its start as the effective end of title/MMD attribute parsing.
539
+ 3. Merge Bear attributes after existing definition attributes.
540
+ 4. Create a reference entry whenever the definition has Bear attributes.
541
+ 5. Mark that definition for reference-image expansion.
542
+
543
+ When an attribute-bearing definition is removed, write the original comment
544
+ and its original leading spaces to the output buffer before copying the
545
+ line ending. Do not copy the definition itself. This leaves one standalone
546
+ raw HTML comment for cmark to render or suppress according to safe mode.
547
+
548
+ - [ ] **Step 5: Expand every reference image form**
549
+
550
+ Extend the second pass to identify:
551
+
552
+ ```markdown
553
+ ![alt][id]
554
+ ![id][]
555
+ ![id]
556
+ ```
557
+
558
+ For collapsed and shortcut forms, use the alt text as the reference label.
559
+ Do not treat `![alt](url)` as a shortcut reference. Expand a reference only
560
+ when `find_image_attr_by_ref()` returns an attribute-bearing definition.
561
+
562
+ Continue producing `![alt](url attributes)` so the existing post-expansion
563
+ scan creates indexed entries and avoids URL-based attribute leakage.
564
+
565
+ - [ ] **Step 6: Add the representative fixture**
566
+
567
+ Create `tests/fixtures/images/bear_image_attributes.md`:
568
+
569
+ ```markdown
570
+ # Bear image attributes
571
+
572
+ ![](inline.jpg)<!-- {"width":259} -->
573
+
574
+ ![First][shared]
575
+ ![Second][shared]
576
+
577
+ ![Override][shared]<!-- {"width":320} -->
578
+
579
+ [shared]: shared.jpg <!-- {"width":200,"height":100} -->
580
+ ```
581
+
582
+ - [ ] **Step 7: Run focused and reference regression tests**
583
+
584
+ Run:
585
+
586
+ ```bash
587
+ cmake --build build --target apex_test_runner
588
+ ./build/apex_test_runner bear_image_attributes
589
+ ./build/apex_test_runner multimarkdown_image_attributes
590
+ ./build/apex_test_runner ial
591
+ ```
592
+
593
+ Expected: all suites pass; the new definition assertions find one preserved
594
+ comment and attributes on every reference form.
595
+
596
+ - [ ] **Step 8: Commit definition support**
597
+
598
+ ```bash
599
+ git add src/extensions/ial.c tests/test_bear_image_attrs.c \
600
+ tests/fixtures/images/bear_image_attributes.md
601
+ git commit -m "$(cat <<'EOF'
602
+ Apply Bear attributes from image references
603
+
604
+ @new **Reference metadata** Apply attributes to every matching image.
605
+ EOF
606
+ )"
607
+ ```
608
+
609
+ ### Task 4: Per-Use Reference Overrides
610
+
611
+ **Files:**
612
+
613
+ - Modify: `src/extensions/ial.c:3775-4074`
614
+ - Modify: `tests/test_bear_image_attrs.c`
615
+
616
+ **Interfaces:**
617
+
618
+ - Consumes: normalized reference definitions from Task 3.
619
+ - Produces: merged indexed attributes for one explicit `![alt][id]`
620
+ occurrence without mutating the shared definition entry.
621
+
622
+ - [ ] **Step 1: Add failing per-use tests**
623
+
624
+ Add:
625
+
626
+ ```c
627
+ static void test_bear_reference_overrides(void) {
628
+ const char *md =
629
+ "![Default][shared]\n\n"
630
+ "![Wide][shared]<!-- {\"width\":320,\"title\":\"Wide\"} -->\n\n"
631
+ "![Default again][shared]\n\n"
632
+ "[shared]: shared.jpg <!-- {\"width\":200,\"height\":100} -->";
633
+ char *html = render_bear(md, APEX_MODE_UNIFIED, true);
634
+
635
+ const char *cursor = html;
636
+ int defaults = 0;
637
+ while ((cursor = strstr(cursor, "width=\"200\"")) != NULL) {
638
+ defaults++;
639
+ cursor += strlen("width=\"200\"");
640
+ }
641
+ test_result(defaults == 2, "Sibling references retain definition width");
642
+ test_result(
643
+ strstr(html, "width=\"320\"") != NULL,
644
+ "Per-use metadata overrides the definition width");
645
+ assert_contains(
646
+ html, "title=\"Wide\"", "Per-use title is applied");
647
+
648
+ cursor = html;
649
+ int inherited_heights = 0;
650
+ while ((cursor = strstr(cursor, "height=\"100\"")) != NULL) {
651
+ inherited_heights++;
652
+ cursor += strlen("height=\"100\"");
653
+ }
654
+ test_result(
655
+ inherited_heights == 3,
656
+ "Per-use metadata inherits the definition height");
657
+
658
+ assert_contains(
659
+ html,
660
+ "<!-- {\"width\":320,\"title\":\"Wide\"} -->",
661
+ "Per-use comment is preserved");
662
+ apex_free_string(html);
663
+
664
+ char *unresolved = render_bear(
665
+ "![Missing][none]<!-- {\"width\":999} -->",
666
+ APEX_MODE_UNIFIED,
667
+ true);
668
+ assert_not_contains(
669
+ unresolved, "<img", "Unresolved reference remains unresolved");
670
+ assert_contains(
671
+ unresolved,
672
+ "<!-- {\"width\":999} -->",
673
+ "Unresolved metadata comment is preserved");
674
+ apex_free_string(unresolved);
675
+ }
676
+ ```
677
+
678
+ - [ ] **Step 2: Run the suite and verify sibling leakage**
679
+
680
+ Run:
681
+
682
+ ```bash
683
+ cmake --build build --target apex_test_runner
684
+ ./build/apex_test_runner bear_image_attributes
685
+ ```
686
+
687
+ Expected: the override assertion fails or all shared references receive the
688
+ same width because no local merge exists yet.
689
+
690
+ - [ ] **Step 3: Detect metadata on explicit reference uses**
691
+
692
+ During the second-pass `![alt][ref]` scan:
693
+
694
+ 1. Set `after_use` to one byte after the reference closing bracket.
695
+ 2. Call `parse_adjacent_bear_attrs(after_use, line_end, ...)`.
696
+ 3. Leave collapsed and shortcut references ineligible for local metadata.
697
+ 4. If the reference is unresolved, copy the original use and comment.
698
+
699
+ - [ ] **Step 4: Merge without mutating the definition**
700
+
701
+ Add a deep-copy helper:
702
+
703
+ ```c
704
+ static apex_attributes *copy_attributes(const apex_attributes *source);
705
+ ```
706
+
707
+ For an explicit resolved use, copy `def_entry->attrs`, merge the local Bear
708
+ attributes into the copy, and pass only the copy to
709
+ `attributes_to_markdown()`. Free it after writing the expanded image.
710
+
711
+ Set `read2 = ref_end + 1`, not `comment_end`, after expansion. The normal
712
+ copy loop must preserve all spaces and the exact per-use comment.
713
+
714
+ - [ ] **Step 5: Run focused and image regression tests**
715
+
716
+ Run:
717
+
718
+ ```bash
719
+ cmake --build build --target apex_test_runner
720
+ ./build/apex_test_runner bear_image_attributes
721
+ ./build/apex_test_runner image_width_height_conversion
722
+ ./build/apex_test_runner multimarkdown_image_attributes
723
+ ```
724
+
725
+ Expected: all suites pass; two siblings retain width 200 and only the local
726
+ image receives width 320.
727
+
728
+ - [ ] **Step 6: Commit per-use support**
729
+
730
+ ```bash
731
+ git add src/extensions/ial.c tests/test_bear_image_attrs.c
732
+ git commit -m "$(cat <<'EOF'
733
+ Support per-image Bear reference metadata
734
+
735
+ @new **Per-image reference attributes** Override shared metadata on one image.
736
+ EOF
737
+ )"
738
+ ```
739
+
740
+ ### Task 5: Full Regression and Final Review
741
+
742
+ **Files:**
743
+
744
+ - Modify only files required by failures directly caused by this feature.
745
+
746
+ **Interfaces:**
747
+
748
+ - Consumes: all previous tasks.
749
+ - Produces: a warning-clean build and passing complete test suite.
750
+
751
+ - [ ] **Step 1: Build with project warnings enabled**
752
+
753
+ Run:
754
+
755
+ ```bash
756
+ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
757
+ cmake --build build
758
+ ```
759
+
760
+ Expected: build succeeds without new warnings from Bear parser or image
761
+ preprocessing files.
762
+
763
+ - [ ] **Step 2: Run focused suites**
764
+
765
+ Run:
766
+
767
+ ```bash
768
+ ./build/apex_test_runner bear_image_attributes
769
+ ./build/apex_test_runner image_width_height_conversion
770
+ ./build/apex_test_runner multimarkdown_image_attributes
771
+ ./build/apex_test_runner ial
772
+ ./build/apex_test_runner unsafe_mode
773
+ ./build/apex_test_runner processor_modes
774
+ ```
775
+
776
+ Expected: every suite reports zero failures.
777
+
778
+ - [ ] **Step 3: Run the complete test suite**
779
+
780
+ Run:
781
+
782
+ ```bash
783
+ ctest --test-dir build --output-on-failure
784
+ ```
785
+
786
+ Expected: `100% tests passed, 0 tests failed`.
787
+
788
+ - [ ] **Step 4: Run memory diagnostics when available**
789
+
790
+ Run:
791
+
792
+ ```bash
793
+ cmake -S . -B build-asan -DCMAKE_BUILD_TYPE=Debug \
794
+ -DCMAKE_C_FLAGS="-fsanitize=address,undefined"
795
+ cmake --build build-asan --target apex_test_runner
796
+ ASAN_OPTIONS=detect_leaks=1 \
797
+ ./build-asan/apex_test_runner bear_image_attributes
798
+ ```
799
+
800
+ Expected: all assertions pass with no AddressSanitizer, UndefinedBehavior
801
+ Sanitizer, or leak report.
802
+
803
+ - [ ] **Step 5: Review the final diff**
804
+
805
+ Run:
806
+
807
+ ```bash
808
+ git diff --check
809
+ git status --short
810
+ git diff --stat
811
+ ```
812
+
813
+ Expected: no whitespace errors; only planned source, test, build-list, and
814
+ fixture files are changed.
815
+
816
+ - [ ] **Step 6: Commit any test-only corrections**
817
+
818
+ Skip this step if Task 5 required no changes. Otherwise:
819
+
820
+ ```bash
821
+ git add tests/test_bear_image_attrs.c \
822
+ tests/fixtures/images/bear_image_attributes.md
823
+ git commit -m "$(cat <<'EOF'
824
+ Strengthen Bear image metadata coverage
825
+
826
+ Cover safe rendering, malformed JSON, and reference attribute isolation.
827
+ EOF
828
+ )"
829
+ ```