-
Notifications
You must be signed in to change notification settings - Fork 3
/
any.tsx
286 lines (262 loc) · 12.2 KB
/
any.tsx
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import React from "react";
import * as std from "../../law/std";
import { isSentenceChildEL, SentenceChildEL } from "../../node/cst/inline";
import { EL } from "../../node/el";
import { NotImplementedError } from "../../util";
import { DOCXAmendProvision, HTMLAmendProvision } from "./amendProvision";
import { DOCXAppdxItem, HTMLAppdxItem } from "./appdxItem";
import { DOCXArticle, HTMLArticle } from "./article";
import { DOCXArticleGroup, HTMLArticleGroup } from "./articleGroup";
import { DOCXColumnsOrSentencesRun, HTMLColumnsOrSentencesRun } from "./columnsOrSentencesRun";
import { DOCXComponentProps, wrapDOCXComponent, w } from "../common/docx";
import { DOCXFigRun, HTMLFigRun } from "./figRun";
import { HTMLComponentProps, wrapHTMLComponent } from "../common/html";
import { DOCXItemStruct, HTMLItemStruct } from "./itemStruct";
import { DOCXEnactStatement, DOCXLaw, DOCXPreamble, HTMLEnactStatement, HTMLLaw, HTMLPreamble } from "./law";
import { DOCXList, HTMLList } from "./list";
import { DOCXNoteLike, HTMLNoteLike } from "./noteLike";
import { DOCXParagraphItem, HTMLParagraphItem } from "./paragraphItem";
import { DOCXRemarks, HTMLRemarks } from "./remarks";
import { DOCXSentenceChildrenRun, HTMLSentenceChildrenRun } from "./sentenceChildrenRun";
import { DOCXSupplNote, HTMLSupplNote } from "./supplNote";
import { DOCXTable, HTMLTable } from "./table";
import { DOCXTOC, DOCXTOCItem, HTMLTOC, HTMLTOCItem } from "./toc";
import { withKey } from "../common";
export interface AnyELsProps {
els: (std.StdEL | std.__EL | string)[],
indent: number,
}
export const HTMLAnyELsCSS = /*css*/`
`;
export const HTMLAnyELsToBlocks = (props: HTMLComponentProps & AnyELsProps): (JSX.Element[] | JSX.Element)[] => {
const { els, htmlOptions, indent } = props;
const blocks: (JSX.Element[] | JSX.Element)[] = [];
let currentRuns: JSX.Element[] = [];
const flushRuns = () => {
if (currentRuns.length > 0) {
blocks.push(currentRuns);
currentRuns = [];
}
};
for (let i = 0; i < els.length; i++) {
const el = els[i];
if (std.isLaw(el)) {
flushRuns();
blocks.push(<HTMLLaw el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isArticleGroup(el) || std.isMainProvision(el) || std.isSupplProvision(el)) {
flushRuns();
blocks.push(<HTMLArticleGroup el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isArticle(el)) {
flushRuns();
blocks.push(<HTMLArticle el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isParagraphItem(el)) {
flushRuns();
blocks.push(<HTMLParagraphItem el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isTable(el)) {
flushRuns();
blocks.push(<HTMLTable el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isTableStruct(el) || std.isFigStruct(el) || std.isNoteLikeStruct(el)) {
flushRuns();
blocks.push(<HTMLItemStruct el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isAppdxItem(el)) {
flushRuns();
blocks.push(<HTMLAppdxItem el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isRemarks(el)) {
flushRuns();
blocks.push(<HTMLRemarks el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isNoteLike(el)) {
flushRuns();
blocks.push(<HTMLNoteLike el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isList(el)) {
flushRuns();
blocks.push(<HTMLList el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isAmendProvision(el)) {
flushRuns();
blocks.push(<HTMLAmendProvision el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isSupplNote(el)) {
flushRuns();
blocks.push(<HTMLSupplNote el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isEnactStatement(el)) {
flushRuns();
blocks.push(<HTMLEnactStatement el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isPreamble(el)) {
flushRuns();
blocks.push(<HTMLPreamble el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isTOC(el)) {
flushRuns();
blocks.push(<HTMLTOC el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isTOCItem(el)) {
flushRuns();
blocks.push(<HTMLTOCItem el={el} indent={indent} {...{ htmlOptions }} />);
} else if (std.isArticleGroupTitle(el) || std.isLawTitle(el)) {
flushRuns();
blocks.push((
<div className={`any-els-tag any-els-tag-${el.tag} indent-${indent}`}>
<HTMLSentenceChildrenRun els={el.children} {...{ htmlOptions }} />
</div>
));
} else if (std.isSentence(el)) {
let j = i + 1;
while (j < els.length && std.isSentence(els[j])) j++;
const sentences = els.slice(i, j) as std.Sentence[];
currentRuns.push(<HTMLColumnsOrSentencesRun els={sentences} {...{ htmlOptions }} />);
i = j - 1;
} else if (std.isColumn(el)) {
let j = i + 1;
while (j < els.length && std.isColumn(els[j])) j++;
const columns = els.slice(i, j) as std.Column[];
currentRuns.push(<HTMLColumnsOrSentencesRun els={columns} {...{ htmlOptions }} />);
i = j - 1;
} else if (typeof el === "string" || isSentenceChildEL(el)) {
// "Line", "QuoteStruct", "ArithFormula", "Ruby", "Sup", "Sub"
let j = i + 1;
while (j < els.length && (typeof els[j] === "string" || isSentenceChildEL(els[j] as EL))) j++;
const sentenceChildren = els.slice(i, j) as (SentenceChildEL | string)[];
currentRuns.push(<HTMLSentenceChildrenRun els={sentenceChildren} {...{ htmlOptions }} />);
i = j - 1;
} else if (std.isFig(el)) {
currentRuns.push(<HTMLFigRun el={el} {...{ htmlOptions }} />);
}
else { throw new NotImplementedError(el.tag); }
}
flushRuns();
return blocks;
};
export const HTMLAnyELs = wrapHTMLComponent("HTMLAnyELs", ((props: HTMLComponentProps & AnyELsProps) => {
const { indent } = props;
const rawBlocks = HTMLAnyELsToBlocks(props);
const blocks: JSX.Element[] = [];
for (const rawBlock of rawBlocks) {
if (Array.isArray(rawBlock)) {
blocks.push((
<div className={`any-els-runs indent-${indent}`}>
{withKey(rawBlock)}
</div>
));
} else {
blocks.push(rawBlock);
}
}
return (<>
{withKey(blocks)}
</>);
}));
export const DOCXAnyELsToBlocks = (props: DOCXComponentProps & AnyELsProps): (JSX.Element[] | JSX.Element)[] => {
const { els, docxOptions, indent } = props;
const blocks: (JSX.Element[] | JSX.Element)[] = [];
let currentRuns: JSX.Element[] = [];
const flushRuns = () => {
if (currentRuns.length > 0) {
blocks.push(currentRuns);
currentRuns = [];
}
};
for (let i = 0; i < els.length; i++) {
const el = els[i];
if (std.isLaw(el)) {
flushRuns();
blocks.push(<DOCXLaw el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isArticleGroup(el) || std.isMainProvision(el) || std.isSupplProvision(el)) {
flushRuns();
blocks.push(<DOCXArticleGroup el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isArticle(el)) {
flushRuns();
blocks.push(<DOCXArticle el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isParagraphItem(el)) {
flushRuns();
blocks.push(<DOCXParagraphItem el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isTable(el)) {
flushRuns();
blocks.push(<DOCXTable el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isTableStruct(el) || std.isFigStruct(el) || std.isNoteLikeStruct(el)) {
flushRuns();
blocks.push(<DOCXItemStruct el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isAppdxItem(el)) {
flushRuns();
blocks.push(<DOCXAppdxItem el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isRemarks(el)) {
flushRuns();
blocks.push(<DOCXRemarks el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isNoteLike(el)) {
flushRuns();
blocks.push(<DOCXNoteLike el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isList(el)) {
flushRuns();
blocks.push(<DOCXList el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isAmendProvision(el)) {
flushRuns();
blocks.push(<DOCXAmendProvision el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isSupplNote(el)) {
flushRuns();
blocks.push(<DOCXSupplNote el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isEnactStatement(el)) {
flushRuns();
blocks.push(<DOCXEnactStatement el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isPreamble(el)) {
flushRuns();
blocks.push(<DOCXPreamble el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isTOC(el)) {
flushRuns();
blocks.push(<DOCXTOC el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isTOCItem(el)) {
flushRuns();
blocks.push(<DOCXTOCItem el={el} indent={indent} {...{ docxOptions }} />);
} else if (std.isArticleGroupTitle(el) || std.isLawTitle(el)) {
flushRuns();
blocks.push((
<w.p>
<w.pPr>
<w.pStyle w:val={`Indent${indent}`}/>
</w.pPr>
<DOCXSentenceChildrenRun els={el.children} emphasis={true} {...{ docxOptions }} />
</w.p>
));
} else if (std.isSentence(el)) {
let j = i + 1;
while (j < els.length && std.isSentence(els[j])) j++;
const sentences = els.slice(i, j) as std.Sentence[];
currentRuns.push(<DOCXColumnsOrSentencesRun els={sentences} {...{ docxOptions }} />);
i = j - 1;
} else if (std.isColumn(el)) {
let j = i + 1;
while (j < els.length && std.isColumn(els[j])) j++;
const columns = els.slice(i, j) as std.Column[];
currentRuns.push(<DOCXColumnsOrSentencesRun els={columns} {...{ docxOptions }} />);
i = j - 1;
} else if (typeof el === "string" || isSentenceChildEL(el)) {
// "Line", "QuoteStruct", "ArithFormula", "Ruby", "Sup", "Sub"
let j = i + 1;
while (j < els.length && (typeof els[j] === "string" || isSentenceChildEL(els[j] as EL))) j++;
const sentenceChildren = els.slice(i, j) as (SentenceChildEL | string)[];
currentRuns.push(<DOCXSentenceChildrenRun els={sentenceChildren} {...{ docxOptions }} />);
i = j - 1;
} else if (std.isFig(el)) {
currentRuns.push(<DOCXFigRun el={el} {...{ docxOptions }} />);
}
else { throw new NotImplementedError(el.tag); }
}
flushRuns();
return blocks;
};
export const DOCXAnyELs = wrapDOCXComponent("DOCXAnyELs", ((props: DOCXComponentProps & AnyELsProps) => {
const { indent } = props;
const rawBlocks = DOCXAnyELsToBlocks(props);
const blocks: JSX.Element[] = [];
for (const rawBlock of rawBlocks) {
if (Array.isArray(rawBlock)) {
blocks.push((
<w.p>
<w.pPr>
<w.pStyle w:val={`Indent${indent}`}/>
</w.pPr>
{withKey(rawBlock)}
</w.p>
));
} else {
blocks.push(rawBlock);
}
}
return (<>
{withKey(blocks)}
</>);
}));