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,467 @@
1
+ #include "bear_image_attrs.h"
2
+
3
+ #include <ctype.h>
4
+ #include <stdint.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #define APEX_BEAR_JSON_MAX_DEPTH 32
9
+
10
+ static void skip_json_ws(const char **cursor, const char *end) {
11
+ while (*cursor < end &&
12
+ (**cursor == ' ' || **cursor == '\t' ||
13
+ **cursor == '\n' || **cursor == '\r')) {
14
+ (*cursor)++;
15
+ }
16
+ }
17
+
18
+ static int hex_value(char c) {
19
+ if (c >= '0' && c <= '9') {
20
+ return c - '0';
21
+ }
22
+ if (c >= 'a' && c <= 'f') {
23
+ return c - 'a' + 10;
24
+ }
25
+ if (c >= 'A' && c <= 'F') {
26
+ return c - 'A' + 10;
27
+ }
28
+ return -1;
29
+ }
30
+
31
+ static bool parse_hex_quad(const char **cursor, const char *end,
32
+ uint32_t *value) {
33
+ if ((size_t)(end - *cursor) < 4) {
34
+ return false;
35
+ }
36
+
37
+ uint32_t result = 0;
38
+ for (unsigned i = 0; i < 4; i++) {
39
+ int digit = hex_value((*cursor)[i]);
40
+ if (digit < 0) {
41
+ return false;
42
+ }
43
+ result = (result << 4) | (uint32_t)digit;
44
+ }
45
+ *cursor += 4;
46
+ *value = result;
47
+ return true;
48
+ }
49
+
50
+ static bool append_utf8(char *output, size_t *length, uint32_t codepoint) {
51
+ if (codepoint <= 0x7f) {
52
+ output[(*length)++] = (char)codepoint;
53
+ } else if (codepoint <= 0x7ff) {
54
+ output[(*length)++] = (char)(0xc0 | (codepoint >> 6));
55
+ output[(*length)++] = (char)(0x80 | (codepoint & 0x3f));
56
+ } else if (codepoint <= 0xffff) {
57
+ output[(*length)++] = (char)(0xe0 | (codepoint >> 12));
58
+ output[(*length)++] = (char)(0x80 | ((codepoint >> 6) & 0x3f));
59
+ output[(*length)++] = (char)(0x80 | (codepoint & 0x3f));
60
+ } else if (codepoint <= 0x10ffff) {
61
+ output[(*length)++] = (char)(0xf0 | (codepoint >> 18));
62
+ output[(*length)++] = (char)(0x80 | ((codepoint >> 12) & 0x3f));
63
+ output[(*length)++] = (char)(0x80 | ((codepoint >> 6) & 0x3f));
64
+ output[(*length)++] = (char)(0x80 | (codepoint & 0x3f));
65
+ } else {
66
+ return false;
67
+ }
68
+ return true;
69
+ }
70
+
71
+ static bool parse_json_string(const char **cursor, const char *end,
72
+ char **value) {
73
+ if (*cursor >= end || **cursor != '"') {
74
+ return false;
75
+ }
76
+
77
+ const char *input = ++(*cursor);
78
+ char *output = malloc((size_t)(end - input) + 1);
79
+ if (!output) {
80
+ return false;
81
+ }
82
+ size_t length = 0;
83
+
84
+ while (*cursor < end) {
85
+ unsigned char c = (unsigned char)*(*cursor)++;
86
+ if (c == '"') {
87
+ output[length] = '\0';
88
+ *value = output;
89
+ return true;
90
+ }
91
+ if (c < 0x20) {
92
+ free(output);
93
+ return false;
94
+ }
95
+ if (c != '\\') {
96
+ output[length++] = (char)c;
97
+ continue;
98
+ }
99
+ if (*cursor >= end) {
100
+ free(output);
101
+ return false;
102
+ }
103
+
104
+ c = (unsigned char)*(*cursor)++;
105
+ switch (c) {
106
+ case '"':
107
+ case '\\':
108
+ case '/':
109
+ output[length++] = (char)c;
110
+ break;
111
+ case 'b':
112
+ output[length++] = '\b';
113
+ break;
114
+ case 'f':
115
+ output[length++] = '\f';
116
+ break;
117
+ case 'n':
118
+ output[length++] = '\n';
119
+ break;
120
+ case 'r':
121
+ output[length++] = '\r';
122
+ break;
123
+ case 't':
124
+ output[length++] = '\t';
125
+ break;
126
+ case 'u': {
127
+ uint32_t codepoint;
128
+ if (!parse_hex_quad(cursor, end, &codepoint)) {
129
+ free(output);
130
+ return false;
131
+ }
132
+ if (codepoint >= 0xd800 && codepoint <= 0xdbff) {
133
+ if ((size_t)(end - *cursor) < 6 ||
134
+ (*cursor)[0] != '\\' || (*cursor)[1] != 'u') {
135
+ free(output);
136
+ return false;
137
+ }
138
+ *cursor += 2;
139
+ uint32_t low;
140
+ if (!parse_hex_quad(cursor, end, &low) ||
141
+ low < 0xdc00 || low > 0xdfff) {
142
+ free(output);
143
+ return false;
144
+ }
145
+ codepoint = 0x10000 +
146
+ ((codepoint - 0xd800) << 10) + (low - 0xdc00);
147
+ } else if (codepoint >= 0xdc00 && codepoint <= 0xdfff) {
148
+ free(output);
149
+ return false;
150
+ }
151
+ if (!append_utf8(output, &length, codepoint)) {
152
+ free(output);
153
+ return false;
154
+ }
155
+ break;
156
+ }
157
+ default:
158
+ free(output);
159
+ return false;
160
+ }
161
+ }
162
+
163
+ free(output);
164
+ return false;
165
+ }
166
+
167
+ static bool parse_json_number(const char **cursor, const char *end,
168
+ char **value) {
169
+ const char *start = *cursor;
170
+ const char *p = start;
171
+
172
+ if (p < end && *p == '-') {
173
+ p++;
174
+ }
175
+ if (p >= end) {
176
+ return false;
177
+ }
178
+ if (*p == '0') {
179
+ p++;
180
+ if (p < end && isdigit((unsigned char)*p)) {
181
+ return false;
182
+ }
183
+ } else if (*p >= '1' && *p <= '9') {
184
+ do {
185
+ p++;
186
+ } while (p < end && isdigit((unsigned char)*p));
187
+ } else {
188
+ return false;
189
+ }
190
+
191
+ if (p < end && *p == '.') {
192
+ p++;
193
+ if (p >= end || !isdigit((unsigned char)*p)) {
194
+ return false;
195
+ }
196
+ do {
197
+ p++;
198
+ } while (p < end && isdigit((unsigned char)*p));
199
+ }
200
+ if (p < end && (*p == 'e' || *p == 'E')) {
201
+ p++;
202
+ if (p < end && (*p == '+' || *p == '-')) {
203
+ p++;
204
+ }
205
+ if (p >= end || !isdigit((unsigned char)*p)) {
206
+ return false;
207
+ }
208
+ do {
209
+ p++;
210
+ } while (p < end && isdigit((unsigned char)*p));
211
+ }
212
+
213
+ size_t length = (size_t)(p - start);
214
+ char *copy = malloc(length + 1);
215
+ if (!copy) {
216
+ return false;
217
+ }
218
+ memcpy(copy, start, length);
219
+ copy[length] = '\0';
220
+ *cursor = p;
221
+ *value = copy;
222
+ return true;
223
+ }
224
+
225
+ static bool skip_json_value(const char **cursor, const char *end,
226
+ unsigned depth) {
227
+ skip_json_ws(cursor, end);
228
+ if (*cursor >= end) {
229
+ return false;
230
+ }
231
+
232
+ if (**cursor == '"') {
233
+ char *string = NULL;
234
+ bool ok = parse_json_string(cursor, end, &string);
235
+ free(string);
236
+ return ok;
237
+ }
238
+ if (**cursor == '-' || isdigit((unsigned char)**cursor)) {
239
+ char *number = NULL;
240
+ bool ok = parse_json_number(cursor, end, &number);
241
+ free(number);
242
+ return ok;
243
+ }
244
+ if ((size_t)(end - *cursor) >= 4 &&
245
+ memcmp(*cursor, "true", 4) == 0) {
246
+ *cursor += 4;
247
+ return true;
248
+ }
249
+ if ((size_t)(end - *cursor) >= 5 &&
250
+ memcmp(*cursor, "false", 5) == 0) {
251
+ *cursor += 5;
252
+ return true;
253
+ }
254
+ if ((size_t)(end - *cursor) >= 4 &&
255
+ memcmp(*cursor, "null", 4) == 0) {
256
+ *cursor += 4;
257
+ return true;
258
+ }
259
+ if (**cursor != '[' && **cursor != '{') {
260
+ return false;
261
+ }
262
+ if (depth >= APEX_BEAR_JSON_MAX_DEPTH) {
263
+ return false;
264
+ }
265
+
266
+ char open = *(*cursor)++;
267
+ char close = open == '[' ? ']' : '}';
268
+ skip_json_ws(cursor, end);
269
+ if (*cursor < end && **cursor == close) {
270
+ (*cursor)++;
271
+ return true;
272
+ }
273
+
274
+ for (;;) {
275
+ if (open == '{') {
276
+ char *key = NULL;
277
+ if (!parse_json_string(cursor, end, &key)) {
278
+ return false;
279
+ }
280
+ free(key);
281
+ skip_json_ws(cursor, end);
282
+ if (*cursor >= end || *(*cursor)++ != ':') {
283
+ return false;
284
+ }
285
+ }
286
+ if (!skip_json_value(cursor, end, depth + 1)) {
287
+ return false;
288
+ }
289
+ skip_json_ws(cursor, end);
290
+ if (*cursor >= end) {
291
+ return false;
292
+ }
293
+ if (**cursor == close) {
294
+ (*cursor)++;
295
+ return true;
296
+ }
297
+ if (*(*cursor)++ != ',') {
298
+ return false;
299
+ }
300
+ skip_json_ws(cursor, end);
301
+ if (*cursor >= end || **cursor == close) {
302
+ return false;
303
+ }
304
+ }
305
+ }
306
+
307
+ static bool is_allowed_key(const char *key) {
308
+ static const char *allowed[] = {
309
+ "width", "height", "style", "class", "id", "rel", "title"
310
+ };
311
+ for (size_t i = 0; i < sizeof(allowed) / sizeof(allowed[0]); i++) {
312
+ if (strcmp(key, allowed[i]) == 0) {
313
+ return true;
314
+ }
315
+ }
316
+ return false;
317
+ }
318
+
319
+ static bool key_accepts_number(const char *key) {
320
+ return strcmp(key, "width") == 0 || strcmp(key, "height") == 0;
321
+ }
322
+
323
+ static bool set_attr(apex_bear_image_attrs *attrs, char *key, char *value) {
324
+ for (size_t i = 0; i < attrs->count; i++) {
325
+ if (strcmp(attrs->items[i].key, key) == 0) {
326
+ free(attrs->items[i].value);
327
+ attrs->items[i].value = value;
328
+ free(key);
329
+ return true;
330
+ }
331
+ }
332
+ if (attrs->count >= APEX_BEAR_IMAGE_ATTR_CAPACITY) {
333
+ free(key);
334
+ free(value);
335
+ return false;
336
+ }
337
+ attrs->items[attrs->count].key = key;
338
+ attrs->items[attrs->count].value = value;
339
+ attrs->count++;
340
+ return true;
341
+ }
342
+
343
+ void apex_free_bear_image_attrs(apex_bear_image_attrs *attrs) {
344
+ if (!attrs) {
345
+ return;
346
+ }
347
+ for (size_t i = 0; i < attrs->count; i++) {
348
+ free(attrs->items[i].key);
349
+ free(attrs->items[i].value);
350
+ }
351
+ memset(attrs, 0, sizeof(*attrs));
352
+ }
353
+
354
+ bool apex_parse_bear_image_comment(
355
+ const char *comment_start,
356
+ const char *line_end,
357
+ const char **comment_end,
358
+ apex_bear_image_attrs *attrs) {
359
+ if (!attrs) {
360
+ return false;
361
+ }
362
+ /* Release any previous result so reusing the struct cannot leak. */
363
+ apex_free_bear_image_attrs(attrs);
364
+
365
+ if (!comment_start || !line_end || !comment_end ||
366
+ line_end < comment_start || (size_t)(line_end - comment_start) < 7 ||
367
+ memcmp(comment_start, "<!--", 4) != 0) {
368
+ return false;
369
+ }
370
+
371
+ const char *body_end = NULL;
372
+ for (const char *p = comment_start + 4; p + 2 < line_end; p++) {
373
+ if (p[0] == '-' && p[1] == '-' && p[2] == '>') {
374
+ body_end = p;
375
+ break;
376
+ }
377
+ }
378
+ if (!body_end) {
379
+ return false;
380
+ }
381
+
382
+ const char *cursor = comment_start + 4;
383
+ skip_json_ws(&cursor, body_end);
384
+ if (cursor >= body_end || *cursor != '{') {
385
+ return false;
386
+ }
387
+ cursor++;
388
+ skip_json_ws(&cursor, body_end);
389
+
390
+ if (cursor < body_end && *cursor != '}') {
391
+ for (;;) {
392
+ char *key = NULL;
393
+ char *value = NULL;
394
+ if (!parse_json_string(&cursor, body_end, &key)) {
395
+ goto fail;
396
+ }
397
+ skip_json_ws(&cursor, body_end);
398
+ if (cursor >= body_end || *cursor++ != ':') {
399
+ free(key);
400
+ goto fail;
401
+ }
402
+ skip_json_ws(&cursor, body_end);
403
+
404
+ bool allowed = is_allowed_key(key);
405
+ bool store = false;
406
+ if (cursor < body_end && *cursor == '"') {
407
+ if (!parse_json_string(&cursor, body_end, &value)) {
408
+ free(key);
409
+ goto fail;
410
+ }
411
+ store = allowed;
412
+ } else if (cursor < body_end &&
413
+ (*cursor == '-' ||
414
+ isdigit((unsigned char)*cursor))) {
415
+ if (!parse_json_number(&cursor, body_end, &value)) {
416
+ free(key);
417
+ goto fail;
418
+ }
419
+ store = allowed && key_accepts_number(key);
420
+ } else {
421
+ if (!skip_json_value(&cursor, body_end, 0)) {
422
+ free(key);
423
+ goto fail;
424
+ }
425
+ }
426
+
427
+ if (store) {
428
+ if (!set_attr(attrs, key, value)) {
429
+ goto fail;
430
+ }
431
+ } else {
432
+ free(key);
433
+ free(value);
434
+ }
435
+
436
+ skip_json_ws(&cursor, body_end);
437
+ if (cursor >= body_end) {
438
+ goto fail;
439
+ }
440
+ if (*cursor == '}') {
441
+ break;
442
+ }
443
+ if (*cursor++ != ',') {
444
+ goto fail;
445
+ }
446
+ skip_json_ws(&cursor, body_end);
447
+ if (cursor >= body_end || *cursor == '}') {
448
+ goto fail;
449
+ }
450
+ }
451
+ }
452
+
453
+ if (cursor >= body_end || *cursor++ != '}') {
454
+ goto fail;
455
+ }
456
+ skip_json_ws(&cursor, body_end);
457
+ if (cursor != body_end) {
458
+ goto fail;
459
+ }
460
+
461
+ *comment_end = body_end + 3;
462
+ return true;
463
+
464
+ fail:
465
+ apex_free_bear_image_attrs(attrs);
466
+ return false;
467
+ }
@@ -0,0 +1,27 @@
1
+ #ifndef APEX_BEAR_IMAGE_ATTRS_H
2
+ #define APEX_BEAR_IMAGE_ATTRS_H
3
+
4
+ #include <stdbool.h>
5
+ #include <stddef.h>
6
+
7
+ #define APEX_BEAR_IMAGE_ATTR_CAPACITY 7
8
+
9
+ typedef struct {
10
+ char *key;
11
+ char *value;
12
+ } apex_bear_image_attr;
13
+
14
+ typedef struct {
15
+ apex_bear_image_attr items[APEX_BEAR_IMAGE_ATTR_CAPACITY];
16
+ size_t count;
17
+ } apex_bear_image_attrs;
18
+
19
+ bool apex_parse_bear_image_comment(
20
+ const char *comment_start,
21
+ const char *line_end,
22
+ const char **comment_end,
23
+ apex_bear_image_attrs *attrs);
24
+
25
+ void apex_free_bear_image_attrs(apex_bear_image_attrs *attrs);
26
+
27
+ #endif