-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.js
131 lines (124 loc) · 5.27 KB
/
regexp.js
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
// numberHeadings uses the function
function singleReplace(re,str,isRegExp,useJS,flags) {
singleReplacePartial(re,str,isRegExp,useJS,flags,true,true);
};
// singleReplace use the function
function singleReplacePartial(re,str,isRegExp,useJS,flags,bodyFlag,fnFlag) {
// isRegExp: Is the string passed in a regexp or a literal string?
// useJS:
// Seems that the find function doesn't take a regexp, but just a string...
// https://developers.google.com/apps-script/reference/document/text#replaceText(String,String)
// Therefore, you can switch on useJS, to use the JS engine. Because this operates on text, you would lose formatting.
// Hence, the expressing is 'padded', see repad below, and only the matching text is retained. Then GAS replace is used.
// useJS may have unexpected side effects and should only be used if you require features that are not in re2 but are in JS (suchas '$1')
if (!isRegExp) {
re = re.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
// re = re.replace(/[\|\\\{\}\(\)\[\]^$\+\*\?\.]/g, '\\$&');
};
//var reg = new RegExp(re);
// string - for use in findtext and replacetext
var regu = re;
// regexp - for use in replace
var regexp = null;
// why this?
// var repad = ".*?(?:"+re+").$";
var repad = re;
if (flags) {
regexp = new RegExp(repad, flags);
} else {
regexp = new RegExp(repad);
};
// DocumentApp.getUi().alert("Search: " + regu.toString() );
/*
var bodyElement = DocumentApp.getActiveDocument().getBody();
*/
var p;
try {
p = getParagraphsInBodyAndFootnotesExtended(false, bodyFlag, fnFlag);
} catch (e) {
alert('Error in singleReplacePartial calling getParagraphsInBodyAndFootnotesExtended: ' + e);
};
if (p) {
for (var i=0; i < p.length; i++) {
var bodyElement = p[i];
var rangeElement = bodyElement.findText(regu);
var replacement;
while (rangeElement !== null) {
//DocumentApp.getUi().alert("Found" );
var thisElement = rangeElement.getElement();
//var thisElementText = thisElement.asText();
// work-around to be able to use JS regexp engine
// https://stackoverflow.com/questions/30968419/replacetext-regex-not-followed-by/33528920#33528920
if (useJS) {
alert("useJS not working");
/*
// Determine the input text:
var mytext = rangeElement.getElement().getText();
var elem = rangeElement.getElement().copy().editAsText();
var elemlength = elem.getText().length;
if (rangeElement.isPartial()) {
if (rangeElement.getEndOffsetInclusive()+1 < elemlength-1) {
elem.deleteText(rangeElement.getEndOffsetInclusive()+1,elemlength-1);
}
if (rangeElement.getStartOffset()-1>0) {
elem.deleteText(0,rangeElement.getStartOffset()-1);
};
elemlength = elem.getText().length;
};
var url = null;
// var text = elem.getText();
alert(regexp+ "\n" + str + "\n" + thisElement.getText() + "\n" + elem.getText());
replacement = thisElement.getText().replace(regexp,str);
var eat = rangeElement.getElement().editAsText();
eat.deleteText(rangeElement.getStartOffset(),rangeElement.getEndOffsetInclusive());
eat.insertText(rangeElement.getStartOffset(),replacement);
*/
alert(regexp+ "\n" + str + "\n" + thisElement.getText());
replacement = thisElement.getText().replace(regexp,str);
} else {
replacement = str;
}
thisElement = thisElement.replaceText(regu, replacement);
//.setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(),backgroundColor);
// search for next match
rangeElement = bodyElement.findText(regu, rangeElement);
};
};
} else {
alert("singleReplacePartial: No paragraphs found.");
};
}
// minifyParaSenMarker, maxifyParaSenMarker use the function
function regexpRestyle(target,mystyle,elementsIn) {
regexpRestyleOffset(target,mystyle,0,0,elementsIn);
};
// numberHeadings, regexpRestyle use the function
function regexpRestyleOffset(target,mystyle,startOff,endOff,elementsIn) {
// If no search parameter was provided, ask for one
if (arguments.length == 0) {
var ui = DocumentApp.getUi();
var result = ui.prompt('Text Highlighter',
'Enter text to highlight:', ui.ButtonSet.OK_CANCEL);
// Exit if user hit Cancel.
if (result.getSelectedButton() !== ui.Button.OK) return;
// else
target = result.getResponseText();
}
var elements = [];
if (!elementsIn) {
elements = getParagraphsInBodyAndFootnotesExtended(false,true,true);
} else {
elements = elementsIn;
};
for (var i=0; i<elements.length; i++) {
var thisEl = elements[i];
var searchResult = thisEl.findText(target);
while (searchResult !== null) {
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText();
// thisElementText.setAttributes(mystyle);
thisElementText.setAttributes(searchResult.getStartOffset()+startOff, searchResult.getEndOffsetInclusive()+endOff,mystyle);
searchResult = thisEl.findText(target, searchResult);
};
}
}