jats-xml 0.0.2 → 0.0.4
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/README.md +74 -1
- package/dist/cjs/cli/index.js +1 -1
- package/dist/cjs/cli/parse.js +7 -10
- package/dist/cjs/resolvers.js +7 -7
- package/dist/cjs/session.js +0 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/cli/index.js +1 -1
- package/dist/esm/cli/parse.js +10 -13
- package/dist/esm/download.js +2 -2
- package/dist/esm/resolvers.js +8 -8
- package/dist/esm/session.js +0 -1
- package/dist/esm/utils.js +2 -2
- package/dist/esm/version.js +1 -1
- package/dist/jats.js +47 -43
- package/dist/types/cli/parse.d.ts.map +1 -1
- package/dist/types/resolvers.d.ts +1 -1
- package/dist/types/resolvers.d.ts.map +1 -1
- package/dist/types/session.d.ts +0 -1
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
1
|
# jats-xml
|
|
2
2
|
|
|
3
|
-
Types and utilities for working with JATS XML documents.
|
|
3
|
+
Types and utilities for working with JATS XML documents in Node and Typescript.
|
|
4
|
+
|
|
5
|
+
Read and write JATS XML from node or see summries from the command line.
|
|
6
|
+
|
|
7
|
+
To use from the command line, use the `-g` to create a global install, which will provide a `jats` CLI:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install -g jats-xml
|
|
11
|
+
jats -v
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## What is JATS?
|
|
15
|
+
|
|
16
|
+
JATS is a NISO standard for Journal Article Tags Schema, which is a way to define the XML structure of a scientific article semantically. This includes the `front`-matter (authors, funding, title, abstract, etc.), the `body` of the article (sections, figures, equations, tables, etc.), and `back`-matter (references, footnotes, etc.). The JATS can also contain `sub-articles`.
|
|
17
|
+
|
|
18
|
+
The standard documents are hosted by the NIH <https://jats.nlm.nih.gov/>. There are three flavours, this library currently uses in most cases the most precriptive tag set (for article authoring). Another helpful resource is <https://jats4r.org/>, which provides other examples and recomendations for JATS.
|
|
19
|
+
|
|
20
|
+
Note that most publishers do **not** provide the XML as a first class output - they should, it is an important part of open-science to have the content programatically accessible and interoperable. It is only [FAIR](https://www.go-fair.org/fair-principles/) 😉.
|
|
21
|
+
|
|
22
|
+
## From the command line
|
|
23
|
+
|
|
24
|
+
Commands available:
|
|
25
|
+
|
|
26
|
+
`download`: attempt to find the JATS file and download it locally.
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
jats download https://docs.python.org/3.7 article.jats
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Note, currently this just downloads the XML, **not** the associated files.
|
|
33
|
+
|
|
34
|
+
`sumamry`: summarize the contents of the JATS, given a URL, DOI, or local file
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
jats summary https://elifesciences.org/articles/81952
|
|
38
|
+
jats summary 10.1371/journal.pclm.0000068
|
|
39
|
+
jats summary /local/article.jats
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
This will provide a summary, including a list of what the JATS file contains.
|
|
43
|
+
|
|
44
|
+

|
|
45
|
+
|
|
46
|
+
## Working in Typescript
|
|
47
|
+
|
|
48
|
+
All tags are accessible as types/enums. There is also documentation from each node-type
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { Tags } from 'jats-xml';
|
|
52
|
+
|
|
53
|
+
Tags.journalId;
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Reading JATS in Node
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import 'fs' from 'fs';
|
|
60
|
+
import { Inventory, toDate } from 'jats-xml';
|
|
61
|
+
import { toText } from 'myst-common';
|
|
62
|
+
import { select, selectAll } from 'unist-util-select';
|
|
63
|
+
|
|
64
|
+
const data = fs.readFileSync('article.jats').toString();
|
|
65
|
+
const jats = new JATS(data);
|
|
66
|
+
// Easy access to properties
|
|
67
|
+
jats.doi
|
|
68
|
+
jats.body // A tree of the body (or front/back)
|
|
69
|
+
toDate(jats.publicationDate) // as a Javascript Date object
|
|
70
|
+
select('[id=fig1]', jats.body) // select a figure by an ID
|
|
71
|
+
selectAll('fig', jats.body) // Or selectAll figures
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Write JATS in Node
|
|
75
|
+
|
|
76
|
+
TODO!
|
package/dist/cjs/cli/index.js
CHANGED
|
@@ -9,6 +9,6 @@ const version_1 = __importDefault(require("../version"));
|
|
|
9
9
|
const parse_1 = require("./parse");
|
|
10
10
|
const program = new commander_1.default.Command();
|
|
11
11
|
(0, parse_1.addDownloadCLI)(program);
|
|
12
|
-
program.version(`v${version_1.default}`, '-v, --version', 'Print the current version of
|
|
12
|
+
program.version(`v${version_1.default}`, '-v, --version', 'Print the current version of jats-xml');
|
|
13
13
|
program.option('-d, --debug', 'Log out any errors to the console.');
|
|
14
14
|
program.parse(process.argv);
|
package/dist/cjs/cli/parse.js
CHANGED
|
@@ -30,7 +30,7 @@ const utils_1 = require("../utils");
|
|
|
30
30
|
function hasValidExtension(output) {
|
|
31
31
|
return ['.xml', '.jats'].includes((0, path_1.extname)(output).toLowerCase());
|
|
32
32
|
}
|
|
33
|
-
function
|
|
33
|
+
function downloadAndSaveJats(session, urlOrDoi, output) {
|
|
34
34
|
return __awaiter(this, void 0, void 0, function* () {
|
|
35
35
|
if (fs_1.default.existsSync(urlOrDoi)) {
|
|
36
36
|
throw new Error(`File "${urlOrDoi}" is local and cannot be downloaded!`);
|
|
@@ -55,12 +55,9 @@ function parseJats(session, file) {
|
|
|
55
55
|
session.log.debug(toc(`Parsed JATS file from disk in %s`));
|
|
56
56
|
return new jats_1.Jats(data);
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return new jats_1.Jats(data);
|
|
62
|
-
}
|
|
63
|
-
throw new Error(`Could not find ${file} locally, and it doesn't look like a URL or DOI`);
|
|
58
|
+
const data = yield (0, download_1.downloadJatsFromUrl)(session, file, resolvers_1.DEFAULT_RESOLVERS);
|
|
59
|
+
session.log.debug(toc(`Downloaded and parsed JATS file in %s`));
|
|
60
|
+
return new jats_1.Jats(data);
|
|
64
61
|
});
|
|
65
62
|
}
|
|
66
63
|
function formatLongString(data, offset = 0, length = 88 - offset) {
|
|
@@ -145,7 +142,7 @@ function jatsReferencesCLI(session, file) {
|
|
|
145
142
|
const jats = yield parseJats(session, file);
|
|
146
143
|
const sorted = jats.references
|
|
147
144
|
.map((ref) => {
|
|
148
|
-
const
|
|
145
|
+
const doiString = (0, utils_1.findDoi)(ref);
|
|
149
146
|
const title = (0, myst_common_1.toText)((0, unist_util_select_1.select)(types_1.Tags.articleTitle, ref));
|
|
150
147
|
const year = (0, myst_common_1.toText)((0, unist_util_select_1.select)(types_1.Tags.year, ref));
|
|
151
148
|
const surnames = (0, unist_util_select_1.selectAll)(types_1.Tags.surname, ref);
|
|
@@ -158,7 +155,7 @@ function jatsReferencesCLI(session, file) {
|
|
|
158
155
|
return {
|
|
159
156
|
Citation: `${short} (${year})`,
|
|
160
157
|
Title: title,
|
|
161
|
-
DOI:
|
|
158
|
+
DOI: doiString ? doi_utils_1.default.buildUrl(doiString) : null,
|
|
162
159
|
Count: s.length,
|
|
163
160
|
};
|
|
164
161
|
})
|
|
@@ -188,7 +185,7 @@ function makeDownloadCLI(program) {
|
|
|
188
185
|
.description('Parse a JATS file and provide a summary')
|
|
189
186
|
.argument('<url>', 'The JATS url or a DOI')
|
|
190
187
|
.argument('<output>', 'The JATS url or a DOI')
|
|
191
|
-
.action((0, myst_cli_utils_1.clirun)(
|
|
188
|
+
.action((0, myst_cli_utils_1.clirun)(downloadAndSaveJats, { program, getSession: session_1.getSession }));
|
|
192
189
|
return command;
|
|
193
190
|
}
|
|
194
191
|
function addDownloadCLI(program) {
|
package/dist/cjs/resolvers.js
CHANGED
|
@@ -33,22 +33,22 @@ exports.plos = {
|
|
|
33
33
|
};
|
|
34
34
|
exports.joss = {
|
|
35
35
|
test(url) {
|
|
36
|
-
return new URL(url).hostname === 'joss.theoj.org';
|
|
36
|
+
return new URL(url).hostname === 'joss.theoj.org' && doi_utils_1.default.validate(url);
|
|
37
37
|
},
|
|
38
38
|
jatsUrl(url) {
|
|
39
39
|
// Probably a better way to do this, the joss papers on on github!
|
|
40
|
-
const
|
|
41
|
-
const [org, jossId] =
|
|
40
|
+
const doiString = doi_utils_1.default.normalize(url);
|
|
41
|
+
const [org, jossId] = doiString.split('/');
|
|
42
42
|
const id = jossId.split('.')[1];
|
|
43
43
|
return `https://raw.githubusercontent.com/openjournals/joss-papers/master/joss.${id}/${org}.${jossId}.jats`;
|
|
44
44
|
},
|
|
45
45
|
};
|
|
46
46
|
exports.DEFAULT_RESOLVERS = [exports.elife, exports.plos, exports.joss];
|
|
47
|
-
function resolveJatsUrlFromDoi(session,
|
|
47
|
+
function resolveJatsUrlFromDoi(session, doiString, resolvers = exports.DEFAULT_RESOLVERS) {
|
|
48
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
-
if (!doi_utils_1.default.validate(
|
|
50
|
-
throw new Error(`The doi ${
|
|
51
|
-
const doiUrl = doi_utils_1.default.buildUrl(
|
|
49
|
+
if (!doi_utils_1.default.validate(doiString))
|
|
50
|
+
throw new Error(`The doi ${doiString} is not valid`);
|
|
51
|
+
const doiUrl = doi_utils_1.default.buildUrl(doiString);
|
|
52
52
|
session.log.debug(`Resolving DOI ${doiUrl}`);
|
|
53
53
|
const resp = yield (0, node_fetch_1.default)(doiUrl);
|
|
54
54
|
const articleUrl = resp.url;
|
package/dist/cjs/session.js
CHANGED
|
@@ -5,7 +5,6 @@ const myst_cli_utils_1 = require("myst-cli-utils");
|
|
|
5
5
|
class Session {
|
|
6
6
|
constructor(opts) {
|
|
7
7
|
var _a;
|
|
8
|
-
this.API_URL = 'https://api.myst.tools';
|
|
9
8
|
this.log = (_a = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _a !== void 0 ? _a : (0, myst_cli_utils_1.chalkLogger)(myst_cli_utils_1.LogLevel.debug);
|
|
10
9
|
}
|
|
11
10
|
}
|
package/dist/cjs/version.js
CHANGED
package/dist/esm/cli/index.js
CHANGED
|
@@ -4,6 +4,6 @@ import version from '../version';
|
|
|
4
4
|
import { addDownloadCLI } from './parse';
|
|
5
5
|
const program = new commander.Command();
|
|
6
6
|
addDownloadCLI(program);
|
|
7
|
-
program.version(`v${version}`, '-v, --version', 'Print the current version of
|
|
7
|
+
program.version(`v${version}`, '-v, --version', 'Print the current version of jats-xml');
|
|
8
8
|
program.option('-d, --debug', 'Log out any errors to the console.');
|
|
9
9
|
program.parse(process.argv);
|
package/dist/esm/cli/parse.js
CHANGED
|
@@ -11,7 +11,7 @@ import { Command } from 'commander';
|
|
|
11
11
|
import fs from 'fs';
|
|
12
12
|
import { extname } from 'path';
|
|
13
13
|
import { clirun, isUrl, tic, writeFileToFolder } from 'myst-cli-utils';
|
|
14
|
-
import
|
|
14
|
+
import doi from 'doi-utils';
|
|
15
15
|
import chalk from 'chalk';
|
|
16
16
|
import { getSession } from '../session';
|
|
17
17
|
import { Tags } from '../types';
|
|
@@ -24,12 +24,12 @@ import { findDoi, formatDate, toDate } from '../utils';
|
|
|
24
24
|
function hasValidExtension(output) {
|
|
25
25
|
return ['.xml', '.jats'].includes(extname(output).toLowerCase());
|
|
26
26
|
}
|
|
27
|
-
function
|
|
27
|
+
function downloadAndSaveJats(session, urlOrDoi, output) {
|
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
29
|
if (fs.existsSync(urlOrDoi)) {
|
|
30
30
|
throw new Error(`File "${urlOrDoi}" is local and cannot be downloaded!`);
|
|
31
31
|
}
|
|
32
|
-
if (!(
|
|
32
|
+
if (!(doi.validate(urlOrDoi) || isUrl(urlOrDoi))) {
|
|
33
33
|
throw new Error(`Path must be a URL or DOI, not "${urlOrDoi}"`);
|
|
34
34
|
}
|
|
35
35
|
if (!hasValidExtension(output)) {
|
|
@@ -49,12 +49,9 @@ function parseJats(session, file) {
|
|
|
49
49
|
session.log.debug(toc(`Parsed JATS file from disk in %s`));
|
|
50
50
|
return new Jats(data);
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return new Jats(data);
|
|
56
|
-
}
|
|
57
|
-
throw new Error(`Could not find ${file} locally, and it doesn't look like a URL or DOI`);
|
|
52
|
+
const data = yield downloadJatsFromUrl(session, file, DEFAULT_RESOLVERS);
|
|
53
|
+
session.log.debug(toc(`Downloaded and parsed JATS file in %s`));
|
|
54
|
+
return new Jats(data);
|
|
58
55
|
});
|
|
59
56
|
}
|
|
60
57
|
function formatLongString(data, offset = 0, length = 88 - offset) {
|
|
@@ -94,7 +91,7 @@ function jatsSummaryCLI(session, file) {
|
|
|
94
91
|
return __awaiter(this, void 0, void 0, function* () {
|
|
95
92
|
const jats = yield parseJats(session, file);
|
|
96
93
|
const summary = {
|
|
97
|
-
DOI: jats.doi ?
|
|
94
|
+
DOI: jats.doi ? doi.buildUrl(jats.doi) : null,
|
|
98
95
|
Title: (_a = toText(jats.articleTitle)) === null || _a === void 0 ? void 0 : _a.replace(/\n/g, ' '),
|
|
99
96
|
Date: formatDate(toDate(jats.publicationDate)),
|
|
100
97
|
Authors: jats.articleAuthors
|
|
@@ -139,7 +136,7 @@ function jatsReferencesCLI(session, file) {
|
|
|
139
136
|
const jats = yield parseJats(session, file);
|
|
140
137
|
const sorted = jats.references
|
|
141
138
|
.map((ref) => {
|
|
142
|
-
const
|
|
139
|
+
const doiString = findDoi(ref);
|
|
143
140
|
const title = toText(select(Tags.articleTitle, ref));
|
|
144
141
|
const year = toText(select(Tags.year, ref));
|
|
145
142
|
const surnames = selectAll(Tags.surname, ref);
|
|
@@ -152,7 +149,7 @@ function jatsReferencesCLI(session, file) {
|
|
|
152
149
|
return {
|
|
153
150
|
Citation: `${short} (${year})`,
|
|
154
151
|
Title: title,
|
|
155
|
-
DOI:
|
|
152
|
+
DOI: doiString ? doi.buildUrl(doiString) : null,
|
|
156
153
|
Count: s.length,
|
|
157
154
|
};
|
|
158
155
|
})
|
|
@@ -182,7 +179,7 @@ function makeDownloadCLI(program) {
|
|
|
182
179
|
.description('Parse a JATS file and provide a summary')
|
|
183
180
|
.argument('<url>', 'The JATS url or a DOI')
|
|
184
181
|
.argument('<output>', 'The JATS url or a DOI')
|
|
185
|
-
.action(clirun(
|
|
182
|
+
.action(clirun(downloadAndSaveJats, { program, getSession }));
|
|
186
183
|
return command;
|
|
187
184
|
}
|
|
188
185
|
export function addDownloadCLI(program) {
|
package/dist/esm/download.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import
|
|
10
|
+
import doi from 'doi-utils';
|
|
11
11
|
import fetch from 'node-fetch';
|
|
12
12
|
import { isUrl } from 'myst-cli-utils';
|
|
13
13
|
import { resolveJatsUrlFromDoi } from './resolvers';
|
|
@@ -27,7 +27,7 @@ function dowloadFromUrl(session, jatsUrl) {
|
|
|
27
27
|
}
|
|
28
28
|
export function downloadJatsFromUrl(session, urlOrDoi, resolvers) {
|
|
29
29
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
if (
|
|
30
|
+
if (doi.validate(urlOrDoi)) {
|
|
31
31
|
const jatsUrl = yield resolveJatsUrlFromDoi(session, urlOrDoi, resolvers);
|
|
32
32
|
const data = yield dowloadFromUrl(session, jatsUrl);
|
|
33
33
|
return data;
|
package/dist/esm/resolvers.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import
|
|
10
|
+
import doi from 'doi-utils';
|
|
11
11
|
import fetch from 'node-fetch';
|
|
12
12
|
export const elife = {
|
|
13
13
|
test(url) {
|
|
@@ -27,22 +27,22 @@ export const plos = {
|
|
|
27
27
|
};
|
|
28
28
|
export const joss = {
|
|
29
29
|
test(url) {
|
|
30
|
-
return new URL(url).hostname === 'joss.theoj.org';
|
|
30
|
+
return new URL(url).hostname === 'joss.theoj.org' && doi.validate(url);
|
|
31
31
|
},
|
|
32
32
|
jatsUrl(url) {
|
|
33
33
|
// Probably a better way to do this, the joss papers on on github!
|
|
34
|
-
const
|
|
35
|
-
const [org, jossId] =
|
|
34
|
+
const doiString = doi.normalize(url);
|
|
35
|
+
const [org, jossId] = doiString.split('/');
|
|
36
36
|
const id = jossId.split('.')[1];
|
|
37
37
|
return `https://raw.githubusercontent.com/openjournals/joss-papers/master/joss.${id}/${org}.${jossId}.jats`;
|
|
38
38
|
},
|
|
39
39
|
};
|
|
40
40
|
export const DEFAULT_RESOLVERS = [elife, plos, joss];
|
|
41
|
-
export function resolveJatsUrlFromDoi(session,
|
|
41
|
+
export function resolveJatsUrlFromDoi(session, doiString, resolvers = DEFAULT_RESOLVERS) {
|
|
42
42
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
if (!
|
|
44
|
-
throw new Error(`The doi ${
|
|
45
|
-
const doiUrl =
|
|
43
|
+
if (!doi.validate(doiString))
|
|
44
|
+
throw new Error(`The doi ${doiString} is not valid`);
|
|
45
|
+
const doiUrl = doi.buildUrl(doiString);
|
|
46
46
|
session.log.debug(`Resolving DOI ${doiUrl}`);
|
|
47
47
|
const resp = yield fetch(doiUrl);
|
|
48
48
|
const articleUrl = resp.url;
|
package/dist/esm/session.js
CHANGED
|
@@ -2,7 +2,6 @@ import { chalkLogger, LogLevel } from 'myst-cli-utils';
|
|
|
2
2
|
export class Session {
|
|
3
3
|
constructor(opts) {
|
|
4
4
|
var _a;
|
|
5
|
-
this.API_URL = 'https://api.myst.tools';
|
|
6
5
|
this.log = (_a = opts === null || opts === void 0 ? void 0 : opts.logger) !== null && _a !== void 0 ? _a : chalkLogger(LogLevel.debug);
|
|
7
6
|
}
|
|
8
7
|
}
|
package/dist/esm/utils.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toText } from 'myst-common';
|
|
2
|
-
import
|
|
2
|
+
import doi from 'doi-utils';
|
|
3
3
|
import { select, selectAll } from 'unist-util-select';
|
|
4
4
|
import { Tags } from './types';
|
|
5
5
|
export function convertToUnist(node) {
|
|
@@ -83,6 +83,6 @@ export function findDoi(node) {
|
|
|
83
83
|
const id = select('[pub-id-type=doi]', node);
|
|
84
84
|
if (id && toText(id))
|
|
85
85
|
return toText(id);
|
|
86
|
-
const doiTag = selectAll(`${Tags.articleId},${Tags.pubId}`, node).find((t) =>
|
|
86
|
+
const doiTag = selectAll(`${Tags.articleId},${Tags.pubId}`, node).find((t) => doi.validate(toText(t)));
|
|
87
87
|
return toText(doiTag) || null;
|
|
88
88
|
}
|
package/dist/esm/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = '0.0.
|
|
1
|
+
const version = '0.0.4';
|
|
2
2
|
export default version;
|
package/dist/jats.js
CHANGED
|
@@ -3279,7 +3279,7 @@ var require_resolvers = __commonJS({
|
|
|
3279
3279
|
Object.defineProperty(exports, "__esModule", {
|
|
3280
3280
|
value: true
|
|
3281
3281
|
});
|
|
3282
|
-
exports.DEFAULT_RESOLVERS = void 0;
|
|
3282
|
+
exports.DEFAULT_RESOLVERS = exports.STRICT_RESOLVERS = void 0;
|
|
3283
3283
|
var _validatePart = require_validatePart();
|
|
3284
3284
|
var doiOrg = {
|
|
3285
3285
|
test(url) {
|
|
@@ -3329,6 +3329,10 @@ var require_resolvers = __commonJS({
|
|
|
3329
3329
|
return clumpParts(url).find(_validatePart.validatePart);
|
|
3330
3330
|
}
|
|
3331
3331
|
};
|
|
3332
|
+
var STRICT_RESOLVERS = [
|
|
3333
|
+
doiOrg
|
|
3334
|
+
];
|
|
3335
|
+
exports.STRICT_RESOLVERS = STRICT_RESOLVERS;
|
|
3332
3336
|
var DEFAULT_RESOLVERS2 = [
|
|
3333
3337
|
doiOrg,
|
|
3334
3338
|
doiSubdomain,
|
|
@@ -3347,53 +3351,57 @@ var require_cjs = __commonJS({
|
|
|
3347
3351
|
Object.defineProperty(exports, "__esModule", {
|
|
3348
3352
|
value: true
|
|
3349
3353
|
});
|
|
3350
|
-
Object.defineProperty(exports, "
|
|
3354
|
+
Object.defineProperty(exports, "DEFAULT_RESOLVERS", {
|
|
3355
|
+
enumerable: true,
|
|
3356
|
+
get: function() {
|
|
3357
|
+
return _resolvers.DEFAULT_RESOLVERS;
|
|
3358
|
+
}
|
|
3359
|
+
});
|
|
3360
|
+
Object.defineProperty(exports, "STRICT_RESOLVERS", {
|
|
3351
3361
|
enumerable: true,
|
|
3352
3362
|
get: function() {
|
|
3353
|
-
return
|
|
3363
|
+
return _resolvers.STRICT_RESOLVERS;
|
|
3354
3364
|
}
|
|
3355
3365
|
});
|
|
3356
|
-
exports.validate = validate;
|
|
3357
|
-
exports.normalize = normalize;
|
|
3358
|
-
exports.buildUrl = buildUrl;
|
|
3359
3366
|
exports.default = void 0;
|
|
3360
3367
|
var _resolvers = require_resolvers();
|
|
3361
3368
|
var _validatePart = require_validatePart();
|
|
3362
|
-
function validate(possibleDOI) {
|
|
3369
|
+
function validate(possibleDOI, opts) {
|
|
3363
3370
|
if (!possibleDOI)
|
|
3364
3371
|
return false;
|
|
3365
|
-
return !!normalize(possibleDOI);
|
|
3372
|
+
return !!normalize(possibleDOI, opts);
|
|
3366
3373
|
}
|
|
3367
|
-
function normalize(possibleDOI) {
|
|
3368
|
-
let
|
|
3374
|
+
function normalize(possibleDOI, opts) {
|
|
3375
|
+
let doi5 = void 0;
|
|
3369
3376
|
if (!possibleDOI)
|
|
3370
3377
|
return void 0;
|
|
3371
3378
|
if ((0, _validatePart).validatePart(possibleDOI))
|
|
3372
3379
|
return possibleDOI;
|
|
3373
3380
|
if (possibleDOI.startsWith("doi:")) {
|
|
3374
|
-
|
|
3375
|
-
if ((0, _validatePart).validatePart(
|
|
3376
|
-
return
|
|
3381
|
+
doi5 = possibleDOI.slice(4);
|
|
3382
|
+
if ((0, _validatePart).validatePart(doi5))
|
|
3383
|
+
return doi5;
|
|
3377
3384
|
}
|
|
3378
3385
|
try {
|
|
3379
3386
|
const url = new URL(possibleDOI.startsWith("http") ? possibleDOI : `http://${possibleDOI}`);
|
|
3380
|
-
const
|
|
3387
|
+
const resolvers = (opts === null || opts === void 0 ? void 0 : opts.strict) ? _resolvers.STRICT_RESOLVERS : _resolvers.DEFAULT_RESOLVERS;
|
|
3388
|
+
const resolver = resolvers.find(
|
|
3381
3389
|
(r) => r.test(url)
|
|
3382
3390
|
);
|
|
3383
3391
|
if (!resolver)
|
|
3384
3392
|
return void 0;
|
|
3385
|
-
|
|
3393
|
+
doi5 = resolver.parse(url);
|
|
3386
3394
|
} catch (error) {
|
|
3387
3395
|
}
|
|
3388
|
-
if ((0, _validatePart).validatePart(
|
|
3389
|
-
return
|
|
3396
|
+
if ((0, _validatePart).validatePart(doi5))
|
|
3397
|
+
return doi5;
|
|
3390
3398
|
return void 0;
|
|
3391
3399
|
}
|
|
3392
|
-
function buildUrl(possibleDOI) {
|
|
3393
|
-
const
|
|
3394
|
-
if (!
|
|
3400
|
+
function buildUrl(possibleDOI, opts) {
|
|
3401
|
+
const doi5 = normalize(possibleDOI, opts);
|
|
3402
|
+
if (!doi5)
|
|
3395
3403
|
return void 0;
|
|
3396
|
-
return `https://doi.org/${
|
|
3404
|
+
return `https://doi.org/${doi5}`;
|
|
3397
3405
|
}
|
|
3398
3406
|
var _default = {
|
|
3399
3407
|
validatePart: _validatePart.validatePart,
|
|
@@ -9094,7 +9102,7 @@ var require_lib4 = __commonJS({
|
|
|
9094
9102
|
var import_commander2 = __toESM(require_commander());
|
|
9095
9103
|
|
|
9096
9104
|
// src/version.ts
|
|
9097
|
-
var version = "0.0.
|
|
9105
|
+
var version = "0.0.4";
|
|
9098
9106
|
var version_default = version;
|
|
9099
9107
|
|
|
9100
9108
|
// src/cli/parse.ts
|
|
@@ -9243,7 +9251,6 @@ var import_chalk2 = __toESM(require_source());
|
|
|
9243
9251
|
// src/session.ts
|
|
9244
9252
|
var Session2 = class {
|
|
9245
9253
|
constructor(opts) {
|
|
9246
|
-
this.API_URL = "https://api.myst.tools";
|
|
9247
9254
|
var _a;
|
|
9248
9255
|
this.log = (_a = opts == null ? void 0 : opts.logger) != null ? _a : chalkLogger(LogLevel.debug);
|
|
9249
9256
|
}
|
|
@@ -10205,21 +10212,21 @@ var plos = {
|
|
|
10205
10212
|
};
|
|
10206
10213
|
var joss = {
|
|
10207
10214
|
test(url) {
|
|
10208
|
-
return new URL(url).hostname === "joss.theoj.org";
|
|
10215
|
+
return new URL(url).hostname === "joss.theoj.org" && import_doi_utils2.default.validate(url);
|
|
10209
10216
|
},
|
|
10210
10217
|
jatsUrl(url) {
|
|
10211
|
-
const
|
|
10212
|
-
const [org, jossId] =
|
|
10218
|
+
const doiString = import_doi_utils2.default.normalize(url);
|
|
10219
|
+
const [org, jossId] = doiString.split("/");
|
|
10213
10220
|
const id = jossId.split(".")[1];
|
|
10214
10221
|
return `https://raw.githubusercontent.com/openjournals/joss-papers/master/joss.${id}/${org}.${jossId}.jats`;
|
|
10215
10222
|
}
|
|
10216
10223
|
};
|
|
10217
10224
|
var DEFAULT_RESOLVERS = [elife, plos, joss];
|
|
10218
10225
|
function resolveJatsUrlFromDoi(_0, _1) {
|
|
10219
|
-
return __async(this, arguments, function* (session,
|
|
10220
|
-
if (!import_doi_utils2.default.validate(
|
|
10221
|
-
throw new Error(`The doi ${
|
|
10222
|
-
const doiUrl = import_doi_utils2.default.buildUrl(
|
|
10226
|
+
return __async(this, arguments, function* (session, doiString, resolvers = DEFAULT_RESOLVERS) {
|
|
10227
|
+
if (!import_doi_utils2.default.validate(doiString))
|
|
10228
|
+
throw new Error(`The doi ${doiString} is not valid`);
|
|
10229
|
+
const doiUrl = import_doi_utils2.default.buildUrl(doiString);
|
|
10223
10230
|
session.log.debug(`Resolving DOI ${doiUrl}`);
|
|
10224
10231
|
const resp = yield (0, import_node_fetch.default)(doiUrl);
|
|
10225
10232
|
const articleUrl = resp.url;
|
|
@@ -10268,7 +10275,7 @@ function downloadJatsFromUrl(session, urlOrDoi, resolvers) {
|
|
|
10268
10275
|
function hasValidExtension(output) {
|
|
10269
10276
|
return [".xml", ".jats"].includes((0, import_path2.extname)(output).toLowerCase());
|
|
10270
10277
|
}
|
|
10271
|
-
function
|
|
10278
|
+
function downloadAndSaveJats(session, urlOrDoi, output) {
|
|
10272
10279
|
return __async(this, null, function* () {
|
|
10273
10280
|
if (import_fs2.default.existsSync(urlOrDoi)) {
|
|
10274
10281
|
throw new Error(`File "${urlOrDoi}" is local and cannot be downloaded!`);
|
|
@@ -10293,16 +10300,13 @@ function parseJats(session, file) {
|
|
|
10293
10300
|
const toc = tic();
|
|
10294
10301
|
if (import_fs2.default.existsSync(file)) {
|
|
10295
10302
|
session.log.debug(`Found ${file} locally, parsing`);
|
|
10296
|
-
const
|
|
10303
|
+
const data2 = import_fs2.default.readFileSync(file).toString();
|
|
10297
10304
|
session.log.debug(toc(`Parsed JATS file from disk in %s`));
|
|
10298
|
-
return new Jats(
|
|
10299
|
-
}
|
|
10300
|
-
if (import_doi_utils4.default.validate(file) || isUrl(file)) {
|
|
10301
|
-
const data = yield downloadJatsFromUrl(session, file, DEFAULT_RESOLVERS);
|
|
10302
|
-
session.log.debug(toc(`Downloaded and parsed JATS file in %s`));
|
|
10303
|
-
return new Jats(data);
|
|
10305
|
+
return new Jats(data2);
|
|
10304
10306
|
}
|
|
10305
|
-
|
|
10307
|
+
const data = yield downloadJatsFromUrl(session, file, DEFAULT_RESOLVERS);
|
|
10308
|
+
session.log.debug(toc(`Downloaded and parsed JATS file in %s`));
|
|
10309
|
+
return new Jats(data);
|
|
10306
10310
|
});
|
|
10307
10311
|
}
|
|
10308
10312
|
function formatLongString(data, offset = 0, length = 88 - offset) {
|
|
@@ -10380,7 +10384,7 @@ function jatsReferencesCLI(session, file) {
|
|
|
10380
10384
|
return __async(this, null, function* () {
|
|
10381
10385
|
const jats = yield parseJats(session, file);
|
|
10382
10386
|
const sorted = jats.references.map((ref) => {
|
|
10383
|
-
const
|
|
10387
|
+
const doiString = findDoi(ref);
|
|
10384
10388
|
const title = toText(select("article-title" /* articleTitle */, ref));
|
|
10385
10389
|
const year = toText(select("year" /* year */, ref));
|
|
10386
10390
|
const surnames = selectAll("surname" /* surname */, ref);
|
|
@@ -10389,7 +10393,7 @@ function jatsReferencesCLI(session, file) {
|
|
|
10389
10393
|
return {
|
|
10390
10394
|
Citation: `${short} (${year})`,
|
|
10391
10395
|
Title: title,
|
|
10392
|
-
DOI:
|
|
10396
|
+
DOI: doiString ? import_doi_utils4.default.buildUrl(doiString) : null,
|
|
10393
10397
|
Count: s.length
|
|
10394
10398
|
};
|
|
10395
10399
|
}).sort((a, b) => b.Count - a.Count);
|
|
@@ -10407,7 +10411,7 @@ function makeReferencesCLI(program2) {
|
|
|
10407
10411
|
return command;
|
|
10408
10412
|
}
|
|
10409
10413
|
function makeDownloadCLI(program2) {
|
|
10410
|
-
const command = new import_commander.Command("download").description("Parse a JATS file and provide a summary").argument("<url>", "The JATS url or a DOI").argument("<output>", "The JATS url or a DOI").action(clirun(
|
|
10414
|
+
const command = new import_commander.Command("download").description("Parse a JATS file and provide a summary").argument("<url>", "The JATS url or a DOI").argument("<output>", "The JATS url or a DOI").action(clirun(downloadAndSaveJats, { program: program2, getSession: getSession2 }));
|
|
10411
10415
|
return command;
|
|
10412
10416
|
}
|
|
10413
10417
|
function addDownloadCLI(program2) {
|
|
@@ -10419,7 +10423,7 @@ function addDownloadCLI(program2) {
|
|
|
10419
10423
|
// src/cli/index.ts
|
|
10420
10424
|
var program = new import_commander2.default.Command();
|
|
10421
10425
|
addDownloadCLI(program);
|
|
10422
|
-
program.version(`v${version_default}`, "-v, --version", "Print the current version of
|
|
10426
|
+
program.version(`v${version_default}`, "-v, --version", "Print the current version of jats-xml");
|
|
10423
10427
|
program.option("-d, --debug", "Log out any errors to the console.");
|
|
10424
10428
|
program.parse(process.argv);
|
|
10425
10429
|
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/cli/parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/cli/parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAqMpC,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,QAI9C"}
|
|
@@ -7,5 +7,5 @@ export declare const elife: Resolver;
|
|
|
7
7
|
export declare const plos: Resolver;
|
|
8
8
|
export declare const joss: Resolver;
|
|
9
9
|
export declare const DEFAULT_RESOLVERS: Resolver[];
|
|
10
|
-
export declare function resolveJatsUrlFromDoi(session: ISession,
|
|
10
|
+
export declare function resolveJatsUrlFromDoi(session: ISession, doiString: string, resolvers?: Resolver[]): Promise<string>;
|
|
11
11
|
//# sourceMappingURL=resolvers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../src/resolvers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IAC/B,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CAClC;AAED,eAAO,MAAM,KAAK,EAAE,QAOnB,CAAC;AAEF,eAAO,MAAM,IAAI,EAAE,QAOlB,CAAC;AAEF,eAAO,MAAM,IAAI,EAAE,QAWlB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,EAAwB,CAAC;AAEjE,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,QAAQ,EACjB,
|
|
1
|
+
{"version":3,"file":"resolvers.d.ts","sourceRoot":"","sources":["../../src/resolvers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;IAC/B,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CAClC;AAED,eAAO,MAAM,KAAK,EAAE,QAOnB,CAAC;AAEF,eAAO,MAAM,IAAI,EAAE,QAOlB,CAAC;AAEF,eAAO,MAAM,IAAI,EAAE,QAWlB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,EAAwB,CAAC;AAEjE,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,QAAQ,EACjB,SAAS,EAAE,MAAM,EACjB,SAAS,aAAoB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAWjB"}
|
package/dist/types/session.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,qBAAa,OAAQ,YAAW,QAAQ;IACtC,
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,qBAAa,OAAQ,YAAW,QAAQ;IACtC,GAAG,EAAE,MAAM,CAAC;gBACA,IAAI,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE;CAGvC;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,WAExC"}
|
package/dist/types/version.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jats-xml",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Types and utilities for working with JATS in Typescript",
|
|
5
5
|
"author": "Rowan Cockett <rowan@curvenote.com>",
|
|
6
6
|
"homepage": "https://github.com/executablebooks/mystjs/tree/main/packages/jats-xml",
|
|
@@ -34,25 +34,25 @@
|
|
|
34
34
|
"scripts": {
|
|
35
35
|
"copy:version": "echo \"const version = '\"$npm_package_version\"';\nexport default version;\" > src/version.ts",
|
|
36
36
|
"clean": "rm -rf dist",
|
|
37
|
-
"prepublishOnly": "npm run build;",
|
|
37
|
+
"prepublishOnly": "npm run copy:version && npm run build;",
|
|
38
38
|
"unlink": "npm uninstall -g jats-xml;",
|
|
39
39
|
"link": "npm run unlink; npm link;",
|
|
40
|
-
"dev": "npm run link && esbuild src/cli/index.ts --bundle --outfile=dist/jats.js --platform=node --watch",
|
|
41
|
-
"test": "copy:version && jest",
|
|
40
|
+
"dev": "npm run copy:version && npm run link && esbuild src/cli/index.ts --bundle --outfile=dist/jats.js --platform=node --watch",
|
|
41
|
+
"test": "npm run copy:version && jest",
|
|
42
42
|
"test:watch": "jest --watchAll",
|
|
43
|
-
"lint": "eslint \"src/**/*.ts\" -c .eslintrc.js --max-warnings 1",
|
|
43
|
+
"lint": "npm run copy:version && eslint \"src/**/*.ts\" -c .eslintrc.js --max-warnings 1",
|
|
44
44
|
"lint:format": "prettier --check src/*.ts src/**/*.ts",
|
|
45
45
|
"build:esm": "tsc --module es2015 --outDir dist/esm",
|
|
46
46
|
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
47
47
|
"build:cli": "esbuild src/cli/index.ts --bundle --outfile=dist/jats.js --platform=node",
|
|
48
48
|
"declarations": "tsc --declaration --emitDeclarationOnly --declarationMap --outDir dist/types",
|
|
49
|
-
"build": "npm-run-all -l clean
|
|
49
|
+
"build": "npm-run-all -l clean copy:version -p build:cjs build:esm declarations build:cli"
|
|
50
50
|
},
|
|
51
51
|
"bugs": {
|
|
52
52
|
"url": "https://github.com/executablebooks/mystjs/issues"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"myst-common": "^0.0.
|
|
55
|
+
"myst-common": "^0.0.7"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"npm-run-all": "^4.1.5",
|