Skip to content

Commit

Permalink
fix(JATS): Decode <fig> elements as a Table if label starts wit…
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jan 24, 2023
1 parent ec410f0 commit 2ada6a6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/codecs/jats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ async function encodePrepare(node: stencila.Node): Promise<stencila.Node> {
* An alternative approach would be to make the functions
* methods of a class (e.g. the `JatsCodec` class).
*/
interface DecodeState {
export interface DecodeState {
/**
* The current `<article>` element. Used for
* getting the target of internal references.
Expand Down Expand Up @@ -2257,11 +2257,15 @@ function encodeFigGroup(

/**
* Decode a JATS `<fig>` element to a Stencila `Figure` node.
*
* Sometimes the <fig> element is used for tables (where the table is represented
* as an image). So, if the <label> starts with 'Table', this function will return
* a table with the image(s) in the `content` property.
*/
function decodeFigure(
export function decodeFigure(
elem: xml.Element,
state: DecodeState
): [stencila.Figure] {
): [stencila.Figure | stencila.Table] {
state = { ...state, ancestorElem: elem }

const id = decodeInternalId(attr(elem, 'id'))
Expand Down Expand Up @@ -2296,7 +2300,11 @@ function decodeFigure(
}
}

return [stencila.figure({ id, label, caption, content, licenses })]
return [
label?.startsWith('Table')
? stencila.table({ id, label, caption, content, licenses, rows: [] })
: stencila.figure({ id, label, caption, content, licenses }),
]
}

/**
Expand Down
44 changes: 43 additions & 1 deletion src/codecs/jats/jats.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { decodeAff, JatsCodec } from '.'
import { decodeAff, decodeFigure, DecodeState, JatsCodec } from '.'
import { fixture, snapshot } from '../../__tests__/helpers'
import {
asciimathFragment,
Expand Down Expand Up @@ -97,6 +97,48 @@ test('decode: affiliation retains content not in <institution> or address elemen
})
})

test('decode: <fig> element that is actually a table', () => {
const fig = xml.load(`
<fig id="tbl1" orientation="portrait" position="float">
<label>Table 1.</label>
<caption><p>Descriptive statistics of the adolescent lifestyle-related variables in all twins and in the subsample of twins with information on biological aging.</p></caption>
<graphic xlink:href="22275761v1_tbl1.tif"/>
<graphic xlink:href="22275761v1_tbl1a.tif"/>
</fig>
`).elements?.[0]!

expect(decodeFigure(fig, {} as DecodeState)[0]).toEqual({
type: 'Table',
id: 'tbl1',
label: 'Table 1.',
caption: [
{
type: 'Paragraph',
content: [
'Descriptive statistics of the adolescent lifestyle-related variables in all twins and in the subsample of twins with information on biological aging.',
],
},
],
content: [
{
type: 'ImageObject',
contentUrl: '22275761v1_tbl1.tif',
meta: {
inline: false,
},
},
{
type: 'ImageObject',
contentUrl: '22275761v1_tbl1a.tif',
meta: {
inline: false,
},
},
],
rows: [],
})
})

test.each([
'fig.xml',
'statement.xml',
Expand Down

0 comments on commit 2ada6a6

Please sign in to comment.