layoutbridge-light 0.0.0-stage → 1.0.1
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/CHANGELOG.md +105 -0
- package/LICENSE +26 -0
- package/README.de.md +367 -0
- package/README.md +353 -2
- package/THIRD-PARTY-NOTICES.md +2878 -0
- package/dist/src/mcp/catalog.js +131 -0
- package/dist/src/mcp/config.js +211 -0
- package/dist/src/mcp/content-credentials.js +65 -0
- package/dist/src/mcp/dispatch.js +171 -0
- package/dist/src/mcp/epub-check.js +331 -0
- package/dist/src/mcp/epub-orientation.js +132 -0
- package/dist/src/mcp/license/free-tools.js +16 -0
- package/dist/src/mcp/license/gate.js +210 -0
- package/dist/src/mcp/license/index.js +107 -0
- package/dist/src/mcp/license/instance-name.js +57 -0
- package/dist/src/mcp/license/key-source.js +59 -0
- package/dist/src/mcp/license/lemonsqueezy-verifier.js +207 -0
- package/dist/src/mcp/license/placeholder-verifier.js +10 -0
- package/dist/src/mcp/license/polar-ids.js +100 -0
- package/dist/src/mcp/license/polar-verifier.js +285 -0
- package/dist/src/mcp/license/product-ids.js +73 -0
- package/dist/src/mcp/license/state-store.js +91 -0
- package/dist/src/mcp/license/types.js +11 -0
- package/dist/src/mcp/logging.js +271 -0
- package/dist/src/mcp/pdf-postprocess.js +798 -0
- package/dist/src/mcp/product-info.js +54 -0
- package/dist/src/mcp/product.js +56 -0
- package/dist/src/mcp/server.js +253 -0
- package/dist/src/mcp/setup.js +680 -0
- package/dist/src/mcp/start.cjs +117 -0
- package/dist/src/mcp/target-folder.js +25 -0
- package/dist/src/mcp/tool-definition.js +2 -0
- package/dist/src/mcp/tools.js +2680 -0
- package/dist/src/mcp/typography-rules.js +70 -0
- package/dist/src/transport/com.js +436 -0
- package/dist/src/transport/osascript.js +171 -0
- package/dist/src/transport/queue.js +19 -0
- package/dist/src/transport/script-bundle.js +92 -0
- package/dist/src/transport/transport.js +52 -0
- package/indesign/commands/10-app.js +6 -0
- package/indesign/commands/20-documents.js +517 -0
- package/indesign/commands/22-document-setup.js +204 -0
- package/indesign/commands/23-save-package.js +258 -0
- package/indesign/commands/25-export.js +695 -0
- package/indesign/commands/26-export-image.js +202 -0
- package/indesign/commands/27-textdefaults.js +151 -0
- package/indesign/commands/28-selection.js +174 -0
- package/indesign/commands/30-pages.js +1032 -0
- package/indesign/commands/31-view.js +310 -0
- package/indesign/commands/32-sections.js +203 -0
- package/indesign/commands/35-parents.js +448 -0
- package/indesign/commands/37-layout.js +837 -0
- package/indesign/commands/40-frames.js +886 -0
- package/indesign/commands/41-shapes.js +505 -0
- package/indesign/commands/42-groups.js +117 -0
- package/indesign/commands/43-layers.js +310 -0
- package/indesign/commands/44-effects.js +274 -0
- package/indesign/commands/45-frame-options.js +247 -0
- package/indesign/commands/47-anchored.js +473 -0
- package/indesign/commands/50-modify.js +803 -0
- package/indesign/commands/52-align.js +139 -0
- package/indesign/commands/55-fit.js +106 -0
- package/indesign/commands/60-images.js +1154 -0
- package/indesign/commands/62-embed.js +120 -0
- package/indesign/commands/69-placeholders.js +132 -0
- package/indesign/commands/70-text.js +710 -0
- package/indesign/commands/71-outlines.js +92 -0
- package/indesign/commands/72-place-text.js +476 -0
- package/indesign/commands/73-story-lock.js +157 -0
- package/indesign/commands/74-stories.js +121 -0
- package/indesign/commands/75-tables.js +702 -0
- package/indesign/commands/76-text-variables.js +260 -0
- package/indesign/commands/77-story-options.js +86 -0
- package/indesign/commands/78-frame-lines.js +511 -0
- package/indesign/commands/79-check-typography.js +480 -0
- package/indesign/commands/80-styles.js +1960 -0
- package/indesign/commands/81-composition.js +355 -0
- package/indesign/commands/82-object-styles.js +263 -0
- package/indesign/commands/83-drop-caps.js +87 -0
- package/indesign/commands/83-indents-tabs.js +264 -0
- package/indesign/commands/84-import-styles.js +142 -0
- package/indesign/commands/85-swatches.js +112 -0
- package/indesign/commands/86-gradients.js +185 -0
- package/indesign/commands/87-replace-swatch.js +180 -0
- package/indesign/commands/88-replace-font.js +212 -0
- package/indesign/commands/89-toc-index.js +1002 -0
- package/indesign/commands/90-print-on-demand.js +495 -0
- package/indesign/commands/91-epub.js +629 -0
- package/indesign/commands/92-accessibility.js +1131 -0
- package/indesign/interpreter.js +829 -0
- package/package.json +41 -4
- package/rules/typography-rules.json +32 -0
|
@@ -0,0 +1,1002 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Sitzung 42 (A-41, E-97) – Inhaltsverzeichnis und Register:
|
|
3
|
+
* list_toc_styles, define_toc_style, create_toc; add_index_entry,
|
|
4
|
+
* list_index, generate_index.
|
|
5
|
+
*
|
|
6
|
+
* Adobe-DOM (gelesen 09.10.2026): TOCStyle (title, titleStyle,
|
|
7
|
+
* includeHidden, runIn, includeBookDocuments, createBookmarks,
|
|
8
|
+
* tocStyleEntries), TOCStyleEntry (name, level, formatStyle,
|
|
9
|
+
* pageNumberPosition, separator, pageNumberStyle, separatorStyle,
|
|
10
|
+
* sortAlphabet), Document.createTOC(using, replacing, fromBook, placePoint,
|
|
11
|
+
* includeOverset, destinationLayer); Index (topics, allTopics,
|
|
12
|
+
* generate(on, placePoint, destinationLayer, autoflowing, includeOverset)),
|
|
13
|
+
* Topic (name, sortOrder, topics, pageReferences, crossReferences),
|
|
14
|
+
* PageReference (sourceText, pageReferenceType, pageReferenceLimit,
|
|
15
|
+
* pageNumberStyleOverride), CrossReference (referencedTopic,
|
|
16
|
+
* crossReferenceType, customTypeString), Document.indexGenerationOptions
|
|
17
|
+
* (IndexOptions), Story.storyType (StoryTypes).
|
|
18
|
+
*
|
|
19
|
+
* Live-Probe 09.10.2026 (InDesign 21.6): createTOC und generate liefern ein
|
|
20
|
+
* Array mit einer Story; createTOC setzt auf die aktive Seite; replace
|
|
21
|
+
* behaelt die Story; ein Seitenverweis setzt ein Markierungszeichen in den
|
|
22
|
+
* Text.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var IDC_PAGE_NUMBER_POSITIONS = [
|
|
26
|
+
['after', 'AFTER_ENTRY'],
|
|
27
|
+
['before', 'BEFORE_ENTRY'],
|
|
28
|
+
['none', 'NONE']
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
var IDC_PAGE_REFERENCE_TYPES = [
|
|
32
|
+
['current_page', 'CURRENT_PAGE'],
|
|
33
|
+
['to_next_style_change', 'TO_NEXT_STYLE_CHANGE'],
|
|
34
|
+
['to_next_use_of_style', 'TO_NEXT_USE_OF_STYLE'],
|
|
35
|
+
['to_end_of_story', 'TO_END_OF_STORY'],
|
|
36
|
+
['to_end_of_document', 'TO_END_OF_DOCUMENT'],
|
|
37
|
+
['to_end_of_section', 'TO_END_OF_SECTION'],
|
|
38
|
+
['for_next_paragraphs', 'FOR_NEXT_N_PARAGRAPHS'],
|
|
39
|
+
['for_next_pages', 'FOR_NEXT_N_PAGES'],
|
|
40
|
+
['suppress_page_numbers', 'SUPPRESS_PAGE_NUMBERS']
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
var IDC_CROSS_REFERENCE_TYPES = [
|
|
44
|
+
['see', 'SEE'],
|
|
45
|
+
['see_also', 'SEE_ALSO'],
|
|
46
|
+
['see_or_see_also', 'SEE_OR_ALSO_BRACKET'],
|
|
47
|
+
['see_herein', 'SEE_HEREIN'],
|
|
48
|
+
['see_also_herein', 'SEE_ALSO_HEREIN'],
|
|
49
|
+
['custom', 'CUSTOM_CROSS_REFERENCE'],
|
|
50
|
+
['custom_before', 'CUSTOM_CROSS_REFERENCE_BEFORE'],
|
|
51
|
+
['custom_after', 'CUSTOM_CROSS_REFERENCE_AFTER']
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
/* Name zu einem Enumerationswert (Tabelle [name, KONSTANTE]), sonst der Rohwert als Text. */
|
|
55
|
+
function idcTocEnumName(table, enumObject, value) {
|
|
56
|
+
var i;
|
|
57
|
+
if (value === undefined || value === null) { return null; }
|
|
58
|
+
if (enumObject === undefined || enumObject === null) { return String(value); }
|
|
59
|
+
for (i = 0; i < table.length; i++) {
|
|
60
|
+
try { if (IDC.enumIs(value, enumObject[table[i][1]])) { return table[i][0]; } } catch (e) { /* naechster */ }
|
|
61
|
+
}
|
|
62
|
+
return String(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* Enumerationswert zu einem Namen, sonst invalid_argument. */
|
|
66
|
+
function idcTocEnumFor(table, enumObject, enumName, name, field) {
|
|
67
|
+
var i, value = null;
|
|
68
|
+
for (i = 0; i < table.length; i++) {
|
|
69
|
+
if (table[i][0] === name) {
|
|
70
|
+
if (enumObject === undefined || enumObject === null) { IDC.fail('invalid_state', 'This runtime has no ' + enumName + ' enumeration.', null); }
|
|
71
|
+
try { value = enumObject[table[i][1]]; } catch (e) { value = null; }
|
|
72
|
+
if (value === null || value === undefined) { IDC.fail('invalid_state', 'This InDesign has no ' + enumName + ' value ' + name + '.', null); }
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
IDC.fail('invalid_argument', field + ' must be one of ' + idcTableNames(table).join(', ') + '.', { value: name });
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function idcPageNumberPositionsEnum() { return typeof PageNumberPosition === 'undefined' ? null : PageNumberPosition; }
|
|
81
|
+
function idcPageReferenceTypesEnum() { return typeof PageReferenceType === 'undefined' ? null : PageReferenceType; }
|
|
82
|
+
function idcCrossReferenceTypesEnum() { return typeof CrossReferenceType === 'undefined' ? null : CrossReferenceType; }
|
|
83
|
+
|
|
84
|
+
/* Formatname aus einer Formateigenschaft (Format oder Text), sonst null. */
|
|
85
|
+
function idcStyleNameOf(value) {
|
|
86
|
+
if (value === undefined || value === null) { return null; }
|
|
87
|
+
if (typeof value === 'string') { return value === '' ? null : value; }
|
|
88
|
+
try { if (value.isValid === false) { return null; } } catch (e0) { /* weiter */ }
|
|
89
|
+
try { return String(value.name); } catch (e1) { return null; }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* Optionales Zeichenformat ueber den Namen; '' oder null = nicht gesetzt. */
|
|
93
|
+
function idcOptionalCharacterStyle(doc, name, field) {
|
|
94
|
+
var style;
|
|
95
|
+
if (name === undefined || name === null) { return null; }
|
|
96
|
+
if (typeof name !== 'string' || name === '') { IDC.fail('invalid_argument', field + ' must be the name of a character style.', { field: field }); }
|
|
97
|
+
style = idcCharacterStyleByName(doc, name);
|
|
98
|
+
if (style === null) { IDC.fail('not_found', 'Character style "' + name + '" not found (' + field + ').', { style_name: name, field: field }); }
|
|
99
|
+
return style;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function idcOptionalParagraphStyle(doc, name, field) {
|
|
103
|
+
if (name === undefined || name === null) { return null; }
|
|
104
|
+
return idcParagraphStyleArg(doc, name, field);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function idcBoolArg(args, field) {
|
|
108
|
+
var value = args[field];
|
|
109
|
+
if (value === undefined || value === null) { return null; }
|
|
110
|
+
if (typeof value !== 'boolean') { IDC.fail('invalid_argument', field + ' must be true or false.', { field: field }); }
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* Erstes Element, wenn InDesign ein Array liefert (Live-Probe: createTOC, generate). */
|
|
115
|
+
function idcFirstOf(result) {
|
|
116
|
+
if (result === undefined || result === null) { return null; }
|
|
117
|
+
if (IDC.isArray(result)) { return result.length > 0 ? result[0] : null; }
|
|
118
|
+
try {
|
|
119
|
+
if (result.id === undefined && result.length !== undefined && result.length > 0 && result[0] !== undefined) { return result[0]; }
|
|
120
|
+
} catch (e) { /* kein Array */ }
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* Stories eines Typs (TOC_STORY, INDEXING_STORY) als Array. */
|
|
125
|
+
function idcStoriesOfType(doc, typeName) {
|
|
126
|
+
var out = [], i, story, wanted = null;
|
|
127
|
+
if (typeof StoryTypes === 'undefined') { return out; }
|
|
128
|
+
try { wanted = StoryTypes[typeName]; } catch (e0) { wanted = null; }
|
|
129
|
+
if (wanted === null || wanted === undefined) { return out; }
|
|
130
|
+
try {
|
|
131
|
+
for (i = 0; i < doc.stories.length; i++) {
|
|
132
|
+
story = doc.stories.item(i);
|
|
133
|
+
try { if (IDC.enumIs(story.storyType, wanted)) { out.push(story); } } catch (e1) { /* ohne Typ */ }
|
|
134
|
+
}
|
|
135
|
+
} catch (e2) { /* keine Stories */ }
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function idcStoryIsGenerated(story) {
|
|
140
|
+
var type;
|
|
141
|
+
if (typeof StoryTypes === 'undefined') { return false; }
|
|
142
|
+
try { type = story.storyType; } catch (e) { return false; }
|
|
143
|
+
try { return IDC.enumIs(type, StoryTypes.TOC_STORY) || IDC.enumIs(type, StoryTypes.INDEXING_STORY); } catch (e2) { return false; }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/* Rahmen einer Story als Array konkreter Objekte. */
|
|
147
|
+
function idcStoryAmong(story, stories) {
|
|
148
|
+
var i;
|
|
149
|
+
for (i = 0; i < stories.length; i++) {
|
|
150
|
+
try { if (Number(stories[i].id) === Number(story.id)) { return true; } } catch (e) { /* weiter */ }
|
|
151
|
+
}
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function idcGeneratedFrames(story) {
|
|
156
|
+
var out = [], containers = null, i;
|
|
157
|
+
try { containers = story.textContainers; } catch (e) { containers = null; }
|
|
158
|
+
if (containers === null || containers === undefined) { return out; }
|
|
159
|
+
try {
|
|
160
|
+
for (i = 0; i < containers.length; i++) { out.push(IDC.concrete(containers[i])); }
|
|
161
|
+
} catch (e2) { /* leer */ }
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function idcStoryOverflows(story, frames) {
|
|
166
|
+
if (frames.length > 0) { return idcOverflows(frames[frames.length - 1]); }
|
|
167
|
+
try { return story.overflows === true; } catch (e) { return null; }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* Absatztext ohne Absatzende. */
|
|
171
|
+
function idcParagraphText(paragraph) {
|
|
172
|
+
var text = '';
|
|
173
|
+
try { text = String(paragraph.contents); } catch (e) { return ''; }
|
|
174
|
+
if (text.length > 0 && (text.charAt(text.length - 1) === '\r' || text.charAt(text.length - 1) === '\n')) { text = text.substring(0, text.length - 1); }
|
|
175
|
+
return text;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* Erzeugte Story (Verzeichnis oder Register) beschreiben. */
|
|
179
|
+
function idcDescribeGeneratedStory(doc, story) {
|
|
180
|
+
var out = { story_id: null, frame_ids: [], page_ids: [], page_names: [], paragraph_count: null, length: null, overset: null };
|
|
181
|
+
var frames = idcGeneratedFrames(story), i, landing;
|
|
182
|
+
try { out.story_id = Number(story.id); } catch (e0) { /* null */ }
|
|
183
|
+
for (i = 0; i < frames.length; i++) {
|
|
184
|
+
out.frame_ids.push(Number(frames[i].id));
|
|
185
|
+
landing = IDC.landing(frames[i]);
|
|
186
|
+
out.page_ids.push(landing.page_id);
|
|
187
|
+
out.page_names.push(landing.page_name);
|
|
188
|
+
}
|
|
189
|
+
try { out.paragraph_count = Number(story.paragraphs.length); } catch (e1) { /* null */ }
|
|
190
|
+
try { out.length = Number(story.characters.length); } catch (e2) { /* null */ }
|
|
191
|
+
out.overset = idcStoryOverflows(story, frames);
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/* Absaetze einer erzeugten Story: [{text, paragraph_style}], hoechstens max. */
|
|
196
|
+
function idcStoryLines(story, max) {
|
|
197
|
+
var out = { lines: [], total: 0 }, i, p, count = 0;
|
|
198
|
+
try { count = Number(story.paragraphs.length); } catch (e) { count = 0; }
|
|
199
|
+
out.total = count;
|
|
200
|
+
for (i = 0; i < count && i < max; i++) {
|
|
201
|
+
p = story.paragraphs.item(i);
|
|
202
|
+
out.lines.push({ text: idcParagraphText(p), paragraph_style: idcParagraphStyleName(p) });
|
|
203
|
+
}
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* ------------------------------------------------------------ TOC-Formate */
|
|
208
|
+
|
|
209
|
+
function idcTocStyleByName(doc, name) {
|
|
210
|
+
var i, style;
|
|
211
|
+
try {
|
|
212
|
+
for (i = 0; i < doc.tocStyles.length; i++) {
|
|
213
|
+
style = doc.tocStyles.item(i);
|
|
214
|
+
if (String(style.name) === String(name)) { return style; }
|
|
215
|
+
}
|
|
216
|
+
} catch (e) { /* keins */ }
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function idcDescribeTocEntry(entry) {
|
|
221
|
+
var out = { paragraph_style: null, level: null, entry_style: null, page_number_position: null, separator: null, page_number_style: null, separator_style: null, sort_alphabetical: null };
|
|
222
|
+
try { out.paragraph_style = String(entry.name); } catch (e0) { /* null */ }
|
|
223
|
+
try { out.level = Number(entry.level); } catch (e1) { /* null */ }
|
|
224
|
+
try { out.entry_style = idcStyleNameOf(entry.formatStyle); } catch (e2) { /* null */ }
|
|
225
|
+
try { out.page_number_position = idcTocEnumName(IDC_PAGE_NUMBER_POSITIONS, idcPageNumberPositionsEnum(), entry.pageNumberPosition); } catch (e3) { /* null */ }
|
|
226
|
+
try { out.separator = String(entry.separator); } catch (e4) { /* null */ }
|
|
227
|
+
try { out.page_number_style = idcStyleNameOf(entry.pageNumberStyle); } catch (e5) { /* null */ }
|
|
228
|
+
try { out.separator_style = idcStyleNameOf(entry.separatorStyle); } catch (e6) { /* null */ }
|
|
229
|
+
try { out.sort_alphabetical = entry.sortAlphabet === true; } catch (e7) { /* null */ }
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function idcDescribeTocStyle(style) {
|
|
234
|
+
var out = { name: null, title: null, title_style: null, include_hidden_layers: null, run_in: null, include_book_documents: null, create_bookmarks: null, entries: [] };
|
|
235
|
+
var i, entries = null;
|
|
236
|
+
try { out.name = String(style.name); } catch (e0) { /* null */ }
|
|
237
|
+
try { out.title = String(style.title); } catch (e1) { /* null */ }
|
|
238
|
+
try { out.title_style = idcStyleNameOf(style.titleStyle); } catch (e2) { /* null */ }
|
|
239
|
+
try { out.include_hidden_layers = style.includeHidden === true; } catch (e3) { /* null */ }
|
|
240
|
+
try { out.run_in = style.runIn === true; } catch (e4) { /* null */ }
|
|
241
|
+
try { out.include_book_documents = style.includeBookDocuments === true; } catch (e5) { /* null */ }
|
|
242
|
+
try { out.create_bookmarks = style.createBookmarks === true; } catch (e6) { /* null */ }
|
|
243
|
+
try { entries = style.tocStyleEntries; } catch (e7) { entries = null; }
|
|
244
|
+
if (entries !== null && entries !== undefined) {
|
|
245
|
+
try {
|
|
246
|
+
for (i = 0; i < entries.length; i++) { out.entries.push(idcDescribeTocEntry(entries.item(i))); }
|
|
247
|
+
} catch (e8) { /* leer */ }
|
|
248
|
+
}
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/* entries[] pruefen und Eigenschaften je Eintrag vorbereiten (vor jeder Aenderung). */
|
|
253
|
+
function idcTocEntriesArg(doc, entries) {
|
|
254
|
+
var out = [], i, spec, props, level;
|
|
255
|
+
if (!IDC.isArray(entries) || entries.length === 0) { IDC.fail('invalid_argument', 'entries must be a non-empty array.', null); }
|
|
256
|
+
for (i = 0; i < entries.length; i++) {
|
|
257
|
+
spec = entries[i];
|
|
258
|
+
if (!spec || typeof spec !== 'object') { IDC.fail('invalid_argument', 'entries[' + i + '] must be an object.', null); }
|
|
259
|
+
idcParagraphStyleArg(doc, spec.paragraph_style, 'entries[' + i + '].paragraph_style');
|
|
260
|
+
props = {};
|
|
261
|
+
level = spec.level === undefined || spec.level === null ? 1 : spec.level;
|
|
262
|
+
if (typeof level !== 'number' || Math.floor(level) !== level || level < 1 || level > 9) {
|
|
263
|
+
IDC.fail('invalid_argument', 'entries[' + i + '].level must be a whole number from 1 to 9.', { level: spec.level });
|
|
264
|
+
}
|
|
265
|
+
props.level = level;
|
|
266
|
+
if (spec.entry_style !== undefined && spec.entry_style !== null) { props.formatStyle = idcParagraphStyleArg(doc, spec.entry_style, 'entries[' + i + '].entry_style'); }
|
|
267
|
+
if (spec.page_number_position !== undefined && spec.page_number_position !== null) {
|
|
268
|
+
props.pageNumberPosition = idcTocEnumFor(IDC_PAGE_NUMBER_POSITIONS, idcPageNumberPositionsEnum(), 'PageNumberPosition', spec.page_number_position, 'entries[' + i + '].page_number_position');
|
|
269
|
+
}
|
|
270
|
+
if (spec.separator !== undefined && spec.separator !== null) {
|
|
271
|
+
if (typeof spec.separator !== 'string') { IDC.fail('invalid_argument', 'entries[' + i + '].separator must be a string.', null); }
|
|
272
|
+
props.separator = spec.separator;
|
|
273
|
+
}
|
|
274
|
+
if (spec.page_number_style !== undefined && spec.page_number_style !== null) { props.pageNumberStyle = idcOptionalCharacterStyle(doc, spec.page_number_style, 'entries[' + i + '].page_number_style'); }
|
|
275
|
+
if (spec.separator_style !== undefined && spec.separator_style !== null) { props.separatorStyle = idcOptionalCharacterStyle(doc, spec.separator_style, 'entries[' + i + '].separator_style'); }
|
|
276
|
+
if (spec.sort_alphabetical !== undefined && spec.sort_alphabetical !== null) {
|
|
277
|
+
if (typeof spec.sort_alphabetical !== 'boolean') { IDC.fail('invalid_argument', 'entries[' + i + '].sort_alphabetical must be true or false.', null); }
|
|
278
|
+
props.sortAlphabet = spec.sort_alphabetical;
|
|
279
|
+
}
|
|
280
|
+
out.push({ style_name: String(spec.paragraph_style), props: props });
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
IDC.register('list_toc_styles', { needs_document: true, read_only: true, ruler: 'none' },
|
|
286
|
+
function (ctx) {
|
|
287
|
+
var doc = ctx.doc;
|
|
288
|
+
var styles = [], stories = [], i, list;
|
|
289
|
+
try {
|
|
290
|
+
for (i = 0; i < doc.tocStyles.length; i++) { styles.push(idcDescribeTocStyle(doc.tocStyles.item(i))); }
|
|
291
|
+
} catch (e) { /* leer */ }
|
|
292
|
+
list = idcStoriesOfType(doc, 'TOC_STORY');
|
|
293
|
+
for (i = 0; i < list.length; i++) { stories.push(idcDescribeGeneratedStory(doc, list[i])); }
|
|
294
|
+
return { toc_styles: styles, count: styles.length, toc_stories: stories };
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
IDC.register('define_toc_style', { needs_document: true, read_only: false, ruler: 'none' },
|
|
298
|
+
function (ctx) {
|
|
299
|
+
var args = ctx.args;
|
|
300
|
+
var doc = ctx.doc;
|
|
301
|
+
var name = args.name;
|
|
302
|
+
var style, created = false, entries = null, titleStyle = null, props = {}, i, description, flag;
|
|
303
|
+
var flags = [['include_hidden_layers', 'includeHidden'], ['run_in', 'runIn'], ['create_bookmarks', 'createBookmarks']];
|
|
304
|
+
|
|
305
|
+
if (typeof name !== 'string' || name === '') { IDC.fail('invalid_argument', 'name is required.', null); }
|
|
306
|
+
style = idcTocStyleByName(doc, name);
|
|
307
|
+
if (args.remove === true) {
|
|
308
|
+
if (style === null) { IDC.fail('not_found', 'No TOC style named "' + name + '"; see list_toc_styles.', { name: name }); }
|
|
309
|
+
description = idcDescribeTocStyle(style);
|
|
310
|
+
try { style.remove(); } catch (e0) {
|
|
311
|
+
IDC.fail('script_error', 'InDesign could not remove the TOC style "' + name + '": ' + String(e0.message ? e0.message : e0), { name: name });
|
|
312
|
+
}
|
|
313
|
+
ctx.changes = { modified_document_ids: [Number(doc.id)] };
|
|
314
|
+
return { removed: true, created: false, updated: false, toc_style: description };
|
|
315
|
+
}
|
|
316
|
+
/* Erst alles pruefen, dann schreiben. */
|
|
317
|
+
if (args.entries !== undefined && args.entries !== null) { entries = idcTocEntriesArg(doc, args.entries); }
|
|
318
|
+
if (args.title !== undefined && args.title !== null) {
|
|
319
|
+
if (typeof args.title !== 'string') { IDC.fail('invalid_argument', 'title must be a string ("" for no title).', null); }
|
|
320
|
+
props.title = args.title;
|
|
321
|
+
}
|
|
322
|
+
if (args.title_style !== undefined && args.title_style !== null) { titleStyle = idcParagraphStyleArg(doc, args.title_style, 'title_style'); props.titleStyle = titleStyle; }
|
|
323
|
+
for (i = 0; i < flags.length; i++) {
|
|
324
|
+
flag = idcBoolArg(args, flags[i][0]);
|
|
325
|
+
if (flag !== null) { props[flags[i][1]] = flag; }
|
|
326
|
+
}
|
|
327
|
+
if (style === null) {
|
|
328
|
+
if (entries === null) { IDC.fail('invalid_argument', 'A new TOC style needs entries (at least one paragraph style).', { name: name }); }
|
|
329
|
+
try {
|
|
330
|
+
style = doc.tocStyles.add({ name: name });
|
|
331
|
+
} catch (e1) {
|
|
332
|
+
IDC.fail('script_error', 'InDesign could not create the TOC style "' + name + '": ' + String(e1.message ? e1.message : e1), { name: name });
|
|
333
|
+
}
|
|
334
|
+
created = true;
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
for (var key in props) {
|
|
338
|
+
if (IDC.hasOwn(props, key)) { style[key] = props[key]; }
|
|
339
|
+
}
|
|
340
|
+
if (entries !== null) { idcReplaceTocEntries(style, entries); }
|
|
341
|
+
} catch (e2) {
|
|
342
|
+
if (created) { try { style.remove(); } catch (e3) { /* bleibt */ } }
|
|
343
|
+
IDC.fail('script_error', 'InDesign could not set the TOC style "' + name + '": ' + String(e2.message ? e2.message : e2), { name: name });
|
|
344
|
+
}
|
|
345
|
+
description = idcDescribeTocStyle(style);
|
|
346
|
+
ctx.changes = { modified_document_ids: [Number(doc.id)] };
|
|
347
|
+
return { removed: false, created: created, updated: !created, toc_style: description };
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
/* ------------------------------------------------- Ziel fuer create/generate */
|
|
351
|
+
|
|
352
|
+
/* Zielangaben pruefen: { page, rect (oder null), point [x, y] in Seitenkoordinaten }. */
|
|
353
|
+
function idcPlacementArg(doc, args, needed) {
|
|
354
|
+
var out = { page: null, rect: null, point: null };
|
|
355
|
+
var found, margins = null;
|
|
356
|
+
if (args.page_id === undefined || args.page_id === null) {
|
|
357
|
+
if (needed) { IDC.fail('invalid_argument', 'page_id is required (the page that gets the new frame).', null); }
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
found = idcAnyPageById(doc, args.page_id);
|
|
361
|
+
if (found.is_parent_page) { IDC.fail('invalid_argument', 'page_id must be a document page, not a parent page.', { page_id: args.page_id }); }
|
|
362
|
+
out.page = found.page;
|
|
363
|
+
if (args.bounds_mm !== undefined && args.bounds_mm !== null) {
|
|
364
|
+
out.rect = idcBoundsArg(args.bounds_mm, 'bounds_mm');
|
|
365
|
+
out.point = [out.rect.x, out.rect.y];
|
|
366
|
+
} else if (args.position_mm !== undefined && args.position_mm !== null) {
|
|
367
|
+
var p = idcPointArg(args.position_mm, 'position_mm');
|
|
368
|
+
out.point = [p.x, p.y];
|
|
369
|
+
} else {
|
|
370
|
+
try { margins = out.page.marginPreferences; out.point = [Number(margins.left), Number(margins.top)]; } catch (e) { out.point = [0, 0]; }
|
|
371
|
+
}
|
|
372
|
+
return out;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/* Ebene ueber layer_name, sonst null. */
|
|
376
|
+
function idcOptionalLayer(doc, args) {
|
|
377
|
+
if (args.layer_name === undefined || args.layer_name === null) { return null; }
|
|
378
|
+
return idcLayerArg(doc, { layer_name: args.layer_name }).layer;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/* Rahmen der neuen Story an Seite und Begrenzung bringen; Warnungen bei Abweichung. */
|
|
382
|
+
function idcSettleGeneratedFrame(ctx, doc, story, placement) {
|
|
383
|
+
var frames = idcGeneratedFrames(story), frame, landing;
|
|
384
|
+
if (frames.length === 0) { ctx.warn('InDesign created the story without a frame.'); return; }
|
|
385
|
+
frame = frames[0];
|
|
386
|
+
landing = IDC.landing(frame);
|
|
387
|
+
if (landing.page_id !== Number(placement.page.id)) {
|
|
388
|
+
try { frame.move(placement.page); } catch (e0) { /* Landepruefung meldet es */ }
|
|
389
|
+
}
|
|
390
|
+
if (placement.rect !== null) {
|
|
391
|
+
try {
|
|
392
|
+
idcWriteSpreadBounds(doc, frame, idcToSpreadBounds(placement.page, idcRectToBounds(placement.rect), 'page'));
|
|
393
|
+
} catch (e1) {
|
|
394
|
+
ctx.warn('Could not set bounds_mm on the new frame: ' + String(e1.message ? e1.message : e1));
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
idcCheckLanding(ctx, frame, placement.page, 'frame');
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/* Seite des ersten Rahmens einer erzeugten Story (fuer replace), sonst undefined. */
|
|
401
|
+
function idcGeneratedStoryPage(story) {
|
|
402
|
+
var frames = idcGeneratedFrames(story), page = null;
|
|
403
|
+
if (frames.length === 0) { return undefined; }
|
|
404
|
+
try { page = frames[0].parentPage; } catch (e) { page = null; }
|
|
405
|
+
return page === null ? undefined : page;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/*
|
|
409
|
+
* Aenderungsangaben fuer create_toc/generate_index (A-43 Nebenbefund): Rahmen,
|
|
410
|
+
* die es vorher nicht gab, sind created_frame_ids, vorhandene (replace)
|
|
411
|
+
* modified_frame_ids. Bis 1.0.1 standen auch neue Rahmen unter modified.
|
|
412
|
+
*/
|
|
413
|
+
function idcGeneratedChanges(out, framesBefore, pagesBefore, doc) {
|
|
414
|
+
var created = [], modified = [], i, j, known, changes = {};
|
|
415
|
+
for (i = 0; i < out.frame_ids.length; i++) {
|
|
416
|
+
known = false;
|
|
417
|
+
for (j = 0; j < framesBefore.length; j++) { if (framesBefore[j] === out.frame_ids[i]) { known = true; break; } }
|
|
418
|
+
if (known) { modified.push(out.frame_ids[i]); } else { created.push(out.frame_ids[i]); }
|
|
419
|
+
}
|
|
420
|
+
if (created.length > 0) { changes.created_frame_ids = created; }
|
|
421
|
+
if (modified.length > 0) { changes.modified_frame_ids = modified; }
|
|
422
|
+
if (pagesBefore !== null) {
|
|
423
|
+
var pageIds = [], k, page;
|
|
424
|
+
try {
|
|
425
|
+
for (k = 0; k < doc.pages.length; k++) {
|
|
426
|
+
page = doc.pages.item(k);
|
|
427
|
+
known = false;
|
|
428
|
+
for (j = 0; j < pagesBefore.length; j++) { if (pagesBefore[j] === Number(page.id)) { known = true; break; } }
|
|
429
|
+
if (!known) { pageIds.push(Number(page.id)); }
|
|
430
|
+
}
|
|
431
|
+
} catch (e0) { pageIds = []; }
|
|
432
|
+
if (pageIds.length > 0) { changes.created_page_ids = pageIds; }
|
|
433
|
+
}
|
|
434
|
+
return changes;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/* Rahmen-IDs aller Storys einer Liste (vor dem Erzeugen). */
|
|
438
|
+
function idcFrameIdsOfStories(stories) {
|
|
439
|
+
var out = [], i, frames, j;
|
|
440
|
+
for (i = 0; i < stories.length; i++) {
|
|
441
|
+
frames = idcGeneratedFrames(stories[i]);
|
|
442
|
+
for (j = 0; j < frames.length; j++) { try { out.push(Number(frames[j].id)); } catch (e) { /* ohne */ } }
|
|
443
|
+
}
|
|
444
|
+
return out;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/* Seiten-IDs des Dokuments. */
|
|
448
|
+
function idcDocumentPageIds(doc) {
|
|
449
|
+
var out = [], i;
|
|
450
|
+
try { for (i = 0; i < doc.pages.length; i++) { out.push(Number(doc.pages.item(i).id)); } } catch (e) { /* ohne */ }
|
|
451
|
+
return out;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/* Gemeinsame Rueckgabe von create_toc und generate_index. */
|
|
455
|
+
function idcGeneratedResult(doc, story, replaced, maxLines) {
|
|
456
|
+
var info = idcDescribeGeneratedStory(doc, story);
|
|
457
|
+
var lines = idcStoryLines(story, maxLines);
|
|
458
|
+
var frames = idcGeneratedFrames(story);
|
|
459
|
+
info.replaced = replaced;
|
|
460
|
+
info.frame_id = info.frame_ids.length > 0 ? info.frame_ids[0] : null;
|
|
461
|
+
info.page_id = info.page_ids.length > 0 ? info.page_ids[0] : null;
|
|
462
|
+
info.page_name = info.page_names.length > 0 ? info.page_names[0] : '';
|
|
463
|
+
info.bounds_mm = frames.length > 0 ? idcItemBounds(doc, frames[0]) : null;
|
|
464
|
+
info.lines = lines.lines;
|
|
465
|
+
info.lines_truncated = lines.total > lines.lines.length;
|
|
466
|
+
return info;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
IDC.register('create_toc', { needs_document: true, read_only: false, ruler: 'page' },
|
|
470
|
+
function (ctx) {
|
|
471
|
+
var args = ctx.args;
|
|
472
|
+
var doc = ctx.doc;
|
|
473
|
+
var style, existing, replace, placement, layer, view, win, result, story, out, includeOverset, title = '', entries = 0, i, lines;
|
|
474
|
+
|
|
475
|
+
if (typeof args.toc_style !== 'string' || args.toc_style === '') { IDC.fail('invalid_argument', 'toc_style is required (name from list_toc_styles).', null); }
|
|
476
|
+
style = idcTocStyleByName(doc, args.toc_style);
|
|
477
|
+
if (style === null) { IDC.fail('not_found', 'No TOC style named "' + args.toc_style + '"; see list_toc_styles or define_toc_style.', { toc_style: args.toc_style }); }
|
|
478
|
+
replace = idcBoolArg(args, 'replace') === true;
|
|
479
|
+
includeOverset = idcBoolArg(args, 'include_overset') === true;
|
|
480
|
+
existing = idcStoriesOfType(doc, 'TOC_STORY');
|
|
481
|
+
if (replace && existing.length === 0) {
|
|
482
|
+
ctx.warn('No table of contents to replace yet – a new one is created.');
|
|
483
|
+
replace = false;
|
|
484
|
+
}
|
|
485
|
+
placement = idcPlacementArg(doc, args, !replace);
|
|
486
|
+
if (replace && placement.page !== null) { ctx.warn('replace keeps the existing frame where it is; page_id, position_mm and bounds_mm are ignored.'); }
|
|
487
|
+
if (!replace && existing.length > 0) { ctx.warn('The document already has ' + existing.length + ' table(s) of contents; pass replace true to update instead of adding another.'); }
|
|
488
|
+
layer = replace ? null : idcOptionalLayer(doc, args);
|
|
489
|
+
var framesBefore = idcFrameIdsOfStories(existing);
|
|
490
|
+
|
|
491
|
+
/* createTOC setzt auf die aktive Seite (Live-Probe 09.10.2026). */
|
|
492
|
+
view = idcCaptureView(doc);
|
|
493
|
+
win = view.window;
|
|
494
|
+
try {
|
|
495
|
+
if (!replace && win !== null) {
|
|
496
|
+
try { win.activePage = placement.page; } catch (e0) { /* Rahmen wird danach verschoben */ }
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
if (replace) {
|
|
500
|
+
result = doc.createTOC(style, true, undefined, undefined, includeOverset);
|
|
501
|
+
} else if (layer !== null) {
|
|
502
|
+
result = doc.createTOC(style, false, undefined, placement.point, includeOverset, layer);
|
|
503
|
+
} else {
|
|
504
|
+
result = doc.createTOC(style, false, undefined, placement.point, includeOverset);
|
|
505
|
+
}
|
|
506
|
+
} catch (e1) {
|
|
507
|
+
IDC.fail('script_error', 'InDesign could not create the table of contents: ' + String(e1.message ? e1.message : e1), { toc_style: args.toc_style });
|
|
508
|
+
}
|
|
509
|
+
} finally {
|
|
510
|
+
if (args.keep_view !== false) { idcRestoreView(view); }
|
|
511
|
+
}
|
|
512
|
+
story = idcFirstOf(result);
|
|
513
|
+
if (story === null) {
|
|
514
|
+
existing = idcStoriesOfType(doc, 'TOC_STORY');
|
|
515
|
+
story = existing.length > 0 ? existing[existing.length - 1] : null;
|
|
516
|
+
}
|
|
517
|
+
if (story === null) { IDC.fail('script_error', 'InDesign created no table of contents story.', { toc_style: args.toc_style }); }
|
|
518
|
+
if (!replace) { idcSettleGeneratedFrame(ctx, doc, story, placement); }
|
|
519
|
+
else if (!idcStoryAmong(story, existing)) { idcTocIntoOldPlace(ctx, doc, story, existing[0]); }
|
|
520
|
+
|
|
521
|
+
out = idcGeneratedResult(doc, story, replace, 500);
|
|
522
|
+
try { title = String(style.title); } catch (e2) { title = ''; }
|
|
523
|
+
/* Eintraege: Absaetze mit Text ausser dem Titel. */
|
|
524
|
+
lines = out.lines;
|
|
525
|
+
for (i = 0; i < lines.length; i++) {
|
|
526
|
+
if (i === 0 && title !== '' && lines[i].text === title) { continue; }
|
|
527
|
+
if (lines[i].text !== '') { entries += 1; }
|
|
528
|
+
}
|
|
529
|
+
if (out.lines_truncated) { entries = null; }
|
|
530
|
+
out.entry_count = entries;
|
|
531
|
+
out.toc_style = String(args.toc_style);
|
|
532
|
+
if (entries === 0) { ctx.warn('The table of contents has no entries – no paragraph uses the styles of "' + args.toc_style + '" (include_overset true also reads overset text).'); }
|
|
533
|
+
if (out.overset === true) { ctx.warn('The table of contents does not fit its frame (Übersatz); enlarge the frame or thread it.'); }
|
|
534
|
+
ctx.changes = idcGeneratedChanges(out, framesBefore, null, doc);
|
|
535
|
+
return out;
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
/*
|
|
539
|
+
* A-68 (11), Sitzung 81: InDesign lehnt ein Inhaltsverzeichnisformat ohne
|
|
540
|
+
* Eintrag ab (live: „Jedes Inhaltsverzeichnisformat sollte mindestens einen
|
|
541
|
+
* Inhaltsverzeichniseintrag enthalten“), sobald der letzte Eintrag entfernt
|
|
542
|
+
* wird. Deshalb bleibt ein alter Eintrag stehen, bis die neuen angelegt
|
|
543
|
+
* sind; nennt die neue Liste sein Absatzformat, wird er weiterverwendet.
|
|
544
|
+
*/
|
|
545
|
+
function idcReplaceTocEntries(style, entries) {
|
|
546
|
+
var old = [], i, keep = null, first = null, firstName = null, key;
|
|
547
|
+
for (i = 0; i < style.tocStyleEntries.length; i++) { old.push(style.tocStyleEntries.item(i)); }
|
|
548
|
+
for (i = old.length - 1; i >= 1; i--) { old[i].remove(); }
|
|
549
|
+
if (old.length > 0) { first = old[0]; try { firstName = String(first.name); } catch (e0) { firstName = null; } }
|
|
550
|
+
for (i = 0; i < entries.length; i++) {
|
|
551
|
+
if (keep === null && first !== null && entries[i].style_name === firstName) {
|
|
552
|
+
for (key in entries[i].props) {
|
|
553
|
+
if (IDC.hasOwn(entries[i].props, key)) { first[key] = entries[i].props[key]; }
|
|
554
|
+
}
|
|
555
|
+
keep = first;
|
|
556
|
+
} else {
|
|
557
|
+
style.tocStyleEntries.add(entries[i].style_name, entries[i].props);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (first !== null && keep === null) { first.remove(); }
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/*
|
|
564
|
+
* A-68 (12), Sitzung 81: createTOC(replacing) ersetzt nur ein Verzeichnis
|
|
565
|
+
* desselben Formats; mit einem anderen Format legte InDesign ein zweites an
|
|
566
|
+
* (live auf Seite 1). Dann kommt das neue an Seite, Lage, Groesse und Ebene
|
|
567
|
+
* des ersten Rahmens des alten, und die Rahmen des alten werden entfernt.
|
|
568
|
+
*/
|
|
569
|
+
function idcTocIntoOldPlace(ctx, doc, story, oldStory) {
|
|
570
|
+
var oldFrames = idcGeneratedFrames(oldStory), first, page = null, rect = null, layer = null, frames, i;
|
|
571
|
+
if (oldFrames.length === 0) { return false; }
|
|
572
|
+
first = oldFrames[0];
|
|
573
|
+
try { page = first.parentPage; } catch (e0) { page = null; }
|
|
574
|
+
if (page === null || page === undefined) { return false; }
|
|
575
|
+
rect = idcItemBounds(doc, first);
|
|
576
|
+
try { layer = first.itemLayer; } catch (e1) { layer = null; }
|
|
577
|
+
idcSettleGeneratedFrame(ctx, doc, story, { page: page, rect: rect, point: rect === null ? null : [rect.x, rect.y] });
|
|
578
|
+
frames = idcGeneratedFrames(story);
|
|
579
|
+
if (layer !== null && layer !== undefined && frames.length > 0) {
|
|
580
|
+
try { frames[0].itemLayer = layer; } catch (e2) { /* bleibt auf der aktiven Ebene */ }
|
|
581
|
+
}
|
|
582
|
+
for (i = oldFrames.length - 1; i >= 0; i--) {
|
|
583
|
+
try { oldFrames[i].remove(); } catch (e3) { ctx.warn('A frame of the previous table of contents could not be removed: ' + String(e3.message ? e3.message : e3)); }
|
|
584
|
+
}
|
|
585
|
+
ctx.warn('The existing table of contents was made with another TOC style; InDesign added a new one, which now takes the place of the old frame (the old one was removed).');
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/* ------------------------------------------------------------------ Register */
|
|
590
|
+
|
|
591
|
+
function idcIndexOf(doc, create) {
|
|
592
|
+
var count = 0;
|
|
593
|
+
try { count = Number(doc.indexes.length); } catch (e0) { count = 0; }
|
|
594
|
+
if (count > 0) { return doc.indexes.item(0); }
|
|
595
|
+
if (!create) { return null; }
|
|
596
|
+
try { return doc.indexes.add(); } catch (e1) {
|
|
597
|
+
IDC.fail('script_error', 'InDesign could not create the index: ' + String(e1.message ? e1.message : e1), null);
|
|
598
|
+
}
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/* Themenpfad: 1 bis 4 nicht leere Texte. */
|
|
603
|
+
function idcTopicPathArg(path, field) {
|
|
604
|
+
var i;
|
|
605
|
+
if (!IDC.isArray(path) || path.length < 1 || path.length > 4) { IDC.fail('invalid_argument', field + ' must list 1 to 4 topic levels, e.g. ["Fruit", "Apples"].', null); }
|
|
606
|
+
for (i = 0; i < path.length; i++) {
|
|
607
|
+
if (typeof path[i] !== 'string' || path[i] === '') { IDC.fail('invalid_argument', field + '[' + i + '] must be a non-empty string.', null); }
|
|
608
|
+
}
|
|
609
|
+
return path;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
function idcTopicIn(collection, name) {
|
|
613
|
+
var i, topic;
|
|
614
|
+
try {
|
|
615
|
+
for (i = 0; i < collection.length; i++) {
|
|
616
|
+
topic = collection.item(i);
|
|
617
|
+
if (String(topic.name) === String(name)) { return topic; }
|
|
618
|
+
}
|
|
619
|
+
} catch (e) { /* keins */ }
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/* Thema am Pfad finden oder anlegen; created bekommt die neu angelegten Pfade. */
|
|
624
|
+
function idcTopicAt(index, path, sortAs, created) {
|
|
625
|
+
var collection = index.topics, topic = null, i, sort;
|
|
626
|
+
for (i = 0; i < path.length; i++) {
|
|
627
|
+
topic = idcTopicIn(collection, path[i]);
|
|
628
|
+
sort = sortAs !== null && sortAs[i] !== undefined && sortAs[i] !== null && sortAs[i] !== '' ? String(sortAs[i]) : null;
|
|
629
|
+
if (topic === null) {
|
|
630
|
+
try {
|
|
631
|
+
topic = sort === null ? collection.add(path[i]) : collection.add(path[i], sort);
|
|
632
|
+
} catch (e0) {
|
|
633
|
+
IDC.fail('script_error', 'InDesign could not create the topic "' + path[i] + '": ' + String(e0.message ? e0.message : e0), { topic: path.slice(0, i + 1) });
|
|
634
|
+
}
|
|
635
|
+
created.push(path.slice(0, i + 1));
|
|
636
|
+
} else if (sort !== null) {
|
|
637
|
+
try { topic.sortOrder = sort; } catch (e1) { /* bleibt */ }
|
|
638
|
+
}
|
|
639
|
+
collection = topic.topics;
|
|
640
|
+
}
|
|
641
|
+
return topic;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/* Pfad eines Themas von der obersten Ebene bis zu ihm. */
|
|
645
|
+
function idcTopicPath(topic) {
|
|
646
|
+
var path = [], current = topic, step;
|
|
647
|
+
for (step = 0; step < 6; step++) {
|
|
648
|
+
if (current === null || current === undefined) { break; }
|
|
649
|
+
if (IDC.className(current) !== 'Topic') { break; }
|
|
650
|
+
try { path.unshift(String(current.name)); } catch (e0) { break; }
|
|
651
|
+
try { current = current.parent; } catch (e1) { break; }
|
|
652
|
+
}
|
|
653
|
+
return path;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/* Seitenverweis beschreiben: Seite, Story, Index, Uebersatz. */
|
|
657
|
+
function idcDescribePageReference(ref) {
|
|
658
|
+
var out = { page_reference_id: null, type: null, page_id: null, page_name: '', story_id: null, index: null, in_overset: null, frame_id: null };
|
|
659
|
+
var source = null, frames = null, frame, landing;
|
|
660
|
+
try { out.page_reference_id = Number(ref.id); } catch (e0) { /* null */ }
|
|
661
|
+
try { out.type = idcTocEnumName(IDC_PAGE_REFERENCE_TYPES, idcPageReferenceTypesEnum(), ref.pageReferenceType); } catch (e1) { /* null */ }
|
|
662
|
+
try { source = ref.sourceText; } catch (e2) { source = null; }
|
|
663
|
+
if (source !== null && source !== undefined) {
|
|
664
|
+
try { out.story_id = Number(source.parentStory.id); } catch (e3) { /* null */ }
|
|
665
|
+
try { out.index = Number(source.index); } catch (e4) { /* null */ }
|
|
666
|
+
try { frames = source.parentTextFrames; } catch (e5) { frames = null; }
|
|
667
|
+
if (frames !== null && frames !== undefined) {
|
|
668
|
+
try {
|
|
669
|
+
if (frames.length > 0) {
|
|
670
|
+
frame = IDC.concrete(frames[0]);
|
|
671
|
+
out.frame_id = Number(frame.id);
|
|
672
|
+
landing = IDC.landing(frame);
|
|
673
|
+
out.page_id = landing.page_id;
|
|
674
|
+
out.page_name = landing.page_name;
|
|
675
|
+
out.in_overset = false;
|
|
676
|
+
} else {
|
|
677
|
+
out.in_overset = true;
|
|
678
|
+
}
|
|
679
|
+
} catch (e6) { /* null */ }
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return out;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/* Querverweis-Angabe pruefen: { type, topic (Pfad), custom_text }. */
|
|
686
|
+
function idcCrossReferenceArg(spec) {
|
|
687
|
+
var type;
|
|
688
|
+
if (!spec || typeof spec !== 'object') { IDC.fail('invalid_argument', 'cross_reference must be an object { type, topic, custom_text }.', null); }
|
|
689
|
+
type = spec.type === undefined || spec.type === null ? 'see' : spec.type;
|
|
690
|
+
idcTocEnumFor(IDC_CROSS_REFERENCE_TYPES, idcCrossReferenceTypesEnum(), 'CrossReferenceType', type, 'cross_reference.type');
|
|
691
|
+
idcTopicPathArg(spec.topic, 'cross_reference.topic');
|
|
692
|
+
if (type.substring(0, 6) === 'custom' && (typeof spec.custom_text !== 'string' || spec.custom_text === '')) {
|
|
693
|
+
IDC.fail('invalid_argument', 'cross_reference.custom_text is required for type ' + type + '.', null);
|
|
694
|
+
}
|
|
695
|
+
return { type: type, topic: spec.topic, custom_text: typeof spec.custom_text === 'string' ? spec.custom_text : null };
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
IDC.register('add_index_entry', { needs_document: true, read_only: false, ruler: 'none' },
|
|
699
|
+
function (ctx) {
|
|
700
|
+
var args = ctx.args;
|
|
701
|
+
var doc = ctx.doc;
|
|
702
|
+
var path = idcTopicPathArg(args.topic, 'topic');
|
|
703
|
+
var sortAs = null, refType = null, refTypeName, limit = undefined, numberStyle = null, cross = null, mode, sources = [], frame = null, story = null;
|
|
704
|
+
var start, end, length, found, split = { open: [], stories: [], skipped: [] }, i, maxMatches, index, indexCreated, created = [], topic, ref, refs = [], added = [], target, crossOut = null, matchesFound = 0, frameIds = [], seen = {}, info;
|
|
705
|
+
|
|
706
|
+
if (args.sort_as !== undefined && args.sort_as !== null) {
|
|
707
|
+
if (!IDC.isArray(args.sort_as) || args.sort_as.length > path.length) { IDC.fail('invalid_argument', 'sort_as must be an array with at most one entry per topic level.', null); }
|
|
708
|
+
sortAs = args.sort_as;
|
|
709
|
+
}
|
|
710
|
+
refTypeName = args.page_reference_type === undefined || args.page_reference_type === null ? 'current_page' : args.page_reference_type;
|
|
711
|
+
refType = idcTocEnumFor(IDC_PAGE_REFERENCE_TYPES, idcPageReferenceTypesEnum(), 'PageReferenceType', refTypeName, 'page_reference_type');
|
|
712
|
+
if (refTypeName === 'to_next_use_of_style') {
|
|
713
|
+
if (args.limit_paragraph_style !== undefined && args.limit_paragraph_style !== null) { limit = idcParagraphStyleArg(doc, args.limit_paragraph_style, 'limit_paragraph_style'); }
|
|
714
|
+
} else if (refTypeName === 'for_next_paragraphs' || refTypeName === 'for_next_pages') {
|
|
715
|
+
if (typeof args.limit_count !== 'number' || Math.floor(args.limit_count) !== args.limit_count || args.limit_count < 1) {
|
|
716
|
+
IDC.fail('invalid_argument', 'limit_count (a whole number of 1 or more) is required for ' + refTypeName + '.', null);
|
|
717
|
+
}
|
|
718
|
+
limit = args.limit_count;
|
|
719
|
+
}
|
|
720
|
+
numberStyle = idcOptionalCharacterStyle(doc, args.page_number_style, 'page_number_style');
|
|
721
|
+
if (args.cross_reference !== undefined && args.cross_reference !== null) { cross = idcCrossReferenceArg(args.cross_reference); }
|
|
722
|
+
|
|
723
|
+
/* Ziel: Textbereich, Suchtext oder keins (nur Thema bzw. Querverweis). */
|
|
724
|
+
if (typeof args.find_text === 'string' && args.find_text !== '') {
|
|
725
|
+
mode = 'find_text';
|
|
726
|
+
if (args.start_index !== undefined && args.start_index !== null) { IDC.fail('invalid_argument', 'Pass either find_text or start_index, not both.', null); }
|
|
727
|
+
} else if (args.start_index !== undefined && args.start_index !== null) {
|
|
728
|
+
mode = 'range';
|
|
729
|
+
} else {
|
|
730
|
+
mode = 'none';
|
|
731
|
+
if (cross === null && args.frame_id !== undefined && args.frame_id !== null) {
|
|
732
|
+
IDC.fail('invalid_argument', 'frame_id needs start_index (a text position) or find_text.', null);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (mode === 'range') {
|
|
736
|
+
frame = idcTextFrameArg(doc, args, 'frame_id').item;
|
|
737
|
+
story = idcStoryOf(frame);
|
|
738
|
+
try { length = Number(story.characters.length); } catch (e0) { length = 0; }
|
|
739
|
+
start = args.start_index;
|
|
740
|
+
end = args.end_index === undefined || args.end_index === null ? start : args.end_index;
|
|
741
|
+
if (typeof start !== 'number' || Math.floor(start) !== start || start < 0 || start > length) {
|
|
742
|
+
IDC.fail('invalid_argument', 'start_index must be a whole number from 0 to the story length (' + length + ').', { start_index: start, story_length: length });
|
|
743
|
+
}
|
|
744
|
+
if (typeof end !== 'number' || Math.floor(end) !== end || end < start || end > length) {
|
|
745
|
+
IDC.fail('invalid_argument', 'end_index must be a whole number from start_index to the story length (' + length + ').', { end_index: end, story_length: length });
|
|
746
|
+
}
|
|
747
|
+
idcRejectStoryLocked(story, Number(frame.id), 'add_index_entry');
|
|
748
|
+
if (idcStoryIsGenerated(story)) { IDC.fail('invalid_state', 'Frame ' + args.frame_id + ' holds a generated table of contents or index; index entries belong in the body text.', { frame_id: args.frame_id }); }
|
|
749
|
+
try {
|
|
750
|
+
target = end > start ? story.characters.itemByRange(start, end - 1) : story.insertionPoints.item(start);
|
|
751
|
+
} catch (e1) {
|
|
752
|
+
IDC.fail('script_error', 'InDesign could not address the text range ' + start + '–' + end + '.', { start_index: start, end_index: end });
|
|
753
|
+
}
|
|
754
|
+
sources.push(target);
|
|
755
|
+
} else if (mode === 'find_text') {
|
|
756
|
+
maxMatches = args.max_matches === undefined || args.max_matches === null ? 100 : args.max_matches;
|
|
757
|
+
if (typeof maxMatches !== 'number' || Math.floor(maxMatches) !== maxMatches || maxMatches < 1 || maxMatches > 1000) {
|
|
758
|
+
IDC.fail('invalid_argument', 'max_matches must be a whole number from 1 to 1000.', { max_matches: args.max_matches });
|
|
759
|
+
}
|
|
760
|
+
if (args.frame_id !== undefined && args.frame_id !== null) {
|
|
761
|
+
frame = idcTextFrameArg(doc, args, 'frame_id').item;
|
|
762
|
+
story = idcStoryOf(frame);
|
|
763
|
+
}
|
|
764
|
+
try {
|
|
765
|
+
app.findTextPreferences = NothingEnum.NOTHING;
|
|
766
|
+
idcSetFindOptions(app.findChangeTextOptions, { case_sensitive: args.case_sensitive === true, whole_word: args.whole_word === true, include_master_pages: false, include_hidden_layers: false, include_locked_layers: true });
|
|
767
|
+
app.findTextPreferences.findWhat = args.find_text;
|
|
768
|
+
found = story === null ? doc.findText() : story.findText();
|
|
769
|
+
} catch (e2) {
|
|
770
|
+
IDC.fail('script_error', 'InDesign could not search for "' + args.find_text + '": ' + String(e2.message ? e2.message : e2), { find_text: args.find_text });
|
|
771
|
+
} finally {
|
|
772
|
+
try { app.findTextPreferences = NothingEnum.NOTHING; } catch (e3) { ctx.warn('Could not reset the find preferences.'); }
|
|
773
|
+
}
|
|
774
|
+
/* Verzeichnis- und Registertext nicht markieren; gesperrte Stories auslassen (A-23). */
|
|
775
|
+
var plain = [];
|
|
776
|
+
for (i = 0; i < found.length; i++) {
|
|
777
|
+
try { if (idcStoryIsGenerated(found[i].parentStory)) { continue; } } catch (e4) { /* weiter */ }
|
|
778
|
+
plain.push(found[i]);
|
|
779
|
+
}
|
|
780
|
+
split = idcSplitMatchesByLock(plain);
|
|
781
|
+
matchesFound = split.open.length;
|
|
782
|
+
if (matchesFound === 0 && cross === null) {
|
|
783
|
+
IDC.fail('not_found', 'No occurrence of "' + args.find_text + '" in ' + (story === null ? 'the document' : 'the story of frame ' + args.frame_id) +
|
|
784
|
+
(split.skipped.length > 0 ? ' outside locked stories (see details.skipped_locked)' : '') + '.', { find_text: args.find_text, skipped_locked: split.skipped });
|
|
785
|
+
}
|
|
786
|
+
if (matchesFound > maxMatches) { ctx.warn('Only the first ' + maxMatches + ' of ' + matchesFound + ' occurrences got a page reference (max_matches).'); }
|
|
787
|
+
for (i = 0; i < split.open.length && i < maxMatches; i++) { sources.push(split.open[i]); }
|
|
788
|
+
if (split.skipped.length > 0) { ctx.warn('Occurrences in ' + split.skipped.length + ' locked story/stories got no page reference (see skipped_locked).'); }
|
|
789
|
+
} else if (cross === null) {
|
|
790
|
+
IDC.fail('invalid_argument', 'Pass a text position (frame_id with start_index), find_text or a cross_reference.', null);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
indexCreated = idcIndexOf(doc, false) === null;
|
|
794
|
+
index = idcIndexOf(doc, true);
|
|
795
|
+
topic = idcTopicAt(index, path, sortAs, created);
|
|
796
|
+
|
|
797
|
+
/* Von hinten nach vorn: jeder Verweis setzt ein Zeichen vor seine Quelle (Live-Probe). */
|
|
798
|
+
for (i = sources.length - 1; i >= 0; i--) {
|
|
799
|
+
try {
|
|
800
|
+
if (numberStyle !== null) { ref = topic.pageReferences.add(sources[i], refType, limit, numberStyle); }
|
|
801
|
+
else if (limit !== undefined) { ref = topic.pageReferences.add(sources[i], refType, limit); }
|
|
802
|
+
else { ref = topic.pageReferences.add(sources[i], refType); }
|
|
803
|
+
} catch (e5) {
|
|
804
|
+
IDC.fail('script_error', 'InDesign could not add the page reference: ' + String(e5.message ? e5.message : e5), { topic: path, added_before: added.length });
|
|
805
|
+
}
|
|
806
|
+
added.unshift(ref);
|
|
807
|
+
}
|
|
808
|
+
/* Erst nach allen Einfuegungen beschreiben: jede Marke verschiebt die Stellen hinter ihr. */
|
|
809
|
+
for (i = 0; i < added.length; i++) { refs.push(idcDescribePageReference(added[i])); }
|
|
810
|
+
for (i = 0; i < refs.length; i++) {
|
|
811
|
+
if (refs[i].frame_id !== null && !IDC.hasOwn(seen, String(refs[i].frame_id))) { seen[String(refs[i].frame_id)] = true; frameIds.push(refs[i].frame_id); }
|
|
812
|
+
}
|
|
813
|
+
if (cross !== null) {
|
|
814
|
+
var other = idcTopicAt(index, cross.topic, null, created);
|
|
815
|
+
var crossType = idcTocEnumFor(IDC_CROSS_REFERENCE_TYPES, idcCrossReferenceTypesEnum(), 'CrossReferenceType', cross.type, 'cross_reference.type');
|
|
816
|
+
try {
|
|
817
|
+
if (cross.custom_text !== null) { ref = topic.crossReferences.add(other, crossType, cross.custom_text); }
|
|
818
|
+
else { ref = topic.crossReferences.add(other, crossType); }
|
|
819
|
+
} catch (e6) {
|
|
820
|
+
IDC.fail('script_error', 'InDesign could not add the cross reference: ' + String(e6.message ? e6.message : e6), { topic: path, cross_reference: cross.topic });
|
|
821
|
+
}
|
|
822
|
+
crossOut = { cross_reference_id: null, type: cross.type, topic: idcTopicPath(other), custom_text: cross.custom_text };
|
|
823
|
+
try { crossOut.cross_reference_id = Number(ref.id); } catch (e7) { /* null */ }
|
|
824
|
+
}
|
|
825
|
+
for (i = 0; i < refs.length; i++) {
|
|
826
|
+
if (refs[i].in_overset === true) { ctx.warn('A page reference sits in overset text; generate_index lists it only with include_overset true.'); break; }
|
|
827
|
+
}
|
|
828
|
+
info = { modified_document_ids: [Number(doc.id)] };
|
|
829
|
+
if (frameIds.length > 0) { info.modified_frame_ids = frameIds; }
|
|
830
|
+
ctx.changes = info;
|
|
831
|
+
return {
|
|
832
|
+
topic: idcTopicPath(topic),
|
|
833
|
+
index_created: indexCreated,
|
|
834
|
+
created_topics: created,
|
|
835
|
+
page_references: refs,
|
|
836
|
+
page_reference_count: refs.length,
|
|
837
|
+
matches_found: mode === 'find_text' ? matchesFound : null,
|
|
838
|
+
skipped_locked: split.skipped,
|
|
839
|
+
cross_reference: crossOut
|
|
840
|
+
};
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
/* Themen eines Bereichs depth-first in out schreiben. */
|
|
844
|
+
function idcCollectTopics(collection, out, filter, withRefs, totals) {
|
|
845
|
+
var i, j, topic, path, entry, refs, crosses, match, k, cross, sortOrder;
|
|
846
|
+
var count = 0;
|
|
847
|
+
try { count = Number(collection.length); } catch (e0) { count = 0; }
|
|
848
|
+
for (i = 0; i < count; i++) {
|
|
849
|
+
topic = collection.item(i);
|
|
850
|
+
path = idcTopicPath(topic);
|
|
851
|
+
match = filter === null;
|
|
852
|
+
if (!match) {
|
|
853
|
+
for (k = 0; k < path.length; k++) { if (path[k].toLowerCase().indexOf(filter) >= 0) { match = true; break; } }
|
|
854
|
+
}
|
|
855
|
+
if (match) {
|
|
856
|
+
sortOrder = null;
|
|
857
|
+
try { sortOrder = String(topic.sortOrder); } catch (e1) { sortOrder = null; }
|
|
858
|
+
entry = { path: path, level: path.length, name: path[path.length - 1], sort_as: sortOrder === '' ? null : sortOrder, page_reference_count: 0, cross_reference_count: 0 };
|
|
859
|
+
refs = [];
|
|
860
|
+
try {
|
|
861
|
+
for (j = 0; j < topic.pageReferences.length; j++) { refs.push(idcDescribePageReference(topic.pageReferences.item(j))); }
|
|
862
|
+
} catch (e2) { /* keine */ }
|
|
863
|
+
crosses = [];
|
|
864
|
+
try {
|
|
865
|
+
for (j = 0; j < topic.crossReferences.length; j++) {
|
|
866
|
+
cross = topic.crossReferences.item(j);
|
|
867
|
+
var c = { cross_reference_id: null, type: null, topic: null, custom_text: null };
|
|
868
|
+
try { c.cross_reference_id = Number(cross.id); } catch (e3) { /* null */ }
|
|
869
|
+
try { c.type = idcTocEnumName(IDC_CROSS_REFERENCE_TYPES, idcCrossReferenceTypesEnum(), cross.crossReferenceType); } catch (e4) { /* null */ }
|
|
870
|
+
try { c.topic = idcTopicPath(cross.referencedTopic); } catch (e5) { /* null */ }
|
|
871
|
+
try { c.custom_text = String(cross.customTypeString) === '' ? null : String(cross.customTypeString); } catch (e6) { /* null */ }
|
|
872
|
+
crosses.push(c);
|
|
873
|
+
}
|
|
874
|
+
} catch (e7) { /* keine */ }
|
|
875
|
+
entry.page_reference_count = refs.length;
|
|
876
|
+
entry.cross_reference_count = crosses.length;
|
|
877
|
+
if (withRefs) { entry.page_references = refs; entry.cross_references = crosses; }
|
|
878
|
+
totals.page_references += refs.length;
|
|
879
|
+
totals.cross_references += crosses.length;
|
|
880
|
+
out.push(entry);
|
|
881
|
+
}
|
|
882
|
+
idcCollectTopics(topic.topics, out, filter, withRefs, totals);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
IDC.register('list_index', { needs_document: true, read_only: true, ruler: 'none' },
|
|
887
|
+
function (ctx) {
|
|
888
|
+
var args = ctx.args;
|
|
889
|
+
var doc = ctx.doc;
|
|
890
|
+
var index = idcIndexOf(doc, false);
|
|
891
|
+
var topics = [], totals = { page_references: 0, cross_references: 0 }, stories = [], list, i, filter = null;
|
|
892
|
+
if (args.topic_contains !== undefined && args.topic_contains !== null) {
|
|
893
|
+
if (typeof args.topic_contains !== 'string' || args.topic_contains === '') { IDC.fail('invalid_argument', 'topic_contains must be a non-empty string.', null); }
|
|
894
|
+
filter = args.topic_contains.toLowerCase();
|
|
895
|
+
}
|
|
896
|
+
if (index !== null) { idcCollectTopics(index.topics, topics, filter, args.include_references !== false, totals); }
|
|
897
|
+
list = idcStoriesOfType(doc, 'INDEXING_STORY');
|
|
898
|
+
for (i = 0; i < list.length; i++) { stories.push(idcDescribeGeneratedStory(doc, list[i])); }
|
|
899
|
+
return {
|
|
900
|
+
index_exists: index !== null,
|
|
901
|
+
topics: topics,
|
|
902
|
+
topic_count: topics.length,
|
|
903
|
+
page_reference_count: totals.page_references,
|
|
904
|
+
cross_reference_count: totals.cross_references,
|
|
905
|
+
index_stories: stories
|
|
906
|
+
};
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
IDC.register('generate_index', { needs_document: true, read_only: false, ruler: 'page' },
|
|
910
|
+
function (ctx) {
|
|
911
|
+
var args = ctx.args;
|
|
912
|
+
var doc = ctx.doc;
|
|
913
|
+
var index = idcIndexOf(doc, false);
|
|
914
|
+
var existing = idcStoriesOfType(doc, 'INDEXING_STORY');
|
|
915
|
+
var replace = idcBoolArg(args, 'replace') === true;
|
|
916
|
+
var placement, options, layer, sets = [], i, key, levels, sep, result, story, out, pagesBefore, flag;
|
|
917
|
+
var flags = [['include_section_headings', 'includeSectionHeadings'], ['include_empty_sections', 'includeEmptyIndexSections'], ['include_hidden_layers', 'includeHiddenEntries']];
|
|
918
|
+
var charStyles = [['page_number_style', 'pageNumberStyle'], ['cross_reference_style', 'crossReferenceStyle'], ['cross_reference_topic_style', 'crossReferenceTopicStyle']];
|
|
919
|
+
var separators = [['following_topic', 'followingTopicSeparator'], ['between_entries', 'betweenEntriesSeparator'], ['page_range', 'pageRangeSeparator'], ['between_page_numbers', 'betweenPageNumbersSeparator'], ['before_cross_reference', 'beforeCrossReferenceSeparator'], ['entry_end', 'entryEndSeparator']];
|
|
920
|
+
|
|
921
|
+
if (index === null) { IDC.fail('invalid_state', 'The document has no index entries yet; add them with add_index_entry first.', null); }
|
|
922
|
+
if (replace && existing.length === 0) {
|
|
923
|
+
ctx.warn('No index to replace yet – a new one is created.');
|
|
924
|
+
replace = false;
|
|
925
|
+
}
|
|
926
|
+
placement = idcPlacementArg(doc, args, !replace);
|
|
927
|
+
if (replace && placement.page !== null) { ctx.warn('replace keeps the existing frame where it is; page_id, position_mm and bounds_mm are ignored.'); }
|
|
928
|
+
if (!replace && existing.length > 0) { ctx.warn('The document already has ' + existing.length + ' generated index story/stories; pass replace true to update instead of adding another.'); }
|
|
929
|
+
layer = replace ? null : idcOptionalLayer(doc, args);
|
|
930
|
+
|
|
931
|
+
/* Einstellungen sammeln und pruefen, dann schreiben (Dokumenteinstellung, bleibt gespeichert). */
|
|
932
|
+
if (args.title !== undefined && args.title !== null) {
|
|
933
|
+
if (typeof args.title !== 'string') { IDC.fail('invalid_argument', 'title must be a string ("" for no title).', null); }
|
|
934
|
+
sets.push(['title', args.title]);
|
|
935
|
+
}
|
|
936
|
+
if (args.title_style !== undefined && args.title_style !== null) { sets.push(['titleStyle', idcParagraphStyleArg(doc, args.title_style, 'title_style')]); }
|
|
937
|
+
if (args.format !== undefined && args.format !== null) {
|
|
938
|
+
if (typeof IndexFormat === 'undefined') { IDC.fail('invalid_state', 'This runtime has no IndexFormat enumeration.', null); }
|
|
939
|
+
if (args.format === 'nested') { sets.push(['indexFormat', IndexFormat.NESTED_FORMAT]); }
|
|
940
|
+
else if (args.format === 'run_in') { sets.push(['indexFormat', IndexFormat.RUNIN_FORMAT]); }
|
|
941
|
+
else { IDC.fail('invalid_argument', 'format must be nested or run_in.', { format: args.format }); }
|
|
942
|
+
}
|
|
943
|
+
for (i = 0; i < flags.length; i++) {
|
|
944
|
+
flag = idcBoolArg(args, flags[i][0]);
|
|
945
|
+
if (flag !== null) { sets.push([flags[i][1], flag]); }
|
|
946
|
+
}
|
|
947
|
+
if (args.level_styles !== undefined && args.level_styles !== null) {
|
|
948
|
+
levels = args.level_styles;
|
|
949
|
+
if (!IDC.isArray(levels) || levels.length < 1 || levels.length > 4) { IDC.fail('invalid_argument', 'level_styles must list 1 to 4 paragraph style names (level 1 first).', null); }
|
|
950
|
+
for (i = 0; i < levels.length; i++) {
|
|
951
|
+
if (levels[i] !== null && levels[i] !== '') { sets.push(['level' + (i + 1) + 'Style', idcParagraphStyleArg(doc, levels[i], 'level_styles[' + i + ']')]); }
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
if (args.section_heading_style !== undefined && args.section_heading_style !== null) { sets.push(['sectionHeadingStyle', idcParagraphStyleArg(doc, args.section_heading_style, 'section_heading_style')]); }
|
|
955
|
+
for (i = 0; i < charStyles.length; i++) {
|
|
956
|
+
if (args[charStyles[i][0]] !== undefined && args[charStyles[i][0]] !== null) { sets.push([charStyles[i][1], idcOptionalCharacterStyle(doc, args[charStyles[i][0]], charStyles[i][0])]); }
|
|
957
|
+
}
|
|
958
|
+
if (args.separators !== undefined && args.separators !== null) {
|
|
959
|
+
if (typeof args.separators !== 'object') { IDC.fail('invalid_argument', 'separators must be an object.', null); }
|
|
960
|
+
for (i = 0; i < separators.length; i++) {
|
|
961
|
+
sep = args.separators[separators[i][0]];
|
|
962
|
+
if (sep !== undefined && sep !== null) {
|
|
963
|
+
if (typeof sep !== 'string') { IDC.fail('invalid_argument', 'separators.' + separators[i][0] + ' must be a string.', null); }
|
|
964
|
+
sets.push([separators[i][1], sep]);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
sets.push(['replaceExistingIndex', replace]);
|
|
969
|
+
try {
|
|
970
|
+
options = doc.indexGenerationOptions;
|
|
971
|
+
for (i = 0; i < sets.length; i++) { key = sets[i][0]; options[key] = sets[i][1]; }
|
|
972
|
+
} catch (e0) {
|
|
973
|
+
IDC.fail('script_error', 'InDesign could not set the index options (' + key + '): ' + String(e0.message ? e0.message : e0), { option: key });
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
try { pagesBefore = Number(doc.pages.length); } catch (e1) { pagesBefore = null; }
|
|
977
|
+
var framesBefore = idcFrameIdsOfStories(idcStoriesOfType(doc, 'INDEXING_STORY'));
|
|
978
|
+
var pageIdsBefore = idcDocumentPageIds(doc);
|
|
979
|
+
try {
|
|
980
|
+
if (replace) {
|
|
981
|
+
result = index.generate(idcGeneratedStoryPage(existing[0]), undefined, undefined, args.autoflow === true, args.include_overset === true);
|
|
982
|
+
} else if (layer !== null) {
|
|
983
|
+
result = index.generate(placement.page, placement.point, layer, args.autoflow === true, args.include_overset === true);
|
|
984
|
+
} else {
|
|
985
|
+
result = index.generate(placement.page, placement.point, undefined, args.autoflow === true, args.include_overset === true);
|
|
986
|
+
}
|
|
987
|
+
} catch (e2) {
|
|
988
|
+
IDC.fail('script_error', 'InDesign could not generate the index: ' + String(e2.message ? e2.message : e2), null);
|
|
989
|
+
}
|
|
990
|
+
story = idcFirstOf(result);
|
|
991
|
+
if (story === null) {
|
|
992
|
+
existing = idcStoriesOfType(doc, 'INDEXING_STORY');
|
|
993
|
+
story = existing.length > 0 ? existing[existing.length - 1] : null;
|
|
994
|
+
}
|
|
995
|
+
if (story === null) { IDC.fail('script_error', 'InDesign created no index story.', null); }
|
|
996
|
+
if (!replace) { idcSettleGeneratedFrame(ctx, doc, story, placement); }
|
|
997
|
+
out = idcGeneratedResult(doc, story, replace, 500);
|
|
998
|
+
out.pages_added = pagesBefore === null ? null : Number(doc.pages.length) - pagesBefore;
|
|
999
|
+
if (out.overset === true) { ctx.warn('The index does not fit its frame(s) (Übersatz); enlarge the frame, thread it or pass autoflow true.'); }
|
|
1000
|
+
ctx.changes = idcGeneratedChanges(out, framesBefore, pageIdsBefore, doc);
|
|
1001
|
+
return out;
|
|
1002
|
+
});
|