Skip to content

Commit

Permalink
feat: it working
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Feb 14, 2022
1 parent fed7aa9 commit 53f8f03
Show file tree
Hide file tree
Showing 85 changed files with 144,164 additions and 97,835 deletions.
6 changes: 3 additions & 3 deletions libs/ooxast/ooxast-util-to-jast/src/lib/handlers/body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { removePosition } from 'unist-util-remove-position'
it('should work', () => {
const doc = x('w:document', {}, [
x('w:body', {}, [
x('w:p', {}, []),
x('w:p', {}, [
x('w:pPr', {}, [x('w:pStyle', { val: 'Heading1' })]),
x('w:pPr', {}, [x('w:pStyle', { 'w:val': 'Heading1' })]),
x('w:r', {}, [
x('w:rPr', {}, []),
x('w:t', {}, [{ type: 'text', value: 'hey' }]),
]),
]),
x('w:p', {}, []),
]),
])
expect(toJast(doc)).toEqual(
x('sec', {}, [x('title', {}, [{ type: 'text', value: 'hey' }])])
x('body', x('sec', {}, [x('title', {}, [{ type: 'text', value: 'hey' }])]))
)
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { T, Text } from 'ooxast'
import { getCitationCSL } from './citation'
const mendeleyCitation: T = {
type: 'element',
name: 'w:instrText',
Expand Down
17 changes: 9 additions & 8 deletions libs/ooxast/ooxast-util-to-jast/src/lib/handlers/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export function citation(j: J, citation: T, parent: Parent) {
// i const t = select('', citation) as T
// if (!t) return
const text = citation.children[0].value
if (text.includes('PAGE \\* MERGEFORMAT')) return
const type = text.includes('CSL_CITATION') || '}'
// Zotero/mendely citation: easy
if (type) {
if (type && !text?.slice(0, 50)?.includes('Bibliography')) {
let citation: { [key: string]: any }
const json = text.replace(/ADDIN .*?CSL_CITATION/, '')
try {
Expand Down Expand Up @@ -53,21 +54,21 @@ export function citation(j: J, citation: T, parent: Parent) {
const citation: CSL = cite.itemData
j.collectCitation(citation)
j.citationNumber++
return {
type: 'element',
name: 'xref',
attributes: {
return j(
citation,
'xref',
{
id: `_xref-${j.citationNumber}`,
refType: 'bibr',
rid: `bib${j.citationNumber}`,
},
children: [
[
{
type: 'text',
value: formattedCitations?.[i] || `[${j.citationNumber}]`,
},
],
}
]
)
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion libs/ooxast/ooxast-util-to-jast/src/lib/handlers/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { Document } from 'ooxast'
export function document(j: J, node: Document): Article {
return x('article', [
x('front') as Front,
x('body', {}, all(j, node)) as Body,
// @ts-ignore: hush.
all(j, node),
x('back') as Back,
]) as Article
}
4 changes: 3 additions & 1 deletion libs/ooxast/ooxast-util-to-jast/src/lib/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import { footnotes } from './footnotes'
import { footnote } from './footnote'
import { document } from './document'
import { r } from './r'
import { tr } from './tr'

export const handlers = {
p,
body,
root,
text: text,
text,
document,
footnotes,
footnote,
t: all,
r,
instrText: citation,
tbl,
tr,
}
12 changes: 2 additions & 10 deletions libs/ooxast/ooxast-util-to-jast/src/lib/handlers/p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ import { wrapChildren } from '../util/wrap-children'
import { getPStyle } from '../util/get-pstyle'

export function p(j: J, p: P, parent: Parent) {
if (j.inTable) return all(j, p)
const style = getPStyle(p)
return j(
p,
'element',
{
name: 'p',

...(style ? { style } : {}),
},
all(j, p)
)
return j(p, 'p', { ...(style ? { style } : {}) }, all(j, p))
}
21 changes: 14 additions & 7 deletions libs/ooxast/ooxast-util-to-jast/src/lib/handlers/r.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,44 @@ import { J } from '../types'
import { Italic, Bold, Underline, Strike, Sc } from 'jjast'

export function r(j: J, node: R) {
if (!node?.children?.[1] || node?.children?.[1]?.name === 'w:fldChar') return
if (node?.children?.[1]?.name === 'w:fldChar') return
if (node?.children?.[1]?.name === 'w:instrText') {
j.deleteNextRun = false
return all(j, node)
}
if (j.deleteNextRun) {
j.deleteNextRun = false
return
}

const props = select('w\\:rPr', node) as RPr
let text = j(node, 'text', all(j, node))
const mergedText = all(j, node).reduce((acc, curr) => {
if (curr.type !== 'text') return acc
acc = acc + curr.value
return acc
}, '')

let text = { type: 'text', value: mergedText } as any
if (!props) return text

for (let i = 0; i < props.children.length; i++) {
const prop = props.children[i]
switch (prop.name.replace('w:', '')) {
case 'i':
text = x('italic', {}, text) as Italic
text = x('italic', {}, text)
continue
case 'b':
text = x('bold', {}, text) as Bold
text = x('bold', {}, text)
continue
case 'u':
text = x('underline', {}, text) as Underline
text = x('underline', {}, text)
continue
case 'strike':
case 'dstrike':
text = x('strike', {}, text) as Strike
text = x('strike', {}, text)
continue
case 'smallCaps':
text = x('sc', {}, text) as Sc
text = x('sc', {}, text)
continue
default:
continue
Expand Down
20 changes: 19 additions & 1 deletion libs/ooxast/ooxast-util-to-jast/src/lib/handlers/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,25 @@ import { J, Node, Root } from '../types'

export function root(j: J, node: Root) {
// if (!article) {
return j(node, 'root', all(j, node))
const children: any[] = [
{
type: 'instruction',
name: 'xml',
value: 'version="1.0" encoding="utf-8"',
},
{
type: 'doctype',
name: 'article',
public:
'-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.2 20190208//EN',
system:
'https://jats.nlm.nih.gov/publishing/1.2/JATS-journalpublishing1.dtd',
},
]

const content = all(j, node)
content.unshift(...children)
return j(node, 'root', content)
// }

// return j(article, 'root', [
Expand Down
7 changes: 5 additions & 2 deletions libs/ooxast/ooxast-util-to-jast/src/lib/handlers/tbl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { Tbl } from 'ooxast'
import { all } from '../all'

export function tbl(j: J, tbl: Tbl) {
return {
j.inTable = true
const table = {
type: 'element',
name: 'tableWrap',
attributes: {},
children: [j(tbl, 'element', { name: 'table' }, all(j, tbl))],
children: [j(tbl, 'table', {}, [j(tbl, 'tbody', {}, all(j, tbl))])],
}
j.inTable = false
return table
}
6 changes: 3 additions & 3 deletions libs/ooxast/ooxast-util-to-jast/src/lib/handlers/tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export function tr(j: J, tr: Row) {
type: 'element',
name: 'td',
attributes: {},
// its fine
children: [cell as any],
// @ts-ignore
children: cell?.children || [cell],
})
}
return j(tr, 'element', { name: 'tr' }, celldRowContents)
return j(tr, 'tr', celldRowContents)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export function toJast(
}

// @ts-ignore Assume valid `type` and `children`/`value`.
const result: Node = { type, ...attributes }
const result: Node = Object.assign(
{},
['root', 'text'].includes(type)
? { type }
: { type: 'element', name: type },
{ attributes }
)

if (typeof children === 'string') {
// @ts-ignore: Looks like a literal.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 53f8f03

Please sign in to comment.