Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Serialize JATS in a standardized way #666

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 47 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/jats-to-myst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
},
"dependencies": {
"doi-utils": "^2.0.0",
"jats-tags": "^1.0.0",
"jats-xml": "^1.0.6",
"jats-tags": "^1.0.8",
"jats-xml": "^1.0.8",
"myst-common": "^1.1.7",
"myst-frontmatter": "^1.1.7",
"myst-spec": "^0.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"jtex": "^1.0.8",
"latest-version": "^7.0.0",
"mdast": "^3.0.0",
"meca": "^1.0.6",
"meca": "^1.0.8",
"mime-types": "^2.1.35",
"myst-cli-utils": "^2.0.4",
"myst-common": "^1.1.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/build/jats/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function runJatsExport(
const jats = writeJats(vfile, processedArticle as any, {
subArticles: processedSubArticles as any,
writeFullArticle: true,
spaces: 0,
format: 'pretty',
abstractParts: [
{ part: 'abstract' },
{
Expand Down
5 changes: 3 additions & 2 deletions packages/myst-to-jats/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"citation-js-utils": "^1.0.2",
"credit-roles": "^2.1.0",
"doi-utils": "^2.0.1",
"jats-tags": "^1.0.1",
"jats-tags": "^1.0.8",
"jats-utils": "^1.0.8",
"katex": "^0.15.2",
"myst-common": "^1.1.7",
"myst-frontmatter": "^1.1.7",
Expand All @@ -55,7 +56,7 @@
"@types/js-yaml": "^4.0.5",
"@types/katex": "^0.14.0",
"@types/mdast": "^3.0.10",
"jats-xml": "^1.0.6",
"jats-xml": "^1.0.7",
"js-yaml": "^4.1.0",
"myst-cli-utils": "^2.0.4"
}
Expand Down
18 changes: 4 additions & 14 deletions packages/myst-to-jats/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Root, CrossReference, TableCell as SpecTableCell } from 'myst-spec
import type { Cite, Code, FootnoteDefinition, FootnoteReference } from 'myst-spec-ext';
import type { Plugin } from 'unified';
import type { VFile } from 'vfile';
import { js2xml, xml2js } from 'xml-js';
import { xml2js } from 'xml-js';
import katex from 'katex';
import type { CitationRenderer } from 'citation-js-utils';
import type { MessageInfo, GenericNode, GenericParent } from 'myst-common';
Expand All @@ -11,6 +11,7 @@ import type { PageFrontmatter, Contributor } from 'myst-frontmatter';
import { SourceFileKind } from 'myst-spec-ext';
import type { Affiliation } from 'jats-tags';
import { Tags, RefType } from 'jats-tags';
import { serializeJatsXml } from 'jats-utils';
import type { MinifiedOutput } from 'nbtx';
import { getBack } from './backmatter.js';
import { getArticleMeta, getFront } from './frontmatter.js';
Expand Down Expand Up @@ -870,19 +871,8 @@ export function writeJats(file: VFile, content: ArticleContent, opts?: DocumentO
declaration: { attributes: { version: '1.0', encoding: 'UTF-8' } },
}
: doc.body();
const jats = js2xml(element, {
compact: false,
// No way to write XML with new lines, but no indentation with js2xml.
// If you use 0 or '', you get a single line.
spaces: opts?.spaces === 'flat' ? 0 : opts?.spaces || 1,
attributeValueFn: escapeForXML,
});
if (!opts?.spaces) {
// either `0` or `''`
file.result = jats.replace(/\n(\s*)</g, '\n<');
} else {
file.result = jats;
}
const xml = serializeJatsXml(element, opts);
file.result = xml;
return file;
}

Expand Down
16 changes: 6 additions & 10 deletions packages/myst-to-jats/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PageFrontmatter } from 'myst-frontmatter';
import type { Root } from 'myst-spec';
import type { SourceFileKind } from 'myst-spec-ext';
import type { CitationRenderer } from 'citation-js-utils';
import type { SerializationOptions } from 'jats-utils';

export type Attributes = Record<string, string | undefined>;

Expand Down Expand Up @@ -31,16 +32,11 @@ export type Options = {
backSections?: JatsPart[];
};

export type DocumentOptions = Options & {
subArticles?: ArticleContent[];
/**
* When 'flat', the xml will be on a single line (with exception of CDATA),
* When `0`, the XML will be on different lines with 0 spaces.
* When any other value (e.g. `2` or `\t`) the XML will be indented at the start of the line by that amount.
*/
spaces?: number | 'flat' | '\t';
writeFullArticle?: boolean;
};
export type DocumentOptions = Options &
SerializationOptions & {
subArticles?: ArticleContent[];
writeFullArticle?: boolean;
};

export type StateData = {
isInContainer?: boolean;
Expand Down
14 changes: 7 additions & 7 deletions packages/myst-to-jats/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('Basic JATS body', () => {
undefined,
undefined,
undefined,
{ spaces: 'flat' },
{ format: 'flat' },
);
pipe.runSync(tree as any);
const vfile = pipe.stringify(tree as any);
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('JATS full article', () => {
undefined,
{
writeFullArticle: true,
spaces: 2,
format: 2,
abstractParts: [
{ part: 'abstract' },
{
Expand All @@ -126,7 +126,7 @@ describe('JATS full article', () => {
);
pipe.runSync(tree as any);
const vfile = pipe.stringify(tree as any);
expect(vfile.result).toEqual(jats);
expect((vfile.result as string).trim()).toEqual(jats);
if (TEST_DTD) expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
},
);
Expand All @@ -151,10 +151,10 @@ describe('JATS multi-article', () => {
{
subArticles: subArticles as any,
writeFullArticle: true,
spaces: 2,
format: 2,
},
);
expect(vfile.result).toEqual(jats);
expect((vfile.result as string)?.trim()).toEqual(jats);
if (TEST_DTD) expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
},
);
Expand All @@ -173,12 +173,12 @@ describe('JATS full notebook', () => {
undefined,
{
writeFullArticle: true,
spaces: 2,
format: 2,
},
);
pipe.runSync(tree as any);
const vfile = pipe.stringify(tree as any);
expect(vfile.result).toEqual(jats);
expect((vfile.result as string).trim()).toEqual(jats);
if (TEST_DTD) expect(await writeValidateDelete(vfile.result as string)).toBeTruthy();
},
);
Expand Down
2 changes: 1 addition & 1 deletion packages/mystmd/tests/outputs/basic-md-and-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<body>
<p>Hello world</p>
</body>
</article>
</article>
Loading