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

✍️ LaTeX/JATS improvements for Proofs #694

Merged
merged 3 commits into from
Oct 22, 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
9 changes: 9 additions & 0 deletions .changeset/gold-plants-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'tex-to-myst': patch
---

Add `algorithm` and `algorithmic` handlers for latex parsing

- Figures: Environment centering
- newtheorem in frontmatter is parsed
- safely ignore `itemsep`, `setcounter`, `cmidrule` for now
5 changes: 5 additions & 0 deletions .changeset/modern-pigs-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-spec-ext': patch
---

Add `Line` to myst-spec-ext
5 changes: 5 additions & 0 deletions .changeset/wise-spiders-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-jats': patch
---

Add statements to JATS, count and number `statements`
20 changes: 19 additions & 1 deletion packages/myst-spec-ext/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
Code as SpecCode,
ListItem as SpecListItem,
Container as SpecContainer,
InlineMath as SpecInlineMath,
} from 'myst-spec';

type Visibility = 'show' | 'hide' | 'remove';
Expand All @@ -36,6 +37,22 @@ export type CaptionNumber = Parent & {
enumerator: string;
};

/**
* AlgorithmLine is, e.g., a line in an algorithm and can be numbered as well as indented.
* Otherwise this works the same as a paragraph, ideally with tighter styling.
* The Line is used in Algorithms (e.g. when parsing from LaTeX)
*/
export type AlgorithmLine = Parent & {
type: 'algorithmLine';
indent?: number;
enumerator?: string;
};

export type InlineMath = SpecInlineMath & {
label?: string;
identifier?: string;
};

