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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +118 -0
- package/changelog/0.1.x/0.1.0.md +17 -0
- package/changelog/template.md +150 -0
- package/dist/detect.d.ts +11 -0
- package/dist/detect.d.ts.map +1 -0
- package/dist/detect.js +51 -0
- package/dist/detect.js.map +1 -0
- package/dist/formats/html/blocks.d.ts +29 -0
- package/dist/formats/html/blocks.d.ts.map +1 -0
- package/dist/formats/html/blocks.js +527 -0
- package/dist/formats/html/blocks.js.map +1 -0
- package/dist/formats/html/context.d.ts +19 -0
- package/dist/formats/html/context.d.ts.map +1 -0
- package/dist/formats/html/context.js +122 -0
- package/dist/formats/html/context.js.map +1 -0
- package/dist/formats/html/index.d.ts +7 -0
- package/dist/formats/html/index.d.ts.map +1 -0
- package/dist/formats/html/index.js +7 -0
- package/dist/formats/html/index.js.map +1 -0
- package/dist/formats/html/inline.d.ts +26 -0
- package/dist/formats/html/inline.d.ts.map +1 -0
- package/dist/formats/html/inline.js +253 -0
- package/dist/formats/html/inline.js.map +1 -0
- package/dist/formats/html/metadata.d.ts +30 -0
- package/dist/formats/html/metadata.d.ts.map +1 -0
- package/dist/formats/html/metadata.js +212 -0
- package/dist/formats/html/metadata.js.map +1 -0
- package/dist/formats/html/parse.d.ts +17 -0
- package/dist/formats/html/parse.d.ts.map +1 -0
- package/dist/formats/html/parse.js +405 -0
- package/dist/formats/html/parse.js.map +1 -0
- package/dist/formats/html/references.d.ts +12 -0
- package/dist/formats/html/references.d.ts.map +1 -0
- package/dist/formats/html/references.js +123 -0
- package/dist/formats/html/references.js.map +1 -0
- package/dist/formats/jats/blocks.d.ts +52 -0
- package/dist/formats/jats/blocks.d.ts.map +1 -0
- package/dist/formats/jats/blocks.js +614 -0
- package/dist/formats/jats/blocks.js.map +1 -0
- package/dist/formats/jats/context.d.ts +17 -0
- package/dist/formats/jats/context.d.ts.map +1 -0
- package/dist/formats/jats/context.js +2 -0
- package/dist/formats/jats/context.js.map +1 -0
- package/dist/formats/jats/fragment.d.ts +7 -0
- package/dist/formats/jats/fragment.d.ts.map +1 -0
- package/dist/formats/jats/fragment.js +42 -0
- package/dist/formats/jats/fragment.js.map +1 -0
- package/dist/formats/jats/front.d.ts +25 -0
- package/dist/formats/jats/front.d.ts.map +1 -0
- package/dist/formats/jats/front.js +380 -0
- package/dist/formats/jats/front.js.map +1 -0
- package/dist/formats/jats/index.d.ts +8 -0
- package/dist/formats/jats/index.d.ts.map +1 -0
- package/dist/formats/jats/index.js +8 -0
- package/dist/formats/jats/index.js.map +1 -0
- package/dist/formats/jats/inline.d.ts +24 -0
- package/dist/formats/jats/inline.d.ts.map +1 -0
- package/dist/formats/jats/inline.js +274 -0
- package/dist/formats/jats/inline.js.map +1 -0
- package/dist/formats/jats/parse.d.ts +9 -0
- package/dist/formats/jats/parse.d.ts.map +1 -0
- package/dist/formats/jats/parse.js +247 -0
- package/dist/formats/jats/parse.js.map +1 -0
- package/dist/formats/jats/references.d.ts +25 -0
- package/dist/formats/jats/references.d.ts.map +1 -0
- package/dist/formats/jats/references.js +406 -0
- package/dist/formats/jats/references.js.map +1 -0
- package/dist/formats/jats/tables.d.ts +18 -0
- package/dist/formats/jats/tables.d.ts.map +1 -0
- package/dist/formats/jats/tables.js +105 -0
- package/dist/formats/jats/tables.js.map +1 -0
- package/dist/formats/jats/text.d.ts +23 -0
- package/dist/formats/jats/text.d.ts.map +1 -0
- package/dist/formats/jats/text.js +114 -0
- package/dist/formats/jats/text.js.map +1 -0
- package/dist/formats/latexml/blocks.d.ts +13 -0
- package/dist/formats/latexml/blocks.d.ts.map +1 -0
- package/dist/formats/latexml/blocks.js +454 -0
- package/dist/formats/latexml/blocks.js.map +1 -0
- package/dist/formats/latexml/context.d.ts +19 -0
- package/dist/formats/latexml/context.d.ts.map +1 -0
- package/dist/formats/latexml/context.js +18 -0
- package/dist/formats/latexml/context.js.map +1 -0
- package/dist/formats/latexml/index.d.ts +7 -0
- package/dist/formats/latexml/index.d.ts.map +1 -0
- package/dist/formats/latexml/index.js +7 -0
- package/dist/formats/latexml/index.js.map +1 -0
- package/dist/formats/latexml/inline.d.ts +16 -0
- package/dist/formats/latexml/inline.d.ts.map +1 -0
- package/dist/formats/latexml/inline.js +113 -0
- package/dist/formats/latexml/inline.js.map +1 -0
- package/dist/formats/latexml/parse.d.ts +16 -0
- package/dist/formats/latexml/parse.d.ts.map +1 -0
- package/dist/formats/latexml/parse.js +405 -0
- package/dist/formats/latexml/parse.js.map +1 -0
- package/dist/formats/pdf/blocks.d.ts +14 -0
- package/dist/formats/pdf/blocks.d.ts.map +1 -0
- package/dist/formats/pdf/blocks.js +203 -0
- package/dist/formats/pdf/blocks.js.map +1 -0
- package/dist/formats/pdf/index.d.ts +7 -0
- package/dist/formats/pdf/index.d.ts.map +1 -0
- package/dist/formats/pdf/index.js +7 -0
- package/dist/formats/pdf/index.js.map +1 -0
- package/dist/formats/pdf/inline.d.ts +8 -0
- package/dist/formats/pdf/inline.d.ts.map +1 -0
- package/dist/formats/pdf/inline.js +91 -0
- package/dist/formats/pdf/inline.js.map +1 -0
- package/dist/formats/pdf/layout.d.ts +33 -0
- package/dist/formats/pdf/layout.d.ts.map +1 -0
- package/dist/formats/pdf/layout.js +358 -0
- package/dist/formats/pdf/layout.js.map +1 -0
- package/dist/formats/pdf/lines.d.ts +40 -0
- package/dist/formats/pdf/lines.d.ts.map +1 -0
- package/dist/formats/pdf/lines.js +104 -0
- package/dist/formats/pdf/lines.js.map +1 -0
- package/dist/formats/pdf/load.d.ts +43 -0
- package/dist/formats/pdf/load.d.ts.map +1 -0
- package/dist/formats/pdf/load.js +126 -0
- package/dist/formats/pdf/load.js.map +1 -0
- package/dist/formats/pdf/parse.d.ts +9 -0
- package/dist/formats/pdf/parse.d.ts.map +1 -0
- package/dist/formats/pdf/parse.js +126 -0
- package/dist/formats/pdf/parse.js.map +1 -0
- package/dist/formats/pdf/references.d.ts +14 -0
- package/dist/formats/pdf/references.d.ts.map +1 -0
- package/dist/formats/pdf/references.js +104 -0
- package/dist/formats/pdf/references.js.map +1 -0
- package/dist/formats/pdf/structure.d.ts +29 -0
- package/dist/formats/pdf/structure.d.ts.map +1 -0
- package/dist/formats/pdf/structure.js +533 -0
- package/dist/formats/pdf/structure.js.map +1 -0
- package/dist/formats/tei/index.d.ts +7 -0
- package/dist/formats/tei/index.d.ts.map +1 -0
- package/dist/formats/tei/index.js +7 -0
- package/dist/formats/tei/index.js.map +1 -0
- package/dist/formats/tei/inline.d.ts +24 -0
- package/dist/formats/tei/inline.d.ts.map +1 -0
- package/dist/formats/tei/inline.js +107 -0
- package/dist/formats/tei/inline.js.map +1 -0
- package/dist/formats/tei/parse.d.ts +4 -0
- package/dist/formats/tei/parse.d.ts.map +1 -0
- package/dist/formats/tei/parse.js +560 -0
- package/dist/formats/tei/parse.js.map +1 -0
- package/dist/html/dom.d.ts +35 -0
- package/dist/html/dom.d.ts.map +1 -0
- package/dist/html/dom.js +70 -0
- package/dist/html/dom.js.map +1 -0
- package/dist/html/interstitial.d.ts +14 -0
- package/dist/html/interstitial.d.ts.map +1 -0
- package/dist/html/interstitial.js +67 -0
- package/dist/html/interstitial.js.map +1 -0
- package/dist/html/tables.d.ts +12 -0
- package/dist/html/tables.d.ts.map +1 -0
- package/dist/html/tables.js +62 -0
- package/dist/html/tables.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/model/diagnostics.d.ts +15 -0
- package/dist/model/diagnostics.d.ts.map +1 -0
- package/dist/model/diagnostics.js +37 -0
- package/dist/model/diagnostics.js.map +1 -0
- package/dist/model/document.d.ts +289 -0
- package/dist/model/document.d.ts.map +1 -0
- package/dist/model/document.js +13 -0
- package/dist/model/document.js.map +1 -0
- package/dist/model/extremes.d.ts +11 -0
- package/dist/model/extremes.d.ts.map +1 -0
- package/dist/model/extremes.js +23 -0
- package/dist/model/extremes.js.map +1 -0
- package/dist/model/index.d.ts +8 -0
- package/dist/model/index.d.ts.map +1 -0
- package/dist/model/index.js +2 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model/input.d.ts +14 -0
- package/dist/model/input.d.ts.map +1 -0
- package/dist/model/input.js +47 -0
- package/dist/model/input.js.map +1 -0
- package/dist/model/result.d.ts +53 -0
- package/dist/model/result.d.ts.map +1 -0
- package/dist/model/result.js +40 -0
- package/dist/model/result.js.map +1 -0
- package/dist/model/section-ids.d.ts +8 -0
- package/dist/model/section-ids.d.ts.map +1 -0
- package/dist/model/section-ids.js +28 -0
- package/dist/model/section-ids.js.map +1 -0
- package/dist/model/section-kinds.d.ts +19 -0
- package/dist/model/section-kinds.d.ts.map +1 -0
- package/dist/model/section-kinds.js +42 -0
- package/dist/model/section-kinds.js.map +1 -0
- package/dist/model/table-grid.d.ts +40 -0
- package/dist/model/table-grid.d.ts.map +1 -0
- package/dist/model/table-grid.js +79 -0
- package/dist/model/table-grid.js.map +1 -0
- package/dist/parse.d.ts +18 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +35 -0
- package/dist/parse.js.map +1 -0
- package/dist/render/escape.d.ts +53 -0
- package/dist/render/escape.d.ts.map +1 -0
- package/dist/render/escape.js +94 -0
- package/dist/render/escape.js.map +1 -0
- package/dist/render/index.d.ts +8 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +8 -0
- package/dist/render/index.js.map +1 -0
- package/dist/render/inline.d.ts +39 -0
- package/dist/render/inline.d.ts.map +1 -0
- package/dist/render/inline.js +87 -0
- package/dist/render/inline.js.map +1 -0
- package/dist/render/markdown.d.ts +35 -0
- package/dist/render/markdown.d.ts.map +1 -0
- package/dist/render/markdown.js +279 -0
- package/dist/render/markdown.js.map +1 -0
- package/dist/render/sections.d.ts +25 -0
- package/dist/render/sections.d.ts.map +1 -0
- package/dist/render/sections.js +45 -0
- package/dist/render/sections.js.map +1 -0
- package/dist/render/text.d.ts +13 -0
- package/dist/render/text.d.ts.map +1 -0
- package/dist/render/text.js +66 -0
- package/dist/render/text.js.map +1 -0
- package/dist/xml/mathml.d.ts +24 -0
- package/dist/xml/mathml.d.ts.map +1 -0
- package/dist/xml/mathml.js +201 -0
- package/dist/xml/mathml.js.map +1 -0
- package/dist/xml/ordered.d.ts +49 -0
- package/dist/xml/ordered.d.ts.map +1 -0
- package/dist/xml/ordered.js +151 -0
- package/dist/xml/ordered.js.map +1 -0
- package/dist/xml/well-formed.d.ts +15 -0
- package/dist/xml/well-formed.d.ts.map +1 -0
- package/dist/xml/well-formed.js +120 -0
- package/dist/xml/well-formed.js.map +1 -0
- package/package.json +137 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elements that are page furniture, not paper content. arXiv injects a hidden "Report
|
|
3
|
+
* issue" button after most paragraphs, inside the article element.
|
|
4
|
+
*/
|
|
5
|
+
export const SKIP_TAGS = new Set([
|
|
6
|
+
'button',
|
|
7
|
+
'dialog',
|
|
8
|
+
'form',
|
|
9
|
+
'input',
|
|
10
|
+
'label',
|
|
11
|
+
'nav',
|
|
12
|
+
'noscript',
|
|
13
|
+
'script',
|
|
14
|
+
'style',
|
|
15
|
+
'svg',
|
|
16
|
+
'template',
|
|
17
|
+
]);
|
|
18
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/formats/latexml/context.ts"],"names":[],"mappings":"AAeA;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAwB,IAAI,GAAG,CAAC;IACpD,QAAQ;IACR,QAAQ;IACR,MAAM;IACN,OAAO;IACP,OAAO;IACP,KAAK;IACL,UAAU;IACV,QAAQ;IACR,OAAO;IACP,KAAK;IACL,UAAU;CACX,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `scholarly-parse/latexml` — HTML produced by LaTeXML: arXiv's HTML
|
|
3
|
+
* renders and ar5iv. Requires the optional `linkedom` peer.
|
|
4
|
+
* @module src/formats/latexml
|
|
5
|
+
*/
|
|
6
|
+
export { type LatexmlOptions, parseLatexml } from './parse.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/formats/latexml/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,KAAK,cAAc,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `scholarly-parse/latexml` — HTML produced by LaTeXML: arXiv's HTML
|
|
3
|
+
* renders and ar5iv. Requires the optional `linkedom` peer.
|
|
4
|
+
* @module src/formats/latexml
|
|
5
|
+
*/
|
|
6
|
+
export { parseLatexml } from './parse.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/formats/latexml/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAuB,YAAY,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Inline Markdown for LaTeXML HTML: fonts, links, cross-references and
|
|
3
|
+
* citations as printed, footnotes collected with their mark left in place, and math as
|
|
4
|
+
* the TeX LaTeXML keeps beside every MathML tree.
|
|
5
|
+
* @module src/formats/latexml/inline
|
|
6
|
+
*/
|
|
7
|
+
import { type LatexmlContext } from './context.js';
|
|
8
|
+
/** Inline Markdown for a list of nodes, spacing intact. */
|
|
9
|
+
export declare function inlineMarkdown(nodes: Node[], ctx: LatexmlContext): string;
|
|
10
|
+
/** Inline Markdown of an element's content, whitespace collapsed. */
|
|
11
|
+
export declare function inlineText(element: Element | null | undefined, ctx: LatexmlContext): string;
|
|
12
|
+
/**
|
|
13
|
+
* TeX LaTeXML keeps for a `<math>`: the `application/x-tex` annotation, else `@alttext`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function mathTex(math: Element): string;
|
|
16
|
+
//# sourceMappingURL=inline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inline.d.ts","sourceRoot":"","sources":["../../../src/formats/latexml/inline.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAsBH,OAAO,EAAE,KAAK,cAAc,EAAa,MAAM,cAAc,CAAC;AAE9D,2DAA2D;AAC3D,wBAAgB,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,cAAc,GAAG,MAAM,CAIzE;AAED,qEAAqE;AACrE,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,EAAE,GAAG,EAAE,cAAc,GAAG,MAAM,CAI3F;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAK7C"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Inline Markdown for LaTeXML HTML: fonts, links, cross-references and
|
|
3
|
+
* citations as printed, footnotes collected with their mark left in place, and math as
|
|
4
|
+
* the TeX LaTeXML keeps beside every MathML tree.
|
|
5
|
+
* @module src/formats/latexml/inline
|
|
6
|
+
*/
|
|
7
|
+
import { childNodes, childWhere, hasClass, isElement, resolveUrl, TEXT_NODE, tagOf, textOfElement, } from '../../html/dom.js';
|
|
8
|
+
import { escapeInline } from '../../render/escape.js';
|
|
9
|
+
import { emphasis, inlineCode, inlineMath, joinAdjacentMath, link, subscript, superscript, } from '../../render/inline.js';
|
|
10
|
+
import { SKIP_TAGS } from './context.js';
|
|
11
|
+
/** Inline Markdown for a list of nodes, spacing intact. */
|
|
12
|
+
export function inlineMarkdown(nodes, ctx) {
|
|
13
|
+
let out = '';
|
|
14
|
+
for (const node of nodes)
|
|
15
|
+
out += inlineNode(node, ctx);
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
/** Inline Markdown of an element's content, whitespace collapsed. */
|
|
19
|
+
export function inlineText(element, ctx) {
|
|
20
|
+
return element
|
|
21
|
+
? joinAdjacentMath(inlineMarkdown(childNodes(element), ctx).replace(/\s+/g, ' ').trim())
|
|
22
|
+
: '';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* TeX LaTeXML keeps for a `<math>`: the `application/x-tex` annotation, else `@alttext`.
|
|
26
|
+
*/
|
|
27
|
+
export function mathTex(math) {
|
|
28
|
+
const annotation = Array.from(math.getElementsByTagName('annotation')).find((a) => (a.getAttribute('encoding') ?? '').toLowerCase() === 'application/x-tex');
|
|
29
|
+
return (annotation?.textContent ?? math.getAttribute('alttext') ?? '').trim();
|
|
30
|
+
}
|
|
31
|
+
function inlineNode(node, ctx) {
|
|
32
|
+
if (node.nodeType === TEXT_NODE)
|
|
33
|
+
return escapeInline(node.textContent ?? '');
|
|
34
|
+
if (!isElement(node))
|
|
35
|
+
return '';
|
|
36
|
+
const tag = tagOf(node);
|
|
37
|
+
if (SKIP_TAGS.has(tag))
|
|
38
|
+
return '';
|
|
39
|
+
const inner = () => inlineMarkdown(childNodes(node), ctx);
|
|
40
|
+
if (tag === 'math')
|
|
41
|
+
return inlineMath(mathTex(node));
|
|
42
|
+
// LaTeXML's marker for a macro it could not expand holds only the macro's name
|
|
43
|
+
// (`\argmax`, `{inparablank}`); the content around it is converted as usual.
|
|
44
|
+
if (hasClass(node, 'ltx_ERROR'))
|
|
45
|
+
return '';
|
|
46
|
+
if (hasClass(node, 'ltx_note')) {
|
|
47
|
+
// Other note roles (thanks, affiliation and license notices, venue lines) are front matter.
|
|
48
|
+
return hasClass(node, 'ltx_role_footnote') || hasClass(node, 'ltx_role_endnote')
|
|
49
|
+
? footnote(node, ctx)
|
|
50
|
+
: '';
|
|
51
|
+
}
|
|
52
|
+
if (hasClass(node, 'ltx_font_bold') || tag === 'b' || tag === 'strong')
|
|
53
|
+
return emphasis(inner(), '**');
|
|
54
|
+
if (hasClass(node, 'ltx_font_italic') ||
|
|
55
|
+
hasClass(node, 'ltx_emph') ||
|
|
56
|
+
tag === 'em' ||
|
|
57
|
+
tag === 'i') {
|
|
58
|
+
return emphasis(inner(), '*');
|
|
59
|
+
}
|
|
60
|
+
if ((hasClass(node, 'ltx_font_typewriter') && tag !== 'a') || tag === 'code' || tag === 'tt') {
|
|
61
|
+
// A typewriter link (`\url` sets its anchor in typewriter) keeps its link; typewriter text is code.
|
|
62
|
+
return node.querySelector('a[href]') ? inner() : inlineCode(textOfElement(node));
|
|
63
|
+
}
|
|
64
|
+
switch (tag) {
|
|
65
|
+
case 'sup':
|
|
66
|
+
return superscript(escapeInline(textOfElement(node)), textOfElement(node), false);
|
|
67
|
+
case 'sub':
|
|
68
|
+
return subscript(escapeInline(textOfElement(node)), textOfElement(node));
|
|
69
|
+
case 'a': {
|
|
70
|
+
// A BibTeX `doi` field holding a whole URL comes out as `https://dx.doi.org/https://doi.org/10.…`.
|
|
71
|
+
const href = (node.getAttribute('href') ?? '').replace(/^https?:\/\/(?:dx\.)?doi\.org\/(?=https?:\/\/)/i, '');
|
|
72
|
+
// Cross-references and citations point inside the page; they read as printed.
|
|
73
|
+
if (!href || href.startsWith('#'))
|
|
74
|
+
return inner();
|
|
75
|
+
// `\url{example.org}` with no scheme is an address as written, not a path on the page.
|
|
76
|
+
if (hasClass(node, 'ltx_url') && !/^[a-z][a-z\d+.-]*:/i.test(href))
|
|
77
|
+
return inlineCode(textOfElement(node));
|
|
78
|
+
return link(inner(), resolveUrl(href, ctx.baseUrl));
|
|
79
|
+
}
|
|
80
|
+
case 'br':
|
|
81
|
+
return ' ';
|
|
82
|
+
case 'img':
|
|
83
|
+
return '';
|
|
84
|
+
// Block-shaped elements met in an inline context (a tabular nested in a table
|
|
85
|
+
// cell): space them so their words do not run together.
|
|
86
|
+
case 'tr':
|
|
87
|
+
case 'td':
|
|
88
|
+
case 'th':
|
|
89
|
+
case 'p':
|
|
90
|
+
case 'div':
|
|
91
|
+
case 'li':
|
|
92
|
+
return ` ${inner()} `;
|
|
93
|
+
default:
|
|
94
|
+
return inner();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A footnote: its content collected into the document's footnotes, its mark left as a
|
|
99
|
+
* superscript. LaTeXML repeats the mark inside the content; that copy is dropped.
|
|
100
|
+
*/
|
|
101
|
+
function footnote(note, ctx) {
|
|
102
|
+
const mark = textOfElement(childWhere(note, (child) => hasClass(child, 'ltx_note_mark')));
|
|
103
|
+
const content = note.querySelector('.ltx_note_content');
|
|
104
|
+
if (content) {
|
|
105
|
+
const parts = childNodes(content).filter((child) => !isElement(child) || !(hasClass(child, 'ltx_note_mark') || hasClass(child, 'ltx_tag_note')));
|
|
106
|
+
const text = inlineMarkdown(parts, ctx).replace(/\s+/g, ' ').trim();
|
|
107
|
+
const id = note.getAttribute('id') ?? undefined;
|
|
108
|
+
if (text)
|
|
109
|
+
ctx.footnotes.push({ ...(id && { id }), ...(mark && { label: mark }), text });
|
|
110
|
+
}
|
|
111
|
+
return mark ? superscript(escapeInline(mark), mark, false) : '';
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=inline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inline.js","sourceRoot":"","sources":["../../../src/formats/latexml/inline.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,UAAU,EACV,UAAU,EACV,QAAQ,EACR,SAAS,EACT,UAAU,EACV,SAAS,EACT,KAAK,EACL,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EACL,QAAQ,EACR,UAAU,EACV,UA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AI,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `parseLatexml`: an HTML page produced by LaTeXML — arXiv's
|
|
3
|
+
* `arxiv.org/html/<id>` renders and ar5iv — → `ScholarlyDocument`. Only the
|
|
4
|
+
* `ltx_document` article is read, so the site's header, navigation, and footer never
|
|
5
|
+
* reach the output.
|
|
6
|
+
* @module src/formats/latexml/parse
|
|
7
|
+
*/
|
|
8
|
+
import { type ParseOptions, type ParseResult } from '../../model/result.js';
|
|
9
|
+
/** Options for {@link parseLatexml}. */
|
|
10
|
+
export interface LatexmlOptions extends ParseOptions {
|
|
11
|
+
/** The page's URL, so relative image paths resolve (`x1.png` → `https://arxiv.org/html/…/x1.png`). */
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Parse a LaTeXML HTML page. Requires the optional `linkedom` peer. */
|
|
15
|
+
export declare function parseLatexml(input: string | Uint8Array, options?: LatexmlOptions): Promise<ParseResult>;
|
|
16
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/formats/latexml/parse.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAyBH,OAAO,EAGL,KAAK,YAAY,EACjB,KAAK,WAAW,EAEjB,MAAM,uBAAuB,CAAC;AAO/B,wCAAwC;AACxC,MAAM,WAAW,cAAe,SAAQ,YAAY;IAClD,sGAAsG;IACtG,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAQD,wEAAwE;AACxE,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,GAAG,UAAU,EAC1B,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,WAAW,CAAC,CAEtB"}
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview `parseLatexml`: an HTML page produced by LaTeXML — arXiv's
|
|
3
|
+
* `arxiv.org/html/<id>` renders and ar5iv — → `ScholarlyDocument`. Only the
|
|
4
|
+
* `ltx_document` article is read, so the site's header, navigation, and footer never
|
|
5
|
+
* reach the output.
|
|
6
|
+
* @module src/formats/latexml/parse
|
|
7
|
+
*/
|
|
8
|
+
import { childElements, childNodes, hasClass, isElement, loadDocument, resolveUrl, tagOf, textOfElement, } from '../../html/dom.js';
|
|
9
|
+
import { createDiagnostics } from '../../model/diagnostics.js';
|
|
10
|
+
import { decodeText, exceedsBudget } from '../../model/input.js';
|
|
11
|
+
import { failed, guardAsync, parsed, } from '../../model/result.js';
|
|
12
|
+
import { issueId } from '../../model/section-ids.js';
|
|
13
|
+
import { kindFromTitle } from '../../model/section-kinds.js';
|
|
14
|
+
import { flowBlocks, parseSection } from './blocks.js';
|
|
15
|
+
import { inlineMarkdown, inlineText } from './inline.js';
|
|
16
|
+
const DEFAULT_MAX_INPUT_BYTES = 32 * 1024 * 1024;
|
|
17
|
+
/** Messages arXiv shows in place of a paper it could not convert. */
|
|
18
|
+
const CONVERSION_FAILED = /No HTML for|HTML is not available|Conversion to HTML had a Fatal error|conversion failed/i;
|
|
19
|
+
/** Parse a LaTeXML HTML page. Requires the optional `linkedom` peer. */
|
|
20
|
+
export function parseLatexml(input, options = {}) {
|
|
21
|
+
return guardAsync(() => readLatexml(input, options));
|
|
22
|
+
}
|
|
23
|
+
async function readLatexml(input, options) {
|
|
24
|
+
const maxBytes = options.maxInputBytes ?? DEFAULT_MAX_INPUT_BYTES;
|
|
25
|
+
if (exceedsBudget(input, maxBytes))
|
|
26
|
+
return failed('too-large', `Input exceeds the ${maxBytes}-byte budget`);
|
|
27
|
+
const source = decodeText(input);
|
|
28
|
+
if (!/ltx_document|LaTeXML/.test(source)) {
|
|
29
|
+
return CONVERSION_FAILED.test(source)
|
|
30
|
+
? failed('empty', 'The page reports that no HTML conversion of the paper is available')
|
|
31
|
+
: failed('wrong-format', 'Not a LaTeXML page: no ltx_document element');
|
|
32
|
+
}
|
|
33
|
+
const document = await loadDocument(source);
|
|
34
|
+
const article = document.querySelector('.ltx_document');
|
|
35
|
+
if (!article)
|
|
36
|
+
return failed('wrong-format', 'Not a LaTeXML page: no ltx_document element');
|
|
37
|
+
const ctx = {
|
|
38
|
+
baseUrl: options.baseUrl,
|
|
39
|
+
diag: createDiagnostics(),
|
|
40
|
+
footnotes: [],
|
|
41
|
+
sectionIds: new Set(),
|
|
42
|
+
};
|
|
43
|
+
const watermark = parseWatermark(document, options.baseUrl);
|
|
44
|
+
const metadata = extractMetadata(article, document, watermark, ctx);
|
|
45
|
+
const marked = extractAbstracts(article, ctx);
|
|
46
|
+
const content = extractContent(article, ctx);
|
|
47
|
+
const { back, references } = content;
|
|
48
|
+
const { abstracts, body } = marked.length > 0
|
|
49
|
+
? { abstracts: marked, body: content.body }
|
|
50
|
+
: abstractFromBody(content.body, ctx);
|
|
51
|
+
if (!metadata.title && abstracts.length === 0 && body.length === 0) {
|
|
52
|
+
return failed('empty', 'The page carries no title, abstract, or body');
|
|
53
|
+
}
|
|
54
|
+
if (!metadata.title)
|
|
55
|
+
ctx.diag.warn('no-title', 'The paper has no title');
|
|
56
|
+
if (abstracts.length === 0)
|
|
57
|
+
ctx.diag.warn('no-abstract', 'The paper has no abstract');
|
|
58
|
+
if (body.length === 0)
|
|
59
|
+
ctx.diag.warn('no-body', 'The paper has no body text');
|
|
60
|
+
const flavor = /ar5iv/i.test(options.baseUrl ?? '') ||
|
|
61
|
+
/ar5iv\.labs\.arxiv\.org|<title>\s*\[[\d.]+\]/.test(source)
|
|
62
|
+
? 'ar5iv'
|
|
63
|
+
: watermark.arxiv || /arxiv\.org/.test(options.baseUrl ?? '')
|
|
64
|
+
? 'arxiv'
|
|
65
|
+
: undefined;
|
|
66
|
+
const result = {
|
|
67
|
+
abstracts,
|
|
68
|
+
back,
|
|
69
|
+
body,
|
|
70
|
+
diagnostics: ctx.diag.finish('structured'),
|
|
71
|
+
...(flavor && { flavor }),
|
|
72
|
+
floats: [],
|
|
73
|
+
footnotes: ctx.footnotes,
|
|
74
|
+
format: 'latexml',
|
|
75
|
+
metadata,
|
|
76
|
+
references,
|
|
77
|
+
};
|
|
78
|
+
return parsed(result);
|
|
79
|
+
}
|
|
80
|
+
const MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
|
|
81
|
+
/**
|
|
82
|
+
* arXiv stamps each render with `arXiv:2401.04088v1 [cs.LG] 08 Jan 2024`; that line
|
|
83
|
+
* gives the identifier and version date. The page URL is the fallback for the identifier.
|
|
84
|
+
*/
|
|
85
|
+
function parseWatermark(document, baseUrl) {
|
|
86
|
+
const text = textOfElement(document.querySelector('#watermark-tr')) ||
|
|
87
|
+
textOfElement(document.querySelector('.ltx_page_main')).slice(0, 400);
|
|
88
|
+
const match = /arXiv:(\d{4}\.\d{4,5}(?:v\d+)?|[a-z-]+(?:\.[A-Z]{2})?\/\d{7}(?:v\d+)?)\s*\[[^\]]+\]\s*(\d{1,2}) (\w{3}) (\d{4})/.exec(text);
|
|
89
|
+
const fromUrl = /\/(?:html|abs)\/(\d{4}\.\d{4,5}(?:v\d+)?|[a-z-]+\/\d{7}(?:v\d+)?)/.exec(baseUrl ?? '')?.[1];
|
|
90
|
+
const arxiv = match?.[1] ?? fromUrl;
|
|
91
|
+
const month = match ? MONTHS.indexOf((match[3] ?? '').toLowerCase()) + 1 : 0;
|
|
92
|
+
const published = match && month > 0 ? { day: Number(match[2]), month, year: Number(match[4]) } : undefined;
|
|
93
|
+
return { ...(arxiv && { arxiv }), ...(published && { published }) };
|
|
94
|
+
}
|
|
95
|
+
function extractMetadata(article, document, watermark, ctx) {
|
|
96
|
+
const title = inlineText(article.querySelector('.ltx_title_document'), ctx);
|
|
97
|
+
const subtitle = inlineText(article.querySelector('.ltx_subtitle'), ctx);
|
|
98
|
+
const { affiliations, authors } = extractAuthors(article);
|
|
99
|
+
const keywordsEl = article.querySelector('.ltx_keywords');
|
|
100
|
+
const keywords = keywordsEl
|
|
101
|
+
? textOfElement(keywordsEl)
|
|
102
|
+
.replace(/^keywords?\s*[:.]?\s*/i, '')
|
|
103
|
+
.split(/\s*[,;·]\s*/)
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
: [];
|
|
106
|
+
const language = document.documentElement?.getAttribute('lang') ?? undefined;
|
|
107
|
+
return {
|
|
108
|
+
...(title && { title }),
|
|
109
|
+
...(subtitle && { subtitle }),
|
|
110
|
+
...(authors.length > 0 && { authors }),
|
|
111
|
+
...(affiliations.length > 0 && { affiliations }),
|
|
112
|
+
...(watermark.arxiv && { identifiers: { arxiv: watermark.arxiv } }),
|
|
113
|
+
...(watermark.published && { published: watermark.published }),
|
|
114
|
+
...(keywords.length > 0 && { keywords }),
|
|
115
|
+
...(language && { language }),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** Text of a node list with notes left out, whitespace collapsed. */
|
|
119
|
+
function plainWithoutNotes(nodes) {
|
|
120
|
+
return nodes
|
|
121
|
+
.map((node) => {
|
|
122
|
+
if (!isElement(node))
|
|
123
|
+
return node.textContent ?? '';
|
|
124
|
+
if (hasClass(node, 'ltx_note') ||
|
|
125
|
+
hasClass(node, 'ltx_note_mark') ||
|
|
126
|
+
hasClass(node, 'ltx_author_notes'))
|
|
127
|
+
return '';
|
|
128
|
+
if (tagOf(node) === 'br')
|
|
129
|
+
return '\n';
|
|
130
|
+
return plainWithoutNotes(childNodes(node));
|
|
131
|
+
})
|
|
132
|
+
.join('');
|
|
133
|
+
}
|
|
134
|
+
/** Two to five capitalized words: what a person's name looks like when splitting a list of them. */
|
|
135
|
+
const NAME_LIKE = /^(?:[\p{Lu}][\p{L}'.-]*\s+){1,4}[\p{Lu}][\p{L}'.-]*$/u;
|
|
136
|
+
/**
|
|
137
|
+
* Authors from `ltx_creator ltx_role_author`. A name line followed by more lines inside
|
|
138
|
+
* one `ltx_personname` is `\author{Name \\ Affiliation}`: the extra lines are
|
|
139
|
+
* affiliations. One person-name holding a comma- or "and"-separated run of names is
|
|
140
|
+
* split only when every piece looks like a name.
|
|
141
|
+
*/
|
|
142
|
+
function extractAuthors(article) {
|
|
143
|
+
const affiliations = [];
|
|
144
|
+
const affIndex = (value) => {
|
|
145
|
+
const existing = affiliations.indexOf(value);
|
|
146
|
+
return existing === -1 ? affiliations.push(value) - 1 : existing;
|
|
147
|
+
};
|
|
148
|
+
const authors = [];
|
|
149
|
+
for (const creator of Array.from(article.querySelectorAll('.ltx_authors .ltx_creator.ltx_role_author'))) {
|
|
150
|
+
const person = creator.querySelector('.ltx_personname');
|
|
151
|
+
const lines = plainWithoutNotes(person ? childNodes(person) : [])
|
|
152
|
+
.split('\n')
|
|
153
|
+
.map((line) => line.replace(/\s+/g, ' ').trim())
|
|
154
|
+
.filter(Boolean);
|
|
155
|
+
const [nameLine, ...rest] = lines;
|
|
156
|
+
if (!nameLine)
|
|
157
|
+
continue;
|
|
158
|
+
const emails = rest.filter((line) => /@/.test(line));
|
|
159
|
+
const affLines = rest.filter((line) => !/@/.test(line));
|
|
160
|
+
for (const contact of Array.from(creator.querySelectorAll('.ltx_contact'))) {
|
|
161
|
+
const value = textOfElement(contact).replace(/^(affiliation|address|email)\s*:\s*/i, '');
|
|
162
|
+
if (!value)
|
|
163
|
+
continue;
|
|
164
|
+
if (hasClass(contact, 'ltx_role_email'))
|
|
165
|
+
emails.push(value);
|
|
166
|
+
else if (hasClass(contact, 'ltx_role_affiliation') || hasClass(contact, 'ltx_role_address'))
|
|
167
|
+
affLines.push(value);
|
|
168
|
+
}
|
|
169
|
+
const indices = [...new Set(affLines.map(affIndex))];
|
|
170
|
+
const pieces = nameLine.split(/\s*,\s*|\s+and\s+/).filter(Boolean);
|
|
171
|
+
const names = pieces.length > 1 && pieces.every((p) => NAME_LIKE.test(p)) ? pieces : [nameLine];
|
|
172
|
+
for (const name of names) {
|
|
173
|
+
authors.push({
|
|
174
|
+
name,
|
|
175
|
+
...(indices.length > 0 && { affiliations: indices }),
|
|
176
|
+
...(names.length === 1 && emails[0] && { email: emails[0] }),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return { affiliations, authors };
|
|
181
|
+
}
|
|
182
|
+
/** A heading that only names the abstract, as templates print it (`Abstract`, `Abstract.`). */
|
|
183
|
+
const ABSTRACT_HEADING = /^abstract[.:]?$/i;
|
|
184
|
+
function extractAbstracts(article, ctx) {
|
|
185
|
+
return Array.from(article.querySelectorAll('.ltx_abstract')).flatMap((abstract, index) => {
|
|
186
|
+
const heading = childElements(abstract).find((c) => /^h[1-6]$/.test(tagOf(c)));
|
|
187
|
+
const blocks = flowBlocks(childNodes(abstract).filter((n) => n !== heading), ctx);
|
|
188
|
+
if (blocks.length === 0)
|
|
189
|
+
return [];
|
|
190
|
+
const title = textOfElement(heading);
|
|
191
|
+
return [
|
|
192
|
+
{
|
|
193
|
+
kind: 'main',
|
|
194
|
+
sections: [
|
|
195
|
+
{
|
|
196
|
+
blocks,
|
|
197
|
+
id: issueId(ctx.sectionIds, undefined, `abstract-${index + 1}`),
|
|
198
|
+
kind: 'body',
|
|
199
|
+
sections: [],
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
...(title && !ABSTRACT_HEADING.test(title) && { title }),
|
|
203
|
+
},
|
|
204
|
+
];
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/** A paragraph opening with "Abstract" set as a bold run-in heading (`**Abstract.** We…`). */
|
|
208
|
+
const RUN_IN_ABSTRACT = /^\*\*Abstract[.:]?\*\*[.:]?\s*/i;
|
|
209
|
+
/**
|
|
210
|
+
* A paper that sets its abstract by hand rather than with the abstract environment: its
|
|
211
|
+
* first titled body section when that section is an unnumbered "Abstract"
|
|
212
|
+
* (`\section*{Abstract}`), else a paragraph before that section with a bold "Abstract"
|
|
213
|
+
* run-in heading.
|
|
214
|
+
*/
|
|
215
|
+
function abstractFromBody(body, ctx) {
|
|
216
|
+
const index = body.findIndex((section) => section.title);
|
|
217
|
+
const section = body[index];
|
|
218
|
+
if (section && !section.label && ABSTRACT_HEADING.test(section.title ?? '')) {
|
|
219
|
+
const part = {
|
|
220
|
+
blocks: section.blocks,
|
|
221
|
+
id: section.id,
|
|
222
|
+
kind: 'body',
|
|
223
|
+
sections: section.sections,
|
|
224
|
+
};
|
|
225
|
+
return { abstracts: [{ kind: 'main', sections: [part] }], body: body.toSpliced(index, 1) };
|
|
226
|
+
}
|
|
227
|
+
// Front matter set by hand sits in the untitled sections before the first titled one.
|
|
228
|
+
const leading = index === -1 ? body : body.slice(0, index);
|
|
229
|
+
for (const [at, untitled] of leading.entries()) {
|
|
230
|
+
const found = untitled.blocks.findIndex((block) => block.type === 'paragraph' && RUN_IN_ABSTRACT.test(block.text));
|
|
231
|
+
const paragraph = untitled.blocks[found];
|
|
232
|
+
if (paragraph?.type !== 'paragraph')
|
|
233
|
+
continue;
|
|
234
|
+
const part = {
|
|
235
|
+
blocks: [{ text: paragraph.text.replace(RUN_IN_ABSTRACT, ''), type: 'paragraph' }],
|
|
236
|
+
id: issueId(ctx.sectionIds, undefined, 'abstract-1'),
|
|
237
|
+
kind: 'body',
|
|
238
|
+
sections: [],
|
|
239
|
+
};
|
|
240
|
+
const blocks = untitled.blocks.toSpliced(found, 1);
|
|
241
|
+
const rest = blocks.length > 0 || untitled.sections.length > 0
|
|
242
|
+
? body.toSpliced(at, 1, { ...untitled, blocks })
|
|
243
|
+
: body.toSpliced(at, 1);
|
|
244
|
+
return { abstracts: [{ kind: 'main', sections: [part] }], body: rest };
|
|
245
|
+
}
|
|
246
|
+
return { abstracts: [], body };
|
|
247
|
+
}
|
|
248
|
+
/** Direct children of the article that are front matter, not content. */
|
|
249
|
+
const FRONT_CLASSES = [
|
|
250
|
+
'ltx_title_document',
|
|
251
|
+
'ltx_subtitle',
|
|
252
|
+
'ltx_authors',
|
|
253
|
+
'ltx_abstract',
|
|
254
|
+
'ltx_keywords',
|
|
255
|
+
'ltx_classification',
|
|
256
|
+
'ltx_dates',
|
|
257
|
+
'ltx_date',
|
|
258
|
+
'ltx_pubnotes',
|
|
259
|
+
'ltx_role_thanks',
|
|
260
|
+
];
|
|
261
|
+
/** LaTeXML's sectioning classes and the level each opens. */
|
|
262
|
+
const SECTION_LEVELS = [
|
|
263
|
+
['ltx_chapter', 0],
|
|
264
|
+
['ltx_section', 1],
|
|
265
|
+
['ltx_appendix', 1],
|
|
266
|
+
['ltx_bibliography', 1],
|
|
267
|
+
['ltx_acknowledgement', 1],
|
|
268
|
+
['ltx_acknowledgements', 1],
|
|
269
|
+
['ltx_subsection', 2],
|
|
270
|
+
['ltx_subsubsection', 3],
|
|
271
|
+
['ltx_paragraph', 4],
|
|
272
|
+
['ltx_subparagraph', 5],
|
|
273
|
+
];
|
|
274
|
+
function sectionLevel(element) {
|
|
275
|
+
return SECTION_LEVELS.find(([name]) => hasClass(element, name))?.[1];
|
|
276
|
+
}
|
|
277
|
+
/** True when a section sits inside an ancestor section of the same or a deeper level. */
|
|
278
|
+
function misplaced(section, level, article) {
|
|
279
|
+
for (let el = section.parentElement; el && el !== article; el = el.parentElement) {
|
|
280
|
+
const above = sectionLevel(el);
|
|
281
|
+
if (above !== undefined && above >= level)
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Rebuild the section tree when the markup got it wrong. An inline element LaTeXML
|
|
288
|
+
* leaves unclosed (a `<span>` around a macro it could not expand) swallows every section
|
|
289
|
+
* after it, so §4 parses inside a paragraph of §3. Sectioning classes carry their level,
|
|
290
|
+
* so the tree is rebuilt the way a heading outline is: in document order, each section
|
|
291
|
+
* under the nearest section before it of a shallower level, else at the top.
|
|
292
|
+
*/
|
|
293
|
+
function repairSectionNesting(article) {
|
|
294
|
+
const sections = Array.from(article.querySelectorAll('section')).flatMap((element) => {
|
|
295
|
+
const level = sectionLevel(element);
|
|
296
|
+
return level === undefined ? [] : [{ element, level }];
|
|
297
|
+
});
|
|
298
|
+
if (!sections.some(({ element, level }) => misplaced(element, level, article)))
|
|
299
|
+
return;
|
|
300
|
+
const open = [];
|
|
301
|
+
for (const section of sections) {
|
|
302
|
+
while ((open.at(-1)?.level ?? -1) >= section.level)
|
|
303
|
+
open.pop();
|
|
304
|
+
(open.at(-1)?.element ?? article).append(section.element);
|
|
305
|
+
open.push(section);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function extractContent(article, ctx) {
|
|
309
|
+
repairSectionNesting(article);
|
|
310
|
+
const body = [];
|
|
311
|
+
const back = [];
|
|
312
|
+
// A bibliography placed inside the last section is still the paper's reference list, not section content.
|
|
313
|
+
const bibliography = article.querySelector('.ltx_bibliography');
|
|
314
|
+
const references = bibliography ? extractReferences(bibliography, ctx) : [];
|
|
315
|
+
bibliography?.remove();
|
|
316
|
+
let pending = [];
|
|
317
|
+
const flushPending = () => {
|
|
318
|
+
if (pending.length === 0)
|
|
319
|
+
return;
|
|
320
|
+
body.push({
|
|
321
|
+
blocks: pending,
|
|
322
|
+
id: issueId(ctx.sectionIds, undefined, `s${body.length + 1}`),
|
|
323
|
+
kind: 'body',
|
|
324
|
+
sections: [],
|
|
325
|
+
});
|
|
326
|
+
pending = [];
|
|
327
|
+
};
|
|
328
|
+
for (const child of childNodes(article)) {
|
|
329
|
+
if (!isElement(child)) {
|
|
330
|
+
pending.push(...flowBlocks([child], ctx));
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
if (FRONT_CLASSES.some((c) => hasClass(child, c)))
|
|
334
|
+
continue;
|
|
335
|
+
const acknowledgments = hasClass(child, 'ltx_acknowledgement') || hasClass(child, 'ltx_acknowledgements');
|
|
336
|
+
const isSection = tagOf(child) === 'section' || hasClass(child, 'ltx_appendix') || acknowledgments;
|
|
337
|
+
if (!isSection) {
|
|
338
|
+
pending.push(...flowBlocks([child], ctx));
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
flushPending();
|
|
342
|
+
if (hasClass(child, 'ltx_appendix')) {
|
|
343
|
+
const section = parseSection(child, ctx, 'appendix', `appendix${back.length + 1}`);
|
|
344
|
+
if (section)
|
|
345
|
+
back.push(section);
|
|
346
|
+
}
|
|
347
|
+
else if (acknowledgments) {
|
|
348
|
+
const section = parseSection(child, ctx, 'acknowledgments', 'acknowledgments');
|
|
349
|
+
if (section)
|
|
350
|
+
back.push(section);
|
|
351
|
+
}
|
|
352
|
+
else if (!hasClass(child, 'ltx_index')) {
|
|
353
|
+
const section = parseSection(child, ctx, 'body', `s${body.length + 1}`);
|
|
354
|
+
const kind = kindFromTitle(section?.title);
|
|
355
|
+
if (section && kind)
|
|
356
|
+
back.push(withKind(section, kind));
|
|
357
|
+
else if (section)
|
|
358
|
+
body.push(section);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
flushPending();
|
|
362
|
+
return { back, body, references };
|
|
363
|
+
}
|
|
364
|
+
/** A section and its subsections, re-kinded. */
|
|
365
|
+
function withKind(section, kind) {
|
|
366
|
+
return { ...section, kind, sections: section.sections.map((sub) => withKind(sub, kind)) };
|
|
367
|
+
}
|
|
368
|
+
const DOI = /\b(10\.\d{4,9}\/[^\s"<>]+?)(?=[.,;)]?(?:\s|$))/i;
|
|
369
|
+
const ARXIV = /arXiv[:\s]+(\d{4}\.\d{4,5}(?:v\d+)?)/i;
|
|
370
|
+
/** The bibliography: one reference per `ltx_bibitem`, its printed tag as the label. */
|
|
371
|
+
function extractReferences(bibliography, ctx) {
|
|
372
|
+
return Array.from(bibliography.querySelectorAll('.ltx_bibitem')).flatMap((item) => {
|
|
373
|
+
const tagSpan = item.querySelector('.ltx_tag_bibitem');
|
|
374
|
+
const label = textOfElement(tagSpan).replace(/^\[|\]$/g, '') || undefined;
|
|
375
|
+
const blocks = Array.from(item.querySelectorAll('.ltx_bibblock'));
|
|
376
|
+
const text = (blocks.length > 0
|
|
377
|
+
? blocks
|
|
378
|
+
.map((block) => inlineText(block, ctx))
|
|
379
|
+
.filter(Boolean)
|
|
380
|
+
.join(' ')
|
|
381
|
+
: inlineMarkdown(childNodes(item).filter((n) => n !== tagSpan), ctx))
|
|
382
|
+
.replace(/\s+/g, ' ')
|
|
383
|
+
.trim();
|
|
384
|
+
if (!text)
|
|
385
|
+
return [];
|
|
386
|
+
const plain = textOfElement(item);
|
|
387
|
+
const hrefs = Array.from(item.querySelectorAll('a[href]')).map((a) => resolveUrl(a.getAttribute('href') ?? '', ctx.baseUrl));
|
|
388
|
+
const doi = (hrefs.map((h) => /doi\.org\/(10\.\d{4,9}\/\S+)/i.exec(h)?.[1]).find(Boolean) ??
|
|
389
|
+
DOI.exec(plain)?.[1])?.toLowerCase();
|
|
390
|
+
const arxiv = ARXIV.exec(plain)?.[1];
|
|
391
|
+
const url = doi ? undefined : hrefs.find((h) => /^https?:/i.test(h) && !h.includes('#'));
|
|
392
|
+
const id = item.getAttribute('id') ?? undefined;
|
|
393
|
+
return [
|
|
394
|
+
{
|
|
395
|
+
...(id && { id }),
|
|
396
|
+
...(label && { label }),
|
|
397
|
+
text,
|
|
398
|
+
...(doi && { doi }),
|
|
399
|
+
...(arxiv && { arxiv }),
|
|
400
|
+
...(url && { url }),
|
|
401
|
+
},
|
|
402
|
+
];
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
//# sourceMappingURL=parse.js.map
|