Skip to content

Commit

Permalink
fix: enter a new era
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Mar 9, 2023
1 parent 296b3a1 commit bf2e62f
Show file tree
Hide file tree
Showing 30 changed files with 122 additions and 150 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { toUnifiedLatex } from './lib/ooxast-util-to-unified-latex'
export { Options, Handle, H, UnifiedLatexNode } from './lib/types'
export { toUnifiedLatex } from './lib/ooxast-util-to-unified-latex.js'
export { Options, Handle, H, UnifiedLatexNode } from './lib/types.js'
6 changes: 3 additions & 3 deletions src/lib/all.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { one } from './one'
import { H, UnifiedLatexNode, Node, Parent, Handle } from './types'
import { one } from './one.js'
import { H, UnifiedLatexNode, Node, Parent } from './types.js'

/**
* Convert all nodes in tree using j
* Convert all nodes in tree using h
* @param h ooxast constructor function
* @param parent
* @returns
Expand Down
40 changes: 13 additions & 27 deletions src/lib/handlers/body.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
// based on https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/em

import { H, Body, Handle, P, UnifiedLatexNode } from '../types'
import { all } from '../all'
import { getPStyle } from '../util/get-pstyle'
import { getListInfo } from '../util/get-listinfo'
import { one } from '../one'
import { H, Body, Handle, P, UnifiedLatexNode } from '../types.js'
import { all } from '../all.js'
import { getPStyle } from '../util/get-pstyle.js'
import { getListInfo } from '../util/get-listinfo.js'
import { one } from '../one.js'
import { Element } from 'xast-util-to-string/lib'
import {
Argument,
Environment,
Macro,
Verb,
Whitespace,
} from '@unified-latex/unified-latex-types'
import { WhiteSpace } from 'nlcst'
import { arg, m, SP } from '@unified-latex/unified-latex-builder'
import { PB } from '../util/PB'
import { Environment, Macro } from '@unified-latex/unified-latex-types'
import { m, SP } from '@unified-latex/unified-latex-builder'
import { PB } from '../util/PB.js'
import { updateRenderInfo } from '@unified-latex/unified-latex-util-render-info'

const isP = (node: Element): node is P =>
node.type === 'element' && node.name === 'w:p'
const isP = (node: Element): node is P => node.type === 'element' && node.name === 'w:p'

export const body: Handle = (h: H, body: Body) => {
const processedBody = body.children.reduce((acc, child, index) => {
Expand All @@ -46,11 +38,9 @@ export const body: Handle = (h: H, body: Body) => {

const prevChild = body.children[index - 1]

const isPrevListItem =
prevChild && isP(prevChild) && getPStyle(prevChild) === 'ListParagraph'
const isPrevListItem = prevChild && isP(prevChild) && getPStyle(prevChild) === 'ListParagraph'

const { ilvl: prevIlvl, numId: prevNumId } =
(isPrevListItem && getListInfo(prevChild)) || {}
const { ilvl: prevIlvl, numId: prevNumId } = (isPrevListItem && getListInfo(prevChild)) || {}

const listItem = makeItem(h, child)

Expand Down Expand Up @@ -107,8 +97,7 @@ export const body: Handle = (h: H, body: Body) => {
if (isPrevListItem && ilvl < prevIlvl) {
const mainEnv = acc[acc.length - 1] as Environment
const embeddedEnvs = findEmbeddedEnvs(mainEnv)
const toBeEmbeddedEnv =
embeddedEnvs[embeddedEnvs.length - (prevIlvl - ilvl) - 1]
const toBeEmbeddedEnv = embeddedEnvs[embeddedEnvs.length - (prevIlvl - ilvl) - 1]

const env: Environment = {
type: 'environment',
Expand Down Expand Up @@ -159,10 +148,7 @@ export const body: Handle = (h: H, body: Body) => {
return processedBody
}

function makeItem(
h: H,
item: P
): [typeof PB, Macro, typeof SP, ...UnifiedLatexNode[], typeof PB] {
function makeItem(h: H, item: P): [typeof PB, Macro, typeof SP, ...UnifiedLatexNode[], typeof PB] {
const mIte = m('item')
updateRenderInfo(mIte, {
hangingIndent: true,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/handlers/citation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { H, Handle } from '../types'
import { H, Handle } from '../types.js'
import { T } from 'ooxast'
import { Data as CSL } from 'csl-json'

import { CitationItem, MendeleyCitationItem } from 'ooxast-util-citations'

import { m } from '@unified-latex/unified-latex-builder'

import { Parent } from 'unist'

export const citation: Handle = (h: H, citation: T, parent?: Parent) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/handlers/displayMath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DisplayMath } from '@unified-latex/unified-latex-types'
import { all } from '../all'
import { H, Handle } from '../types'
import { all } from '../all.js'
import { H, Handle } from '../types.js'

export const displayMath: Handle = (h: H, node): DisplayMath => {
h.inMath = true
Expand Down
5 changes: 2 additions & 3 deletions src/lib/handlers/document.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { all } from '../all'
import { H, Handle } from '../types'
import { all } from '../all.js'
import { H, Handle } from '../types.js'
import { Body, Document, Endnotes, Footnotes } from 'ooxast'
import { select } from 'xast-util-select'
import { notes } from '../util/notes'
import { convertElement } from 'xast-util-is-element'

const isFootnotes = convertElement<Footnotes>('w:footnotes')
Expand Down
4 changes: 2 additions & 2 deletions src/lib/handlers/drawing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { H, Handle } from '../types'
import { H, Handle } from '../types.js'
import { Drawing } from 'ooxast'
import { select } from 'xast-util-select'
import { env, m } from '@unified-latex/unified-latex-builder'
import { PB } from '../util/PB'
import { PB } from '../util/PB.js'

export const drawing: Handle = (h: H, node: Drawing) => {
const blip = select('a\\:blip', node)
Expand Down
5 changes: 2 additions & 3 deletions src/lib/handlers/endnote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { H, Element } from '../types'
import { m } from '@unified-latex/unified-latex-builder'
import { all } from '../all'
import { H, Element } from '../types.js'
import { all } from '../all.js'

export function footnote(h: H, node: Element) {
if (node?.attributes?.type === 'separator') {
Expand Down
5 changes: 2 additions & 3 deletions src/lib/handlers/footnote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { H, Element } from '../types'
import { m } from '@unified-latex/unified-latex-builder'
import { all } from '../all'
import { H, Element } from '../types.js'
import { all } from '../all.js'

export function footnote(h: H, node: Element) {
if (node?.attributes?.type === 'separator') {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/handlers/footnoteReference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { H, Element } from '../types'
import { H, Element } from '../types.js'
import { arg, m } from '@unified-latex/unified-latex-builder'

export function footnoteReference(h: H, node: Element) {
Expand Down
36 changes: 18 additions & 18 deletions src/lib/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { all } from '../all'
import { body } from './body'
import { p } from './p'
import { root } from './root'
import { text } from './text'
import { citation } from './citation'
import { tbl } from './tbl'
import { footnote } from './footnote'
import { document } from './document'
import { r } from './r'
import { tr } from './tr'
import { drawing } from './drawing'
import { Context } from '../types'
import { displayMath } from './displayMath'
import { oMathPara } from './oMathPara'
import { tc } from './tc'
import { footnoteReference } from './footnoteReference'
import { oMath } from './oMath'
import { all } from '../all.js'
import { body } from './body.js'
import { p } from './p.js'
import { root } from './root.js'
import { text } from './text.js'
import { citation } from './citation.js'
import { tbl } from './tbl.js'
import { footnote } from './footnote.js'
import { document } from './document.js'
import { r } from './r.js'
import { tr } from './tr.js'
import { drawing } from './drawing.js'
import { Context } from '../types.js'
import { displayMath } from './displayMath.js'
import { oMathPara } from './oMathPara.js'
import { tc } from './tc.js'
import { footnoteReference } from './footnoteReference.js'
import { oMath } from './oMath.js'

export const handlers: Context['handlers'] = {
p,
Expand Down
5 changes: 2 additions & 3 deletions src/lib/handlers/oMath.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { s } from '@unified-latex/unified-latex-builder'
import { Math } from 'ooxast'
import { all } from '../all'
import { H, Handle } from '../types'
import { PB } from '../util/PB'
import { all } from '../all.js'
import { H, Handle } from '../types.js'

export const oMath: Handle = (h: H, node: Math.OMath) => {
h.inMath = true
Expand Down
6 changes: 3 additions & 3 deletions src/lib/handlers/oMathPara.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { env, s } from '@unified-latex/unified-latex-builder'
import { all } from '../all'
import { H, Handle, Node, Parent } from '../types'
import { PB } from '../util/PB'
import { all } from '../all.js'
import { H, Handle, Node, Parent } from '../types.js'
import { PB } from '../util/PB.js'

export const oMathPara: Handle = (h: H, node: Node, parent?: Parent) => {
h.inDisplayMath = true
Expand Down
7 changes: 2 additions & 5 deletions src/lib/handlers/p.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { x } from 'xastscript'
import { toUnifiedLatex } from '../ooxast-util-to-unified-latex'
import { toUnifiedLatex } from '../ooxast-util-to-unified-latex.js'
import { s, SP } from '@unified-latex/unified-latex-builder'

describe('p', () => {
it('should do something', () => {
const basicp = x('p', { id: 'ayy' }, [
x('w:pPr', {}, []),
x('w:r', {}, [
x('w:rPr', {}, []),
x('w:t', {}, [{ type: 'text', value: 'lmao' }]),
]),
x('w:r', {}, [x('w:rPr', {}, []), x('w:t', {}, [{ type: 'text', value: 'lmao' }])]),
])
expect(toUnifiedLatex(basicp)).toEqual([SP])
})
Expand Down
8 changes: 4 additions & 4 deletions src/lib/handlers/p.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Macro } from '@unified-latex/unified-latex-types'
import { P } from 'ooxast'
import { SP, m, s, arg, env } from '@unified-latex/unified-latex-builder'
import { all } from '../all'
import { H, Handle } from '../types'
import { getPStyle } from '../util/get-pstyle'
import { all } from '../all.js'
import { H, Handle } from '../types.js'
import { getPStyle } from '../util/get-pstyle.js'
import { updateRenderInfo } from '@unified-latex/unified-latex-util-render-info'
import { PB } from '../util/PB'
import { PB } from '../util/PB.js'
import { toString } from 'xast-util-to-string'

const headingList = [
Expand Down
25 changes: 5 additions & 20 deletions src/lib/handlers/r.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import {
Color,
FldChar,
Highlight,
R,
RPr,
Shd,
VerticalAlignRun,
} from 'ooxast'
import { Color, FldChar, Highlight, R, RPr, Shd, VerticalAlignRun } from 'ooxast'
import { select } from 'xast-util-select'
import { all } from '../all'
import { all } from '../all.js'
import { x } from 'xastscript'
import { H, UnifiedLatexNode } from '../types'
import {
Group,
Macro,
String as UnifiedLatexString,
} from '@unified-latex/unified-latex-types'
import { H, UnifiedLatexNode } from '../types.js'
import { Group, Macro, String as UnifiedLatexString } from '@unified-latex/unified-latex-types'
import { convertElement } from 'xast-util-is-element'
import { m, s } from '@unified-latex/unified-latex-builder'

Expand All @@ -38,10 +26,7 @@ export function r(h: H, node: R) {
return
}

const dontProc = select(
'w\\:footnoteReference, w\\:endnoteReference, w\\:drawing',
node
)
const dontProc = select('w\\:footnoteReference, w\\:endnoteReference, w\\:drawing', node)

if (dontProc) {
const content = all(h, node)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/handlers/root.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// based on https://github.com/syntax-tree/hast-util-to-mdast/blob/main/lib/handlers/em

import { all } from '../all'
import { H, Node, Root } from '../types'
import { all } from '../all.js'
import { H, Root } from '../types.js'

export function root(h: H, node: Root) {
// if (!article) {
Expand Down
14 changes: 5 additions & 9 deletions src/lib/handlers/tbl.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { H, Handle } from '../types'
import { H, Handle } from '../types.js'
import { Tbl } from 'ooxast'
import { all } from '../all'
import { all } from '../all.js'
import { env } from '@unified-latex/unified-latex-builder'

export const tbl: Handle = (h: H, tbl: Tbl) => {
h.inTable = true
const contents = all(h, tbl)
h.inTable = false

const tableRows = tbl.children.filter(
(row) => 'name' in row && row.name === 'w:tr'
)
const tableRows = tbl.children.filter((row) => 'name' in row && row.name === 'w:tr')

const columns = tableRows
.map((row) => `${h.defaultCol}${h.columnSeparator ? ' |' : ''}`)
.join(' ')
const columns = tableRows.map(() => `${h.defaultCol}${h.columnSeparator ? ' |' : ''}`).join(' ')

const colArg = [`@{} ${h.columnSeparator ? '| ' : ''}${columns} @{}`]
const table = env(
'table',
h.tabularx?.width
? env('tabularx', contents, [h.tabularx.width, ...colArg])
: env('tabular', contents, colArg)
: env('tabular', contents, colArg),
)
return table
}
4 changes: 2 additions & 2 deletions src/lib/handlers/tc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { m } from '@unified-latex/unified-latex-builder'
import { Tc } from 'ooxast'
import { select } from 'xast-util-select'
import { all } from '../all'
import { H, Handle, UnifiedLatexNode } from '../types'
import { all } from '../all.js'
import { H, Handle, UnifiedLatexNode } from '../types.js'

export const tc: Handle = (h: H, node: Tc): UnifiedLatexNode[] => {
const gridSpan = select('w\\:gridSpan', node)?.attributes?.['w:val']
Expand Down
2 changes: 1 addition & 1 deletion src/lib/handlers/text.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { s } from '@unified-latex/unified-latex-builder'
import { H, Text } from '../types'
import { H, Text } from '../types.js'

export function text(h: H, node: Text) {
return s(node.value)
Expand Down
7 changes: 3 additions & 4 deletions src/lib/handlers/tr.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { H, Handle, UnifiedLatexNode } from '../types'
import { H, Handle, UnifiedLatexNode } from '../types.js'
import { Row, Tc } from 'ooxast'
import { all } from '../all'
import { PB } from '../util/PB'
import { PB } from '../util/PB.js'
import { select } from 'xast-util-select'
import { tc } from './tc'
import { tc } from './tc.js'
import { m, s, SP } from '@unified-latex/unified-latex-builder'

export const tr: Handle = (h: H, tr: Row, parent) => {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/one.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isElement } from 'xast-util-is-element'
import { Parent } from 'ooxast'
import { all } from './all'
import { Handle, H, UnifiedLatexNode, Node, Element } from './types'
import { own } from './util/own'
import { wrapText } from './util/wrap-text'
import { all } from './all.js'
import { Handle, H, UnifiedLatexNode, Node, Element } from './types.js'
import { own } from './util/own.js'
import { wrapText } from './util/wrap-text.js'

export function one(
h: H,
Expand Down
Loading

0 comments on commit bf2e62f

Please sign in to comment.