-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathautomizer.ts
354 lines (313 loc) · 9.84 KB
/
automizer.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import { Slide } from './classes/slide';
import { FileHelper } from './helper/file-helper';
import {
AutomizerParams,
AutomizerSummary,
SourceSlideIdentifier,
StatusTracker,
} from './types/types';
import { IPresentationProps } from './interfaces/ipresentation-props';
import { PresTemplate } from './interfaces/pres-template';
import { RootPresTemplate } from './interfaces/root-pres-template';
import { Template } from './classes/template';
import { ModifyXmlCallback, TemplateInfo } from './types/xml-types';
import { vd } from './helper/general-helper';
import { Master } from './classes/master';
import path from 'path';
import * as fs from 'fs';
import { XmlHelper } from './helper/xml-helper';
import ModifyPresentationHelper from './helper/modify-presentation-helper';
/**
* Automizer
*
* The basic class for `pptx-automizer` package.
* This class will be exported as `Automizer` by `index.ts`.
*/
export default class Automizer implements IPresentationProps {
rootTemplate: RootPresTemplate;
/**
* Templates of automizer
* @internal
*/
templates: PresTemplate[];
templateDir: string;
templateFallbackDir: string;
outputDir: string;
/**
* Timer of automizer
* @internal
*/
timer: number;
params: AutomizerParams;
status: StatusTracker;
modifyPresentation: ModifyXmlCallback[];
/**
* Creates an instance of `pptx-automizer`.
* @param [params]
*/
constructor(params: AutomizerParams) {
this.templates = [];
this.modifyPresentation = [];
this.params = params;
this.templateDir = params?.templateDir ? params.templateDir + '/' : '';
this.templateFallbackDir = params?.templateFallbackDir
? params.templateFallbackDir + '/'
: '';
this.outputDir = params?.outputDir ? params.outputDir + '/' : '';
this.timer = Date.now();
this.setStatusTracker(params?.statusTracker);
if (params.rootTemplate) {
const location = this.getLocation(params.rootTemplate, 'template');
this.rootTemplate = Template.import(location) as RootPresTemplate;
}
if (params.presTemplates) {
this.params.presTemplates.forEach((file) => {
const location = this.getLocation(file, 'template');
const newTemplate = Template.import(location, file) as PresTemplate;
this.templates.push(newTemplate);
});
}
}
setStatusTracker(statusTracker: StatusTracker['next']): void {
const defaultStatusTracker = (status: StatusTracker) => {
console.log(status.info + ' (' + status.share + '%)');
};
this.status = {
current: 0,
max: 0,
share: 0,
info: undefined,
increment: () => {
this.status.current++;
const nextShare =
this.status.max > 0
? Math.round((this.status.current / this.status.max) * 100)
: 0;
if (this.status.share !== nextShare) {
this.status.share = nextShare;
this.status.next(this.status);
}
},
next: statusTracker || defaultStatusTracker,
};
}
/**
*/
public async presentation(): Promise<this> {
if (this.params?.useCreationIds === true) {
await this.setCreationIds();
}
return this;
}
/**
* Load a pptx file and set it as root template.
* @param location - Filename or path to the template. Will be prefixed with 'templateDir'
* @returns Instance of Automizer
*/
public loadRoot(location: string): this {
return this.loadTemplate(location);
}
/**
* Load a template pptx file.
* @param location - Filename or path to the template. Will be prefixed with 'templateDir'
* @param name - Optional: A short name for the template. If skipped, the template will be named by its location.
* @returns Instance of Automizer
*/
public load(location: string, name?: string): this {
name = name === undefined ? location : name;
return this.loadTemplate(location, name);
}
/**
* Loads a pptx file either as a root template as a template file.
* A name can be specified to give templates an alias.
* @param location
* @param [name]
* @returns template
*/
private loadTemplate(location: string, name?: string): this {
location = this.getLocation(location, 'template');
const alreadyLoaded = this.templates.find(
(template) => template.name === name,
);
if (alreadyLoaded) {
return this;
}
const newTemplate = Template.import(location, name);
if (!this.isPresTemplate(newTemplate)) {
this.rootTemplate = newTemplate;
} else {
this.templates.push(newTemplate);
}
return this;
}
/**
* Parses all loaded templates and collects creationIds for slides and
* elements. This will make finding templates and elements independent
* of slide number and element name.
* @returns Promise<TemplateInfo[]>
*/
public async setCreationIds(): Promise<TemplateInfo[]> {
const templateCreationId = [];
for (const template of this.templates) {
const creationIds =
template.creationIds || (await template.setCreationIds());
templateCreationId.push({
name: template.name,
slides: creationIds,
});
}
return templateCreationId;
}
public modify(cb: ModifyXmlCallback): this {
this.modifyPresentation.push(cb);
return this;
}
/**
* Determines whether template is root or default template.
* @param template
* @returns pres template
*/
private isPresTemplate(
template: PresTemplate | RootPresTemplate,
): template is PresTemplate {
return 'name' in template;
}
/**
* Add a slide from one of the imported templates by slide number or creationId.
* @param name - Name or alias of the template; must have been loaded with `Automizer.load()`
* @param slideIdentifier - Number or creationId of slide in template presentation
* @param callback - Executed after slide was added. The newly created slide will be passed to the callback as first argument.
* @returns Instance of Automizer
*/
public addSlide(
name: string,
slideIdentifier: SourceSlideIdentifier,
callback?: (slide: Slide) => void,
): this {
if (this.rootTemplate === undefined) {
throw new Error('You have to set a root template first.');
}
const template = this.getTemplate(name);
const newSlide = new Slide({
presentation: this,
template,
slideIdentifier,
});
if (callback !== undefined) {
newSlide.root = this;
callback(newSlide);
}
this.rootTemplate.slides.push(newSlide);
return this;
}
/**
* WIP: copy and modify a master from template to output
* @param name
* @param masterNumber
* @param callback
*/
public addMaster(
name: string,
masterNumber: number,
callback?: (slide: Slide) => void,
): this {
const template = this.getTemplate(name);
const newMaster = new Master({
presentation: this,
template,
masterNumber,
});
// this.rootTemplate.slides.push(newMaster);
return this;
}
/**
* Searches this.templates to find template by given name.
* @internal
* @param name Alias name if given to loaded template.
* @returns template
*/
public getTemplate(name: string): PresTemplate {
const template = this.templates.find((t) => t.name === name);
if (template === undefined) {
throw new Error(`Template not found: ${name}`);
}
return template;
}
/**
* Write all imports and modifications to a file.
* @param location - Filename or path for the file. Will be prefixed with 'outputDir'
* @returns summary object.
*/
public async write(location: string): Promise<AutomizerSummary> {
await this.writeSlides();
await this.normalizePresentation();
await this.applyModifyPresentationCallbacks();
const rootArchive = await this.rootTemplate.archive;
const content = await rootArchive.generateAsync({ type: 'nodebuffer' });
return FileHelper.writeOutputFile(
this.getLocation(location, 'output'),
content,
this,
);
}
/**
* Write all slides into archive.
*/
public async writeSlides(): Promise<void> {
await this.rootTemplate.countExistingSlides();
this.status.max = this.rootTemplate.slides.length;
for (const slide of this.rootTemplate.slides) {
await this.rootTemplate.appendSlide(slide);
}
if (this.params.removeExistingSlides) {
await this.rootTemplate.truncate();
}
}
/**
* Applies all callbacks in this.modifyPresentation-array.
* The callback array can be pushed by this.modify()
*/
async applyModifyPresentationCallbacks(): Promise<void> {
await XmlHelper.modifyXmlInArchive(
this.rootTemplate.archive,
`ppt/presentation.xml`,
this.modifyPresentation,
);
}
/**
* Apply some callbacks to restore archive/xml structure
* and prevent corrupted pptx files.
*
* TODO: Remove unused parts (slides, related items) from archive.
* TODO: Use every imported image only once
* TODO: Check for lost relations
*/
normalizePresentation(): void {
this.modify(ModifyPresentationHelper.normalizeSlideIds);
}
/**
* Applies path prefix to given location string.
* @param location path and/or filename
* @param [type] template or output
* @returns location
*/
private getLocation(location: string, type?: string): string {
switch (type) {
case 'template':
if (fs.existsSync(this.templateDir + location)) {
return this.templateDir + location;
} else if (fs.existsSync(this.templateFallbackDir + location)) {
return this.templateFallbackDir + location;
} else {
vd('No file matches "' + location + '"');
vd('@templateDir: ' + this.templateDir);
vd('@templateFallbackDir: ' + this.templateFallbackDir);
}
break;
case 'output':
return this.outputDir + location;
default:
return location;
}
}
}