scholarly-parse 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +201 -0
  3. package/README.md +118 -0
  4. package/changelog/0.1.x/0.1.0.md +17 -0
  5. package/changelog/template.md +150 -0
  6. package/dist/detect.d.ts +11 -0
  7. package/dist/detect.d.ts.map +1 -0
  8. package/dist/detect.js +51 -0
  9. package/dist/detect.js.map +1 -0
  10. package/dist/formats/html/blocks.d.ts +29 -0
  11. package/dist/formats/html/blocks.d.ts.map +1 -0
  12. package/dist/formats/html/blocks.js +527 -0
  13. package/dist/formats/html/blocks.js.map +1 -0
  14. package/dist/formats/html/context.d.ts +19 -0
  15. package/dist/formats/html/context.d.ts.map +1 -0
  16. package/dist/formats/html/context.js +122 -0
  17. package/dist/formats/html/context.js.map +1 -0
  18. package/dist/formats/html/index.d.ts +7 -0
  19. package/dist/formats/html/index.d.ts.map +1 -0
  20. package/dist/formats/html/index.js +7 -0
  21. package/dist/formats/html/index.js.map +1 -0
  22. package/dist/formats/html/inline.d.ts +26 -0
  23. package/dist/formats/html/inline.d.ts.map +1 -0
  24. package/dist/formats/html/inline.js +253 -0
  25. package/dist/formats/html/inline.js.map +1 -0
  26. package/dist/formats/html/metadata.d.ts +30 -0
  27. package/dist/formats/html/metadata.d.ts.map +1 -0
  28. package/dist/formats/html/metadata.js +212 -0
  29. package/dist/formats/html/metadata.js.map +1 -0
  30. package/dist/formats/html/parse.d.ts +17 -0
  31. package/dist/formats/html/parse.d.ts.map +1 -0
  32. package/dist/formats/html/parse.js +405 -0
  33. package/dist/formats/html/parse.js.map +1 -0
  34. package/dist/formats/html/references.d.ts +12 -0
  35. package/dist/formats/html/references.d.ts.map +1 -0
  36. package/dist/formats/html/references.js +123 -0
  37. package/dist/formats/html/references.js.map +1 -0
  38. package/dist/formats/jats/blocks.d.ts +52 -0
  39. package/dist/formats/jats/blocks.d.ts.map +1 -0
  40. package/dist/formats/jats/blocks.js +614 -0
  41. package/dist/formats/jats/blocks.js.map +1 -0
  42. package/dist/formats/jats/context.d.ts +17 -0
  43. package/dist/formats/jats/context.d.ts.map +1 -0
  44. package/dist/formats/jats/context.js +2 -0
  45. package/dist/formats/jats/context.js.map +1 -0
  46. package/dist/formats/jats/fragment.d.ts +7 -0
  47. package/dist/formats/jats/fragment.d.ts.map +1 -0
  48. package/dist/formats/jats/fragment.js +42 -0
  49. package/dist/formats/jats/fragment.js.map +1 -0
  50. package/dist/formats/jats/front.d.ts +25 -0
  51. package/dist/formats/jats/front.d.ts.map +1 -0
  52. package/dist/formats/jats/front.js +380 -0
  53. package/dist/formats/jats/front.js.map +1 -0
  54. package/dist/formats/jats/index.d.ts +8 -0
  55. package/dist/formats/jats/index.d.ts.map +1 -0
  56. package/dist/formats/jats/index.js +8 -0
  57. package/dist/formats/jats/index.js.map +1 -0
  58. package/dist/formats/jats/inline.d.ts +24 -0
  59. package/dist/formats/jats/inline.d.ts.map +1 -0
  60. package/dist/formats/jats/inline.js +274 -0
  61. package/dist/formats/jats/inline.js.map +1 -0
  62. package/dist/formats/jats/parse.d.ts +9 -0
  63. package/dist/formats/jats/parse.d.ts.map +1 -0
  64. package/dist/formats/jats/parse.js +247 -0
  65. package/dist/formats/jats/parse.js.map +1 -0
  66. package/dist/formats/jats/references.d.ts +25 -0
  67. package/dist/formats/jats/references.d.ts.map +1 -0
  68. package/dist/formats/jats/references.js +406 -0
  69. package/dist/formats/jats/references.js.map +1 -0
  70. package/dist/formats/jats/tables.d.ts +18 -0
  71. package/dist/formats/jats/tables.d.ts.map +1 -0
  72. package/dist/formats/jats/tables.js +105 -0
  73. package/dist/formats/jats/tables.js.map +1 -0
  74. package/dist/formats/jats/text.d.ts +23 -0
  75. package/dist/formats/jats/text.d.ts.map +1 -0
  76. package/dist/formats/jats/text.js +114 -0
  77. package/dist/formats/jats/text.js.map +1 -0
  78. package/dist/formats/latexml/blocks.d.ts +13 -0
  79. package/dist/formats/latexml/blocks.d.ts.map +1 -0
  80. package/dist/formats/latexml/blocks.js +454 -0
  81. package/dist/formats/latexml/blocks.js.map +1 -0
  82. package/dist/formats/latexml/context.d.ts +19 -0
  83. package/dist/formats/latexml/context.d.ts.map +1 -0
  84. package/dist/formats/latexml/context.js +18 -0
  85. package/dist/formats/latexml/context.js.map +1 -0
  86. package/dist/formats/latexml/index.d.ts +7 -0
  87. package/dist/formats/latexml/index.d.ts.map +1 -0
  88. package/dist/formats/latexml/index.js +7 -0
  89. package/dist/formats/latexml/index.js.map +1 -0
  90. package/dist/formats/latexml/inline.d.ts +16 -0
  91. package/dist/formats/latexml/inline.d.ts.map +1 -0
  92. package/dist/formats/latexml/inline.js +113 -0
  93. package/dist/formats/latexml/inline.js.map +1 -0
  94. package/dist/formats/latexml/parse.d.ts +16 -0
  95. package/dist/formats/latexml/parse.d.ts.map +1 -0
  96. package/dist/formats/latexml/parse.js +405 -0
  97. package/dist/formats/latexml/parse.js.map +1 -0
  98. package/dist/formats/pdf/blocks.d.ts +14 -0
  99. package/dist/formats/pdf/blocks.d.ts.map +1 -0
  100. package/dist/formats/pdf/blocks.js +203 -0
  101. package/dist/formats/pdf/blocks.js.map +1 -0
  102. package/dist/formats/pdf/index.d.ts +7 -0
  103. package/dist/formats/pdf/index.d.ts.map +1 -0
  104. package/dist/formats/pdf/index.js +7 -0
  105. package/dist/formats/pdf/index.js.map +1 -0
  106. package/dist/formats/pdf/inline.d.ts +8 -0
  107. package/dist/formats/pdf/inline.d.ts.map +1 -0
  108. package/dist/formats/pdf/inline.js +91 -0
  109. package/dist/formats/pdf/inline.js.map +1 -0
  110. package/dist/formats/pdf/layout.d.ts +33 -0
  111. package/dist/formats/pdf/layout.d.ts.map +1 -0
  112. package/dist/formats/pdf/layout.js +358 -0
  113. package/dist/formats/pdf/layout.js.map +1 -0
  114. package/dist/formats/pdf/lines.d.ts +40 -0
  115. package/dist/formats/pdf/lines.d.ts.map +1 -0
  116. package/dist/formats/pdf/lines.js +104 -0
  117. package/dist/formats/pdf/lines.js.map +1 -0
  118. package/dist/formats/pdf/load.d.ts +43 -0
  119. package/dist/formats/pdf/load.d.ts.map +1 -0
  120. package/dist/formats/pdf/load.js +126 -0
  121. package/dist/formats/pdf/load.js.map +1 -0
  122. package/dist/formats/pdf/parse.d.ts +9 -0
  123. package/dist/formats/pdf/parse.d.ts.map +1 -0
  124. package/dist/formats/pdf/parse.js +126 -0
  125. package/dist/formats/pdf/parse.js.map +1 -0
  126. package/dist/formats/pdf/references.d.ts +14 -0
  127. package/dist/formats/pdf/references.d.ts.map +1 -0
  128. package/dist/formats/pdf/references.js +104 -0
  129. package/dist/formats/pdf/references.js.map +1 -0
  130. package/dist/formats/pdf/structure.d.ts +29 -0
  131. package/dist/formats/pdf/structure.d.ts.map +1 -0
  132. package/dist/formats/pdf/structure.js +533 -0
  133. package/dist/formats/pdf/structure.js.map +1 -0
  134. package/dist/formats/tei/index.d.ts +7 -0
  135. package/dist/formats/tei/index.d.ts.map +1 -0
  136. package/dist/formats/tei/index.js +7 -0
  137. package/dist/formats/tei/index.js.map +1 -0
  138. package/dist/formats/tei/inline.d.ts +24 -0
  139. package/dist/formats/tei/inline.d.ts.map +1 -0
  140. package/dist/formats/tei/inline.js +107 -0
  141. package/dist/formats/tei/inline.js.map +1 -0
  142. package/dist/formats/tei/parse.d.ts +4 -0
  143. package/dist/formats/tei/parse.d.ts.map +1 -0
  144. package/dist/formats/tei/parse.js +560 -0
  145. package/dist/formats/tei/parse.js.map +1 -0
  146. package/dist/html/dom.d.ts +35 -0
  147. package/dist/html/dom.d.ts.map +1 -0
  148. package/dist/html/dom.js +70 -0
  149. package/dist/html/dom.js.map +1 -0
  150. package/dist/html/interstitial.d.ts +14 -0
  151. package/dist/html/interstitial.d.ts.map +1 -0
  152. package/dist/html/interstitial.js +67 -0
  153. package/dist/html/interstitial.js.map +1 -0
  154. package/dist/html/tables.d.ts +12 -0
  155. package/dist/html/tables.d.ts.map +1 -0
  156. package/dist/html/tables.js +62 -0
  157. package/dist/html/tables.js.map +1 -0
  158. package/dist/index.d.ts +12 -0
  159. package/dist/index.d.ts.map +1 -0
  160. package/dist/index.js +12 -0
  161. package/dist/index.js.map +1 -0
  162. package/dist/model/diagnostics.d.ts +15 -0
  163. package/dist/model/diagnostics.d.ts.map +1 -0
  164. package/dist/model/diagnostics.js +37 -0
  165. package/dist/model/diagnostics.js.map +1 -0
  166. package/dist/model/document.d.ts +289 -0
  167. package/dist/model/document.d.ts.map +1 -0
  168. package/dist/model/document.js +13 -0
  169. package/dist/model/document.js.map +1 -0
  170. package/dist/model/extremes.d.ts +11 -0
  171. package/dist/model/extremes.d.ts.map +1 -0
  172. package/dist/model/extremes.js +23 -0
  173. package/dist/model/extremes.js.map +1 -0
  174. package/dist/model/index.d.ts +8 -0
  175. package/dist/model/index.d.ts.map +1 -0
  176. package/dist/model/index.js +2 -0
  177. package/dist/model/index.js.map +1 -0
  178. package/dist/model/input.d.ts +14 -0
  179. package/dist/model/input.d.ts.map +1 -0
  180. package/dist/model/input.js +47 -0
  181. package/dist/model/input.js.map +1 -0
  182. package/dist/model/result.d.ts +53 -0
  183. package/dist/model/result.d.ts.map +1 -0
  184. package/dist/model/result.js +40 -0
  185. package/dist/model/result.js.map +1 -0
  186. package/dist/model/section-ids.d.ts +8 -0
  187. package/dist/model/section-ids.d.ts.map +1 -0
  188. package/dist/model/section-ids.js +28 -0
  189. package/dist/model/section-ids.js.map +1 -0
  190. package/dist/model/section-kinds.d.ts +19 -0
  191. package/dist/model/section-kinds.d.ts.map +1 -0
  192. package/dist/model/section-kinds.js +42 -0
  193. package/dist/model/section-kinds.js.map +1 -0
  194. package/dist/model/table-grid.d.ts +40 -0
  195. package/dist/model/table-grid.d.ts.map +1 -0
  196. package/dist/model/table-grid.js +79 -0
  197. package/dist/model/table-grid.js.map +1 -0
  198. package/dist/parse.d.ts +18 -0
  199. package/dist/parse.d.ts.map +1 -0
  200. package/dist/parse.js +35 -0
  201. package/dist/parse.js.map +1 -0
  202. package/dist/render/escape.d.ts +53 -0
  203. package/dist/render/escape.d.ts.map +1 -0
  204. package/dist/render/escape.js +94 -0
  205. package/dist/render/escape.js.map +1 -0
  206. package/dist/render/index.d.ts +8 -0
  207. package/dist/render/index.d.ts.map +1 -0
  208. package/dist/render/index.js +8 -0
  209. package/dist/render/index.js.map +1 -0
  210. package/dist/render/inline.d.ts +39 -0
  211. package/dist/render/inline.d.ts.map +1 -0
  212. package/dist/render/inline.js +87 -0
  213. package/dist/render/inline.js.map +1 -0
  214. package/dist/render/markdown.d.ts +35 -0
  215. package/dist/render/markdown.d.ts.map +1 -0
  216. package/dist/render/markdown.js +279 -0
  217. package/dist/render/markdown.js.map +1 -0
  218. package/dist/render/sections.d.ts +25 -0
  219. package/dist/render/sections.d.ts.map +1 -0
  220. package/dist/render/sections.js +45 -0
  221. package/dist/render/sections.js.map +1 -0
  222. package/dist/render/text.d.ts +13 -0
  223. package/dist/render/text.d.ts.map +1 -0
  224. package/dist/render/text.js +66 -0
  225. package/dist/render/text.js.map +1 -0
  226. package/dist/xml/mathml.d.ts +24 -0
  227. package/dist/xml/mathml.d.ts.map +1 -0
  228. package/dist/xml/mathml.js +201 -0
  229. package/dist/xml/mathml.js.map +1 -0
  230. package/dist/xml/ordered.d.ts +49 -0
  231. package/dist/xml/ordered.d.ts.map +1 -0
  232. package/dist/xml/ordered.js +151 -0
  233. package/dist/xml/ordered.js.map +1 -0
  234. package/dist/xml/well-formed.d.ts +15 -0
  235. package/dist/xml/well-formed.d.ts.map +1 -0
  236. package/dist/xml/well-formed.js +120 -0
  237. package/dist/xml/well-formed.js.map +1 -0
  238. package/package.json +137 -0
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @fileoverview The document model every format parser produces. One shape for a
3
+ * scholarly article regardless of source: front matter, a section tree of typed
4
+ * blocks, references, and diagnostics describing how much structure was recovered.
5
+ *
6
+ * Inline text (paragraph bodies, titles, captions, list items, reference text) is
7
+ * CommonMark with GFM extensions: emphasis, `$…$` inline math where the source
8
+ * carries TeX, links, and citation markers as printed in the source. Block-level
9
+ * structure is typed, so renderers never re-parse Markdown to find a table.
10
+ * @module src/model/document
11
+ */
12
+ export {};
13
+ //# sourceMappingURL=document.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document.js","sourceRoot":"","sources":["../../src/model/document.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @fileoverview The largest and smallest of a list of numbers. `Math.max(...values)`
3
+ * passes every value as an argument, and a document-sized list — a table's rows, a
4
+ * page's text items — overflows the call stack (Node rejects a few hundred thousand).
5
+ * @module src/model/extremes
6
+ */
7
+ /** The largest value, or `-Infinity` for none, as `Math.max()` returns. */
8
+ export declare function largest(values: readonly number[]): number;
9
+ /** The smallest value, or `Infinity` for none, as `Math.min()` returns. */
10
+ export declare function smallest(values: readonly number[]): number;
11
+ //# sourceMappingURL=extremes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extremes.d.ts","sourceRoot":"","sources":["../../src/model/extremes.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,2EAA2E;AAC3E,wBAAgB,OAAO,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAIzD;AAED,2EAA2E;AAC3E,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAI1D"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @fileoverview The largest and smallest of a list of numbers. `Math.max(...values)`
3
+ * passes every value as an argument, and a document-sized list — a table's rows, a
4
+ * page's text items — overflows the call stack (Node rejects a few hundred thousand).
5
+ * @module src/model/extremes
6
+ */
7
+ /** The largest value, or `-Infinity` for none, as `Math.max()` returns. */
8
+ export function largest(values) {
9
+ let result = -Infinity;
10
+ for (const value of values)
11
+ if (value > result)
12
+ result = value;
13
+ return result;
14
+ }
15
+ /** The smallest value, or `Infinity` for none, as `Math.min()` returns. */
16
+ export function smallest(values) {
17
+ let result = Infinity;
18
+ for (const value of values)
19
+ if (value < result)
20
+ result = value;
21
+ return result;
22
+ }
23
+ //# sourceMappingURL=extremes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extremes.js","sourceRoot":"","sources":["../../src/model/extremes.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,2EAA2E;AAC3E,MAAM,UAAU,OAAO,CAAC,MAAyB;IAC/C,IAAI,MAAM,GAAG,CAAC,QAAQ,CAAC;IACvB,KAAK,MAAM,KAAK,IAAI,MAAM;QAAE,IAAI,KAAK,GAAG,MAAM;YAAE,MAAM,GAAG,KAAK,CAAC;IAC/D,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,QAAQ,CAAC,MAAyB;IAChD,IAAI,MAAM,GAAG,QAAQ,CAAC;IACtB,KAAK,MAAM,KAAK,IAAI,MAAM;QAAE,IAAI,KAAK,GAAG,MAAM;YAAE,MAAM,GAAG,KAAK,CAAC;IAC/D,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @fileoverview Public surface of the document model: the types every parser
3
+ * produces and the result wrapper every parser returns.
4
+ * @module src/model
5
+ */
6
+ export type * from './document.js';
7
+ export { failed, type ParseFailure, type ParseFailureReason, type ParseOptions, type ParseResult, parsed, } from './result.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/model/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,mBAAmB,eAAe,CAAC;AACnC,OAAO,EACL,MAAM,EACN,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,MAAM,GACP,MAAM,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { failed, parsed, } from './result.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/model/index.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,MAAM,EAKN,MAAM,GACP,MAAM,aAAa,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @fileoverview Input handling every parser shares: the size budget and turning bytes
3
+ * into text with the encoding the document declares.
4
+ * @module src/model/input
5
+ */
6
+ /** True when the input is larger than `maxBytes`. Strings are measured as UTF-8. */
7
+ export declare function exceedsBudget(input: string | Uint8Array, maxBytes: number): boolean;
8
+ /**
9
+ * Text of the input. Bytes are decoded by their byte-order mark, else the encoding an
10
+ * XML declaration or HTML `<meta charset>` names in the first kilobyte, else UTF-8.
11
+ * A leading byte-order mark is dropped from strings too.
12
+ */
13
+ export declare function decodeText(input: string | Uint8Array): string;
14
+ //# sourceMappingURL=input.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../src/model/input.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oFAAoF;AACpF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAKnF;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAU7D"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @fileoverview Input handling every parser shares: the size budget and turning bytes
3
+ * into text with the encoding the document declares.
4
+ * @module src/model/input
5
+ */
6
+ /** True when the input is larger than `maxBytes`. Strings are measured as UTF-8. */
7
+ export function exceedsBudget(input, maxBytes) {
8
+ if (typeof input !== 'string')
9
+ return input.byteLength > maxBytes;
10
+ // A string needs at most 3 UTF-8 bytes per UTF-16 unit; skip encoding when that bound fits.
11
+ if (input.length * 3 <= maxBytes)
12
+ return false;
13
+ return new TextEncoder().encode(input).byteLength > maxBytes;
14
+ }
15
+ /**
16
+ * Text of the input. Bytes are decoded by their byte-order mark, else the encoding an
17
+ * XML declaration or HTML `<meta charset>` names in the first kilobyte, else UTF-8.
18
+ * A leading byte-order mark is dropped from strings too.
19
+ */
20
+ export function decodeText(input) {
21
+ if (typeof input === 'string')
22
+ return input.replace(/^/, '');
23
+ const label = bomEncoding(input) ?? declaredEncoding(input) ?? 'utf-8';
24
+ let decoder;
25
+ try {
26
+ decoder = new TextDecoder(label);
27
+ }
28
+ catch {
29
+ decoder = new TextDecoder('utf-8');
30
+ }
31
+ return decoder.decode(input).replace(/^/, '');
32
+ }
33
+ function bomEncoding(bytes) {
34
+ if (bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf)
35
+ return 'utf-8';
36
+ if (bytes[0] === 0xff && bytes[1] === 0xfe)
37
+ return 'utf-16le';
38
+ if (bytes[0] === 0xfe && bytes[1] === 0xff)
39
+ return 'utf-16be';
40
+ return;
41
+ }
42
+ function declaredEncoding(bytes) {
43
+ const head = new TextDecoder('latin1').decode(bytes.subarray(0, 1024));
44
+ return (/<\?xml[^>]*\bencoding\s*=\s*["']([\w.:-]+)["']/i.exec(head)?.[1] ??
45
+ /<meta[^>]+charset\s*=\s*["']?([\w.:-]+)/i.exec(head)?.[1]);
46
+ }
47
+ //# sourceMappingURL=input.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input.js","sourceRoot":"","sources":["../../src/model/input.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oFAAoF;AACpF,MAAM,UAAU,aAAa,CAAC,KAA0B,EAAE,QAAgB;IACxE,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,UAAU,GAAG,QAAQ,CAAC;IAClE,4FAA4F;IAC5F,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC/C,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,UAAU,GAAG,QAAQ,CAAC;AAC/D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAA0B;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAC9D,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,gBAAgB,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC;IACvE,IAAI,OAAoB,CAAC;IACzB,IAAI,CAAC;QACH,OAAO,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AACjD,CAAC;AAED,SAAS,WAAW,CAAC,KAAiB;IACpC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI;QAAE,OAAO,OAAO,CAAC;IAChF,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI;QAAE,OAAO,UAAU,CAAC;IAC9D,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI;QAAE,OAAO,UAAU,CAAC;IAC9D,OAAO;AACT,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAiB;IACzC,MAAM,IAAI,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;IACvE,OAAO,CACL,iDAAiD,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QACjE,0CAA0C,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAC3D,CAAC;AACJ,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @fileoverview The value every parser returns. Expected failures (malformed input,
3
+ * the wrong format, an empty body, input over budget) are results, not exceptions;
4
+ * a missing optional peer dependency is a setup error and throws.
5
+ * @module src/model/result
6
+ */
7
+ import type { ScholarlyDocument } from './document.js';
8
+ /** Why a parse produced no document. */
9
+ export type ParseFailureReason =
10
+ /** The bytes are not well-formed for the format (unparseable XML, corrupt PDF). */
11
+ 'malformed'
12
+ /** Well-formed, but not the format this parser reads (e.g. HTML given to the JATS parser). */
13
+ | 'wrong-format'
14
+ /** Parsed, but carried no article content. */
15
+ | 'empty'
16
+ /** An interstitial rather than the document: a captcha, bot check, or login wall. */
17
+ | 'blocked'
18
+ /** Larger than the configured input budget. */
19
+ | 'too-large';
20
+ /** A parse that produced no document, with the reason and a human-readable detail. */
21
+ export interface ParseFailure {
22
+ message: string;
23
+ reason: ParseFailureReason;
24
+ }
25
+ /** The outcome of a parse. */
26
+ export type ParseResult = {
27
+ document: ScholarlyDocument;
28
+ ok: true;
29
+ } | {
30
+ error: ParseFailure;
31
+ ok: false;
32
+ };
33
+ /** Options every parser accepts. */
34
+ export interface ParseOptions {
35
+ /** Reject inputs larger than this many bytes. Defaults vary by format. */
36
+ maxInputBytes?: number;
37
+ }
38
+ /** Build a successful result. */
39
+ export declare function parsed(document: ScholarlyDocument): ParseResult;
40
+ /** Build a failed result. */
41
+ export declare function failed(reason: ParseFailureReason, message: string): ParseResult;
42
+ /** A missing optional peer dependency: a setup error, so it throws rather than failing a parse. */
43
+ export declare class MissingPeerError extends Error {
44
+ }
45
+ /**
46
+ * Run a parse so that a throw it did not anticipate — a stack overflow on deep nesting,
47
+ * an engine's internal error — returns as `malformed` instead of escaping the
48
+ * {@link ParseResult} contract. A {@link MissingPeerError} still throws.
49
+ */
50
+ export declare function guard(parse: () => ParseResult): ParseResult;
51
+ /** {@link guard} for a parser that reads asynchronously. */
52
+ export declare function guardAsync(parse: () => Promise<ParseResult>): Promise<ParseResult>;
53
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/model/result.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEvD,wCAAwC;AACxC,MAAM,MAAM,kBAAkB;AAC5B,mFAAmF;AACjF,WAAW;AACb,8FAA8F;GAC5F,cAAc;AAChB,8CAA8C;GAC5C,OAAO;AACT,qFAAqF;GACnF,SAAS;AACX,+CAA+C;GAC7C,WAAW,CAAC;AAEhB,sFAAsF;AACtF,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,kBAAkB,CAAC;CAC5B;AAED,8BAA8B;AAC9B,MAAM,MAAM,WAAW,GACnB;IAAE,QAAQ,EAAE,iBAAiB,CAAC;IAAC,EAAE,EAAE,IAAI,CAAA;CAAE,GACzC;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,EAAE,EAAE,KAAK,CAAA;CAAE,CAAC;AAEvC,oCAAoC;AACpC,MAAM,WAAW,YAAY;IAC3B,0EAA0E;IAC1E,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,iCAAiC;AACjC,wBAAgB,MAAM,CAAC,QAAQ,EAAE,iBAAiB,GAAG,WAAW,CAE/D;AAED,6BAA6B;AAC7B,wBAAgB,MAAM,CAAC,MAAM,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CAE/E;AAED,mGAAmG;AACnG,qBAAa,gBAAiB,SAAQ,KAAK;CAAG;AAE9C;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,WAAW,GAAG,WAAW,CAM3D;AAED,4DAA4D;AAC5D,wBAAsB,UAAU,CAAC,KAAK,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAMxF"}
@@ -0,0 +1,40 @@
1
+ /** Build a successful result. */
2
+ export function parsed(document) {
3
+ return { document, ok: true };
4
+ }
5
+ /** Build a failed result. */
6
+ export function failed(reason, message) {
7
+ return { error: { message, reason }, ok: false };
8
+ }
9
+ /** A missing optional peer dependency: a setup error, so it throws rather than failing a parse. */
10
+ export class MissingPeerError extends Error {
11
+ }
12
+ /**
13
+ * Run a parse so that a throw it did not anticipate — a stack overflow on deep nesting,
14
+ * an engine's internal error — returns as `malformed` instead of escaping the
15
+ * {@link ParseResult} contract. A {@link MissingPeerError} still throws.
16
+ */
17
+ export function guard(parse) {
18
+ try {
19
+ return parse();
20
+ }
21
+ catch (error) {
22
+ return unexpected(error);
23
+ }
24
+ }
25
+ /** {@link guard} for a parser that reads asynchronously. */
26
+ export async function guardAsync(parse) {
27
+ try {
28
+ return await parse();
29
+ }
30
+ catch (error) {
31
+ return unexpected(error);
32
+ }
33
+ }
34
+ function unexpected(error) {
35
+ if (error instanceof MissingPeerError)
36
+ throw error;
37
+ const detail = error instanceof Error ? error.message : String(error);
38
+ return failed('malformed', `The document could not be read: ${detail}`);
39
+ }
40
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/model/result.ts"],"names":[],"mappings":"AAsCA,iCAAiC;AACjC,MAAM,UAAU,MAAM,CAAC,QAA2B;IAChD,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;AAChC,CAAC;AAED,6BAA6B;AAC7B,MAAM,UAAU,MAAM,CAAC,MAA0B,EAAE,OAAe;IAChE,OAAO,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;AACnD,CAAC;AAED,mGAAmG;AACnG,MAAM,OAAO,gBAAiB,SAAQ,KAAK;CAAG;AAE9C;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,KAAwB;IAC5C,IAAI,CAAC;QACH,OAAO,KAAK,EAAE,CAAC;IACjB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;AACH,CAAC;AAED,4DAA4D;AAC5D,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,KAAiC;IAChE,IAAI,CAAC;QACH,OAAO,MAAM,KAAK,EAAE,CAAC;IACvB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;AACH,CAAC;AAED,SAAS,UAAU,CAAC,KAAc;IAChC,IAAI,KAAK,YAAY,gBAAgB;QAAE,MAAM,KAAK,CAAC;IACnD,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtE,OAAO,MAAM,CAAC,WAAW,EAAE,mCAAmC,MAAM,EAAE,CAAC,CAAC;AAC1E,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @fileoverview Section IDs unique within one document: the source's own ID when it is
3
+ * unused, else a fallback made unique with a numeric suffix.
4
+ * @module src/model/section-ids
5
+ */
6
+ /** Issue an ID into `issued`: `sourceId` when unused, else `fallback` made unique. */
7
+ export declare function issueId(issued: Set<string>, sourceId: string | undefined, fallback: string): string;
8
+ //# sourceMappingURL=section-ids.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"section-ids.d.ts","sourceRoot":"","sources":["../../src/model/section-ids.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,sFAAsF;AACtF,wBAAgB,OAAO,CACrB,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,EACnB,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,QAAQ,EAAE,MAAM,GACf,MAAM,CAaR"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @fileoverview Section IDs unique within one document: the source's own ID when it is
3
+ * unused, else a fallback made unique with a numeric suffix.
4
+ * @module src/model/section-ids
5
+ */
6
+ /**
7
+ * The next suffix to try for each base, per set of issued IDs, so a document that
8
+ * repeats one ID throughout costs one step per section rather than a search from `-2`
9
+ * each time. A suffix freed by a deleted ID is not reissued; IDs stay unique.
10
+ */
11
+ const nextSuffix = new WeakMap();
12
+ /** Issue an ID into `issued`: `sourceId` when unused, else `fallback` made unique. */
13
+ export function issueId(issued, sourceId, fallback) {
14
+ const base = sourceId || fallback;
15
+ let id = base;
16
+ if (issued.has(id)) {
17
+ const suffixes = nextSuffix.get(issued) ?? new Map();
18
+ nextSuffix.set(issued, suffixes);
19
+ let n = suffixes.get(base) ?? 2;
20
+ while (issued.has(`${base}-${n}`))
21
+ n++;
22
+ id = `${base}-${n}`;
23
+ suffixes.set(base, n + 1);
24
+ }
25
+ issued.add(id);
26
+ return id;
27
+ }
28
+ //# sourceMappingURL=section-ids.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"section-ids.js","sourceRoot":"","sources":["../../src/model/section-ids.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,UAAU,GAAG,IAAI,OAAO,EAAoC,CAAC;AAEnE,sFAAsF;AACtF,MAAM,UAAU,OAAO,CACrB,MAAmB,EACnB,QAA4B,EAC5B,QAAgB;IAEhB,MAAM,IAAI,GAAG,QAAQ,IAAI,QAAQ,CAAC;IAClC,IAAI,EAAE,GAAG,IAAI,CAAC;IACd,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC;QACnB,MAAM,QAAQ,GAAG,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,IAAI,GAAG,EAAkB,CAAC;QACrE,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACjC,IAAI,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChC,OAAO,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,IAAI,CAAC,EAAE,CAAC;YAAE,CAAC,EAAE,CAAC;QACvC,EAAE,GAAG,GAAG,IAAI,IAAI,CAAC,EAAE,CAAC;QACpB,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B,CAAC;IACD,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACf,OAAO,EAAE,CAAC;AACZ,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @fileoverview A section's kind from its heading, for sources that do not declare one:
3
+ * the "Acknowledgements" or "Data availability" section a LaTeX paper or a web page
4
+ * writes as an ordinary numbered section.
5
+ * @module src/model/section-kinds
6
+ */
7
+ import type { SectionKind } from './document.js';
8
+ /** The back-matter kind a heading names, or undefined for an ordinary body section. */
9
+ export declare function kindFromTitle(title: string | undefined): SectionKind | undefined;
10
+ /**
11
+ * A heading with its printed number moved into the label, for sources that write the
12
+ * number into the heading text (`1 Introduction`, `2.1. Methods`). A label the source
13
+ * gives separately is kept, and a matching number is removed from the title.
14
+ */
15
+ export declare function splitSectionNumber(title: string | undefined, label: string | undefined): {
16
+ label?: string;
17
+ title?: string;
18
+ };
19
+ //# sourceMappingURL=section-kinds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"section-kinds.d.ts","sourceRoot":"","sources":["../../src/model/section-kinds.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAoBjD,uFAAuF;AACvF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,CAIhF;AAKD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,KAAK,EAAE,MAAM,GAAG,SAAS,GACxB;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAOpC"}
@@ -0,0 +1,42 @@
1
+ const TITLE_KINDS = [
2
+ [/^acknowledg/i, 'acknowledgments'],
3
+ [
4
+ /^(data|code|software)\b[^.]{0,40}\bavailability|^availability of data|^data sharing/i,
5
+ 'data-availability',
6
+ ],
7
+ [
8
+ /^appendix|^appendices|^(supplementary|supplemental|supporting) (materials?|information)$/i,
9
+ 'appendix',
10
+ ],
11
+ // An ethics heading is a declaration only as a statement or approval: a trial protocol's "Ethics and dissemination" is main text.
12
+ [
13
+ /^(competing|conflicts? of) interests?|^declaration|declarations?$|^fund(ing|er)|^author contributions|authorship contribution|^ethic(s|al) (statement|approval|declaration)|^ethics$|^compliance with ethic|^(informed )?consent (statement|for publication)|^institutional review board|^disclosure|^statements$/i,
14
+ 'declarations',
15
+ ],
16
+ [/^(foot|end)notes$|^notes$|^publisher[’']?s note/i, 'notes'],
17
+ ];
18
+ /** The back-matter kind a heading names, or undefined for an ordinary body section. */
19
+ export function kindFromTitle(title) {
20
+ if (!title)
21
+ return;
22
+ const plain = title.replace(/[*_\\]/g, '').trim();
23
+ return TITLE_KINDS.find(([pattern]) => pattern.test(plain))?.[1];
24
+ }
25
+ /** A printed section number at the start of a heading: `1`, `2.1`, `3.2.1.` — one or two digits a part, so `2019 Novel coronavirus` stays whole. */
26
+ const LEADING_NUMBER = /^((?:\d{1,2}\.)*\d{1,2})\.?\s+(?=\S)/;
27
+ /**
28
+ * A heading with its printed number moved into the label, for sources that write the
29
+ * number into the heading text (`1 Introduction`, `2.1. Methods`). A label the source
30
+ * gives separately is kept, and a matching number is removed from the title.
31
+ */
32
+ export function splitSectionNumber(title, label) {
33
+ const match = title === undefined ? null : LEADING_NUMBER.exec(title);
34
+ const number = match?.[1];
35
+ if (!title || !match || !number)
36
+ return { ...(label && { label }), ...(title && { title }) };
37
+ if (label && label.replace(/\.$/, '') !== number)
38
+ return { label, title };
39
+ const rest = title.slice(match[0].length);
40
+ return { label: label ?? number, ...(rest && { title: rest }) };
41
+ }
42
+ //# sourceMappingURL=section-kinds.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"section-kinds.js","sourceRoot":"","sources":["../../src/model/section-kinds.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,GAA4B;IAC3C,CAAC,cAAc,EAAE,iBAAiB,CAAC;IACnC;QACE,sFAAsF;QACtF,mBAAmB;KACpB;IACD;QACE,2FAA2F;QAC3F,UAAU;KACX;IACD,kIAAkI;IAClI;QACE,oTAAoT;QACpT,cAAc;KACf;IACD,CAAC,kDAAkD,EAAE,OAAO,CAAC;CAC9D,CAAC;AAEF,uFAAuF;AACvF,MAAM,UAAU,aAAa,CAAC,KAAyB;IACrD,IAAI,CAAC,KAAK;QAAE,OAAO;IACnB,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAClD,OAAO,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AACnE,CAAC;AAED,oJAAoJ;AACpJ,MAAM,cAAc,GAAG,sCAAsC,CAAC;AAE9D;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAyB,EACzB,KAAyB;IAEzB,MAAM,KAAK,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACtE,MAAM,MAAM,GAAG,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;IAC1B,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;IAC7F,IAAI,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,MAAM;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IAC1E,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAC1C,OAAO,EAAE,KAAK,EAAE,KAAK,IAAI,MAAM,EAAE,GAAG,CAAC,IAAI,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AAClE,CAAC"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @fileoverview Source table rows → a rectangular grid of cell text with `colspan` and
3
+ * `rowspan` expanded. Every format's table reader maps its markup into `SourceRow`s and
4
+ * lets this build the grid, so span handling is written once.
5
+ *
6
+ * A cell covering N columns fills N entries and one covering M rows fills its column in
7
+ * the M−1 rows below, repeating its text: the value belongs to each position it covers,
8
+ * and keeping the source cell count would put values under the wrong headers.
9
+ * (pubmed-mcp-server#111)
10
+ * @module src/model/table-grid
11
+ */
12
+ /**
13
+ * Widest grid a row may occupy, and the ceiling every declared span is clamped to. The
14
+ * widest real row observed is 15 columns; the cap turns `colspan="99999999"` into a
15
+ * bounded row instead of an unbounded allocation.
16
+ */
17
+ export declare const MAX_TABLE_COLUMNS = 512;
18
+ export interface SourceCell {
19
+ colspan: number;
20
+ header: boolean;
21
+ rowspan: number;
22
+ text: string;
23
+ }
24
+ export interface SourceRow {
25
+ cells: SourceCell[];
26
+ /** The row sits in the table head (`<thead>`). */
27
+ inHead: boolean;
28
+ }
29
+ /** A span attribute value, clamped to the grid; anything unparseable is 1. */
30
+ export declare function spanValue(raw: string | null | undefined): number;
31
+ /**
32
+ * Build the grid. Header rows are head rows plus any leading row made only of header
33
+ * cells; a row the source left short is padded so cells never shift under the wrong
34
+ * column.
35
+ */
36
+ export declare function buildGrid(sourceRows: SourceRow[]): {
37
+ headerRows: number;
38
+ rows: string[][];
39
+ };
40
+ //# sourceMappingURL=table-grid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table-grid.d.ts","sourceRoot":"","sources":["../../src/model/table-grid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,MAAM,CAAC;AAErC,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,UAAU,EAAE,CAAC;IACpB,kDAAkD;IAClD,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,8EAA8E;AAC9E,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAIhE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAA;CAAE,CAyC3F"}
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @fileoverview Source table rows → a rectangular grid of cell text with `colspan` and
3
+ * `rowspan` expanded. Every format's table reader maps its markup into `SourceRow`s and
4
+ * lets this build the grid, so span handling is written once.
5
+ *
6
+ * A cell covering N columns fills N entries and one covering M rows fills its column in
7
+ * the M−1 rows below, repeating its text: the value belongs to each position it covers,
8
+ * and keeping the source cell count would put values under the wrong headers.
9
+ * (pubmed-mcp-server#111)
10
+ * @module src/model/table-grid
11
+ */
12
+ import { largest } from './extremes.js';
13
+ /**
14
+ * Widest grid a row may occupy, and the ceiling every declared span is clamped to. The
15
+ * widest real row observed is 15 columns; the cap turns `colspan="99999999"` into a
16
+ * bounded row instead of an unbounded allocation.
17
+ */
18
+ export const MAX_TABLE_COLUMNS = 512;
19
+ /** A span attribute value, clamped to the grid; anything unparseable is 1. */
20
+ export function spanValue(raw) {
21
+ const declared = Number.parseInt(raw ?? '', 10);
22
+ if (!Number.isFinite(declared) || declared < 1)
23
+ return 1;
24
+ return Math.min(declared, MAX_TABLE_COLUMNS);
25
+ }
26
+ /**
27
+ * Build the grid. Header rows are head rows plus any leading row made only of header
28
+ * cells; a row the source left short is padded so cells never shift under the wrong
29
+ * column.
30
+ */
31
+ export function buildGrid(sourceRows) {
32
+ const parsed = [];
33
+ const carried = [];
34
+ const drainCarried = (row, col) => {
35
+ let at = col;
36
+ while (at < MAX_TABLE_COLUMNS) {
37
+ const carry = carried[at];
38
+ if (!carry)
39
+ return at;
40
+ row[at] = carry.value;
41
+ carry.remaining -= 1;
42
+ if (carry.remaining <= 0)
43
+ carried[at] = undefined;
44
+ at += 1;
45
+ }
46
+ return at;
47
+ };
48
+ for (const source of sourceRows) {
49
+ const row = [];
50
+ let col = 0;
51
+ for (const cell of source.cells) {
52
+ col = drainCarried(row, col);
53
+ const width = Math.min(cell.colspan, MAX_TABLE_COLUMNS - col);
54
+ for (let i = 0; i < width; i++) {
55
+ row[col] = cell.text;
56
+ if (cell.rowspan > 1)
57
+ carried[col] = { remaining: cell.rowspan - 1, value: cell.text };
58
+ col += 1;
59
+ }
60
+ }
61
+ const rightmost = carried.findLastIndex(Boolean);
62
+ while (col <= rightmost)
63
+ col = carried[col] ? drainCarried(row, col) : col + 1;
64
+ if (source.cells.length === 0)
65
+ continue;
66
+ for (let i = 0; i < row.length; i++)
67
+ row[i] ??= '';
68
+ parsed.push({ cells: row, header: source.inHead || source.cells.every((c) => c.header) });
69
+ }
70
+ const width = Math.max(0, largest(parsed.map((row) => row.cells.length)));
71
+ for (const row of parsed)
72
+ while (row.cells.length < width)
73
+ row.cells.push('');
74
+ let headerRows = 0;
75
+ while (parsed[headerRows]?.header)
76
+ headerRows++;
77
+ return { headerRows, rows: parsed.map((row) => row.cells) };
78
+ }
79
+ //# sourceMappingURL=table-grid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table-grid.js","sourceRoot":"","sources":["../../src/model/table-grid.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,18 @@
1
+ import type { SourceFormat } from './model/document.js';
2
+ import { type ParseOptions, type ParseResult } from './model/result.js';
3
+ /** Options for {@link parse}. Each applies to the formats that read it. */
4
+ export interface AutoParseOptions extends ParseOptions {
5
+ /** Where the document was retrieved, so relative links and images resolve (HTML, LaTeXML). */
6
+ baseUrl?: string;
7
+ /** The format, when the caller knows it: skips detection. */
8
+ format?: SourceFormat;
9
+ /** Read at most this many pages (PDF). */
10
+ maxPages?: number;
11
+ }
12
+ /**
13
+ * Parse a document of any supported format. The format comes from the payload itself
14
+ * (see `detect`); a challenge or access-denied page is HTML and fails as `blocked`.
15
+ * PDF needs its bytes: PDF passed as a string fails as `wrong-format`.
16
+ */
17
+ export declare function parse(input: string | Uint8Array, options?: AutoParseOptions): Promise<ParseResult>;
18
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAU,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhF,2EAA2E;AAC3E,MAAM,WAAW,gBAAiB,SAAQ,YAAY;IACpD,8FAA8F;IAC9F,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAsB,KAAK,CACzB,KAAK,EAAE,MAAM,GAAG,UAAU,EAC1B,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,WAAW,CAAC,CA6BtB"}
package/dist/parse.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @fileoverview `parse`: detect a payload's format and hand it to that format's parser.
3
+ * Parsers load on first use, so importing the package root never loads an HTML or PDF
4
+ * engine a caller does not need.
5
+ * @module src/parse
6
+ */
7
+ import { detect } from './detect.js';
8
+ import { failed } from './model/result.js';
9
+ /**
10
+ * Parse a document of any supported format. The format comes from the payload itself
11
+ * (see `detect`); a challenge or access-denied page is HTML and fails as `blocked`.
12
+ * PDF needs its bytes: PDF passed as a string fails as `wrong-format`.
13
+ */
14
+ export async function parse(input, options = {}) {
15
+ const { baseUrl, format = detect(input), maxInputBytes, maxPages } = options;
16
+ const budget = maxInputBytes === undefined ? {} : { maxInputBytes };
17
+ const located = baseUrl === undefined ? budget : { ...budget, baseUrl };
18
+ switch (format) {
19
+ case 'jats':
20
+ return (await import('./formats/jats/index.js')).parseJats(input, budget);
21
+ case 'tei':
22
+ return (await import('./formats/tei/index.js')).parseTei(input, budget);
23
+ case 'latexml':
24
+ return (await import('./formats/latexml/index.js')).parseLatexml(input, located);
25
+ case 'html':
26
+ return (await import('./formats/html/index.js')).parseHtml(input, located);
27
+ case 'pdf':
28
+ if (typeof input === 'string')
29
+ return failed('wrong-format', 'PDF must be passed as bytes: text decoding loses its binary content');
30
+ return (await import('./formats/pdf/index.js')).parsePdf(input, maxPages === undefined ? budget : { ...budget, maxPages });
31
+ case undefined:
32
+ return failed('wrong-format', 'Not a format this package reads: no PDF header, XML root element, or HTML markup');
33
+ }
34
+ }
35
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,MAAM,EAAuC,MAAM,mBAAmB,CAAC;AAYhF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CACzB,KAA0B,EAC1B,OAAO,GAAqB,EAAE;IAE9B,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC7E,MAAM,MAAM,GAAG,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC;IACpE,MAAM,OAAO,GAAG,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,CAAC;IACxE,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,MAAM;YACT,OAAO,CAAC,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC5E,KAAK,KAAK;YACR,OAAO,CAAC,MAAM,MAAM,CAAC,wBAAwB,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC1E,KAAK,SAAS;YACZ,OAAO,CAAC,MAAM,MAAM,CAAC,4BAA4B,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnF,KAAK,MAAM;YACT,OAAO,CAAC,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC7E,KAAK,KAAK;YACR,IAAI,OAAO,KAAK,KAAK,QAAQ;gBAC3B,OAAO,MAAM,CACX,cAAc,EACd,qEAAqE,CACtE,CAAC;YACJ,OAAO,CAAC,MAAM,MAAM,CAAC,wBAAwB,CAAC,CAAC,CAAC,QAAQ,CACtD,KAAK,EACL,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,CAC1D,CAAC;QACJ,KAAK,SAAS;YACZ,OAAO,MAAM,CACX,cAAc,EACd,kFAAkF,CACnF,CAAC;IACN,CAAC;AACH,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @fileoverview Escaping for text that came from a source document and is about to
3
+ * become inline Markdown. The goal is text that reads naturally and cannot turn into
4
+ * markup it never was: emphasis, links, math, strikethrough, or live HTML.
5
+ *
6
+ * Escaping is minimal on purpose. A backslash before every punctuation mark is valid
7
+ * Markdown but unreadable to a person or a model, so each rule escapes only where the
8
+ * character could start a construct.
9
+ * @module src/render/escape
10
+ */
11
+ /**
12
+ * Escape source text for use inside inline Markdown.
13
+ *
14
+ * - `\` before punctuation, `*`, `` ` ``, `$` (math delimiter), `~` (GFM strikethrough) — always.
15
+ * - `_` — only where it could delimit emphasis, so `snake_case` and `H_2` stay readable.
16
+ * - `](` — the sequence that would turn bracketed text into a link.
17
+ * - `<` — only before a letter, `/`, `!`, or `?`, where it could open an HTML tag.
18
+ * - `&` — only where it would read as an entity reference.
19
+ *
20
+ * Soft hyphens are dropped: they only mark where a word may break.
21
+ */
22
+ export declare function escapeInline(text: string): string;
23
+ /**
24
+ * Escape a character sequence that would change meaning at the start of a block:
25
+ * an ATX heading, a quote, a list marker, a thematic break, a table row, or a link
26
+ * reference definition.
27
+ */
28
+ export declare function escapeBlockStart(text: string): string;
29
+ /**
30
+ * Keep TeX from reading as markup where a Markdown renderer without math support treats
31
+ * `$…$` as ordinary text: a `<` that could open an HTML tag and a `](` that could open a
32
+ * link get a space between them, which math mode ignores.
33
+ */
34
+ export declare function escapeTex(tex: string): string;
35
+ /** Escape a value for a GFM table cell: pipes and line breaks would break the row. */
36
+ export declare function escapeTableCell(text: string): string;
37
+ /**
38
+ * Each whitespace run holding a line break folded to one space, in one pass: `\s*\n\s*`
39
+ * rescans a long run of spaces from every position in it.
40
+ */
41
+ export declare function foldLineBreaks(text: string): string;
42
+ /** The shortest backtick fence that no run of backticks inside `text` can close. */
43
+ export declare function codeFence(text: string, minimum?: number): string;
44
+ /** Wrap text as an inline code span that no backtick inside it can close. */
45
+ export declare function codeSpan(text: string): string;
46
+ /** True when `url` is safe to emit as a Markdown link target. */
47
+ export declare function isSafeUrl(url: string): boolean;
48
+ /**
49
+ * Escape a URL for use as a Markdown link destination: whitespace, angle brackets, and
50
+ * parentheses (an unbalanced one ends the destination) are percent-encoded.
51
+ */
52
+ export declare function escapeUrl(url: string): string;
53
+ //# sourceMappingURL=escape.d.ts.map