-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
809 lines (732 loc) · 25.1 KB
/
Code.gs
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
var HYPERIZED_COLOR = "#cc0099";
var FIGORDER_COLOR = "#0099cc";
// Added using the project key in https://github.com/simula-innovation/gas-underscore
var _ = Underscore.load();
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem("Hyperize Links", "hyperize")
.addItem("Hide Hyper Links", "hideHyperElements")
.addItem("Show Hyper Links", "showHyperElements")
.addItem("Grab Hyper URLs", "grabUrls")
.addItem("Peek at Text Changes", "checkForTextChanges")
.addItem("Find and Replace URLs", "replaceUrls")
.addItem("Order Figures", "orderFigures")
.addItem("Hide Figure Links", "hideFigureOrderElements")
.addItem("Show Figure Links", "showFigureOrderElements")
.addItem("Remove GitHub Authorization", "reset")
.addToUi();
}
/**
* Runs when the add-on is installed.
*/
function onInstall(e) {
onOpen(e);
}
/**
* Color hyper elements with the hyper color.
*/
function showHyperElements() {
colorHyperElements(HYPERIZED_COLOR);
}
/**
* Set hyper elements back to black.
*/
function hideHyperElements() {
colorHyperElements("#000000");
}
/**
* Set hyper elements to a particular color.
*/
function colorHyperElements(color) {
var links = getAllHyperLinks();
_.each(links, function(link) {
if (link.element.getType() == DocumentApp.ElementType.TEXT) {
link.element.setForegroundColor(link.startOffset,
link.endOffsetInclusive, color);
link.element.setUnderline(false);
}
});
}
/**
* Color figure order elements with the hyper color.
*/
function showFigureOrderElements() {
colorFigureOrderElements(FIGORDER_COLOR);
}
/**
* Set figure order elements back to black.
*/
function hideFigureOrderElements() {
colorFigureOrderElements("#000000");
}
/**
* Set figure order elements to a particular color.
*/
function colorFigureOrderElements(color) {
var links = getAllFigureOrderLinks();
_.each(links, function(link) {
if (link.element.getType() == DocumentApp.ElementType.TEXT) {
link.element.setForegroundColor(link.startOffset,
link.endOffsetInclusive, color);
link.element.setUnderline(false);
}
});
}
/**
* From http://stackoverflow.com/a/28821735
*/
function bodyPath(el, path) {
path = path? path: [];
var parent = el.getParent();
var index = parent.getChildIndex(el);
path.push(index);
var parentType = parent.getType();
if (parentType !== DocumentApp.ElementType.BODY_SECTION) {
path = bodyPath(parent, path);
}
else {
return path;
};
return path;
};
function grabUrls() {
var urls = hyperize(true);
DocumentApp.getUi().alert("URLs: \n" + _.uniq(urls).join("\n"));
}
function checkForTextChanges() {
var changes = hyperize(false, true);
DocumentApp.getUi().alert("Changes: \n" + changes.join("\n"));
}
function replaceUrls() {
// From http://stackoverflow.com/a/17606289
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
var ui = DocumentApp.getUi();
var textToReplaceResponse = ui.prompt(
"Find and Replace Hyper Link URLs",
"URL Text to Replace",
ui.ButtonSet.OK_CANCEL);
var newTextResponse = ui.prompt(
"Find and Replace Hyper Link URLs",
"New URL Text",
ui.ButtonSet.OK_CANCEL);
if (textToReplaceResponse.getSelectedButton() === ui.Button.OK &&
newTextResponse.getSelectedButton() === ui.Button.OK) {
var textToReplace = textToReplaceResponse.getResponseText();
var newText = newTextResponse.getResponseText();
var links = getAllHyperLinks();
_.each(links, function(link) {
if (link.url.indexOf(textToReplace) !== -1) {
var updatedUrl = link.url.replaceAll(textToReplace, newText);
if (link.element.getType() == DocumentApp.ElementType.INLINE_IMAGE) {
link.element.setLinkUrl(updatedUrl);
}
else {
link.element.setLinkUrl(link.startOffset, link.endOffsetInclusive, updatedUrl);
}
}
});
}
}
function orderFigures() {
var changingHyperObjects = {};
var errors = [];
function unpackKey(link) {
var url = link.url;
var fullKey = url.split("?figorder=")[1];
var group = "default";
var section = "default";
if (fullKey.indexOf("&group") !== -1) {
group = fullKey.split("&group=")[1].split("&")[0];
}
if (fullKey.indexOf("§ion") !== -1) {
section = fullKey.split("§ion=")[1].split("&")[0];
}
var key = fullKey.split("&")[0]
return [key, group, section];
}
// First, split elements.
var figOrderLinks = getAllFigureOrderLinks();
if (figOrderLinks.length === 0) {
return;
}
var textElements = getAllTextElements();
_.each(textElements, function(textElement) {
var figOrderLinksFromText = getAllFigureOrderLinks(textElement);
var newTextElements = splitTextByLinks(textElement, figOrderLinksFromText);
var parentElement = textElement.getParent();
var textElementIndex = parentElement.getChildIndex(textElement);
parentElement.removeChild(textElement);
var i = 0;
_.each(newTextElements, function(newTextElement) {
if (newTextElement.getText().length > 0) {
parentElement.insertText(textElementIndex + i, newTextElement);
i += 1;
}
});
});
// Re-run after splitting.
figOrderLinks = getAllFigureOrderLinks();
var labelToOrderedLabel = {};
var sectionToSectionNumber = {};
var sectionFigureCount = {};
var sectionCount = {};
_.each(figOrderLinks, function(link) {
var unpackedKey = unpackKey(link);
var group = unpackedKey[1];
labelToOrderedLabel[group] = {};
sectionToSectionNumber[group] = {};
sectionFigureCount[group] = {};
sectionCount[group] = 1;
});
groups = _.keys(labelToOrderedLabel);
// Tally up figure sections.
_.each(figOrderLinks, function(link) {
var unpackedKey = unpackKey(link);
var group = unpackedKey[1];
var section = unpackedKey[2];
if (!sectionToSectionNumber[group].hasOwnProperty(section)) {
sectionToSectionNumber[group][section] = sectionCount[group];
sectionCount[group] += 1;
}
});
// Tally up figures.
_.each(figOrderLinks, function(link) {
var unpackedKey = unpackKey(link);
var key = unpackedKey[0];
var group = unpackedKey[1];
var section = unpackedKey[2];
if (!labelToOrderedLabel[group].hasOwnProperty(key)) {
if (!sectionFigureCount[group].hasOwnProperty(section)) {
sectionFigureCount[group][section] = 0;
}
labelToOrderedLabel[group][key] = sectionToSectionNumber[group][section] + String.fromCharCode(65 + sectionFigureCount[group][section]);
sectionFigureCount[group][section] += 1;
}
});
_.each(figOrderLinks, function(link) {
var linkElement = link.element;
var parentElement = linkElement.getParent();
var elementIndex = parentElement.getChildIndex(linkElement);
var unpackedKey = unpackKey(link);
var key = unpackedKey[0];
var group = unpackedKey[1];
var figValue = labelToOrderedLabel[group][key];
parentElement.removeChild(linkElement);
parentElement.insertText(elementIndex, figValue);
var newElement = parentElement.getChild(elementIndex);
newElement.setLinkUrl(link.url);
newElement.setUnderline(false);
newElement.setForegroundColor(FIGORDER_COLOR);
});
}
/**
* Replace hyper link elements with the text that they point to.
*/
function hyperize(onlyGrabUrls, onlyCheckForTextChanges) {
var errors = [];
var userProperties = PropertiesService.getUserProperties();
var scriptProperties = PropertiesService.getScriptProperties();
// Cached results after fetching URLs and looking for images in responses.
var fetchedUrls = {};
var responseImages = {};
askForGitHubAccess();
var gitHubService = waitForGitHubAccess();
// Be sure not to grab this on every URL fetch.
var accessToken = gitHubService.getAccessToken();
/**
* Configures the GitHub authorization service.
*/
function getGitHubService() {
return getGitHubServiceWithProperties(scriptProperties, userProperties);
}
/**
* Prompts the user for a modal dialog to gain GitHub access.
*/
function askForGitHubAccess() {
var gitHubService = getGitHubService();
if (!gitHubService.hasAccess()) {
var authorizationUrl = gitHubService.getAuthorizationUrl();
// Close the modal after authorization is attempted.
var template = HtmlService.createTemplateFromFile("github_auth");
template.authorizationUrl = authorizationUrl;
var page = template.evaluate().setHeight(50);
DocumentApp.getUi().showModalDialog(page, "Hyper: Authorize GitHub");
}
}
/**
* Blocks until GitHub access is established.
*/
function waitForGitHubAccess() {
var gitHubService = getGitHubService();
var numTries = 0;
while (!gitHubService.hasAccess()) {
if (numTries > 10) {
throw new Error("Cannot reach GitHub.");
}
Utilities.sleep(1000);
numTries += 1;
}
return gitHubService;
}
/**
* Use GitHub authorization to fetch the contents of a GitHub URL.
*/
function fetchGitHubUrl(gitHubUrl) {
if (fetchedUrls.hasOwnProperty(gitHubUrl)) {
return fetchedUrls[gitHubUrl];
}
gitHubUrlParts = gitHubUrl.split("/");
gitHubIndex = gitHubUrlParts.indexOf("github.com");
orgName = gitHubUrlParts[gitHubIndex + 1];
repoName = gitHubUrlParts[gitHubIndex + 2];
blobIndex = gitHubUrlParts.indexOf("blob");
branch = gitHubUrlParts[blobIndex + 1];
path = gitHubUrlParts.slice(blobIndex + 2).join("/");
var url = "https://api.github.com/repos/" + orgName + "/" + repoName + "/contents/" +
path + "?ref=" + branch;
try {
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: "Bearer " + accessToken,
Accept: "application/vnd.github.v3.raw"
}
});
}
catch (e) {
errors.push(e.message);
return null;
}
var contentText = response.getContentText();
fetchedUrls[gitHubUrl] = contentText;
return contentText;
}
function getAllChangingHyperObjects() {
var links = getAllHyperLinks();
var changingHyperObjects = {};
_.each(links, function(link) {
var url = link.url;
var realUrl = url.split("?hyper")[0]
var response = null;
if (realUrl.indexOf("https://github.com") === 0) {
response = fetchGitHubUrl(realUrl);
}
else {
response = UrlFetchApp.fetch(realUrl).getContentText();
}
function pushError(link, key, label) {
errors.push("Not found in response: " + label + " (" + link.url + ")");
if (!changingHyperObjects.hasOwnProperty(link.url)) {
changingHyperObjects[link.url] = [];
}
changingHyperObjects[link.url].push({"value": "[Not Found]", "to_text": true, "link": link, "image_size": null, "key": key});
}
if (response !== null) {
if (url.indexOf("?hyper=") !== -1) {
var key = url.split("?hyper=")[1]
var responseKey = "{{{" + key + ":";
var responseKeyIndex = response.indexOf(responseKey);
if (responseKeyIndex !== -1) {
var responsePartial = response.substr(responseKeyIndex);
var responsePartialKeyEndIndex = responseKey.length;
var responsePartialValueEndIndex = responsePartial.indexOf("}}}");
var responseValue = responsePartial.substr(
responsePartialKeyEndIndex,
responsePartialValueEndIndex - responsePartialKeyEndIndex);
if (link.isText) {
// This is called before the links are chopped up. (And after, though the links will have different start/end offsets then.)
if (link.element.getText().slice(link.startOffset, link.endOffsetInclusive + 1) !== responseValue) {
if (!changingHyperObjects.hasOwnProperty(link.url)) {
changingHyperObjects[link.url] = [];
}
changingHyperObjects[link.url].push({"value": responseValue, "to_text": true, "link": link, "image_size": null, "key": key});
}
}
}
else {
var keyAndSize = url.split("?hyper=")[1]
var keyAndSizeSplit = keyAndSize.split("&size=")
var key = keyAndSizeSplit[0]
var imageSize = null;
if (keyAndSizeSplit.length > 1) {
imageSize = keyAndSizeSplit[1]
}
var label = "{{{" + key + "}}}";
var responseKeyIndex = response.indexOf(label);
if (responseKeyIndex != -1) {
var responseJSON = JSON.parse(response);
var labelToImage = getAllResponseImages(response, url);
if (labelToImage !== null && labelToImage.hasOwnProperty(label)) {
var responseValue = labelToImage[label];
if (!changingHyperObjects.hasOwnProperty(link.url)) {
changingHyperObjects[link.url] = [];
}
changingHyperObjects[link.url].push({"value": responseValue, "to_text": false, "link": link, "image_size": imageSize, "key": key});
}
else {
pushError(link, key, label);
}
}
else {
pushError(link, key, label);
}
}
}
}
});
return changingHyperObjects;
}
function getAllResponseImages(response, url) {
if (responseImages.hasOwnProperty(response)) {
return responseImages[response];
}
var responseJSON = JSON.parse(response);
var cells = responseJSON.cells;
var labelsToImages = {};
var labels = [];
var lastLabel = null;
var lastImage = null;
_.each(cells, function(cell) {
var outputs = cell.outputs;
_.each(outputs, function(output) {
// TODO: Use better contains.
if ("data" in output) {
if ("image/png" in output["data"]) {
var decoded = Utilities.base64Decode(output["data"]["image/png"]);
var blob = Utilities.newBlob(decoded);
if (lastLabel !== null) {
labelsToImages[lastLabel] = blob;
lastLabel = null;
}
}
}
if ("text" in output) {
var text = output["text"];
_.each(text, function(line) {
var line = String(line);
if (line.indexOf("{{{") !== -1 && line.indexOf("}}}") !== -1) {
var startLabelIndex = line.indexOf("{{{");
var endLabelIndex = line.indexOf("}}}");
var label = line.slice(startLabelIndex, endLabelIndex + "}}}".length);
// Don't include non-image hyper labels
if (label.indexOf(":") === -1) {
labels.push(label);
lastLabel = label;
}
}
});
}
});
});
responseImages[response] = labelsToImages;
if (_.size(labels) !== _.size(labelsToImages)) {
var labelsNotInMap = _.difference(labels, _.keys(labelsToImages));
errors.push("Not every label (" + labelsNotInMap +
") corresponds to an image in: " + url);
return null;
}
return labelsToImages;
}
if (onlyGrabUrls === true) {
var realUrls = {};
var links = getAllHyperLinks();
_.each(links, function(link) {
var url = link.url;
var realUrl = url.split("?hyper")[0];
realUrls[realUrl] = true;
});
return _.keys(realUrls);
}
// Split all links into their own text elements
var changingHyperObjects = getAllChangingHyperObjects();
if (onlyCheckForTextChanges === true) {
var changingTexts = [];
_.each(changingHyperObjects, function(hyperObjects) {
_.each(hyperObjects, function(hyperObject) {
var responseValue = hyperObject.value;
var toText = hyperObject.to_text;
if (toText == true) {
var link = hyperObject.link;
var linkElement = link.element;
var oldText = linkElement.getText().slice(link.startOffset, link.endOffsetInclusive + 1)
if (oldText !== responseValue) {
changingTexts.push("Text [" + oldText + "] (" + hyperObject.key +
") will change to [" + responseValue + "] for " +
link.url);
}
}
});
});
return _.uniq(changingTexts);
}
var textElements = getAllTextElements();
_.each(textElements, function(textElement) {
var links = getLinksFromText(textElement);
var changingLinksFromText = [];
_.each(links, function(link) {
if (changingHyperObjects.hasOwnProperty(link.url)) {
changingLinksFromText.push(link);
}
});
if (changingLinksFromText.length > 0) {
var newTextElements = splitTextByLinks(textElement, changingLinksFromText);
var parentElement = textElement.getParent();
var textElementIndex = parentElement.getChildIndex(textElement);
parentElement.removeChild(textElement);
var i = 0;
_.each(newTextElements, function(newTextElement) {
if (newTextElement.getText().length > 0) {
parentElement.insertText(textElementIndex + i, newTextElement);
i += 1;
}
});
}
});
// We must re-run to get the split elements.
changingHyperObjects = getAllChangingHyperObjects();
_.each(_.allKeys(changingHyperObjects), function(url) {
var objectsForUrl = changingHyperObjects[url];
_.each(objectsForUrl, function(hyperObject) {
var responseValue = hyperObject.value;
var toText = hyperObject.to_text;
var link = hyperObject.link;
var linkElement = link.element;
var parentElement = linkElement.getParent();
var elementIndex = parentElement.getChildIndex(linkElement);
if (toText === true) {
parentElement.removeChild(linkElement);
parentElement.insertText(elementIndex, responseValue);
var newElement = parentElement.getChild(elementIndex);
newElement.setLinkUrl(link.url);
newElement.setUnderline(false);
newElement.setForegroundColor(HYPERIZED_COLOR);
}
// No hyper text label found? Look for images!
else {
var imageSize = hyperObject.image_size;
parentElement.removeChild(linkElement);
parentElement.insertInlineImage(elementIndex, responseValue);
var newElement = parentElement.getChild(elementIndex);
newElement.setLinkUrl(link.url);
// Resize if a ratio is specified.
if (imageSize !== null) {
var curWidth = newElement.getWidth();
var curHeight = newElement.getHeight();
newElement.setWidth(curWidth * imageSize);
newElement.setHeight(curHeight * imageSize);
}
}
});
});
// Some links didn't change, but color them anyway.
showHyperElements();
if (errors.length > 0) {
DocumentApp.getUi().alert("Errors encountered: \n" + _.uniq(errors).join("\n"));
}
}
function splitTextByLinks(textElement, links) {
var newTextElements = [];
// Example starting points
// | | |
// abc[link1]cde[link2]hij
var startingPoint = 0;
var textLength = textElement.getText().length;
_.each(links, function(link) {
// abc, cde
var newTextElement = textElement.copy();
if (startingPoint > 0) {
newTextElement.deleteText(0, startingPoint - 1);
}
newTextElement.deleteText(link.startOffset - startingPoint, textLength - 1 - startingPoint);
newTextElements.push(newTextElement);
startingPoint += newTextElement.getText().length;
// link1, link2
newTextElement = textElement.copy();
if (link.endOffsetInclusive + 1 - link.startOffset <= textLength - 1 - link.startOffset) {
if (link.startOffset > 0) {
newTextElement.deleteText(0, link.startOffset - 1);
}
newTextElement.deleteText(link.endOffsetInclusive + 1 - link.startOffset, textLength - 1 - link.startOffset);
newTextElements.push(newTextElement);
startingPoint += newTextElement.getText().length;
}
});
// hij
newTextElement = textElement.copy();
if (startingPoint > 0 && startingPoint < textLength) {
newTextElement.deleteText(0, startingPoint - 1);
}
newTextElements.push(newTextElement);
return newTextElements;
}
/**
* Returns a list of all hyper elements.
*/
function getAllHyperLinks() {
var links = getAllLinks();
var hyperLinks = [];
for (var i = 0; i < links.length; i++) {
var link = links[i];
var url = link.url;
if (url.indexOf("?hyper") != -1) {
hyperLinks.push(link);
}
}
return hyperLinks;
}
function getAllFigureOrderLinks(element) {
var element = element || DocumentApp.getActiveDocument().getBody();
var links = getAllLinks(element);
var figOrderLinks = [];
for (var i = 0; i < links.length; i++) {
var link = links[i];
var url = link.url;
if (url.indexOf("?figorder") != -1) {
figOrderLinks.push(link);
}
}
return figOrderLinks;
}
function getAllTextElements(element) {
var textElements = [];
var element = element || DocumentApp.getActiveDocument().getBody();
if (element.getType() === DocumentApp.ElementType.TEXT) {
textElements.push(element);
}
else {
var numChildren = 0;
try {
numChildren = element.getNumChildren();
}
catch (e) {}
for (var i = 0; i < numChildren; i++) {
textElements = textElements.concat(getAllTextElements(element.getChild(i)));
}
}
return textElements;
}
function getLinksFromText(element) {
var links = [];
var textObj = element.editAsText();
var text = element.getText();
var inUrl = false;
var url = null;
var curUrl = null;
for (var ch = 0; ch < text.length; ch++) {
url = textObj.getLinkUrl(ch);
if (url != null) {
if (!inUrl) {
inUrl = true;
curUrl = {};
curUrl.isText = true;
curUrl.element = element;
curUrl.url = String(url);
curUrl.startOffset = ch;
}
else {
curUrl.endOffsetInclusive = ch;
}
}
else {
if (inUrl) {
inUrl = false;
if (curUrl.endOffsetInclusive === undefined) {
curUrl.endOffsetInclusive = curUrl.startOffset;
}
links.push(curUrl);
curUrl = {};
}
}
}
// Needed for the case when no non-link character comes after a link.
if (url != null && curUrl != null) {
inUrl = false;
if (curUrl.endOffsetInclusive === undefined) {
curUrl.endOffsetInclusive = curUrl.startOffset;
}
links.push(curUrl);
curUrl = {};
}
return links;
}
/**
* Get all links from the document, which is tricky because links might be only
* a portion of an element.
*
* Mostly taken from https://gist.github.com/mogsdad/6518632#file-getalllinks-js.
* Added a bug fix to the case where no non-link character follows a link.
*/
function getAllLinks(element) {
var links = [];
element = element || DocumentApp.getActiveDocument().getBody();
if (element.getType() === DocumentApp.ElementType.TEXT) {
links = links.concat(getLinksFromText(element));
}
else if (element.getType() === DocumentApp.ElementType.INLINE_IMAGE) {
url = element.getLinkUrl();
if (url != null) {
curUrl = {};
curUrl.isText = false;
curUrl.element = element;
curUrl.url = String(url);
links.push(curUrl);
}
}
else {
var numChildren = 0;
try {
numChildren = element.getNumChildren();
}
catch (e) {}
for (var i = 0; i < numChildren; i++) {
links = links.concat(getAllLinks(element.getChild(i)));
}
}
return links;
}
/**
* Configures the GitHub authorization service.
*/
function getGitHubServiceWithProperties(scriptProperties, userProperties) {
var gitHubClientId = scriptProperties.getProperty("GITHUB_CLIENT_ID");
var gitHubClientSecret = scriptProperties.getProperty("GITHUB_CLIENT_SECRET");
return OAuth2.createService("GitHub")
.setAuthorizationBaseUrl("https://github.com/login/oauth/authorize")
.setTokenUrl("https://github.com/login/oauth/access_token")
.setClientId(gitHubClientId)
.setClientSecret(gitHubClientSecret)
.setCallbackFunction("authCallback")
.setPropertyStore(userProperties)
.setScope("repo") // Need access to code
.setParam("allow_signup", true);
}
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
var userProperties = PropertiesService.getUserProperties();
var scriptProperties = PropertiesService.getScriptProperties();
var service = getGitHubServiceWithProperties(scriptProperties, userProperties);
service.reset();
}
/**
* Handles the GitHub OAuth callback.
*/
function authCallback(request) {
var userProperties = PropertiesService.getUserProperties();
var scriptProperties = PropertiesService.getScriptProperties();
var service = getGitHubServiceWithProperties(scriptProperties, userProperties);
var authorized = service.handleCallback(request);
var template = HtmlService.createTemplateFromFile("github_oauth_callback");
var callbackMessage = template.callbackMessage = authorized ?
"GitHub authorization complete. You may return to the document." :
"GitHub authorization was not successful.";
template.callbackMessage = callbackMessage;
var page = template.evaluate();
return page;
}