Skip to content

Commit 8d6df57

Browse files
author
bastieneichenberger
committed
add grep function
1 parent 3689d81 commit 8d6df57

File tree

5 files changed

+82
-10
lines changed

5 files changed

+82
-10
lines changed

lib/indesign/config/all_indesign_modules.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
//@include "../application/application.jsx"
1010

1111
//@include "../document/document.jsx"
12+
//@include "../document/grep.jsx"
1213

1314
//@include "../link/link.jsx"

lib/indesign/document/grep.jsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* @namespace Document.Grep
44
* @memberOf IN
55
* @author Bastien Eichenberger
6+
* @todo add function to build grep and regex
7+
* @todo improve the function clear_grep
68
*/
79
IN.Document.Grep = (function (my) {
810

@@ -11,7 +13,7 @@ IN.Document.Grep = (function (my) {
1113
};
1214

1315
var UNICODE_HEXA = {
14-
NORMAL_SPACE: 0020,
16+
NORMAL_SPACE: '0020',
1517
NONBREAKABLE_SPACE: '00A0',
1618
NONBREAKABLE_SPACE_FIXED_WITH: '202F',
1719
PUNCTUATION_SPACE: '2008',
@@ -23,7 +25,7 @@ IN.Document.Grep = (function (my) {
2325
SIXTH_SPACE: '2006',
2426
THIRD_SPACE: '2004',
2527
EN_SPACE: '2002',
26-
EM_SPACE: '2003',
28+
EM_SPACE: '2003'
2729
};
2830

2931
var NON_PRINTABLE_CHAR = {
@@ -32,6 +34,11 @@ IN.Document.Grep = (function (my) {
3234
ALL_OTHER_BREAK: '\r'
3335
};
3436

37+
function clear_grep () {
38+
app.findGrepPreferences = NothingEnum.nothing;
39+
app.changeGrepPreferences = NothingEnum.nothing;
40+
}
41+
3542
/**
3643
* Function to find a regular expression in a document
3744
* @param {string} regular_expression
@@ -50,28 +57,38 @@ IN.Document.Grep = (function (my) {
5057
return doc.findGrep();
5158
}
5259

53-
my.find_and_change = function (find, replace, find_grep_preferences, doc) {
60+
/**
61+
* @todo add find_grep_preferences
62+
* @param find
63+
* @param replace
64+
* @param find_grep_preferences
65+
* @param doc
66+
* @returns {*}
67+
*/
68+
my.find_and_change = function (find, replace, doc) {
5469

5570
if (doc === undefined) {
5671
var doc = app.activeDocument;
5772
}
5873

59-
app.findGrepPreferences = NothingEnum.nothing;
74+
clear_grep();
75+
6076
app.findGrepPreferences.findWhat = find;
6177
app.changeGrepPreferences.changeTo = replace;
6278

63-
return doc.changeGrep();
64-
}
79+
doc.changeGrep();
6580

66-
my.delete_double_standard_spaces = function (string, doc) {
81+
clear_grep();
82+
}
6783

84+
my.delete_double_standard_spaces = function (doc) {
6885
if (doc === undefined) {
6986
var doc = app.activeDocument;
7087
}
71-
var pattern = '\x{' + UNICODE_HEXA.NORMAL_SPACE + '}{2,}';
72-
return my.find_and_change(pattern, UNICODE_HEXA.NORMAL_SPACE, doc);
88+
my.find_and_change('\\x{0020}{2,}', '\\x{0020}', doc);
7389
}
7490

91+
7592
my.get = function (name) {
7693
return REGEX[name];
7794
}
140 KB
Binary file not shown.

tests/test/fixtures/indesign/document/test_document.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
IN.Document.create(100, 200);
88

9+
var source = IN.Document.save(results_folder + '/document_result.indd');
10+
911
IN.Document.export_as_PDF(results_folder + '/test.pdf', '[PDF/X-4:2008]', '1-1');
1012

1113
IN.Document.export_as_JPG(results_folder + '/test_jpg_indesign.jpg', JPEGOptionsQuality.MEDIUM, 72);
1214

13-
IN.Document.close(SaveOptions.NO);
15+
IN.Document.close(SaveOptions.NO, source);
1416

1517
}
1618
catch (ex) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(function (results_folder) {
2+
3+
try {
4+
//@include "../../../../../lib/indesign/indesign-lib.jsx"
5+
6+
var SCRIPT_FOLDER, source;
7+
8+
IN.Config.init();
9+
10+
SCRIPT_FOLDER = new File($.fileName).parent;
11+
12+
source = IN.Document.open(SCRIPT_FOLDER + '/grep_document.indd');
13+
14+
IN.Document.Grep.delete_double_standard_spaces();
15+
16+
17+
/*
18+
for(myCounter = 0; myCounter < app.activeDocument.stories.length; myCounter++){
19+
myStory = app.activeDocument.stories.item(myCounter);
20+
myID = myStory.id;
21+
switch(myExportFormat){
22+
case 0:
23+
myFormat = ExportFormat.textType;
24+
myExtension = ".txt"
25+
break;
26+
case 1:
27+
myFormat = ExportFormat.RTF;
28+
myExtension = ".rtf"
29+
break;
30+
case 2:
31+
myFormat = ExportFormat.taggedText;
32+
myExtension = ".txt"
33+
break;
34+
}
35+
myFileName = "StoryID" + myID + myExtension;
36+
myFilePath = myFolder + "/" + myFileName;
37+
myFile = new File(myFilePath);
38+
myStory.exportFile(myFormat, myFile);
39+
}*/
40+
41+
IN.Document.close(SaveOptions.NO, source);
42+
43+
}
44+
catch (ex) {
45+
alert('file: ' + ex.fileName + '\n message: ' + ex.message + '\n line: ' + ex.line);
46+
}
47+
finally {
48+
IN.Application.restore();
49+
}
50+
51+
52+
}).apply(this, [].slice.apply(this.arguments));

0 commit comments

Comments
 (0)