-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateMyXml.jsx
348 lines (300 loc) · 8.11 KB
/
createMyXml.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/** createMyXML.jsx v0.1
*
* This script is intended for use with Adobe InDesign CS2.
*
* This script generates an xml file (.xml) from an indesign document (.indd)
* User assigns each story in the page a unique scripting label and the script tags the
* content accordingly based on the styles used for content.
* Multiple pages are supported!
*
* @Copyright: (c) Mohammad Jangda ([email protected])
* @License: MIT License (http://www.opensource.org/licenses/mit-license.php)
*
*/
/* tells script engine that this script is to be run in indesign (and not another adobe app) */
#target indesign
/** ----EDIT---- **/
defaultMapLocation = "/c/Program Files/Adobe/InDesign CS2/Presets/Scripts/";
/** ----STOP---- **/
//array of styles in document (ensure that all of these styles exist in the document or the script engine will throw an error)
myStyles = new Array();
//array of xml tags (ensure that the position of the tags in this array match up with their desired styles in the myStyles array)
myTags = new Array();
/* global variables */
//reference to the Document currently active in indesign
myDoc = app.activeDocument;
//reference to an array of all the PageItems in the Document
myPageItems = myDoc.allPageItems;
//reference to the Root of the Document's XML structure - Root is, by default, the topmost element in the structure
xmlRoot = myDoc.xmlElements.item("Root");
//an array of all the labels found in the Document
labels = getLabels();
/* call main function that does everything */
createXML();
/**
* main function of the script;
* calls a bunch of other functions which working together create the xml document;
**/
function createXML() {
if(labels!=null) {
//labels were found in the Document, so proceed with the XML creation
// styleOversetText();
readMapFile();
for(var i=0; i<labels.length; i++) {
var xmlElem = xmlRoot.xmlElements.add({markupTag:"Article"});
xmlElem.xmlAttributes.add("Label", labels[i]);
markupItems(returnItemsWithLabel(i), xmlElem);
} //end for
mapStylesToTags();
var exported = exportXMLFile();
// cleanUpXMLFile(exported);
alert("createMyXML complete");
} //end if
else {
//no labels were found in the Document; alert user; script ends
alert("No PageItems are labelled. Please label and run this script again.");
} //end else
} //createXML
/**
* Reads through map file and loads Paragraph Style/XML Tag maps into arrays
**/
function readMapFile() {
var f = new File(defaultMapLocation +"createMyXML.map");
if(!f.exists) {
alert("Map file not found. Please locate and select the map file.");
f = File.OpenDialog();
}
var i = 0;
if(f.open("r:")) {
if(f.length > 0) {
while(f.tell() < f.length) {
var ln = f.readln();
if(ln.charAt(0)!="#") {
var spl = ln.split("||");
if(spl.length==2) {
myStyles[i] = spl[0];
myTags[i] = spl[1];
i++;
}
}
}
}
f.close();
}
} //readMapFile
/**
* CURRENTLY UNUSED
**/
function cleanUpXMLFile(xmlFile) {
var f = new File(xmlFile);
if(f.open("e:")) {
if(f.length > 0) {
while(f.tell() < f.length) {
var content = f.read();
alert(content);
var replaced = makeWebReady(content);
alert(replaced);
f.seek(0,0);
var write = f.write(replaced);
}
}
f.close();
}
} //cleanUpXMLFile
/**
* CURRENTLY UNUSED
**/
function makeWebReady(txt) {
//Newline char: 

// txt = txt.replace(/
/g,"");
txt = txt.replace(/\r/g,"");
//Apostrophe ’
// txt = txt.replace(/’/g,"'");
txt = txt.replace(/’/g,"'");
//Open quote: “
// txt = txt.replace(/“/g,""");
txt = txt.replace(/“/g,""");
//End quote: â€
// txt = txt.replace(/â€/g,""");
txt = txt.replace(/”/g,""");
//Em Dash: —
// txt = txt.replace(/—/g,"—");
txt = txt.replace(/—/g,"—");
//Others go below - single quote, copyright symbol, trademark symbol (??)
return txt;
} //makeWebReady
/**
* Creates an XML file named document.xml where "document" is the name of the active Document;
* XML file is saved in the same location as the indesign Document;
* Images in Document are optimized and saved as high quality JPEGs in a folder called "Images" in the same location as the XML file;
**/
function exportXMLFile() {
/**
* Due to a bug in the scripting engine, app.xmlExportPreferences do not work
* As a workaround, manually export a page to xml and set the desired settings
* The settings will be carried over when you run the script
*/
var path = myDoc.filePath + "/";
var xmlName = myDoc.name.substring(0, myDoc.name.indexOf("."));
var xmlFile = File(path + xmlName + ".xml");
myDoc.exportFile(ExportFormat.xml, xmlFile, false);
return xmlFile;
} //exportXMLFile
/**
*
**/
function mapStylesToTags() {
for(var j=0;j<myStyles.length;j++) {
myDoc.xmlExportMaps.add(myStyles[j], myTags[j]);
}
myDoc.autoTag();
} //mapStylesToTags
/**
*
**/
function markupItems(items,xmlElem) {
for(var i=0; i<items.length; i++) {
if(!isTaggedSingle(items[i])) {
items[i].markup( xmlElem.xmlElements.add({markupTag:"Item"}) );
}
}
} //markupItems
/**
* Returns array of PageItems that have been labelled
**/
function returnItemsWithLabel(index) {
var objLabel;
var theLabel = labels[index];
var toSelect = new Array();
for(var i=0; i < myPageItems.length; i++) {
objLabel = myPageItems[i].label;
if(objLabel==theLabel) {
//check to make sure current pageItem is an instance of a TextFram
//if it is, push into array
if(myPageItems[i].getElements()[0] instanceof TextFrame) {
toSelect.push(myPageItems[i]);
}
}
}
return toSelect;
}
/**
* Returns an array of labels found in the document
**/
function getLabels() {
var myArray = new Array()
var numArrayItems = 0;
var objLabel;
for(var i = myPageItems.length - 1 ; i >= 0 ; i--) {
styleOffPagePI(myPageItems[i]);
objLabel = myPageItems[i].label;
if((objLabel!="") && (contains(myArray, objLabel)==false)) {
myArray[numArrayItems] = objLabel;
numArrayItems++;
}
}
if(myArray.length==0) {
return null;
}
return myArray;
}
/**
* CURRENTLY UNUSED
**/
function ensureParagraphStyles() {
if(myDoc.paragraphStyles.item("Copy: overflow")==null) {
myDoc.paragraphStyles.add({name:"Copy: overflow"});
}
if(myDoc.paragraphStyles.item("Copy: offpage")==null) {
myDoc.paragraphStyles.add({name:"Copy: offpage"});
}
} //ensureParagraphStyles
/**
* CURRENTLY UNUSED
**/
function styleOversetText() {
var stories = myDoc.stories;
for(var i=0;i<stories.length;i++) {
if(stories[i].overflows) {
paras = stories[i].paragraphs;
for(var j=0; j<paras.length; j++) {
if(paras[j].parentTextFrames=="") {
paras[j].applyStyle(myDoc.paragraphStyles.item("Copy: overflow"));
} //end if
}//end for
} //end if
} //end for
} //styleOversetText
/**
* CURRENTLY UNUSED
* Any PageItems that are off the spread are styled
**/
function styleOffPagePI(pageItem) {
if(pageItem instanceof TextFrame) {
if(pageItem.parent instanceof Spread) {
for(var p=0; p < pageItem.paragraphs.length; p++) {
pageItem.paragraphs[p].applyStyle(myDoc.paragraphStyles.item("Copy: offpage"));
}
}
}
}
/** ---- UTILITY FUNCTIONS ---- **/
/**
*
**/
function contains(theArray, str) {
for(var i=0; i<theArray.length; i++) {
if (theArray[i]==str) { return true; }
}
return false;
}
/**
*
**/
function isArticleTag(tag) {
for(var i=0; i<myTags.length; i++) {
if(tag==myTags[i]) {
return true;
}
}
return false;
} //isArticleTag
/**
* Returns true if item is already tagged
**/
function isTaggedSingle(item) {
if(item.associatedXMLElement!=null) {
return true;
}
return false;
}
/**
*
**/
function isTaggedGroup(items) {
for(var i=0;i<items.length;i++) {
if(items[i].associatedXMLElement!=null) {
return true;
}
}
return false;
}
/**
*
**/
function isInGroup(myItem){
var myGroup = myItem.parent;
while (myGroup.constructor.name != "Group") {
if (myGroup.constructor.name == "Application") { return null; }
myGroup = myGroup.parent;
}
return myGroup;
}
/**
* Ungroups grouped elements on the page to allow for xml conversion
**/
function ungroupAll() {
for(var i=0;i<myDoc.groups.length;i++) {
myDoc.groups[i].ungroup();
}
}