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,406 @@
1
+ import { escapeInline } from '../../render/escape.js';
2
+ import { inlineMath, joinAdjacentMath } from '../../render/inline.js';
3
+ import { attrOf, childrenOf, collapseWhitespace, findAll, findAllDescendants, findOne, isTextNode, tagNameOf, textOf, } from '../../xml/ordered.js';
4
+ import { formulaTex, rawText, text } from './text.js';
5
+ /** `pub-id-type` → the label a rendered citation shows before the identifier. */
6
+ const PUB_ID_LABELS = new Map(Object.entries({
7
+ arxiv: 'arXiv',
8
+ doi: 'DOI',
9
+ isbn: 'ISBN',
10
+ medline: 'PMID',
11
+ pmcid: 'PMCID',
12
+ pmid: 'PMID',
13
+ }));
14
+ /** Publisher-internal identifiers: nothing a reader can look up, so a rendered element-citation leaves them out. */
15
+ const INTERNAL_PUB_IDS = new Set(['pii', 'publisher-id']);
16
+ /**
17
+ * Europe PMC's generated Associated Data section: a digest of the article's data
18
+ * citations, supplementary files, and data availability statement, each already present
19
+ * where the article puts it.
20
+ */
21
+ export function isAssociatedData(node) {
22
+ return tagNameOf(node) === 'sec' && attrOf(node, 'sec-type') === 'associated-data';
23
+ }
24
+ /** Every `<ref-list>` under `node`, outside an Associated Data digest. */
25
+ function refLists(node) {
26
+ return childrenOf(node).flatMap((child) => {
27
+ if (isAssociatedData(child))
28
+ return [];
29
+ const nested = refLists(child);
30
+ return tagNameOf(child) === 'ref-list' ? [child, ...nested] : nested;
31
+ });
32
+ }
33
+ /**
34
+ * Every reference under `root`, in document order. `<ref-list>` placement varies —
35
+ * roughly 60% of Europe PMC deposits nest it under `body/sec/sec` — so every one is
36
+ * collected, and a `<ref>` ID already seen is skipped. (pubmed-mcp-server#116)
37
+ */
38
+ export function extractReferences(root) {
39
+ const results = [];
40
+ const seen = new Set();
41
+ for (const refList of refLists(root)) {
42
+ for (const ref of findAll(refList, 'ref')) {
43
+ const id = attrOf(ref, 'id');
44
+ if (id && seen.has(id))
45
+ continue;
46
+ const reference = parseReference(ref);
47
+ if (!reference)
48
+ continue;
49
+ if (id)
50
+ seen.add(id);
51
+ results.push(reference);
52
+ }
53
+ }
54
+ return results;
55
+ }
56
+ /**
57
+ * One `<ref>`. The readable `<mixed-citation>` is preferred for the text, descending into
58
+ * `<citation-alternatives>` when the forms sit there (#66); the structured
59
+ * `<element-citation>` supplies fields when present.
60
+ */
61
+ function parseReference(ref) {
62
+ const container = findOne(ref, 'citation-alternatives') ?? ref;
63
+ const mixed = findOne(container, 'mixed-citation');
64
+ const element = findOne(container, 'element-citation') ?? findOne(container, 'nlm-citation');
65
+ const citation = citationMarkdown(mixed ?? element);
66
+ if (!citation)
67
+ return;
68
+ const fields = citationFields(element ?? mixed);
69
+ const id = attrOf(ref, 'id');
70
+ // `[12]` and `12.` are both printed forms of the label `12`.
71
+ const label = text(findOne(ref, 'label'))
72
+ .replace(/^\[|\]$/g, '')
73
+ .replace(/\.$/, '') || undefined;
74
+ return {
75
+ ...(id && { id }),
76
+ ...(label && { label }),
77
+ text: citation,
78
+ ...fields,
79
+ };
80
+ }
81
+ /**
82
+ * A citation element — `<mixed-citation>`, `<element-citation>`, `<nlm-citation>` — as
83
+ * inline Markdown: its text escaped, its formulas inline math. Empty when it has no text.
84
+ */
85
+ export function citationMarkdown(node) {
86
+ if (!node)
87
+ return '';
88
+ const formulas = [];
89
+ const plain = tagNameOf(node) === 'mixed-citation'
90
+ ? renderMixedCitation(node, formulas)
91
+ : renderElementCitation(node, formulas);
92
+ let markdown = escapeInline(plain);
93
+ for (const [index, tex] of formulas.entries()) {
94
+ markdown = markdown.split(formulaMark(index)).join(inlineMath(tex));
95
+ }
96
+ return joinAdjacentMath(markdown);
97
+ }
98
+ /** Formula tags a citation can hold. */
99
+ const FORMULA_TAGS = new Set(['inline-formula', 'disp-formula']);
100
+ /**
101
+ * Where a citation's formula `index` sits in its text until escaping is done. U+0001
102
+ * cannot occur in XML 1.0 text, and escaping leaves it alone, so the TeX is restored
103
+ * as math afterwards rather than escaped as text.
104
+ */
105
+ function formulaMark(index) {
106
+ return `\u0001${index}\u0001`;
107
+ }
108
+ /** True when a formula sits anywhere below `node`. */
109
+ function holdsFormula(node) {
110
+ return childrenOf(node).some((child) => FORMULA_TAGS.has(tagNameOf(child) ?? '') || holdsFormula(child));
111
+ }
112
+ /**
113
+ * Text of a node inside a citation, as {@link rawText} reads it, except that each
114
+ * formula with TeX stands as a {@link formulaMark} and its TeX is added to `formulas`.
115
+ */
116
+ function citationText(node, formulas) {
117
+ const tag = tagNameOf(node) ?? '';
118
+ if (FORMULA_TAGS.has(tag)) {
119
+ const tex = formulaTex(childrenOf(node).filter((child) => tagNameOf(child) !== 'label'));
120
+ if (!tex)
121
+ return rawText(node);
122
+ formulas.push(tex);
123
+ return formulaMark(formulas.length - 1);
124
+ }
125
+ if (!holdsFormula(node))
126
+ return rawText(node);
127
+ return childrenOf(node)
128
+ .map((child) => citationText(child, formulas))
129
+ .join('');
130
+ }
131
+ /**
132
+ * `ext-link-type`s whose target is an identifier rather than a page: Europe PMC adds
133
+ * empty `<ext-link ext-link-type="pmid" xlink:href="26023781"/>`s to a citation.
134
+ */
135
+ const ID_LINK_TYPES = new Map(Object.entries({ doi: 'doi', pmcid: 'pmcid', pmid: 'pmid', pubmed: 'pmid' }));
136
+ /** A link target a reader can open. */
137
+ const WEB_URL = /^(?:https?|ftp):\/\//i;
138
+ /** Structured fields from a citation element. */
139
+ function citationFields(citation) {
140
+ if (!citation)
141
+ return {};
142
+ const ids = new Map();
143
+ for (const pubId of findAllDescendants(citation, 'pub-id')) {
144
+ const type = attrOf(pubId, 'pub-id-type')?.toLowerCase();
145
+ const value = text(pubId);
146
+ if (type && value && !ids.has(type))
147
+ ids.set(type, value);
148
+ }
149
+ const links = [
150
+ ...findAllDescendants(citation, 'ext-link'),
151
+ ...findAllDescendants(citation, 'uri'),
152
+ ].map((link) => ({
153
+ type: ID_LINK_TYPES.get(attrOf(link, 'ext-link-type')?.toLowerCase() ?? ''),
154
+ target: attrOf(link, 'xlink:href') ?? text(link),
155
+ }));
156
+ for (const { type, target } of links) {
157
+ if (type && target && !ids.has(type))
158
+ ids.set(type, type === 'doi' ? (doiFromUrl(target) ?? target) : target);
159
+ }
160
+ const url = links.find(({ type, target }) => !type && WEB_URL.test(target))?.target;
161
+ const doi = ids.get('doi') ?? doiFromUrl(url);
162
+ const authors = findAllDescendants(citation, 'person-group')
163
+ .filter((group) => (attrOf(group, 'person-group-type') ?? 'author') === 'author')
164
+ .flatMap((group) => childrenOf(group).map(renderName).filter(Boolean));
165
+ const title = text(findOne(citation, 'article-title')) || text(findOne(citation, 'chapter-title'));
166
+ const source = text(findOne(citation, 'source'));
167
+ const year = text(findOne(citation, 'year'));
168
+ const pmcid = ids.get('pmcid') ?? ids.get('pmc');
169
+ const pmid = ids.get('pmid');
170
+ const arxiv = ids.get('arxiv');
171
+ return {
172
+ ...(authors.length > 0 && { authors }),
173
+ ...(title && { title }),
174
+ ...(source && { source }),
175
+ ...(year && { year }),
176
+ ...(doi && { doi: doi.toLowerCase() }),
177
+ ...(pmid && { pmid }),
178
+ ...(pmcid && { pmcid: pmcid.startsWith('PMC') ? pmcid : `PMC${pmcid}` }),
179
+ ...(arxiv && { arxiv: arxiv.replace(/^arxiv:/i, '') }),
180
+ ...(url && !doi && { url }),
181
+ };
182
+ }
183
+ function doiFromUrl(url) {
184
+ return url ? /doi\.org\/(10\.\d{4,9}\/\S+)/i.exec(url)?.[1] : undefined;
185
+ }
186
+ /** A `<person-group>` child — `<name>`, `<string-name>`, `<collab>`, `<etal>` — as text. */
187
+ function renderName(node) {
188
+ switch (tagNameOf(node)) {
189
+ case 'name':
190
+ case 'string-name': {
191
+ const parts = [text(findOne(node, 'surname')), text(findOne(node, 'given-names'))].filter(Boolean);
192
+ return parts.join(' ') || text(node);
193
+ }
194
+ case 'collab':
195
+ return text(node);
196
+ case 'etal':
197
+ return 'et al.';
198
+ default:
199
+ return '';
200
+ }
201
+ }
202
+ /** Element-citation fields placed by `renderElementCitation`; any other field follows them in source order. */
203
+ const PLACED_FIELDS = new Set([
204
+ 'person-group',
205
+ 'name',
206
+ 'string-name',
207
+ 'collab',
208
+ 'etal',
209
+ 'article-title',
210
+ 'chapter-title',
211
+ 'part-title',
212
+ 'data-title',
213
+ 'source',
214
+ 'edition',
215
+ 'publisher-loc',
216
+ 'publisher-name',
217
+ 'year',
218
+ 'month',
219
+ 'day',
220
+ 'volume',
221
+ 'issue',
222
+ 'fpage',
223
+ 'lpage',
224
+ 'page-range',
225
+ 'elocation-id',
226
+ 'pub-id',
227
+ ]);
228
+ /** `text` closed with a full stop unless it already ends a sentence or is a URL. */
229
+ function sentence(text) {
230
+ return /[.?!]$|:\/\/\S+$/.test(text) ? text : `${text}.`;
231
+ }
232
+ /**
233
+ * An `<element-citation>` as text. Its children carry no punctuation, and a flat read
234
+ * runs every field together (`DomanJ.L.…Cell18618…`), so the fields are laid out in the
235
+ * order and punctuation PMC prints citations with, whatever order the source lists them
236
+ * in: `Authors. Title. In: Editors, editors. Source. Edition. Place: Publisher; 2023;186(18):3983–4002.`
237
+ * Other fields follow as written, then the identifiers, labeled and each shown once.
238
+ * A citation with text of its own between the fields is read as written. (#69)
239
+ */
240
+ function renderElementCitation(node, formulas) {
241
+ const children = childrenOf(node);
242
+ if (children.some((child) => isTextNode(child) && textOf(child).trim()))
243
+ return renderMixedCitation(node, formulas);
244
+ const fields = children.filter((child) => !isTextNode(child));
245
+ const fieldText = (child) => collapseWhitespace(citationText(child, formulas));
246
+ const field = (tag) => {
247
+ const found = fields.find((child) => tagNameOf(child) === tag);
248
+ return found ? fieldText(found) : '';
249
+ };
250
+ const isEditors = (group) => attrOf(group, 'person-group-type') === 'editor';
251
+ // Every name outside an editor group — authors, and translators or compilers — leads.
252
+ const names = (editors) => fields
253
+ .flatMap((child) => tagNameOf(child) === 'person-group'
254
+ ? isEditors(child) === editors
255
+ ? childrenOf(child)
256
+ : []
257
+ : editors
258
+ ? []
259
+ : [child])
260
+ .map(renderName)
261
+ .filter(Boolean)
262
+ .join(', ');
263
+ const editors = names(true);
264
+ const place = field('publisher-loc');
265
+ const publisher = field('publisher-name');
266
+ const date = [field('year'), field('month'), field('day')].filter(Boolean).join(' ');
267
+ const volume = [field('volume'), field('issue') && `(${field('issue')})`].join('');
268
+ const fpage = field('fpage');
269
+ const lpage = field('lpage');
270
+ const pages = (fpage && lpage ? `${fpage}–${lpage}` : fpage) || field('page-range') || field('elocation-id');
271
+ let locator = date;
272
+ if (volume)
273
+ locator = locator ? `${locator};${volume}` : volume;
274
+ if (pages)
275
+ locator = locator ? `${locator}${volume ? ':' : ', '}${pages}` : pages;
276
+ const parts = [
277
+ names(false),
278
+ field('article-title') || field('chapter-title') || field('part-title') || field('data-title'),
279
+ editors && `In: ${editors}, editors`,
280
+ field('source'),
281
+ field('edition'),
282
+ [publisher && (place ? `${place}: ${publisher}` : publisher), locator]
283
+ .filter(Boolean)
284
+ .join('; '),
285
+ ...fields.filter((child) => !PLACED_FIELDS.has(tagNameOf(child) ?? '')).map(fieldText),
286
+ ]
287
+ .filter(Boolean)
288
+ .map(sentence);
289
+ const shown = new Set();
290
+ for (const pubId of fields.filter((child) => tagNameOf(child) === 'pub-id')) {
291
+ const id = text(pubId);
292
+ const type = attrOf(pubId, 'pub-id-type') ?? '';
293
+ if (!id || shown.has(id) || INTERNAL_PUB_IDS.has(type))
294
+ continue;
295
+ shown.add(id);
296
+ const label = PUB_ID_LABELS.get(type);
297
+ parts.push(label ? `${label} ${id}` : id);
298
+ }
299
+ return parts.join(' ');
300
+ }
301
+ /** Wrappers publishers put around a whole citation (Europe PMC's `citation-string`), read with the same spacing rules. */
302
+ const CITATION_WRAPPER_TAGS = new Set(['named-content', 'styled-content']);
303
+ /** Name wrappers whose parts often sit adjacent with nothing between them. (#124) */
304
+ const NAME_WRAPPER_TAGS = new Set(['name', 'string-name', 'person-group']);
305
+ /**
306
+ * A name wrapper's text: one space between two adjacent elements with nothing between
307
+ * them, source text verbatim otherwise, so existing punctuation is never doubled.
308
+ */
309
+ function renderNameWrapper(node) {
310
+ let rendered = '';
311
+ let prevWasElement = false;
312
+ for (const child of childrenOf(node)) {
313
+ if (isTextNode(child)) {
314
+ const raw = textOf(child);
315
+ if (raw) {
316
+ rendered += raw;
317
+ prevWasElement = false;
318
+ }
319
+ continue;
320
+ }
321
+ const tag = tagNameOf(child) ?? '';
322
+ const part = NAME_WRAPPER_TAGS.has(tag) ? renderNameWrapper(child) : rawText(child);
323
+ if (!part)
324
+ continue;
325
+ if (prevWasElement)
326
+ rendered += ' ';
327
+ rendered += part;
328
+ prevWasElement = true;
329
+ }
330
+ return rendered;
331
+ }
332
+ const PUB_ID_TAGS = new Set(['pub-id']);
333
+ /**
334
+ * True when `text` prints the identifier `value` as a whole token, not as part of a
335
+ * longer one. A value of digits alone never counts: it could as well be a page, a
336
+ * volume, or a year.
337
+ */
338
+ function printsIdentifier(text, value) {
339
+ if (/^\d+$/.test(value))
340
+ return false;
341
+ const pattern = value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
342
+ return new RegExp(`(?<![\\w.-])${pattern}(?![\\w/-]|\\.\\w)`, 'i').test(text);
343
+ }
344
+ /** True when the text so far already ends with a prefix naming this ID type (`doi:`). (#115) */
345
+ function hasLiteralIdPrefix(rendered, pubIdType) {
346
+ const tail = rendered.trimEnd().replace(/[:.]$/, '').trimEnd();
347
+ return tail.toLowerCase().endsWith(pubIdType.toLowerCase());
348
+ }
349
+ /**
350
+ * A `<mixed-citation>` as text. Its punctuation lives in the text between elements, so
351
+ * the source reads correctly almost everywhere; three zero-gap adjacencies do not —
352
+ * consecutive `<pub-id>`s (#115), an inline title against the volume after it (#123),
353
+ * and surname against given names (#124). Adjacent elements with nothing between them
354
+ * get one space. A typed identifier gets a label unless the text already names it, and
355
+ * is left out when the text already prints it (PMC adds a `<pub-id>` for a DOI the
356
+ * citation spells out).
357
+ */
358
+ function renderMixedCitation(node, formulas) {
359
+ const printed = rawText(node, PUB_ID_TAGS);
360
+ let rendered = '';
361
+ let prevWasElement = false;
362
+ for (const child of childrenOf(node)) {
363
+ if (isTextNode(child)) {
364
+ const raw = textOf(child);
365
+ if (raw) {
366
+ rendered += raw;
367
+ prevWasElement = false;
368
+ }
369
+ continue;
370
+ }
371
+ const tag = tagNameOf(child) ?? '';
372
+ let part;
373
+ let labeled = false;
374
+ if (tag === 'pub-id') {
375
+ const value = text(child);
376
+ if (!value || printsIdentifier(printed, value))
377
+ continue;
378
+ const type = attrOf(child, 'pub-id-type') ?? '';
379
+ const label = PUB_ID_LABELS.get(type);
380
+ if (label && !hasLiteralIdPrefix(rendered, type) && !hasLiteralIdPrefix(rendered, label)) {
381
+ labeled = true;
382
+ part = `${label} ${value}`;
383
+ }
384
+ else {
385
+ part = value;
386
+ }
387
+ }
388
+ else if (NAME_WRAPPER_TAGS.has(tag)) {
389
+ part = renderNameWrapper(child);
390
+ }
391
+ else if (CITATION_WRAPPER_TAGS.has(tag)) {
392
+ part = renderMixedCitation(child, formulas);
393
+ }
394
+ else {
395
+ part = citationText(child, formulas);
396
+ }
397
+ if (prevWasElement || labeled)
398
+ rendered += ' ';
399
+ rendered += part;
400
+ prevWasElement = true;
401
+ }
402
+ // A name part that renders empty (a given name holding only a soft hyphen) or markup
403
+ // whitespace before a comma leaves "Colaneri A. , Staffa N."; the comma closes up.
404
+ return collapseWhitespace(rendered).replace(/\s+(?=[,;])/g, '');
405
+ }
406
+ //# sourceMappingURL=references.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"references.js","sourceRoot":"","sources":["../../../src/formats/jats/references.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACtE,OAAO,EACL,MAAM,EACN,UAAU,EACV,kBAAkB,EAClB,OAAO,EACP,kBAAkB,EAClB,OAAO,EACP,UAAU,EACV,SAAS,EACT,MAAM,GAEP,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtD,iFAAiF;AACjF,MAAM,aAAa,GAAgC,IAAI,GAAG,CACxD,MAAM,CAAC,OAAO,CAAC;IACb,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,KAAK;IACV,IAAI,EAA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AC;gBACN,IAAI,GAAG,KAAK,CAAC;YACf,CAAC;QACH,CAAC;aAAM,IAAI,iBAAiB,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACtC,IAAI,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC;aAAM,IAAI,qBAAqB,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC1C,IAAI,GAAG,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC9C,CAAC;aAAM,CAAC;YACN,IAAI,GAAG,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QACvC,CAAC;QACD,IAAI,cAAc,IAAI,OAAO;YAAE,QAAQ,IAAI,GAAG,CAAC;QAC/C,QAAQ,IAAI,IAAI,CAAC;QACjB,cAAc,GAAG,IAAI,CAAC;IACxB,CAAC;IACD,qFAAqF;IACrF,mFAAmF;IACnF,OAAO,kBAAkB,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;AAClE,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @fileoverview `<table-wrap>` → `TableBlock`: rows of cell text by grid column, with
3
+ * `colspan` and `rowspan` expanded so a well-formed table comes back rectangular.
4
+ *
5
+ * Only XHTML `<table>` bodies are read. Across a 283-table survey of open-access
6
+ * records, 276 were XHTML, 7 graphic-only, and none used the CALS `<tgroup>` model, so
7
+ * graphic-only and CALS bodies return the label and caption with an `unextractable`
8
+ * reason rather than a second table model. (pubmed-mcp-server#111)
9
+ * @module src/formats/jats/tables
10
+ */
11
+ import type { TableBlock } from '../../model/document.js';
12
+ import { type XmlNode } from '../../xml/ordered.js';
13
+ import type { JatsContext } from './context.js';
14
+ /** Parse one `<table-wrap>`. `caption` is rendered by the caller's caption rule. */
15
+ export declare function parseTableWrap(tableWrap: XmlNode, caption: string | undefined, ctx: JatsContext): TableBlock;
16
+ /** A table with no wrapper (JATS `<array>`, or a bare `<table>`). */
17
+ export declare function parseBareTable(table: XmlNode, ctx: JatsContext): TableBlock;
18
+ //# sourceMappingURL=tables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../../../src/formats/jats/tables.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,UAAU,EAA4B,MAAM,yBAAyB,CAAC;AAGpF,OAAO,EAML,KAAK,OAAO,EACb,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAIhD,oFAAoF;AACpF,wBAAgB,cAAc,CAC5B,SAAS,EAAE,OAAO,EAClB,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,GAAG,EAAE,WAAW,GACf,UAAU,CA2BZ;AAED,qEAAqE;AACrE,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,WAAW,GAAG,UAAU,CAQ3E"}
@@ -0,0 +1,105 @@
1
+ import { buildGrid, spanValue } from '../../model/table-grid.js';
2
+ import { escapeInline } from '../../render/escape.js';
3
+ import { attrOf, childrenOf, findAll, findOne, tagNameOf, } from '../../xml/ordered.js';
4
+ import { inlineText } from './inline.js';
5
+ import { text } from './text.js';
6
+ /** Parse one `<table-wrap>`. `caption` is rendered by the caller's caption rule. */
7
+ export function parseTableWrap(tableWrap, caption, ctx) {
8
+ const id = attrOf(tableWrap, 'id');
9
+ const label = text(findOne(tableWrap, 'label')) || undefined;
10
+ const footnotes = tableFootnotes(findOne(tableWrap, 'table-wrap-foot'), ctx);
11
+ const table = findOne(tableWrap, 'table') ?? findOne(findOne(tableWrap, 'alternatives'), 'table');
12
+ const { headerRows, rows } = table ? readTable(table, ctx) : { headerRows: 0, rows: [] };
13
+ const block = {
14
+ type: 'table',
15
+ ...(id && { id }),
16
+ ...(label && { label }),
17
+ ...(caption && { caption }),
18
+ headerRows,
19
+ rows,
20
+ ...(footnotes.length > 0 && { footnotes }),
21
+ };
22
+ if (rows.length === 0) {
23
+ block.unextractable = classifyUnextractable(tableWrap, table);
24
+ // A label usually names the table itself ("Table 2"); a bare number does not.
25
+ const name = label && /^\p{L}/u.test(label) ? label : `Table ${label ?? id ?? ''}`.trim();
26
+ ctx.diag.warn('table-unextractable', `${name} has no readable rows (${block.unextractable})`, id);
27
+ }
28
+ return block;
29
+ }
30
+ /** A table with no wrapper (JATS `<array>`, or a bare `<table>`). */
31
+ export function parseBareTable(table, ctx) {
32
+ const { headerRows, rows } = readTable(table, ctx);
33
+ return {
34
+ type: 'table',
35
+ headerRows,
36
+ rows,
37
+ ...(rows.length === 0 && { unextractable: 'no-rows' }),
38
+ };
39
+ }
40
+ /** Each `<fn>` or `<p>` in a `<table-wrap-foot>`, with its label, as one footnote line. */
41
+ function tableFootnotes(foot, ctx) {
42
+ if (!foot)
43
+ return [];
44
+ const items = [];
45
+ const visit = (node) => {
46
+ for (const child of childrenOf(node)) {
47
+ const tag = tagNameOf(child);
48
+ if (tag === 'fn' || tag === 'p') {
49
+ const label = escapeInline(text(findOne(child, 'label')));
50
+ const body = inlineText(childrenOf(child).filter((c) => tagNameOf(c) !== 'label'), ctx);
51
+ const line = [label, body].filter(Boolean).join(' ');
52
+ if (line)
53
+ items.push(line);
54
+ }
55
+ else if (tag) {
56
+ visit(child);
57
+ }
58
+ }
59
+ };
60
+ visit(foot);
61
+ return items;
62
+ }
63
+ function classifyUnextractable(tableWrap, table) {
64
+ if (findOne(tableWrap, 'tgroup') || findOne(table, 'tgroup'))
65
+ return 'cals-tgroup';
66
+ if (findOne(tableWrap, 'graphic') || findOne(findOne(tableWrap, 'alternatives'), 'graphic')) {
67
+ return 'graphic-only';
68
+ }
69
+ return 'no-rows';
70
+ }
71
+ /**
72
+ * Read an XHTML `<table>` into the shared grid, spans expanded and short rows padded.
73
+ * Header rows are those in `<thead>`, plus any leading row of only `<th>` cells in a
74
+ * table without a `<thead>`.
75
+ */
76
+ function readTable(table, ctx) {
77
+ const sourceRow = (tr, inHead) => ({
78
+ cells: childrenOf(tr).flatMap((cell) => {
79
+ const tag = tagNameOf(cell);
80
+ if (tag !== 'td' && tag !== 'th')
81
+ return [];
82
+ return [
83
+ {
84
+ colspan: spanValue(attrOf(cell, 'colspan')),
85
+ header: tag === 'th',
86
+ rowspan: spanValue(attrOf(cell, 'rowspan')),
87
+ text: inlineText(cell, ctx),
88
+ },
89
+ ];
90
+ }),
91
+ inHead,
92
+ });
93
+ const rows = [];
94
+ for (const child of childrenOf(table)) {
95
+ const tag = tagNameOf(child);
96
+ if (tag === 'tr')
97
+ rows.push(sourceRow(child, false));
98
+ else if (tag === 'thead' || tag === 'tbody' || tag === 'tfoot') {
99
+ for (const tr of findAll(child, 'tr'))
100
+ rows.push(sourceRow(tr, tag === 'thead'));
101
+ }
102
+ }
103
+ return buildGrid(rows);
104
+ }
105
+ //# sourceMappingURL=tables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tables.js","sourceRoot":"","sources":["../../../src/formats/jats/tables.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,SAAS,EAAkB,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EACL,MAAM,EACN,UAAU,EACV,OAAO,EACP,OAAO,EACP,SAAS,GAEV,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,oFAAoF;AACpF,MAAM,UAAU,cAAc,CAC5B,SAAkB,EAClB,OAA2B,EAC3B,GAAgB;IAEhB,MAAM,EAAE,GAAG,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,IAAI,SAAS,CAAC;IAC7D,MAAM,SAAS,GAAG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I,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QACtC,MAAM,GAAG,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;QAC7B,IAAI,GAAG,KAAK,IAAI;YAAE,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;aAChD,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,OAAO,IAAI,GAAG,KAAK,OAAO,EAAE,CAAC;YAC/D,KAAK,MAAM,EAAE,IAAI,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC;gBAAE,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC;QACnF,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;AACzB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { type XmlNode, type XmlNodeList } from '../../xml/ordered.js';
2
+ /**
3
+ * Elements whose text is never content: index entries, alternative-text anchors, and
4
+ * an institution's registry ID beside the name it identifies.
5
+ */
6
+ export declare const SILENT_TAGS: ReadonlySet<string>;
7
+ /**
8
+ * The child of an `<alternatives>` whose text stands for the whole element: a
9
+ * `<tex-math>` with content first, then any non-pointer rendering with text, then a
10
+ * pointer's own alt text. Undefined when no child carries text under `excluded`.
11
+ */
12
+ export declare function selectAlternative(node: XmlNode, excluded?: ReadonlySet<string>): XmlNode | undefined;
13
+ /** Text in document order with source spacing intact — no collapsing, no trimming. */
14
+ export declare function rawText(input: XmlNode | XmlNodeList | undefined, excluded?: ReadonlySet<string>): string;
15
+ /** Text in document order, whitespace collapsed and trimmed. */
16
+ export declare function text(input: XmlNode | XmlNodeList | undefined, excluded?: ReadonlySet<string>): string;
17
+ /**
18
+ * A formula's TeX, from the children of its `<inline-formula>` or `<disp-formula>`: its
19
+ * `<tex-math>` (directly or under `<alternatives>`), else its MathML converted. Undefined
20
+ * when it has neither.
21
+ */
22
+ export declare function formulaTex(children: XmlNodeList): string | undefined;
23
+ //# sourceMappingURL=text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../src/formats/jats/text.ts"],"names":[],"mappings":"AAWA,OAAO,EAOL,KAAK,OAAO,EACZ,KAAK,WAAW,EACjB,MAAM,sBAAsB,CAAC;AAU9B;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,WAAW,CAAC,MAAM,CAK1C,CAAC;AAEH;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,OAAO,EACb,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAC7B,OAAO,GAAG,SAAS,CAErB;AAyCD,sFAAsF;AACtF,wBAAgB,OAAO,CACrB,KAAK,EAAE,OAAO,GAAG,WAAW,GAAG,SAAS,EACxC,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAC7B,MAAM,CAER;AAED,gEAAgE;AAChE,wBAAgB,IAAI,CAClB,KAAK,EAAE,OAAO,GAAG,WAAW,GAAG,SAAS,EACxC,QAAQ,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAC7B,MAAM,CAER;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,CAEpE"}