Skip to content

Commit

Permalink
feat(JATS): Add statement decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
rgieseke committed Apr 14, 2021
1 parent da0c2ce commit 66f7d81
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/codecs/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,7 @@ type CreativeWorkTagMap = {
const creativeWorkTagMap: CreativeWorkTagMap = {
Article: 'article',
AudioObject: 'audio',
Claim: 'div',
Collection: 'div',
Comment: 'div',
CreativeWork: 'div',
Expand Down
21 changes: 21 additions & 0 deletions src/codecs/jats/__fixtures__/statement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<article>
<front>
<article-meta>
<contrib-group />
</article-meta>
</front>
<body>
<statement id="statement1">
<label>Hypothesis 1</label>
<p>It's a hypothesis.</p>
</statement>
<statement id="statement2">
<title>Theorem 1</title>
<p>It's a theorem.</p>
</statement>
</body>
<back>
<app-group />
</back>
</article>
33 changes: 33 additions & 0 deletions src/codecs/jats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,8 @@ function decodeElement(elem: xml.Element, state: DecodeState): stencila.Node[] {
return decodeCode(elem)
case 'fn':
return decodeNote(elem, state)
case 'statement':
return decodeStatement(elem, state)
default:
log.warn(`Using default decoding for JATS element name: "${elem.name}"`)
return decodeDefault(elem, state)
Expand Down Expand Up @@ -2344,3 +2346,34 @@ function decodeNote(elem: xml.Element, state: DecodeState): [stencila.Note] {
}),
]
}

/**
* Decode a JATS `<statement>` element as a Stencila `Claim`.
*/
function decodeStatement(
elem: xml.Element,
state: DecodeState
): [stencila.Claim] {
const id = attrOrUndefined(elem, 'id')
const label = textOrUndefined(child(elem, ['label', 'title']))
const content = []
if (elem.elements) {
for (const item of elem.elements) {
if (
typeof item.name === 'string' &&
!['label', 'title', 'kwd-group', 'attrib', 'permission'].includes(
item.name
)
) {
content.push(...decodeElement(item, state))
}
}
}
return [
stencila.claim({
id,
label,
content: ensureBlockContentArray(content),
}),
]
}
1 change: 1 addition & 0 deletions src/codecs/jats/jats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('encode: Math', () => {

test.each([
'fig.xml',
'statement.xml',
'elife-30274-v1',
'elife-43154-v2',
'elife-46472-v3',
Expand Down

0 comments on commit 66f7d81

Please sign in to comment.