Skip to content

Commit

Permalink
Add support for id in decodeMath for graphic or inline-graphic (#1083)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Byrne <will.alex.byrne@gmail.com>
Co-authored-by: A. K <74007089+soggy-mushroom@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent 3d3976b commit ad06ba0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6141,6 +6141,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn1",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -6282,6 +6283,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn2",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -6369,6 +6371,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn3",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -6481,6 +6484,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn4",
"contentUrl": "",
"meta": {
"inline": false
Expand All @@ -6493,6 +6497,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn5",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -6635,6 +6640,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn6",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -7019,6 +7025,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn7",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -7063,6 +7070,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn8",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -7166,6 +7174,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn9",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -7286,6 +7295,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn10",
"contentUrl": "",
"meta": {
"inline": false
Expand Down Expand Up @@ -7319,6 +7329,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn11",
"contentUrl": "",
"meta": {
"inline": false
Expand All @@ -7337,6 +7348,7 @@
"content": [
{
"type": "ImageObject",
"id": "eqn12",
"contentUrl": "",
"meta": {
"inline": false
Expand Down
23 changes: 23 additions & 0 deletions src/codecs/jats/__file_snapshots__/plosone-0093988.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e010
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2036,6 +2037,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e032
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2292,6 +2294,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e038
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2336,6 +2339,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e044
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2626,6 +2630,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e062
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2665,6 +2670,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e068
contentUrl: ''
meta:
inline: false
Expand All @@ -2675,6 +2681,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e069
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2770,6 +2777,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e082
contentUrl: ''
meta:
inline: false
Expand All @@ -2795,6 +2803,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e085
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2847,6 +2856,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e091
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2875,6 +2885,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e094
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -2948,6 +2959,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e103
contentUrl: ''
meta:
inline: false
Expand All @@ -2958,6 +2970,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e104
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -3009,6 +3022,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e111
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -3236,6 +3250,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e140
contentUrl: ''
meta:
inline: false
Expand All @@ -3252,6 +3267,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e142
contentUrl: ''
meta:
inline: false
Expand All @@ -3264,6 +3280,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e143
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -3350,6 +3367,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e155
contentUrl: ''
meta:
inline: false
Expand All @@ -3366,6 +3384,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e157
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -3525,6 +3544,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e180
contentUrl: ''
meta:
inline: false
Expand All @@ -3541,6 +3561,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e182
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -3921,6 +3942,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e232
contentUrl: ''
meta:
inline: false
Expand Down Expand Up @@ -4047,6 +4069,7 @@ content:
- type: Paragraph
content:
- type: ImageObject
id: pone.0093988.e249
contentUrl: ''
meta:
inline: false
Expand Down
6 changes: 3 additions & 3 deletions src/codecs/jats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2518,20 +2518,20 @@ function decodeSupplementaryMaterial(
* that is not available, uses an image as an alternative (which is wrapped
* in a paragraph for display formulas).
*/
function decodeMath(
export function decodeMath(
formula: xml.Element,
): (stencila.Math | stencila.ImageObject | stencila.Paragraph)[] {
const inline = formula.name === 'inline-formula'
const mathml = first(formula, 'mml:math') ?? first(formula, 'm:math')
const id = attrOrUndefined(formula, 'id')

if (mathml === null) {
const graphic = first(formula, ['graphic', 'inline-graphic'])
if (graphic === null) return []
const image = decodeGraphic(graphic, inline)
const image = [{ id, ...decodeGraphic(graphic, inline)[0] }]
return inline ? image : [stencila.paragraph({ content: image })]
}

const id = attrOrUndefined(formula, 'id')
const label = textOrUndefined(child(formula, 'label'))
const altText = attrOrUndefined(mathml, 'alttext')
const meta = altText !== undefined ? { altText } : undefined
Expand Down
16 changes: 16 additions & 0 deletions src/codecs/jats/jats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
decodeAff,
decodeAuthors,
decodeFigure,
decodeMath,
decodeMetaFront,
decodeReference,
DecodeState,
Expand Down Expand Up @@ -527,3 +528,18 @@ describe('authors', () => {
)
})
})

describe('decode: MathML', () => {
test('adds the id to the alternative graphic', () => {
const formula = xml.load(`
<disp-formula id="eqn6">
<alternatives>
<graphic xlink:href="528254v1_eqn6.gif"/>
</alternatives>
</disp-formula>
`).elements?.[0]!

expect(decodeMath(formula)).toEqual([{"content": [{"contentUrl": "528254v1_eqn6.gif", "meta": {"inline": false}, "type": "ImageObject", "id": "eqn6"}], "type": "Paragraph"}]);
});
});

0 comments on commit ad06ba0

Please sign in to comment.