Skip to content

Commit

Permalink
📖 Update serialization to XML (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Oct 24, 2023
1 parent 570653f commit f88ba59
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/old-keys-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jats-utils': patch
---

Update serialization pretty-print
5 changes: 5 additions & 0 deletions .changeset/strange-mugs-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'jats-xml': patch
---

Ensure table-wrap has correct order
4 changes: 4 additions & 0 deletions packages/jats-utils/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const both = [
'body',
'sec',
'fig',
'statement',
'list',
'disp-formula',
'disp-formula-group',
'table-wrap',
'caption',
'table',
Expand Down Expand Up @@ -74,6 +77,7 @@ const first = [
'meta-value',
'title',
'p',
'license-p',
'tr',
'label',
'graphic',
Expand Down
7 changes: 6 additions & 1 deletion packages/jats-xml/src/jats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type {
} from 'jats-tags';
import type { Logger } from 'myst-cli-utils';
import { tic } from 'myst-cli-utils';
import { articleMetaOrder } from './order.js';
import { articleMetaOrder, tableWrapOrder } from './order.js';
import {
serializeJatsXml,
type SerializationOptions,
Expand Down Expand Up @@ -220,6 +220,11 @@ export class Jats {
articleMetaOrder.findIndex((x) => x === b.type),
);
}
(selectAll('table-wrap', this.tree) as GenericParent[]).forEach((tw) => {
tw.children = tw.children.sort(
(a, b) => (tableWrapOrder[a.type] ?? -1) - (tableWrapOrder[b.type] ?? -1),
);
});
}

serialize(opts?: WriteOptions): string {
Expand Down
39 changes: 39 additions & 0 deletions packages/jats-xml/src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,42 @@ export const articleMetaOrder = [
'counts',
'custom-meta-group',
];

function order(tags: (string | string[])[]): Record<string, number> {
return Object.fromEntries(
tags
.map((tag, i) => {
if (typeof tag === 'string') return [[tag, i]];
return tag.map((t) => [t, i]);
})
.flat() as [string, number][],
);
}

export const tableWrapOrder = order([
'object-id',
'label',
'caption',
'abstract',
'kwd-group',
'subj-group',
['alt-text', 'long-desc', 'email', 'ext-link', 'uri'],
[
'disp-quote',
'speech',
'statement',
'verse-group',
'def-list',
'list',
'alternatives',
'chem-struct-wrap',
'code',
'disp-formula',
'graphic',
'media',
'preformat',
'table',
'xref',
],
['table-wrap-foot', 'attrib', 'permissions'],
]);

0 comments on commit f88ba59

Please sign in to comment.