export type FootnoteDefinition = FND & {
/** @deprecated this should be enumerator */
number?: number;
Expand Down Expand Up @@ -184,6 +201,7 @@ export type Include = {
caption?: (FlowContent | ListContent | PhrasingContent)[];
};

export type Container = SpecContainer & {
export type Container = Omit<SpecContainer, 'kind'> & {
kind?: 'figure' | 'table' | 'quote' | 'code';
source?: Dependency;
};
173 changes: 160 additions & 13 deletions packages/myst-to-jats/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
import type { Root, CrossReference, TableCell as SpecTableCell, Math, InlineMath } from 'myst-spec';
import type { Cite, Code, FootnoteDefinition, FootnoteReference } from 'myst-spec-ext';
import type {
Root,
CrossReference,
TableCell as SpecTableCell,
Math,
Text,
Paragraph,
Blockquote,
List,
ThematicBreak,
Role,
Directive,
Comment,
Strong,
Emphasis,
Underline,
InlineCode,
Subscript,
Superscript,
Abbreviation,
Link,
AdmonitionTitle,
Table,
Caption,
Break,
} from 'myst-spec';
import type {
Block,
Cite,
Code,
DefinitionTerm,
DefinitionDescription,
DefinitionList,
FootnoteDefinition,
FootnoteReference,
Heading,
AlgorithmLine,
ListItem,
InlineMath,
Image,
Delete,
Smallcaps,
Admonition,
Container,
CaptionNumber,
CiteGroup,
} from 'myst-spec-ext';
import type { Plugin } from 'unified';
import { VFile } from 'vfile';
import { xml2js } from 'xml-js';
Expand Down Expand Up @@ -36,6 +81,7 @@ import type { IdInventory } from './transforms/references.js';
import type { Section } from './transforms/sections.js';
import { sectionAttrsFromBlock } from './transforms/sections.js';
import { inlineExpression } from './inlineExpression.js';
import type { DefinitionItem } from './transforms/definitions.js';

type TableCell = SpecTableCell & { colspan?: number; rowspan?: number; width?: number };

Expand All @@ -44,7 +90,7 @@ function escapeForXML(text: string) {
}

function referenceKindToRefType(kind?: string): RefType {
switch (kind) {
switch (kind?.split(':')[0]) {
case 'heading':
return RefType.sec;
case 'figure':
Expand All @@ -53,6 +99,8 @@ function referenceKindToRefType(kind?: string): RefType {
return RefType.dispFormula;
case 'table':
return RefType.table;
case 'proof':
return RefType.statement;
default:
return RefType.custom;
}
Expand Down Expand Up @@ -133,10 +181,12 @@ function addMmlAndRemoveAnnotation(el?: Element) {

function mathToMml(node: Math | InlineMath) {
const math = copyNode(node);
// TODO: add macros
// TODO: add macros, log errors
renderEquation(new VFile(), math, { mathML: true });
const katexJs = xml2js((math as any).html, { compact: false }) as Element;
const spanElement = katexJs.elements?.[0];
const katexJs = (math as any).html
? (xml2js((math as any).html, { compact: false }) as Element)
: undefined;
const spanElement = katexJs?.elements?.[0];
const mathElement = spanElement?.elements?.[0];
if (!mathElement) return;
const inline = node.type === 'inlineMath';
Expand Down Expand Up @@ -165,7 +215,71 @@ function cleanLatex(value?: string): string | undefined {
.trim();
}

const handlers: Record<string, Handler> = {
// TODO: this should be based on some information of the proof, or myst config
function capitalize(kind?: string) {
if (!kind) return '';
return kind.slice(0, 1).toUpperCase() + kind.slice(1);
}

type Handlers = {
text: Handler<Text>;
paragraph: Handler<Paragraph>;
section: Handler<Section>;
heading: Handler<Heading>;
block: Handler<Block>;
blockquote: Handler<Blockquote>;
definitionList: Handler<DefinitionList>;
definitionItem: Handler<DefinitionItem>;
definitionTerm: Handler<DefinitionTerm>;
definitionDescription: Handler<DefinitionDescription>;
code: Handler<Code>;
list: Handler<List>;
listItem: Handler<ListItem>;
thematicBreak: Handler<ThematicBreak>;
inlineMath: Handler<InlineMath>;
math: Handler<Math>;
mystRole: Handler<Role>;
mystDirective: Handler<Directive>;
comment: Handler<Comment>;
strong: Handler<Strong>;
emphasis: Handler<Emphasis>;
underline: Handler<Underline>;
inlineCode: Handler<InlineCode>;
subscript: Handler<Subscript>;
superscript: Handler<Superscript>;
delete: Handler<Delete>;
smallcaps: Handler<Smallcaps>;
break: Handler<Break>;
abbreviation: Handler<Abbreviation>;
link: Handler<Link>;
admonition: Handler<Admonition>;
admonitionTitle: Handler<AdmonitionTitle>;
attrib: Handler<GenericNode>;
table: Handler<Table>;
tableHead: Handler<GenericNode>;
tableBody: Handler<GenericNode>;
tableFooter: Handler<GenericNode>;
tableRow: Handler<GenericNode>;
tableCell: Handler<TableCell>;
image: Handler<Image>;
container: Handler<Container>;
caption: Handler<Caption>;
captionNumber: Handler<CaptionNumber>;
crossReference: Handler<CrossReference>;
citeGroup: Handler<CiteGroup>;
cite: Handler<Cite>;
footnoteReference: Handler<FootnoteReference>;
footnoteDefinition: Handler<FootnoteDefinition>;
si: Handler<GenericNode>;
proof: Handler<GenericNode>;
algorithmLine: Handler<AlgorithmLine>;
output: Handler<GenericNode>;
embed: Handler<GenericNode>;
supplementaryMaterial: Handler<SupplementaryMaterial>;
inlineExpression: Handler<GenericNode>;
};

const handlers: Handlers = {
text(node, state) {
state.text(node.value);
},
Expand Down Expand Up @@ -396,7 +510,7 @@ const handlers: Record<string, Handler> = {
state.renderInline(node, 'caption');
},
captionNumber(node, state) {
delete node.identifier;
delete (node as any).identifier;
state.renderInline(node, 'label');
},
crossReference(node, state) {
Expand Down Expand Up @@ -441,11 +555,42 @@ const handlers: Record<string, Handler> = {
},
si(node, state) {
// <named-content content-type="quantity">5 <abbrev content-type="unit" alt="milli meter">mm</abbrev></named-content>
state.openNode('named-content', { 'content-type': 'quantity' });
if (node.number != null) state.text(`${node.number} `);
const hasNumber = node.number != null;
if (hasNumber) {
state.openNode('named-content', { 'content-type': 'quantity' });
state.text(`${node.number} `);
}
state.openNode('abbrev', { 'content-type': 'unit', alt: node.alt });
state.text(node.unit);
state.closeNode();
if (hasNumber) state.closeNode();
},
proof(node, state) {
state.openNode('statement', { 'specific-use': node.kind, id: node.identifier });
const [title, ...rest] = node.children ?? [];
const useTitle = title && title.type === 'admonitionTitle';
if (node.enumerated) {
state.openNode('label');
state.text(`${capitalize(node.kind)} ${node.enumerator}`);
state.closeNode();
}
if (useTitle) {
state.openNode('title');
state.renderChildren(title);
state.closeNode();
}
state.renderChildren(useTitle ? rest : node.children);
state.closeNode();
},
algorithmLine(node, state) {
state.openNode('p', { 'specific-use': 'line' });
if (node.enumerator) {
state.openNode('x');
state.text(`${node.enumerator}: `);
state.closeNode();
}
state.text(Array(node.indent).fill(' ').join(''));
state.renderChildren(node);
state.closeNode();
},
output(node, state) {
Expand Down Expand Up @@ -652,11 +797,13 @@ class JatsSerializer implements IJatsSerializer {
return node;
}

renderChildren(node: GenericNode) {
node.children?.forEach((child) => {
renderChildren(node: GenericNode | GenericNode[]) {
const parent = Array.isArray(node) ? { children: node } : node;
const children = Array.isArray(node) ? node : node.children;
children?.forEach((child) => {
const handler = this.handlers[child.type];
if (handler) {
handler(child, this, node);
handler(child, this, parent);
} else {
fileError(this.file, `Unhandled JATS conversion for node of "${child.type}"`, {
node: child,
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-jats/src/transforms/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { DefinitionList } from 'myst-spec-ext';
import { selectAll } from 'unist-util-select';
import type { GenericParent } from 'myst-common';

type DefinitionItem = Parent & { type: 'definitionItem' };
export type DefinitionItem = Parent & { type: 'definitionItem' };

export function definitionTransform(mdast: GenericParent) {
const defList = selectAll('definitionList', mdast) as DefinitionList[];
Expand Down
6 changes: 6 additions & 0 deletions packages/myst-to-jats/src/transforms/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type IdInventory = {
quote?: CountAndLookup;
cite?: CountAndLookup;
footnote?: CountAndLookup;
proof?: CountAndLookup;
};

const CONTAINER_KINDS: (keyof IdInventory)[] = ['figure', 'table', 'code', 'quote'];
Expand Down Expand Up @@ -63,6 +64,10 @@ export function referenceTargetTransform(
footnotes.forEach((fn) => {
updateInventory(fn, 'footnote', 'fn', inventory);
});
const proofs = selectAll('proof', mdast) as GenericNode[];
proofs.forEach((fn) => {
updateInventory(fn, 'proof', 'stm', inventory);
});
const containers = selectAll('container', mdast) as GenericNode[];
containers.forEach((container) => {
if (!container.kind || !CONTAINER_KINDS.includes(container.kind as any)) {
Expand Down Expand Up @@ -97,6 +102,7 @@ export function referenceResolutionTransform(mdast: GenericParent, inventory: Id
...inventory.table?.lookup,
...inventory.code?.lookup,
...inventory.quote?.lookup,
...inventory.proof?.lookup,
};
xrefs.forEach((xref) => {
if (xref.identifier && lookup[xref.identifier]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-jats/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type Element = {
elements?: Element[];
};

export type Handler = (node: GenericNode, state: IJatsSerializer, parent: any) => void;
export type Handler<T = any> = (node: T, state: IJatsSerializer, parent: any) => void;

export type MathPlugins = Required<PageFrontmatter>['math'];

Expand Down
2 changes: 1 addition & 1 deletion packages/myst-to-jats/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ beforeEach(() => {
});

describe('Basic JATS body', () => {
const cases = [...loadCases('basic.yml'), ...loadCases('siunit.yml')];
const cases = [...loadCases('basic.yml'), ...loadCases('siunit.yml'), ...loadCases('proof.yml')];
test.each(cases.map((c): [string, TestCase] => [c.title, c]))('%s', async (_, { tree, jats }) => {
const pipe = unified().use(
mystToJats,
Expand Down
27 changes: 27 additions & 0 deletions packages/myst-to-jats/tests/proof.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cases:
- title: Proof
tree:
type: root
children:
- type: proof
kind: algorithm
label: alg:cap
identifier: alg:cap
enumerated: true
enumerator: '1'
children:
- type: admonitionTitle
children:
- type: text
value: An algorithm with caption
- type: algorithmLine
indent: 1
enumerator: '1'
children:
- type: strong
children:
- type: text
value: 'Require: '
- type: text
value: 'alg'
jats: '<statement specific-use="algorithm" id="alg:cap"><label>Algorithm 1</label><title>An algorithm with caption</title><p specific-use="line"><x>1: </x> <bold>Require: </bold>alg</p></statement>'
Loading
Loading