Skip to content

Commit

Permalink
feat: citations from word to latex works
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Feb 15, 2022
1 parent 0646a95 commit 1582e25
Show file tree
Hide file tree
Showing 34 changed files with 235,087 additions and 210,089 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ it('lets try', () => {
__dirname + '/../../../../../reoff/reoff-parse/src/test/ooxasttree'
const x = fs.readFileSync(ooxloc, { encoding: 'utf-8' })
const tree = JSON.parse(x)
expect(toJast(tree)).toMatchSnapshot()

//expect(toJast(tree)).toMatchSnapshot()
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import {
} from './types'
import { convert } from 'unist-util-is'
import rehypeMinifyWhitespace from 'rehype-minify-whitespace'
import { select } from 'xast-util-select'
import { x } from 'xastscript'
import { cslToRefList } from 'csl-to-jast'

export { one } from './one'
export { all } from './all'
Expand Down Expand Up @@ -136,6 +139,10 @@ export function toJast(
// visit(mdast, 'text', ontext)

//console.log(citations)
const back = select('back', jast)
if (!back) return jast

back.children = [cslToRefList(citations)]

return jast

Expand Down
43 changes: 30 additions & 13 deletions libs/ooxast/ooxast-util-to-jast/src/lib/util/wrap-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ function wDepth(sec: Sec | JastBody) {

export function wrapSec(
j: J,
depth: number,
sectionCounter: number[],
child: Element | null,
parent?: Parent
): Sec | JastBody {
const parentSec: Element = {
type: 'element',
name: depth === 0 ? 'body' : 'sec',
attributes: depth === 0 ? {} : { id: `sec-${depth}` },
name: child ? 'sec' : 'body',
attributes: child ? { id: `sec-${sectionCounter.join('-')}` } : {},
//@ts-ignore shush, it's better this way
children: child
? [
Expand Down Expand Up @@ -59,45 +59,62 @@ export function getJastHeadingLevel(p: Element) {
//@ts-ignore
return parseInt(p.attributes.style.slice(-1)) || 0
}
export function currentWrapperDepth(wrapperStack: any[]) {
return wrapperStack[wrapperStack.length - 1]?.attributes?.id
?.replace('sec-')
?.split('-')?.length
}

export function wrapSections(j: J, bodyChildren: Element[]) {
const rootWrapper = wrapSec(j, 0, null)
let sectionCounter: number[] = [1]
const rootWrapper = wrapSec(j, sectionCounter, null)

const wrapperStack: any[] = []

wrapperStack.push(rootWrapper)

function currentWrapper() {
return wrapperStack[wrapperStack.length - 1]
}

function currentWrapperDepth() {
return wDepth(currentWrapper())
}

for (let i = 0; i < bodyChildren.length; i++) {
let elem = bodyChildren[i]

if (isJastHeading(elem)) {
const elemDepth = getJastHeadingLevel(elem)

if (!elemDepth && elemDepth !== 0) {
currentWrapper().children.push(elem)
continue
}

// Child heading
if (elemDepth > j.sectionDepth) {
const childWrapper = wrapSec(j, elemDepth, elem)
if (elemDepth > currentWrapperDepth(wrapperStack)) {
sectionCounter[elemDepth - 1] = 1
const childWrapper = wrapSec(j, sectionCounter, elem)

currentWrapper().children.push(childWrapper)

wrapperStack.push(childWrapper)

j.sectionDepth++

continue
}

// Delimiting heading, i.e. one that ends the current sec
while (elemDepth <= j.sectionDepth) {
while (elemDepth <= currentWrapperDepth(wrapperStack)) {
wrapperStack.pop()
j.sectionDepth--
}
const siblingWrapper = wrapSec(j, elemDepth, elem)
sectionCounter = sectionCounter.slice(0, elemDepth)
sectionCounter[elemDepth - 1]++

const siblingWrapper = wrapSec(j, sectionCounter, elem)

currentWrapper().children.push(siblingWrapper)

wrapperStack.push(siblingWrapper)
continue
}
currentWrapper().children.push(elem)
}
Expand Down
Loading

0 comments on commit 1582e25

Please sign in to comment.