-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooxast-util-to-unified-latex.ts
executable file
·249 lines (220 loc) · 6.83 KB
/
ooxast-util-to-unified-latex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import { one } from './one'
import { handlers } from './handlers/index'
import { Data as CSL } from 'csl-json'
import {
Context,
H,
HWithoutProps,
HWithProps,
UnifiedLatexNode,
UnifiedLatexRoot,
Options,
Root,
Element,
Text,
RenderInfo,
} from './types'
import rehypeMinifyWhitespace from 'rehype-minify-whitespace'
import { args, env, m, s } from '@unified-latex/unified-latex-builder'
import { PB } from './util/PB'
import { makePackage } from './util/make-package'
import { cslToBiblatex } from 'csl-to-biblatex'
export { one } from './one'
export { all } from './all'
export { handlers as defaultHandlers }
const defaultOptions: Options = {
newLines: false,
quotes: ['"'],
topSection: 1,
columnSeparator: false,
documentClass: { name: 'article' },
bibname: 'References',
packages: [
'caption',
'tabularx',
'xcolor',
'figurex',
'hyperref',
{ name: 'biblatex', options: ['backend=biber', 'style=apa'] },
],
}
export function toUnifiedLatex(tree: Root | Element | Text, options: Options) {
options = { ...defaultOptions, ...options }
const whiteSpaceTransformer = rehypeMinifyWhitespace({
newlines: options.newLines === true,
})
if (whiteSpaceTransformer) {
// @ts-expect-error rehype-minify-whitespace is not typed correctly
whiteSpaceTransformer(tree)
}
// const byId: { [s: string]: Element } = {}
let unifiedLatex: UnifiedLatexNode | UnifiedLatexRoot
const citations: { [key: string | number]: CSL } = {}
const h: H = Object.assign(
((
node: UnifiedLatexRoot | UnifiedLatexNode,
type: string,
props?: RenderInfo | string | Array<UnifiedLatexNode>,
content?: string | Array<UnifiedLatexNode>,
): UnifiedLatexNode => {
let _renderInfo: RenderInfo | undefined
if (typeof props === 'string' || Array.isArray(props)) {
content = props
_renderInfo = {}
} else {
_renderInfo = props
}
const result: UnifiedLatexNode = Object.assign(
{},
{ type },
{ _renderInfo },
{ content },
{ position: node.position },
)
return result as UnifiedLatexNode
}) as HWithProps & HWithoutProps,
{
// nodeById: byId,
baseFound: false,
inTable: false,
wrapText: true,
/** @type {string|null} */
frozenBaseUrl: null,
qNesting: 0,
handlers: options.handlers ? { ...handlers, ...options.handlers } : handlers,
document: options.document,
quotes: options.quotes || ['"'],
italics: options.italics || 'emph',
sectionDepth: options.topSection ?? 1,
documentClass: options.documentClass || { name: 'article' },
bibname: options.bibname || 'bibliography',
columnSeparator: !!options.columnSeparator,
citationNumber: 0,
collectCitation: options.collectCitation || collectCitation,
parseCitation: options.parseCitation || parseCitation,
partialCitation: '',
deleteNextRun: false,
relations: options.relations || {},
citeKeys: {},
citationType: options.citationType || 'mendeley',
displayMath: options.displayMath || 'equation',
inlineMath: options.inlineMath || '$',
xcolor: options.xcolor ?? 'true',
strikethrough: options.strikethrough ?? 'sout',
inMath: false,
title: options.title,
footnotes: {},
endnotes: {},
defaultCol: options.defaultCol || 'l',
tabularx: options.tabularx
? {
width:
typeof options.tabularx === 'object' && 'width' in options.tabularx
? options.tabularx.width
: '1.0\\textwidth',
}
: false,
simpleParagraph: false,
bibliography: options.bibliography || [],
inDisplayMath: false,
} as Context,
)
const result = one(h, tree, undefined)
if (!result) {
return { type: 'root', content: [] } as UnifiedLatexRoot
}
unifiedLatex = env('document', result)
const biblatex = h.bibliography
? Array.isArray(h.bibliography)
? cslToBiblatex(h.bibliography)
: s(h.bibliography)
: null
const packages =
options.packages
?.filter((p) => {
const name = typeof p === 'string' ? p : p.name
if (!h.xcolor) {
return name !== 'xcolor'
}
if (!h.tabularx) {
return name !== 'tabularx'
}
return true
})
?.flatMap((p) => [
typeof p === 'string' ? makePackage(p) : makePackage(p.name, p.options),
PB,
]) || []
const preamble = options.preamble ?? [
...(h.title ? [PB, m('title', h.title), PB] : []),
...(h.bibliography && h.bibliography.length
? [PB, m('addbibresource', 'bibliography.bib'), PB]
: []),
]
unifiedLatex = {
type: 'root',
content: [
h.documentClass.options
? m(
'documentclass',
args([h.documentClass.options?.join(', '), h.documentClass.name], {
braces: '[]{}',
}),
)
: m('documentclass', h.documentClass.name),
PB,
...packages,
...(typeof preamble === 'string' ? [s(preamble)] : preamble),
...(biblatex ? [env('filecontents', biblatex, 'bibliography.bib')] : []),
PB,
unifiedLatex,
],
} as UnifiedLatexRoot
return unifiedLatex
/**
* Collapse text nodes, and fix whitespace.
* Most of this is taken care of by `rehype-minify-whitespace`, but
* we’re generating some whitespace too, and some nodes are in the end
* ignored.
* So clean up.
*
//* {import('unist-util-visit/complex-types').BuildVisitor UnifiedLatexRoot, 'text'>}
*/
// function ontext(node: any, index: any, parent: any) {
// /* c8 ignore next 3 */
// if (index === null || !parent) {
// return
// }
// const previous = parent.children[index - 1]
// if (previous && previous.type === node.type) {
// previous.value += node.value
// parent.children.splice(index, 1)
// if (previous.position && node.position) {
// previous.position.end = node.position.end
// }
// // Iterate over the previous node again, to handle its total value.
// return index - 1
// }
// node.value = node.value.replace(/[\t ]*(\r?\n|\r)[\t ]*/, '$1')
// // We don’t care about other phrasing nodes in between (e.g., `[ asd ]()`),
// // as there the whitespace matters.
// if (parent && block(parent)) {
// if (!index) {
// node.value = node.value.replace(/^[\t ]+/, '')
// }
// if (index === parent.content.length - 1) {
// node.value = node.value.replace(/[\t ]+$/, '')
// }
// }
// if (!node.value) {
// parent.children.splice(index, 1)
// return index
// }
// }
function parseCitation(citation: any) {
//
}
function collectCitation(citation: any, index: number) {
citations[index] = citation
}
}