pptx-templates 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI-INSTRUCTOR.md +2253 -0
- package/CHANGELOG.md +49 -0
- package/LICENSE +23 -0
- package/README.md +86 -2
- package/dist/automizer.d.ts +224 -0
- package/dist/automizer.js +704 -0
- package/dist/automizer.js.map +1 -0
- package/dist/classes/content-type-registry.d.ts +46 -0
- package/dist/classes/content-type-registry.js +170 -0
- package/dist/classes/content-type-registry.js.map +1 -0
- package/dist/classes/element-importer.d.ts +49 -0
- package/dist/classes/element-importer.js +254 -0
- package/dist/classes/element-importer.js.map +1 -0
- package/dist/classes/has-shapes.d.ts +286 -0
- package/dist/classes/has-shapes.js +385 -0
- package/dist/classes/has-shapes.js.map +1 -0
- package/dist/classes/layout.d.ts +42 -0
- package/dist/classes/layout.js +113 -0
- package/dist/classes/layout.js.map +1 -0
- package/dist/classes/master.d.ts +45 -0
- package/dist/classes/master.js +146 -0
- package/dist/classes/master.js.map +1 -0
- package/dist/classes/placeholder-normalizer.d.ts +42 -0
- package/dist/classes/placeholder-normalizer.js +136 -0
- package/dist/classes/placeholder-normalizer.js.map +1 -0
- package/dist/classes/related-content-copier.d.ts +18 -0
- package/dist/classes/related-content-copier.js +114 -0
- package/dist/classes/related-content-copier.js.map +1 -0
- package/dist/classes/shape.d.ts +62 -0
- package/dist/classes/shape.js +194 -0
- package/dist/classes/shape.js.map +1 -0
- package/dist/classes/slide-master-remover.d.ts +44 -0
- package/dist/classes/slide-master-remover.js +201 -0
- package/dist/classes/slide-master-remover.js.map +1 -0
- package/dist/classes/slide-notes-copier.d.ts +26 -0
- package/dist/classes/slide-notes-copier.js +76 -0
- package/dist/classes/slide-notes-copier.js.map +1 -0
- package/dist/classes/slide.d.ts +91 -0
- package/dist/classes/slide.js +286 -0
- package/dist/classes/slide.js.map +1 -0
- package/dist/classes/template.d.ts +137 -0
- package/dist/classes/template.js +311 -0
- package/dist/classes/template.js.map +1 -0
- package/dist/constants/constants.d.ts +11 -0
- package/dist/constants/constants.js +182 -0
- package/dist/constants/constants.js.map +1 -0
- package/dist/enums/chart-type.d.ts +11 -0
- package/dist/enums/chart-type.js +16 -0
- package/dist/enums/chart-type.js.map +1 -0
- package/dist/enums/content-type-map.d.ts +16 -0
- package/dist/enums/content-type-map.js +23 -0
- package/dist/enums/content-type-map.js.map +1 -0
- package/dist/enums/element-type.d.ts +14 -0
- package/dist/enums/element-type.js +20 -0
- package/dist/enums/element-type.js.map +1 -0
- package/dist/errors.d.ts +81 -0
- package/dist/errors.js +100 -0
- package/dist/errors.js.map +1 -0
- package/dist/extensions/index.d.ts +5 -0
- package/dist/extensions/index.js +31 -0
- package/dist/extensions/index.js.map +1 -0
- package/dist/extensions/pptxgenjs-compat/img-broken-png.d.ts +5 -0
- package/dist/extensions/pptxgenjs-compat/img-broken-png.js +9 -0
- package/dist/extensions/pptxgenjs-compat/img-broken-png.js.map +1 -0
- package/dist/extensions/pptxgenjs-compat/index.d.ts +5 -0
- package/dist/extensions/pptxgenjs-compat/index.js +10 -0
- package/dist/extensions/pptxgenjs-compat/index.js.map +1 -0
- package/dist/extensions/pptxgenjs-compat/svg-fallback.d.ts +14 -0
- package/dist/extensions/pptxgenjs-compat/svg-fallback.js +139 -0
- package/dist/extensions/pptxgenjs-compat/svg-fallback.js.map +1 -0
- package/dist/extensions/template-logic/commands.d.ts +22 -0
- package/dist/extensions/template-logic/commands.js +83 -0
- package/dist/extensions/template-logic/commands.js.map +1 -0
- package/dist/extensions/template-logic/expression.d.ts +61 -0
- package/dist/extensions/template-logic/expression.js +499 -0
- package/dist/extensions/template-logic/expression.js.map +1 -0
- package/dist/extensions/template-logic/images.d.ts +26 -0
- package/dist/extensions/template-logic/images.js +183 -0
- package/dist/extensions/template-logic/images.js.map +1 -0
- package/dist/extensions/template-logic/index.d.ts +14 -0
- package/dist/extensions/template-logic/index.js +64 -0
- package/dist/extensions/template-logic/index.js.map +1 -0
- package/dist/extensions/template-logic/list-commands.d.ts +9 -0
- package/dist/extensions/template-logic/list-commands.js +72 -0
- package/dist/extensions/template-logic/list-commands.js.map +1 -0
- package/dist/extensions/template-logic/renderer.d.ts +85 -0
- package/dist/extensions/template-logic/renderer.js +867 -0
- package/dist/extensions/template-logic/renderer.js.map +1 -0
- package/dist/extensions/template-logic/types.d.ts +62 -0
- package/dist/extensions/template-logic/types.js +3 -0
- package/dist/extensions/template-logic/types.js.map +1 -0
- package/dist/helper/archive/archive-fs.d.ts +45 -0
- package/dist/helper/archive/archive-fs.js +206 -0
- package/dist/helper/archive/archive-fs.js.map +1 -0
- package/dist/helper/archive/archive-jszip.d.ts +31 -0
- package/dist/helper/archive/archive-jszip.js +173 -0
- package/dist/helper/archive/archive-jszip.js.map +1 -0
- package/dist/helper/archive/archive.d.ts +27 -0
- package/dist/helper/archive/archive.js +78 -0
- package/dist/helper/archive/archive.js.map +1 -0
- package/dist/helper/cell-id-helper.d.ts +25 -0
- package/dist/helper/cell-id-helper.js +95 -0
- package/dist/helper/cell-id-helper.js.map +1 -0
- package/dist/helper/compute-src-rect.d.ts +9 -0
- package/dist/helper/compute-src-rect.js +52 -0
- package/dist/helper/compute-src-rect.js.map +1 -0
- package/dist/helper/content-tracker.d.ts +31 -0
- package/dist/helper/content-tracker.js +227 -0
- package/dist/helper/content-tracker.js.map +1 -0
- package/dist/helper/count-helper.d.ts +33 -0
- package/dist/helper/count-helper.js +140 -0
- package/dist/helper/count-helper.js.map +1 -0
- package/dist/helper/css-style-parser.d.ts +52 -0
- package/dist/helper/css-style-parser.js +246 -0
- package/dist/helper/css-style-parser.js.map +1 -0
- package/dist/helper/file-helper.d.ts +36 -0
- package/dist/helper/file-helper.js +150 -0
- package/dist/helper/file-helper.js.map +1 -0
- package/dist/helper/general-helper.d.ts +6 -0
- package/dist/helper/general-helper.js +34 -0
- package/dist/helper/general-helper.js.map +1 -0
- package/dist/helper/generate/generate-pptxgenjs.d.ts +37 -0
- package/dist/helper/generate/generate-pptxgenjs.js +134 -0
- package/dist/helper/generate/generate-pptxgenjs.js.map +1 -0
- package/dist/helper/html-to-multitext-helper.d.ts +84 -0
- package/dist/helper/html-to-multitext-helper.js +490 -0
- package/dist/helper/html-to-multitext-helper.js.map +1 -0
- package/dist/helper/hyperlink-processor.d.ts +54 -0
- package/dist/helper/hyperlink-processor.js +289 -0
- package/dist/helper/hyperlink-processor.js.map +1 -0
- package/dist/helper/image-dimensions.d.ts +21 -0
- package/dist/helper/image-dimensions.js +202 -0
- package/dist/helper/image-dimensions.js.map +1 -0
- package/dist/helper/jszip-helper.d.ts +21 -0
- package/dist/helper/jszip-helper.js +162 -0
- package/dist/helper/jszip-helper.js.map +1 -0
- package/dist/helper/layout-index.d.ts +55 -0
- package/dist/helper/layout-index.js +155 -0
- package/dist/helper/layout-index.js.map +1 -0
- package/dist/helper/logger.d.ts +53 -0
- package/dist/helper/logger.js +73 -0
- package/dist/helper/logger.js.map +1 -0
- package/dist/helper/media-deduplicator.d.ts +42 -0
- package/dist/helper/media-deduplicator.js +88 -0
- package/dist/helper/media-deduplicator.js.map +1 -0
- package/dist/helper/modify-background-helper.d.ts +19 -0
- package/dist/helper/modify-background-helper.js +59 -0
- package/dist/helper/modify-background-helper.js.map +1 -0
- package/dist/helper/modify-chart-helper.d.ts +159 -0
- package/dist/helper/modify-chart-helper.js +642 -0
- package/dist/helper/modify-chart-helper.js.map +1 -0
- package/dist/helper/modify-cleanup-helper.d.ts +69 -0
- package/dist/helper/modify-cleanup-helper.js +343 -0
- package/dist/helper/modify-cleanup-helper.js.map +1 -0
- package/dist/helper/modify-color-helper.d.ts +35 -0
- package/dist/helper/modify-color-helper.js +211 -0
- package/dist/helper/modify-color-helper.js.map +1 -0
- package/dist/helper/modify-helper.d.ts +23 -0
- package/dist/helper/modify-helper.js +61 -0
- package/dist/helper/modify-helper.js.map +1 -0
- package/dist/helper/modify-hyperlink-element.d.ts +9 -0
- package/dist/helper/modify-hyperlink-element.js +34 -0
- package/dist/helper/modify-hyperlink-element.js.map +1 -0
- package/dist/helper/modify-hyperlink-helper.d.ts +42 -0
- package/dist/helper/modify-hyperlink-helper.js +258 -0
- package/dist/helper/modify-hyperlink-helper.js.map +1 -0
- package/dist/helper/modify-image-helper.d.ts +23 -0
- package/dist/helper/modify-image-helper.js +157 -0
- package/dist/helper/modify-image-helper.js.map +1 -0
- package/dist/helper/modify-presentation-helper.d.ts +55 -0
- package/dist/helper/modify-presentation-helper.js +177 -0
- package/dist/helper/modify-presentation-helper.js.map +1 -0
- package/dist/helper/modify-shape-helper.d.ts +111 -0
- package/dist/helper/modify-shape-helper.js +340 -0
- package/dist/helper/modify-shape-helper.js.map +1 -0
- package/dist/helper/modify-table-helper.d.ts +11 -0
- package/dist/helper/modify-table-helper.js +100 -0
- package/dist/helper/modify-table-helper.js.map +1 -0
- package/dist/helper/modify-text-helper.d.ts +62 -0
- package/dist/helper/modify-text-helper.js +244 -0
- package/dist/helper/modify-text-helper.js.map +1 -0
- package/dist/helper/modify-xml-helper.d.ts +52 -0
- package/dist/helper/modify-xml-helper.js +305 -0
- package/dist/helper/modify-xml-helper.js.map +1 -0
- package/dist/helper/multitext-helper.d.ts +116 -0
- package/dist/helper/multitext-helper.js +462 -0
- package/dist/helper/multitext-helper.js.map +1 -0
- package/dist/helper/ppt-paths.d.ts +76 -0
- package/dist/helper/ppt-paths.js +116 -0
- package/dist/helper/ppt-paths.js.map +1 -0
- package/dist/helper/shape-type-detector.d.ts +34 -0
- package/dist/helper/shape-type-detector.js +99 -0
- package/dist/helper/shape-type-detector.js.map +1 -0
- package/dist/helper/text-replace-helper.d.ts +30 -0
- package/dist/helper/text-replace-helper.js +147 -0
- package/dist/helper/text-replace-helper.js.map +1 -0
- package/dist/helper/xml/lnLRTB.d.ts +1 -0
- package/dist/helper/xml/lnLRTB.js +62 -0
- package/dist/helper/xml/lnLRTB.js.map +1 -0
- package/dist/helper/xml/prstGeom.d.ts +1 -0
- package/dist/helper/xml/prstGeom.js +11 -0
- package/dist/helper/xml/prstGeom.js.map +1 -0
- package/dist/helper/xml-elements.d.ts +134 -0
- package/dist/helper/xml-elements.js +538 -0
- package/dist/helper/xml-elements.js.map +1 -0
- package/dist/helper/xml-helper.d.ts +120 -0
- package/dist/helper/xml-helper.js +495 -0
- package/dist/helper/xml-helper.js.map +1 -0
- package/dist/helper/xml-placeholder-helper.d.ts +92 -0
- package/dist/helper/xml-placeholder-helper.js +454 -0
- package/dist/helper/xml-placeholder-helper.js.map +1 -0
- package/dist/helper/xml-pretty-print.d.ts +17 -0
- package/dist/helper/xml-pretty-print.js +88 -0
- package/dist/helper/xml-pretty-print.js.map +1 -0
- package/dist/helper/xml-relationship-helper.d.ts +103 -0
- package/dist/helper/xml-relationship-helper.js +397 -0
- package/dist/helper/xml-relationship-helper.js.map +1 -0
- package/dist/helper/xml-slide-helper.d.ts +107 -0
- package/dist/helper/xml-slide-helper.js +676 -0
- package/dist/helper/xml-slide-helper.js.map +1 -0
- package/dist/helper/xml-template-helper.d.ts +42 -0
- package/dist/helper/xml-template-helper.js +252 -0
- package/dist/helper/xml-template-helper.js.map +1 -0
- package/dist/helper/xmldom-sax-patch.d.ts +18 -0
- package/dist/helper/xmldom-sax-patch.js +58 -0
- package/dist/helper/xmldom-sax-patch.js.map +1 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +177 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/iarchive.d.ts +43 -0
- package/dist/interfaces/iarchive.js +3 -0
- package/dist/interfaces/iarchive.js.map +1 -0
- package/dist/interfaces/ichart.d.ts +7 -0
- package/dist/interfaces/ichart.js +3 -0
- package/dist/interfaces/ichart.js.map +1 -0
- package/dist/interfaces/icounter.d.ts +7 -0
- package/dist/interfaces/icounter.js +3 -0
- package/dist/interfaces/icounter.js.map +1 -0
- package/dist/interfaces/igenerator.d.ts +4 -0
- package/dist/interfaces/igenerator.js +3 -0
- package/dist/interfaces/igenerator.js.map +1 -0
- package/dist/interfaces/iimage.d.ts +7 -0
- package/dist/interfaces/iimage.js +3 -0
- package/dist/interfaces/iimage.js.map +1 -0
- package/dist/interfaces/ilayout.d.ts +14 -0
- package/dist/interfaces/ilayout.js +3 -0
- package/dist/interfaces/ilayout.js.map +1 -0
- package/dist/interfaces/imaster.d.ts +15 -0
- package/dist/interfaces/imaster.js +3 -0
- package/dist/interfaces/imaster.js.map +1 -0
- package/dist/interfaces/imulti-text.d.ts +50 -0
- package/dist/interfaces/imulti-text.js +3 -0
- package/dist/interfaces/imulti-text.js.map +1 -0
- package/dist/interfaces/ipptxgenjs-slide.d.ts +40 -0
- package/dist/interfaces/ipptxgenjs-slide.js +3 -0
- package/dist/interfaces/ipptxgenjs-slide.js.map +1 -0
- package/dist/interfaces/ipresentation-props.d.ts +16 -0
- package/dist/interfaces/ipresentation-props.js +3 -0
- package/dist/interfaces/ipresentation-props.js.map +1 -0
- package/dist/interfaces/ishape-action.d.ts +18 -0
- package/dist/interfaces/ishape-action.js +3 -0
- package/dist/interfaces/ishape-action.js.map +1 -0
- package/dist/interfaces/ishape.d.ts +5 -0
- package/dist/interfaces/ishape.js +3 -0
- package/dist/interfaces/ishape.js.map +1 -0
- package/dist/interfaces/islide.d.ts +71 -0
- package/dist/interfaces/islide.js +3 -0
- package/dist/interfaces/islide.js.map +1 -0
- package/dist/interfaces/itemplate.d.ts +9 -0
- package/dist/interfaces/itemplate.js +3 -0
- package/dist/interfaces/itemplate.js.map +1 -0
- package/dist/interfaces/pres-template.d.ts +9 -0
- package/dist/interfaces/pres-template.js +3 -0
- package/dist/interfaces/pres-template.js.map +1 -0
- package/dist/interfaces/root-pres-template.d.ts +38 -0
- package/dist/interfaces/root-pres-template.js +3 -0
- package/dist/interfaces/root-pres-template.js.map +1 -0
- package/dist/modify/modify-chart.d.ts +74 -0
- package/dist/modify/modify-chart.js +753 -0
- package/dist/modify/modify-chart.js.map +1 -0
- package/dist/modify/modify-table.d.ts +64 -0
- package/dist/modify/modify-table.js +435 -0
- package/dist/modify/modify-table.js.map +1 -0
- package/dist/shapes/chart.d.ts +91 -0
- package/dist/shapes/chart.js +437 -0
- package/dist/shapes/chart.js.map +1 -0
- package/dist/shapes/diagram.d.ts +33 -0
- package/dist/shapes/diagram.js +167 -0
- package/dist/shapes/diagram.js.map +1 -0
- package/dist/shapes/generic.d.ts +27 -0
- package/dist/shapes/generic.js +118 -0
- package/dist/shapes/generic.js.map +1 -0
- package/dist/shapes/hyperlink.d.ts +27 -0
- package/dist/shapes/hyperlink.js +193 -0
- package/dist/shapes/hyperlink.js.map +1 -0
- package/dist/shapes/image.d.ts +47 -0
- package/dist/shapes/image.js +244 -0
- package/dist/shapes/image.js.map +1 -0
- package/dist/shapes/ole.d.ts +30 -0
- package/dist/shapes/ole.js +237 -0
- package/dist/shapes/ole.js.map +1 -0
- package/dist/types/chart-types.d.ts +115 -0
- package/dist/types/chart-types.js +3 -0
- package/dist/types/chart-types.js.map +1 -0
- package/dist/types/modify-types.d.ts +128 -0
- package/dist/types/modify-types.js +3 -0
- package/dist/types/modify-types.js.map +1 -0
- package/dist/types/shape-types.d.ts +40 -0
- package/dist/types/shape-types.js +3 -0
- package/dist/types/shape-types.js.map +1 -0
- package/dist/types/table-types.d.ts +38 -0
- package/dist/types/table-types.js +3 -0
- package/dist/types/table-types.js.map +1 -0
- package/dist/types/types.d.ts +317 -0
- package/dist/types/types.js +16 -0
- package/dist/types/types.js.map +1 -0
- package/dist/types/xml-types.d.ts +168 -0
- package/dist/types/xml-types.js +3 -0
- package/dist/types/xml-types.js.map +1 -0
- package/package.json +92 -4
|
@@ -0,0 +1,867 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TemplateRenderer = exports.TemplateRenderError = void 0;
|
|
13
|
+
const xml_helper_1 = require("../../helper/xml-helper");
|
|
14
|
+
const xml_relationship_helper_1 = require("../../helper/xml-relationship-helper");
|
|
15
|
+
const multitext_helper_1 = require("../../helper/multitext-helper");
|
|
16
|
+
const html_to_multitext_helper_1 = require("../../helper/html-to-multitext-helper");
|
|
17
|
+
const expression_1 = require("./expression");
|
|
18
|
+
const commands_1 = require("./commands");
|
|
19
|
+
const images_1 = require("./images");
|
|
20
|
+
/** Tags that open, split or close a block. */
|
|
21
|
+
const BLOCK_TYPES = [
|
|
22
|
+
'if',
|
|
23
|
+
'elseif',
|
|
24
|
+
'else',
|
|
25
|
+
'endif',
|
|
26
|
+
'for',
|
|
27
|
+
'endfor',
|
|
28
|
+
];
|
|
29
|
+
/** Tags that leave nothing in their own paragraph. */
|
|
30
|
+
const STRUCTURAL_TYPES = [
|
|
31
|
+
...BLOCK_TYPES,
|
|
32
|
+
'exec',
|
|
33
|
+
'alias',
|
|
34
|
+
'html',
|
|
35
|
+
'image',
|
|
36
|
+
];
|
|
37
|
+
/** Tags that produce visible content (keep a table row). */
|
|
38
|
+
const CONTENT_TYPES = ['insert', 'html', 'link'];
|
|
39
|
+
/** Children of <a:rPr>, schema order (CT_TextCharacterProperties). */
|
|
40
|
+
const RPR_CHILD_ORDER = [
|
|
41
|
+
'a:ln',
|
|
42
|
+
'a:noFill',
|
|
43
|
+
'a:solidFill',
|
|
44
|
+
'a:gradFill',
|
|
45
|
+
'a:blipFill',
|
|
46
|
+
'a:pattFill',
|
|
47
|
+
'a:grpFill',
|
|
48
|
+
'a:effectLst',
|
|
49
|
+
'a:effectDag',
|
|
50
|
+
'a:highlight',
|
|
51
|
+
'a:uLnTx',
|
|
52
|
+
'a:uLn',
|
|
53
|
+
'a:uFillTx',
|
|
54
|
+
'a:uFill',
|
|
55
|
+
'a:latin',
|
|
56
|
+
'a:ea',
|
|
57
|
+
'a:cs',
|
|
58
|
+
'a:sym',
|
|
59
|
+
'a:hlinkClick',
|
|
60
|
+
'a:hlinkMouseOver',
|
|
61
|
+
'a:rtl',
|
|
62
|
+
'a:extLst',
|
|
63
|
+
];
|
|
64
|
+
const REL_TYPE_HYPERLINK = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink';
|
|
65
|
+
const REL_TYPE_SLIDE = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide';
|
|
66
|
+
const textControl = (token) => token.kind === 'tag' ? token : undefined;
|
|
67
|
+
const isThenable = (value) => !!value && typeof value.then === 'function';
|
|
68
|
+
class Scope {
|
|
69
|
+
constructor(vars, parent, writable = true) {
|
|
70
|
+
this.vars = vars;
|
|
71
|
+
this.parent = parent;
|
|
72
|
+
this.writable = writable;
|
|
73
|
+
}
|
|
74
|
+
lookup(name) {
|
|
75
|
+
if (this.vars && Object.prototype.hasOwnProperty.call(this.vars, name)) {
|
|
76
|
+
return { found: true, value: this.vars[name] };
|
|
77
|
+
}
|
|
78
|
+
return this.parent ? this.parent.lookup(name) : { found: false };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Updates the nearest writable scope that defines `name`, else defines it
|
|
82
|
+
* here. The user's data and additionalJsContext are never written.
|
|
83
|
+
*/
|
|
84
|
+
assign(name, value) {
|
|
85
|
+
if (!this.assignExisting(name, value))
|
|
86
|
+
this.vars[name] = value;
|
|
87
|
+
}
|
|
88
|
+
assignExisting(name, value) {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
if (this.writable &&
|
|
91
|
+
Object.prototype.hasOwnProperty.call(this.vars, name)) {
|
|
92
|
+
this.vars[name] = value;
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.assignExisting(name, value)) !== null && _b !== void 0 ? _b : false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
class TemplateRenderError extends Error {
|
|
99
|
+
constructor(message) {
|
|
100
|
+
super(message);
|
|
101
|
+
this.name = 'TemplateRenderError';
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.TemplateRenderError = TemplateRenderError;
|
|
105
|
+
class TemplateRenderer {
|
|
106
|
+
constructor(data, options = {}, context = {}) {
|
|
107
|
+
var _a, _b;
|
|
108
|
+
this.options = options;
|
|
109
|
+
this.context = context;
|
|
110
|
+
this.aliases = new Map();
|
|
111
|
+
this.errors = [];
|
|
112
|
+
this.links = new Map();
|
|
113
|
+
/** IMAGE values per shape text body, placed once the body is written. */
|
|
114
|
+
this.pendingImages = new Map();
|
|
115
|
+
const syntax = (0, commands_1.tagSyntax)(options);
|
|
116
|
+
this.opening = syntax.opening;
|
|
117
|
+
this.tagPattern = syntax.pattern;
|
|
118
|
+
this.strict = (_a = options.strict) !== null && _a !== void 0 ? _a : false;
|
|
119
|
+
const jsContext = new Scope((_b = options.additionalJsContext) !== null && _b !== void 0 ? _b : {}, undefined, false);
|
|
120
|
+
this.root = new Scope({}, new Scope(data !== null && data !== void 0 ? data : {}, jsContext, false));
|
|
121
|
+
}
|
|
122
|
+
render(element) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
this.collectAliases(element);
|
|
125
|
+
for (const target of this.findTargets(element)) {
|
|
126
|
+
if (target.tagName === 'a:tbl') {
|
|
127
|
+
yield this.renderTable(target);
|
|
128
|
+
}
|
|
129
|
+
else if (this.containsTag(target)) {
|
|
130
|
+
const bodyTokens = this.tokenizeBody(target);
|
|
131
|
+
yield this.writeBody(target, bodyTokens, this.parse(bodyTokens.tokens, textControl), this.root);
|
|
132
|
+
yield this.placePendingImages(target);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (this.errors.length) {
|
|
136
|
+
throw new TemplateRenderError(`Template rendering failed with ${this.errors.length} error(s):\n` +
|
|
137
|
+
this.errors.map((e) => `- ${e}`).join('\n'));
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/** Shape text bodies and tables, in document order. */
|
|
142
|
+
findTargets(element) {
|
|
143
|
+
const targets = [];
|
|
144
|
+
const isTarget = (el) => el.tagName === 'a:tbl' || el.tagName === 'p:txBody';
|
|
145
|
+
const visit = (parent) => {
|
|
146
|
+
this.childElements(parent).forEach((child) => {
|
|
147
|
+
if (isTarget(child))
|
|
148
|
+
targets.push(child);
|
|
149
|
+
else
|
|
150
|
+
visit(child);
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
if (isTarget(element))
|
|
154
|
+
targets.push(element);
|
|
155
|
+
else
|
|
156
|
+
visit(element);
|
|
157
|
+
return targets;
|
|
158
|
+
}
|
|
159
|
+
containsTag(element) {
|
|
160
|
+
var _a;
|
|
161
|
+
return ((_a = element.textContent) !== null && _a !== void 0 ? _a : '').includes(this.opening);
|
|
162
|
+
}
|
|
163
|
+
/** ALIAS definitions apply everywhere, so collect them up front. */
|
|
164
|
+
collectAliases(element) {
|
|
165
|
+
xml_helper_1.XmlHelper.collectionToArray(element.getElementsByTagName('a:p')).forEach((paragraph) => {
|
|
166
|
+
const text = xml_helper_1.XmlHelper.collectionToArray(paragraph.getElementsByTagName('a:t'))
|
|
167
|
+
.map((t) => t.textContent)
|
|
168
|
+
.join('');
|
|
169
|
+
for (const match of text.matchAll(this.tagPattern)) {
|
|
170
|
+
const command = (0, commands_1.parseCommand)(match[1], match[0]);
|
|
171
|
+
if (command.type === 'ALIAS') {
|
|
172
|
+
this.aliases.set(command.name, command.code);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/*
|
|
178
|
+
* Tokenizing
|
|
179
|
+
*/
|
|
180
|
+
tokenizeBody(body) {
|
|
181
|
+
const tokens = [];
|
|
182
|
+
const controlOnly = new Set();
|
|
183
|
+
this.childElements(body, 'a:p').forEach((paragraph, index) => {
|
|
184
|
+
if (index > 0)
|
|
185
|
+
tokens.push({ kind: 'break', paragraph });
|
|
186
|
+
const paragraphTokens = this.tokenizeParagraph(paragraph);
|
|
187
|
+
tokens.push(...paragraphTokens);
|
|
188
|
+
const isStructural = (t) => t.kind === 'tag' && STRUCTURAL_TYPES.includes(t.directive.type);
|
|
189
|
+
const onlyStructural = paragraphTokens.every((t) => isStructural(t) || (t.kind === 'text' && t.text.trim() === ''));
|
|
190
|
+
if (paragraphTokens.some(isStructural) && onlyStructural) {
|
|
191
|
+
controlOnly.add(paragraph);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return { tokens, controlOnly };
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* PowerPoint splits text into runs at will (spell check, edits, styling),
|
|
198
|
+
* so a tag like `{{user.name}}` often spans several <a:r>. Consecutive runs
|
|
199
|
+
* are joined; each tag takes the formatting of the run it starts in.
|
|
200
|
+
*/
|
|
201
|
+
tokenizeParagraph(paragraph) {
|
|
202
|
+
const tokens = [];
|
|
203
|
+
let segment = [];
|
|
204
|
+
const flush = () => {
|
|
205
|
+
tokens.push(...this.tokenizeRuns(segment));
|
|
206
|
+
segment = [];
|
|
207
|
+
};
|
|
208
|
+
this.childElements(paragraph).forEach((child) => {
|
|
209
|
+
if (child.tagName === 'a:r') {
|
|
210
|
+
segment.push(child);
|
|
211
|
+
}
|
|
212
|
+
else if (child.tagName !== 'a:pPr' &&
|
|
213
|
+
child.tagName !== 'a:endParaRPr') {
|
|
214
|
+
flush();
|
|
215
|
+
tokens.push({ kind: 'node', node: child });
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
flush();
|
|
219
|
+
return tokens;
|
|
220
|
+
}
|
|
221
|
+
tokenizeRuns(runs) {
|
|
222
|
+
const texts = runs.map((run) => this.runText(run));
|
|
223
|
+
const full = texts.join('');
|
|
224
|
+
const starts = [];
|
|
225
|
+
texts.reduce((offset, text) => {
|
|
226
|
+
starts.push(offset);
|
|
227
|
+
return offset + text.length;
|
|
228
|
+
}, 0);
|
|
229
|
+
const runAt = (offset) => {
|
|
230
|
+
let r = 0;
|
|
231
|
+
while (r + 1 < runs.length && starts[r + 1] <= offset)
|
|
232
|
+
r++;
|
|
233
|
+
return r;
|
|
234
|
+
};
|
|
235
|
+
const tokens = [];
|
|
236
|
+
const pushText = (from, to) => {
|
|
237
|
+
// Plain text keeps its original run boundaries and formatting.
|
|
238
|
+
while (from < to) {
|
|
239
|
+
const r = runAt(from);
|
|
240
|
+
const end = Math.min(to, starts[r] + texts[r].length);
|
|
241
|
+
tokens.push({
|
|
242
|
+
kind: 'text',
|
|
243
|
+
run: runs[r],
|
|
244
|
+
text: full.slice(from, end),
|
|
245
|
+
});
|
|
246
|
+
from = end;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
let last = 0;
|
|
250
|
+
for (const match of full.matchAll(this.tagPattern)) {
|
|
251
|
+
pushText(last, match.index);
|
|
252
|
+
tokens.push({
|
|
253
|
+
kind: 'tag',
|
|
254
|
+
run: runs[runAt(match.index)],
|
|
255
|
+
tag: match[0],
|
|
256
|
+
directive: this.parseDirective(match[1], match[0]),
|
|
257
|
+
});
|
|
258
|
+
last = match.index + match[0].length;
|
|
259
|
+
}
|
|
260
|
+
pushText(last, full.length);
|
|
261
|
+
// An empty run (<a:t/>) still carries formatting; keep it.
|
|
262
|
+
if (!tokens.length && runs.length) {
|
|
263
|
+
runs.forEach((run) => tokens.push({ kind: 'node', node: run }));
|
|
264
|
+
}
|
|
265
|
+
return tokens;
|
|
266
|
+
}
|
|
267
|
+
parseDirective(inner, tag) {
|
|
268
|
+
const command = (0, commands_1.parseCommand)((0, commands_1.resolveAlias)(inner, this.aliases, tag), tag);
|
|
269
|
+
const compile = (source) => this.compile(source, tag);
|
|
270
|
+
switch (command.type) {
|
|
271
|
+
case 'IF':
|
|
272
|
+
return { type: 'if', expr: compile(command.code) };
|
|
273
|
+
case 'ELSE-IF':
|
|
274
|
+
return { type: 'elseif', expr: compile(command.code) };
|
|
275
|
+
case 'ELSE':
|
|
276
|
+
return { type: 'else' };
|
|
277
|
+
case 'END-IF':
|
|
278
|
+
return { type: 'endif' };
|
|
279
|
+
case 'FOR':
|
|
280
|
+
return {
|
|
281
|
+
type: 'for',
|
|
282
|
+
variable: command.name,
|
|
283
|
+
expr: compile(command.code),
|
|
284
|
+
};
|
|
285
|
+
case 'END-FOR':
|
|
286
|
+
return { type: 'endfor' };
|
|
287
|
+
case 'ALIAS':
|
|
288
|
+
return { type: 'alias' };
|
|
289
|
+
case 'EXEC':
|
|
290
|
+
return { type: 'exec', statements: this.compileStatements(command.code, tag) };
|
|
291
|
+
case 'HTML':
|
|
292
|
+
return { type: 'html', expr: compile(command.code) };
|
|
293
|
+
case 'LINK':
|
|
294
|
+
return { type: 'link', expr: compile(command.code) };
|
|
295
|
+
case 'IMAGE':
|
|
296
|
+
return { type: 'image', expr: compile(command.code) };
|
|
297
|
+
case 'INS':
|
|
298
|
+
return { type: 'insert', expr: compile(command.code) };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* A syntax error fails right away - unless errors are handled
|
|
303
|
+
* (errorHandler, failFast: false); then it fails when evaluated.
|
|
304
|
+
*/
|
|
305
|
+
compile(source, tag) {
|
|
306
|
+
try {
|
|
307
|
+
return new expression_1.TemplateExpression(source);
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
if (!this.handlesErrors)
|
|
311
|
+
throw this.tagged(error, tag);
|
|
312
|
+
return {
|
|
313
|
+
source,
|
|
314
|
+
evaluate: () => {
|
|
315
|
+
throw error;
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
compileStatements(source, tag) {
|
|
321
|
+
try {
|
|
322
|
+
return (0, expression_1.parseStatements)(source);
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
if (!this.handlesErrors)
|
|
326
|
+
throw this.tagged(error, tag);
|
|
327
|
+
return [{ expr: this.compile(source, tag) }];
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
get handlesErrors() {
|
|
331
|
+
return !!this.options.errorHandler || this.options.failFast === false;
|
|
332
|
+
}
|
|
333
|
+
tagged(error, tag) {
|
|
334
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
335
|
+
return new TemplateRenderError(`${tag}: ${message}`);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Evaluates one command. Promises are awaited, so data functions may be
|
|
339
|
+
* async. Errors go to errorHandler, are collected (failFast: false) or
|
|
340
|
+
* reject the render.
|
|
341
|
+
*/
|
|
342
|
+
value(tag, fn) {
|
|
343
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
344
|
+
try {
|
|
345
|
+
let result = fn();
|
|
346
|
+
if (isThenable(result))
|
|
347
|
+
result = yield result;
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
352
|
+
if (this.options.errorHandler) {
|
|
353
|
+
return this.options.errorHandler(err, tag);
|
|
354
|
+
}
|
|
355
|
+
if (this.options.failFast === false) {
|
|
356
|
+
this.errors.push(`${tag}: ${err.message}`);
|
|
357
|
+
return undefined;
|
|
358
|
+
}
|
|
359
|
+
throw this.tagged(err, tag);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/*
|
|
364
|
+
* Parsing & evaluating blocks (shared by text bodies and table rows)
|
|
365
|
+
*/
|
|
366
|
+
parse(items, control) {
|
|
367
|
+
const root = [];
|
|
368
|
+
const stack = [];
|
|
369
|
+
const current = () => (stack.length ? stack[stack.length - 1].body : root);
|
|
370
|
+
items.forEach((item) => {
|
|
371
|
+
const tag = control(item);
|
|
372
|
+
const directive = tag === null || tag === void 0 ? void 0 : tag.directive;
|
|
373
|
+
if (!directive || !BLOCK_TYPES.includes(directive.type)) {
|
|
374
|
+
current().push({ kind: 'item', item });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const top = stack[stack.length - 1];
|
|
378
|
+
switch (directive.type) {
|
|
379
|
+
case 'if': {
|
|
380
|
+
const body = [];
|
|
381
|
+
const block = {
|
|
382
|
+
kind: 'if',
|
|
383
|
+
branches: [{ expr: directive.expr, tag: tag.tag, body }],
|
|
384
|
+
};
|
|
385
|
+
current().push(block);
|
|
386
|
+
stack.push({ block, tag: tag.tag, body });
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case 'for': {
|
|
390
|
+
const body = [];
|
|
391
|
+
const block = {
|
|
392
|
+
kind: 'for',
|
|
393
|
+
variable: directive.variable,
|
|
394
|
+
expr: directive.expr,
|
|
395
|
+
tag: tag.tag,
|
|
396
|
+
body,
|
|
397
|
+
};
|
|
398
|
+
current().push(block);
|
|
399
|
+
stack.push({ block, tag: tag.tag, body });
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case 'elseif':
|
|
403
|
+
case 'else': {
|
|
404
|
+
if ((top === null || top === void 0 ? void 0 : top.block.kind) !== 'if') {
|
|
405
|
+
throw new TemplateRenderError(`${tag.tag} without a matching {{IF …}}`);
|
|
406
|
+
}
|
|
407
|
+
const branches = top.block.branches;
|
|
408
|
+
if (!branches[branches.length - 1].expr) {
|
|
409
|
+
throw new TemplateRenderError(`${tag.tag} after {{ELSE}} in ${top.tag}`);
|
|
410
|
+
}
|
|
411
|
+
const body = [];
|
|
412
|
+
branches.push({
|
|
413
|
+
expr: directive.type === 'elseif' ? directive.expr : undefined,
|
|
414
|
+
tag: tag.tag,
|
|
415
|
+
body,
|
|
416
|
+
});
|
|
417
|
+
top.body = body;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case 'endif':
|
|
421
|
+
case 'endfor': {
|
|
422
|
+
const expected = directive.type === 'endif' ? 'if' : 'for';
|
|
423
|
+
if ((top === null || top === void 0 ? void 0 : top.block.kind) !== expected) {
|
|
424
|
+
throw new TemplateRenderError(`${tag.tag} without a matching ${expected.toUpperCase()}` +
|
|
425
|
+
(top ? ` (open: ${top.tag})` : ''));
|
|
426
|
+
}
|
|
427
|
+
stack.pop();
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
if (stack.length) {
|
|
433
|
+
throw new TemplateRenderError(`Unclosed template block ${stack[stack.length - 1].tag}`);
|
|
434
|
+
}
|
|
435
|
+
return root;
|
|
436
|
+
}
|
|
437
|
+
evaluate(blocks, scope, emit) {
|
|
438
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
439
|
+
for (const block of blocks) {
|
|
440
|
+
if (block.kind === 'item') {
|
|
441
|
+
yield emit(block.item, scope);
|
|
442
|
+
}
|
|
443
|
+
else if (block.kind === 'if') {
|
|
444
|
+
for (const branch of block.branches) {
|
|
445
|
+
const pass = !branch.expr ||
|
|
446
|
+
(yield this.value(branch.tag, () => branch.expr.evaluate(scope, this.strict)));
|
|
447
|
+
if (pass) {
|
|
448
|
+
yield this.evaluate(branch.body, scope, emit);
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
const value = yield this.value(block.tag, () => {
|
|
455
|
+
const list = block.expr.evaluate(scope, this.strict);
|
|
456
|
+
if (isThenable(list))
|
|
457
|
+
return list.then((v) => this.toList(v, block.expr));
|
|
458
|
+
return this.toList(list, block.expr);
|
|
459
|
+
});
|
|
460
|
+
const entries = Array.isArray(value) ? value : [];
|
|
461
|
+
for (let idx = 0; idx < entries.length; idx++) {
|
|
462
|
+
const loopScope = new Scope({
|
|
463
|
+
[block.variable]: entries[idx],
|
|
464
|
+
[`$${block.variable}`]: entries[idx],
|
|
465
|
+
$idx: idx,
|
|
466
|
+
}, scope);
|
|
467
|
+
yield this.evaluate(block.body, loopScope, emit);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
toList(value, expr) {
|
|
474
|
+
if (value === null || value === undefined)
|
|
475
|
+
return [];
|
|
476
|
+
if (typeof value[Symbol.iterator] !== 'function') {
|
|
477
|
+
throw new Error(`"${expr.source}" is not a list`);
|
|
478
|
+
}
|
|
479
|
+
return Array.from(value);
|
|
480
|
+
}
|
|
481
|
+
/*
|
|
482
|
+
* Writing text bodies
|
|
483
|
+
*/
|
|
484
|
+
writeBody(body, bodyTokens, blocks, scope) {
|
|
485
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
486
|
+
const sourceParagraphs = this.childElements(body, 'a:p');
|
|
487
|
+
const first = sourceParagraphs[0];
|
|
488
|
+
if (!first)
|
|
489
|
+
return;
|
|
490
|
+
const paragraphs = [{ source: first, nodes: [] }];
|
|
491
|
+
const current = () => paragraphs[paragraphs.length - 1];
|
|
492
|
+
yield this.evaluate(blocks, scope, (token, tokenScope) => __awaiter(this, void 0, void 0, function* () {
|
|
493
|
+
switch (token.kind) {
|
|
494
|
+
case 'break':
|
|
495
|
+
paragraphs.push({ source: token.paragraph, nodes: [] });
|
|
496
|
+
break;
|
|
497
|
+
case 'node':
|
|
498
|
+
current().nodes.push(token.node.cloneNode(true));
|
|
499
|
+
break;
|
|
500
|
+
case 'text':
|
|
501
|
+
current().nodes.push(...this.createRuns(token.run, token.text));
|
|
502
|
+
break;
|
|
503
|
+
case 'tag':
|
|
504
|
+
yield this.emitTag(token, tokenScope, body, paragraphs);
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
}));
|
|
508
|
+
let kept = paragraphs.filter((p) => p.raw ||
|
|
509
|
+
!(bodyTokens.controlOnly.has(p.source) || p.continuation) ||
|
|
510
|
+
p.nodes.some((n) => n.tagName !== 'a:r' || this.runText(n).trim() !== ''));
|
|
511
|
+
// A text body needs at least one paragraph.
|
|
512
|
+
if (!kept.length)
|
|
513
|
+
kept = [{ source: first, nodes: [] }];
|
|
514
|
+
sourceParagraphs.forEach((p) => body.removeChild(p));
|
|
515
|
+
kept.forEach((p) => { var _a; return body.appendChild((_a = p.raw) !== null && _a !== void 0 ? _a : this.createParagraph(p)); });
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
emitTag(token, scope, body, paragraphs) {
|
|
519
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
520
|
+
var _a;
|
|
521
|
+
const { directive, tag, run } = token;
|
|
522
|
+
const current = paragraphs[paragraphs.length - 1];
|
|
523
|
+
const evaluate = (expr) => this.value(tag, () => expr.evaluate(scope, this.strict));
|
|
524
|
+
switch (directive.type) {
|
|
525
|
+
case 'insert': {
|
|
526
|
+
const value = yield this.value(tag, () => __awaiter(this, void 0, void 0, function* () {
|
|
527
|
+
const result = yield directive.expr.evaluate(scope, this.strict);
|
|
528
|
+
if (this.options.rejectNullish &&
|
|
529
|
+
(result === null || result === undefined)) {
|
|
530
|
+
throw new Error(`"${directive.expr.source}" is ${result}`);
|
|
531
|
+
}
|
|
532
|
+
return result;
|
|
533
|
+
}));
|
|
534
|
+
current.nodes.push(...this.createRuns(run, this.format(value)));
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
case 'exec':
|
|
538
|
+
for (const statement of directive.statements) {
|
|
539
|
+
const value = yield evaluate(statement.expr);
|
|
540
|
+
if (statement.assign)
|
|
541
|
+
scope.assign(statement.assign, value);
|
|
542
|
+
}
|
|
543
|
+
break;
|
|
544
|
+
case 'html': {
|
|
545
|
+
const html = this.format(yield evaluate(directive.expr));
|
|
546
|
+
if (!html)
|
|
547
|
+
break;
|
|
548
|
+
const generated = this.htmlParagraphs(body, current.source, run, html, yield this.relations());
|
|
549
|
+
generated.forEach((raw) => paragraphs.push({ source: current.source, nodes: [], raw }));
|
|
550
|
+
paragraphs.push({ source: current.source, nodes: [], continuation: true });
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
case 'link': {
|
|
554
|
+
const value = yield evaluate(directive.expr);
|
|
555
|
+
if (value === null || value === undefined || value === '')
|
|
556
|
+
break;
|
|
557
|
+
const runs = yield this.value(tag, () => __awaiter(this, void 0, void 0, function* () {
|
|
558
|
+
const { link, label } = this.toLink(value);
|
|
559
|
+
const relations = yield this.relations();
|
|
560
|
+
if (!relations) {
|
|
561
|
+
throw new Error('LINK needs the slide relationships');
|
|
562
|
+
}
|
|
563
|
+
const relId = this.linkRelation(relations, link);
|
|
564
|
+
const created = this.createRuns(run, label);
|
|
565
|
+
created
|
|
566
|
+
.filter((n) => n.tagName === 'a:r')
|
|
567
|
+
.forEach((r) => this.setHyperlink(r, relId, link.internal));
|
|
568
|
+
return created;
|
|
569
|
+
}));
|
|
570
|
+
if (Array.isArray(runs))
|
|
571
|
+
current.nodes.push(...runs);
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
case 'image': {
|
|
575
|
+
const value = yield evaluate(directive.expr);
|
|
576
|
+
if (value === null || value === undefined || value === '')
|
|
577
|
+
break;
|
|
578
|
+
const image = yield this.value(tag, () => __awaiter(this, void 0, void 0, function* () {
|
|
579
|
+
if (body.tagName !== 'p:txBody') {
|
|
580
|
+
throw new Error('IMAGE is not supported inside table cells');
|
|
581
|
+
}
|
|
582
|
+
if (!(yield this.imageTarget())) {
|
|
583
|
+
throw new Error('IMAGE needs slide.modify(modify.renderTemplate(…)); ' +
|
|
584
|
+
'modifyElement() callbacks cannot add media files');
|
|
585
|
+
}
|
|
586
|
+
return (0, images_1.loadImage)(value);
|
|
587
|
+
}));
|
|
588
|
+
if (!image || typeof image !== 'object' || !('content' in image))
|
|
589
|
+
break;
|
|
590
|
+
const pending = (_a = this.pendingImages.get(body)) !== null && _a !== void 0 ? _a : [];
|
|
591
|
+
pending.push(image);
|
|
592
|
+
this.pendingImages.set(body, pending);
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
imageTarget() {
|
|
599
|
+
var _a;
|
|
600
|
+
(_a = this.imageTargetPromise) !== null && _a !== void 0 ? _a : (this.imageTargetPromise = this.context.images
|
|
601
|
+
? this.context.images()
|
|
602
|
+
: Promise.resolve(undefined));
|
|
603
|
+
return this.imageTargetPromise;
|
|
604
|
+
}
|
|
605
|
+
/** Pictures go where the shape is; a shape left without text is removed. */
|
|
606
|
+
placePendingImages(body) {
|
|
607
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
608
|
+
const images = this.pendingImages.get(body);
|
|
609
|
+
if (!(images === null || images === void 0 ? void 0 : images.length))
|
|
610
|
+
return;
|
|
611
|
+
this.pendingImages.delete(body);
|
|
612
|
+
const shape = body.parentNode;
|
|
613
|
+
const text = xml_helper_1.XmlHelper.collectionToArray(body.getElementsByTagName('a:t'))
|
|
614
|
+
.map((t) => t.textContent)
|
|
615
|
+
.join('');
|
|
616
|
+
try {
|
|
617
|
+
yield (0, images_1.placeImages)(yield this.imageTarget(), shape, images, text.trim() === '');
|
|
618
|
+
}
|
|
619
|
+
catch (error) {
|
|
620
|
+
throw this.tagged(error, 'IMAGE');
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
createParagraph(output) {
|
|
625
|
+
const paragraph = output.source.cloneNode(false);
|
|
626
|
+
const pPr = this.childElements(output.source, 'a:pPr')[0];
|
|
627
|
+
const endParaRPr = this.childElements(output.source, 'a:endParaRPr')[0];
|
|
628
|
+
if (pPr)
|
|
629
|
+
paragraph.appendChild(pPr.cloneNode(true));
|
|
630
|
+
output.nodes.forEach((node) => paragraph.appendChild(node));
|
|
631
|
+
if (endParaRPr)
|
|
632
|
+
paragraph.appendChild(endParaRPr.cloneNode(true));
|
|
633
|
+
return paragraph;
|
|
634
|
+
}
|
|
635
|
+
/** Clone a run with new text; line breaks become <a:br/>. */
|
|
636
|
+
createRuns(template, text) {
|
|
637
|
+
if (text === '')
|
|
638
|
+
return [];
|
|
639
|
+
const doc = template.ownerDocument;
|
|
640
|
+
const rPr = this.childElements(template, 'a:rPr')[0];
|
|
641
|
+
const nodes = [];
|
|
642
|
+
const lines = this.options.processLineBreaks === false
|
|
643
|
+
? [text]
|
|
644
|
+
: text.split(/\r\n|\n|\v/); // \v: PowerPoint's soft line break
|
|
645
|
+
lines.forEach((line, index) => {
|
|
646
|
+
if (index > 0) {
|
|
647
|
+
const br = doc.createElement('a:br');
|
|
648
|
+
if (rPr)
|
|
649
|
+
br.appendChild(rPr.cloneNode(true));
|
|
650
|
+
nodes.push(br);
|
|
651
|
+
}
|
|
652
|
+
if (line === '')
|
|
653
|
+
return;
|
|
654
|
+
const run = template.cloneNode(true);
|
|
655
|
+
const t = run.getElementsByTagName('a:t')[0];
|
|
656
|
+
while (t.firstChild)
|
|
657
|
+
t.removeChild(t.firstChild);
|
|
658
|
+
t.appendChild(doc.createTextNode(xml_helper_1.XmlHelper.sanitizeText(line)));
|
|
659
|
+
nodes.push(run);
|
|
660
|
+
});
|
|
661
|
+
return nodes;
|
|
662
|
+
}
|
|
663
|
+
format(value) {
|
|
664
|
+
if (value === null || value === undefined)
|
|
665
|
+
return '';
|
|
666
|
+
if (Array.isArray(value))
|
|
667
|
+
return value.map((v) => this.format(v)).join(', ');
|
|
668
|
+
return String(value);
|
|
669
|
+
}
|
|
670
|
+
/*
|
|
671
|
+
* HTML and hyperlinks
|
|
672
|
+
*/
|
|
673
|
+
relations() {
|
|
674
|
+
var _a;
|
|
675
|
+
(_a = this.relationsPromise) !== null && _a !== void 0 ? _a : (this.relationsPromise = this.context.relations
|
|
676
|
+
? this.context.relations()
|
|
677
|
+
: Promise.resolve(undefined));
|
|
678
|
+
return this.relationsPromise;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Converts HTML with the library's html → MultiText pipeline. The tag's
|
|
682
|
+
* run provides the default size and color.
|
|
683
|
+
*/
|
|
684
|
+
htmlParagraphs(body, source, run, html, relations) {
|
|
685
|
+
const doc = body.ownerDocument;
|
|
686
|
+
const host = doc.createElement('p:sp');
|
|
687
|
+
const scratch = body.cloneNode(false);
|
|
688
|
+
['a:bodyPr', 'a:lstStyle'].forEach((name) => this.childElements(body, name).forEach((child) => scratch.appendChild(child.cloneNode(true))));
|
|
689
|
+
const paragraph = source.cloneNode(false);
|
|
690
|
+
paragraph.appendChild(run.cloneNode(true));
|
|
691
|
+
scratch.appendChild(paragraph);
|
|
692
|
+
host.appendChild(scratch);
|
|
693
|
+
// The html pipeline wants a <body>; template values are usually fragments.
|
|
694
|
+
const document = /<body[\s>]/i.test(html)
|
|
695
|
+
? html
|
|
696
|
+
: `<html><body>${html}</body></html>`;
|
|
697
|
+
new multitext_helper_1.MultiTextHelper(host, relations).run(new html_to_multitext_helper_1.HtmlToMultiTextHelper().run(document));
|
|
698
|
+
return this.childElements(scratch, 'a:p');
|
|
699
|
+
}
|
|
700
|
+
toLink(value) {
|
|
701
|
+
var _a;
|
|
702
|
+
const spec = typeof value === 'object'
|
|
703
|
+
? value
|
|
704
|
+
: { url: value };
|
|
705
|
+
const url = spec.url;
|
|
706
|
+
if (url === null || url === undefined || url === '') {
|
|
707
|
+
throw new Error('LINK needs a url, e.g. {{LINK ({ url, label })}}');
|
|
708
|
+
}
|
|
709
|
+
const internal = typeof url === 'number';
|
|
710
|
+
const target = internal ? `../slides/slide${url}.xml` : String(url);
|
|
711
|
+
const label = this.format((_a = spec.label) !== null && _a !== void 0 ? _a : url);
|
|
712
|
+
return { link: { target, internal }, label };
|
|
713
|
+
}
|
|
714
|
+
linkRelation(relations, link) {
|
|
715
|
+
const key = `${link.internal}:${link.target}`;
|
|
716
|
+
const known = this.links.get(key);
|
|
717
|
+
if (known)
|
|
718
|
+
return known;
|
|
719
|
+
const root = relations.tagName === 'Relationships'
|
|
720
|
+
? relations
|
|
721
|
+
: relations.ownerDocument.documentElement;
|
|
722
|
+
const id = `rId${xml_relationship_helper_1.XmlRelationshipHelper.getMaxId(root.getElementsByTagName('Relationship'), 'Id', true)}`;
|
|
723
|
+
const rel = root.ownerDocument.createElement('Relationship');
|
|
724
|
+
rel.setAttribute('Id', id);
|
|
725
|
+
rel.setAttribute('Type', link.internal ? REL_TYPE_SLIDE : REL_TYPE_HYPERLINK);
|
|
726
|
+
rel.setAttribute('Target', link.target);
|
|
727
|
+
if (!link.internal)
|
|
728
|
+
rel.setAttribute('TargetMode', 'External');
|
|
729
|
+
root.appendChild(rel);
|
|
730
|
+
this.links.set(key, id);
|
|
731
|
+
return id;
|
|
732
|
+
}
|
|
733
|
+
setHyperlink(run, relId, internal) {
|
|
734
|
+
const doc = run.ownerDocument;
|
|
735
|
+
let rPr = this.childElements(run, 'a:rPr')[0];
|
|
736
|
+
if (!rPr) {
|
|
737
|
+
rPr = doc.createElement('a:rPr');
|
|
738
|
+
rPr.setAttribute('lang', 'en-US');
|
|
739
|
+
run.insertBefore(rPr, run.firstChild);
|
|
740
|
+
}
|
|
741
|
+
this.childElements(rPr, 'a:hlinkClick').forEach((h) => rPr.removeChild(h));
|
|
742
|
+
const hlink = doc.createElement('a:hlinkClick');
|
|
743
|
+
hlink.setAttribute('r:id', relId);
|
|
744
|
+
if (internal)
|
|
745
|
+
hlink.setAttribute('action', 'ppaction://hlinksldjump');
|
|
746
|
+
xml_helper_1.XmlHelper.insertInSchemaOrder(rPr, hlink, RPR_CHILD_ORDER);
|
|
747
|
+
}
|
|
748
|
+
/*
|
|
749
|
+
* Tables: blocks left open inside a cell span (and repeat) table rows.
|
|
750
|
+
*/
|
|
751
|
+
renderTable(table) {
|
|
752
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
753
|
+
if (!this.containsTag(table))
|
|
754
|
+
return;
|
|
755
|
+
const rows = this.childElements(table, 'a:tr');
|
|
756
|
+
const rowTokens = [];
|
|
757
|
+
rows.forEach((row) => {
|
|
758
|
+
const openers = [];
|
|
759
|
+
const closers = [];
|
|
760
|
+
let hasContent = false;
|
|
761
|
+
const cells = this.childElements(row, 'a:tc').map((cell) => {
|
|
762
|
+
const body = this.childElements(cell, 'a:txBody')[0];
|
|
763
|
+
if (!body)
|
|
764
|
+
return undefined;
|
|
765
|
+
const bodyTokens = this.tokenizeBody(body);
|
|
766
|
+
const rowLevel = this.extractRowLevelTags(bodyTokens.tokens);
|
|
767
|
+
rowLevel.openers.forEach((t) => openers.push({ tag: t.tag, directive: t.directive }));
|
|
768
|
+
rowLevel.closers.forEach((t) => closers.push({ tag: t.tag, directive: t.directive }));
|
|
769
|
+
const extracted = new Set([
|
|
770
|
+
...rowLevel.openers,
|
|
771
|
+
...rowLevel.closers,
|
|
772
|
+
]);
|
|
773
|
+
bodyTokens.tokens = bodyTokens.tokens.filter((t) => !extracted.has(t));
|
|
774
|
+
hasContent =
|
|
775
|
+
hasContent ||
|
|
776
|
+
bodyTokens.tokens.some((t) => (t.kind === 'tag' && CONTENT_TYPES.includes(t.directive.type)) ||
|
|
777
|
+
(t.kind === 'text' && t.text.trim() !== '') ||
|
|
778
|
+
(t.kind === 'node' && t.node.tagName === 'a:fld'));
|
|
779
|
+
return bodyTokens;
|
|
780
|
+
});
|
|
781
|
+
const isControlRow = !hasContent && (openers.length || closers.length);
|
|
782
|
+
rowTokens.push(...openers);
|
|
783
|
+
if (!isControlRow)
|
|
784
|
+
rowTokens.push({ row, cells });
|
|
785
|
+
rowTokens.push(...closers);
|
|
786
|
+
});
|
|
787
|
+
const output = [];
|
|
788
|
+
yield this.evaluate(this.parse(rowTokens, (t) => t.directive ? { tag: t.tag, directive: t.directive } : undefined), this.root, (token, scope) => __awaiter(this, void 0, void 0, function* () {
|
|
789
|
+
const row = token.row.cloneNode(true);
|
|
790
|
+
const cells = this.childElements(row, 'a:tc');
|
|
791
|
+
for (let c = 0; c < cells.length; c++) {
|
|
792
|
+
const bodyTokens = token.cells[c];
|
|
793
|
+
const body = this.childElements(cells[c], 'a:txBody')[0];
|
|
794
|
+
if (!bodyTokens || !body)
|
|
795
|
+
continue;
|
|
796
|
+
yield this.writeBody(body, bodyTokens, this.parse(bodyTokens.tokens, textControl), scope);
|
|
797
|
+
}
|
|
798
|
+
output.push(row);
|
|
799
|
+
}));
|
|
800
|
+
const anchor = rows[rows.length - 1].nextSibling;
|
|
801
|
+
rows.forEach((row) => table.removeChild(row));
|
|
802
|
+
output.forEach((row, r) => {
|
|
803
|
+
table.insertBefore(row, anchor);
|
|
804
|
+
const rowId = row.getElementsByTagName('a16:rowId')[0];
|
|
805
|
+
if (rowId)
|
|
806
|
+
rowId.setAttribute('val', String(10000 + r));
|
|
807
|
+
});
|
|
808
|
+
this.updateFrameHeight(table, output);
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
/** Block tags that are not closed (or opened) within the same cell. */
|
|
812
|
+
extractRowLevelTags(tokens) {
|
|
813
|
+
const stack = [];
|
|
814
|
+
const closers = [];
|
|
815
|
+
tokens.forEach((token) => {
|
|
816
|
+
if (token.kind !== 'tag')
|
|
817
|
+
return;
|
|
818
|
+
const type = token.directive.type;
|
|
819
|
+
if (type === 'if' || type === 'for') {
|
|
820
|
+
stack.push(token);
|
|
821
|
+
}
|
|
822
|
+
else if (type === 'endif' || type === 'endfor') {
|
|
823
|
+
if (stack.length)
|
|
824
|
+
stack.pop();
|
|
825
|
+
else
|
|
826
|
+
closers.push(token);
|
|
827
|
+
}
|
|
828
|
+
else if ((type === 'else' || type === 'elseif') && !stack.length) {
|
|
829
|
+
throw new TemplateRenderError(`${token.tag} cannot span table rows; use it inside a cell`);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
return { openers: stack, closers };
|
|
833
|
+
}
|
|
834
|
+
updateFrameHeight(table, rows) {
|
|
835
|
+
var _a;
|
|
836
|
+
let frame = table.parentNode;
|
|
837
|
+
while (frame && frame.tagName !== 'p:graphicFrame') {
|
|
838
|
+
frame = frame.parentNode;
|
|
839
|
+
}
|
|
840
|
+
const ext = (_a = frame === null || frame === void 0 ? void 0 : frame.getElementsByTagName('p:xfrm')[0]) === null || _a === void 0 ? void 0 : _a.getElementsByTagName('a:ext')[0];
|
|
841
|
+
if (!ext)
|
|
842
|
+
return;
|
|
843
|
+
const height = rows.reduce((sum, row) => sum + Number(row.getAttribute('h') || 0), 0);
|
|
844
|
+
if (height > 0)
|
|
845
|
+
ext.setAttribute('cy', String(height));
|
|
846
|
+
}
|
|
847
|
+
/*
|
|
848
|
+
* DOM utilities
|
|
849
|
+
*/
|
|
850
|
+
childElements(parent, tagName) {
|
|
851
|
+
const children = [];
|
|
852
|
+
for (let node = parent.firstChild; node; node = node.nextSibling) {
|
|
853
|
+
if (node.nodeType !== 1)
|
|
854
|
+
continue;
|
|
855
|
+
const element = node;
|
|
856
|
+
if (!tagName || element.tagName === tagName)
|
|
857
|
+
children.push(element);
|
|
858
|
+
}
|
|
859
|
+
return children;
|
|
860
|
+
}
|
|
861
|
+
runText(run) {
|
|
862
|
+
var _a, _b;
|
|
863
|
+
return (_b = (_a = run.getElementsByTagName('a:t')[0]) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : '';
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
exports.TemplateRenderer = TemplateRenderer;
|
|
867
|
+
//# sourceMappingURL=renderer.js.map
|