-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.jsx
195 lines (144 loc) · 6.13 KB
/
main.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
// WP -> InDesign - Import posts from WordPress into InDesign
// Author: Keanan Koppenhaver
#include "./JSInterface.jsx"
#targetengine JSEngine
var postList;
var posts;
// Call our code
Main();
function postsHandler(data) {
posts = eval(data);
for (var i = 0; i < posts.length; i++){
var postString = decodeHTMLEntities(posts[i].title.rendered) + " @ " + posts[i].date.split("T")[0];
postList.add("item", postString);
}
}
function Main(){
var postContent;
var doc = app.properties.activeDocument;
var myWindow = new Window ("dialog", "WordPress Importer");
var urlInputGroup = myWindow.add("group");
urlInputGroup.add ("statictext", undefined, "WordPress Site URL:");
var siteUrl = urlInputGroup.add ("edittext", undefined, "");
siteUrl.characters = 50;
var existingUrl = app.activeDocument.extractLabel('wpRestURL');
if(existingUrl != ''){
siteUrl.text = existingUrl;
}
urlUpdateButton = urlInputGroup.add ("button", undefined, "Update");
var searchGroup = myWindow.add("group");
searchGroup.add ("statictext", undefined, "Search:");
searchGroup.alignment = "right";
var postSearch = searchGroup.add ("edittext", undefined, "");
postSearch.characters = 50;
var searchButton = searchGroup.add ("button", undefined, "Search");
var postListGroup = myWindow.add("group");
postList = postListGroup.add ("listbox", undefined, ["Set your WordPress URL above to see posts here"]);
postList.minimumSize = [500,200]
postListGroup.alignment = "right";
var actionButtonGroup = myWindow.add("group");
actionButtonGroup.alignment = "right";
var cancelButton = actionButtonGroup.add ("button", undefined, "Cancel");
var insertButton = actionButtonGroup.add ("button", undefined, "Insert");
urlUpdateButton.onClick = function(){
populatePosts(postList, siteUrl, false);
}
searchButton.onClick = function(){
populatePosts(postList, siteUrl, true, postSearch);
}
insertButton.onClick = function(){
var postIndex = postList.selection.index;
postContent = posts[postIndex].content.rendered;
postContent = sanitizePostContent(postContent);
myWindow.close(1);
}
if(siteUrl.text != ''){
populatePosts(postList, siteUrl, false);
}
// If there is no text frame selected, alert the user and make them go back and select one
if( !app.selection[0] ) {
alert('Please select a text frame first.');
}
else {
var result = myWindow.show();
}
if (result == 1) {
app.selection[0].parentStory.contents = postContent;
//Tagging all headers
var head1 = doc.characterStyles.itemByName('Head1');
var head2 = doc.characterStyles.itemByName('Head2');
var head3 = doc.characterStyles.itemByName('Head3');
var head4 = doc.characterStyles.itemByName('Head4');
var head5 = doc.characterStyles.itemByName('Head5');
var head6 = doc.characterStyles.itemByName('Head6');
var bold = doc.characterStyles.itemByName('Bold');
var italic = doc.characterStyles.itemByName('Italic');
var blockquote = doc.characterStyles.itemByName('BlockQuote');
stripTagsAndStyle('h1', head1, doc);
stripTagsAndStyle('h2', head2, doc);
stripTagsAndStyle('h3', head3, doc);
stripTagsAndStyle('h4', head4, doc);
stripTagsAndStyle('h5', head5, doc);
stripTagsAndStyle('h6', head6, doc);
stripTagsAndStyle('strong', bold, doc);
stripTagsAndStyle('em', italic, doc);
stripTagsAndStyle('blockquote', blockquote, doc);
// Storing the latest URL
app.activeDocument.insertLabel('wpRestURL', siteUrl.text);
}
else {
app.activeDocument.insertLabel('wpRestURL', siteUrl.text);
}
}
function populatePosts(postList, siteUrl,isSearch, postSearch) {
postList.removeAll();
if(siteUrl.text[siteUrl.text.length-1] != '/'){
siteUrl.text = siteUrl.text + '/';
}
if(isSearch){
var url = siteUrl.text + "wp-json/wp/v2/posts?search=" + encodeURIComponent(postSearch.text);
}
else {
var url = siteUrl.text + "wp-json/wp/v2/posts";
}
JSInterface.evalScript("JSInterface.plugins.getURL(JSInterface.getData())", url, postsHandler);
}
function sanitizePostContent(content){
//Strip all opening <p> tags, Replace all closing </p> tags with new lines
content = content.replace(/<p[^>]*>/g, '').replace(/<\/p>/g, '\r');
//Strip links for v0.1
content = content.replace(/<a\b[^>]*>/ig,"").replace(/<\/a>/ig, "");
content = content.replace(/<br[^>]*>/g, '').replace(/<hr[^>]*>/g, '\r');
content = content.replace(/<ul[^>]*>/g, '').replace(/<\/ul>/g, "");
content = content.replace(/<ol[^>]*>/g, '').replace(/<\/ol>/g, "");
content = content.replace(/<li[^>]*>/g, '').replace(/<\/li>/g, "");
content = content.replace(/<div[^>]*>/g, '').replace(/<\/div>/g, "");
image_regex = /<img .*src="(.*?)".*\/>/g;
while ( ( image_matches = image_regex.exec( content ) ) !== null) {
image_url = image_matches[1];
image_name = image_url.split('/');
image_name = image_name[image_name.length - 1];
image_placeholder = '[image ' + image_name + ']';
content = content.replace(image_matches[0], image_placeholder);
}
content = decodeHTMLEntities(content);
return content;
}
function stripTagsAndStyle(tag, characterStyle, document){
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "<" + tag + ".*>(.*)<\/" + tag + ">";
if(tag == 'blockquote'){
app.findGrepPreferences.findWhat = "(?s)<blockquote>(.*)<\/blockquote>";
}
app.changeGrepPreferences.properties = {
changeTo: '$1',
appliedCharacterStyle: characterStyle,
};
document.changeGrep();
}
function decodeHTMLEntities(input_string){
input_string = input_string.replace(' ', ' ');
return (input_string+"").replace(/&#\d+;/gm,function(s) {
return String.fromCharCode(s.match(/\d+/gm)[0]);
});
}