-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcompetencySyncScript.gs
635 lines (544 loc) · 22.1 KB
/
competencySyncScript.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
//Competency Sync Script
//////////////////////////////////////////////////////////////////////////////////////////////
//Config:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//Get config values from spreadsheet:
var personalAccessToken = readFromCell(sheet, 2, 1);
var emailAddress = readFromCell(sheet, 4, 1);
var repoName = readFromCell(sheet, 6, 1);
var driveFolderNames = getRowContents(8, 1);
var githubFolderNames = getRowContents(10, 1);
var excludedFileNames = getRowContents(12, 1);
var branchName = readFromCell(sheet, 14, 1);
var wordToExclude = "Private";
//////////////////////////////////////////////////////////////////////////////////////////////
//Main:
function main() {
var errors = [];
//var githubRepoFilesDataUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/contents?ref=master&access_token=" + personalAccessToken;
//Creates branch if none exists with config name:
if (branchExists(branchName) == false) {
createBranch(branchName);
}
for (n=0; n<driveFolderNames.length; n++) {
var driveFolderId = DriveApp.getFoldersByName(driveFolderNames[n]).next().getId();
var driveFiles = getDriveFiles(driveFolderId);
var githubRepoFilesDataUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/contents/" + githubFolderNames[n] + "?ref=master&access_token=" + personalAccessToken;
var githubRepoFilesData = UrlFetchApp.fetch(githubRepoFilesDataUrl);
githubRepoFilesData = JSON.parse(githubRepoFilesData);
//Get names of all files in GitHub repo:
var githubFileNames = [];
for (githubFilesIndex=0; githubFilesIndex<githubRepoFilesData.length; githubFilesIndex++) {
githubFileNames[githubFilesIndex] = githubRepoFilesData[githubFilesIndex].name;
}
//Commit files to GitHub:
for (driveFilesIndex=0; driveFilesIndex<driveFiles.length; driveFilesIndex++) {
//Get data from object:
var driveFileName = driveFiles[driveFilesIndex].name;
var driveFileNameWithoutSpaces = driveFiles[driveFilesIndex].nameWithoutSpaces;
var driveFileNameWithExtNoSpaces = driveFileNameWithoutSpaces + ".md";
var driveFileContent = driveFiles[driveFilesIndex].content;
//Create file if none exists:
if (githubFileNames.indexOf(driveFileNameWithExtNoSpaces) == -1) { //file does not exist in GitHub already
createFile(driveFileName, driveFileNameWithoutSpaces, driveFileNameWithExtNoSpaces, driveFileContent, githubFolderNames[n], errors); //create new file in GitHub repo
}
createCommit(driveFileName, driveFileNameWithoutSpaces, driveFileNameWithExtNoSpaces, driveFileContent, githubFolderNames[n]); //commit Drive file changes to GitHub
}
}
createPullRequest(errors);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//Functions:
function getDriveFiles(driveFolderId) {
var driveFiles = [], driveFileIndex = 0;
//var folder = DriveApp.getFoldersByName(driveFolderName); //get folder as object from Drive
var folder = DriveApp.getFolderById(driveFolderId);
//var files = folder.next().getFiles(); //get files from folder
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fileType = file.getMimeType().toString();
var fileName = file.getName(); //get the file name
var fileSrc = DocumentApp.openById(file.getId()); //get document as Document object
var titleElement = getTitleElementOfDoc(file);
var textToCheck = fileName + titleElement;
if (isInRow(fileName, excludedFileNames) == false && substrFoundCaseInsensitive(wordToExclude, textToCheck) == false) { //fileName is allowed by config
var fileNameWithoutSpaces = removeSpacesFromStr(fileName);
var fileTextMarkdown = convertToMarkdown(fileSrc);
//Store file data in object
var fileObject = {
"name" : fileName,
"nameWithoutSpaces" : fileNameWithoutSpaces,
"content" : fileTextMarkdown
};
driveFiles[driveFileIndex] = fileObject;
driveFileIndex ++;
}
}
return driveFiles; //return array of fileObjects
}
function createBranch(branchName) {
branchName.toString();
var masterBranchUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/branches/master?access_token=" + personalAccessToken;
var masterBranchData = UrlFetchApp.fetch(masterBranchUrl);
masterBranchData = JSON.parse(masterBranchData);
//Get sha for master branch:
var masterBranchSha = masterBranchData.commit.sha;
//Create branch:
var refUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/git/refs?access_token=" + personalAccessToken;
var content = {
"ref" : "refs/heads/" + branchName,
"sha" : masterBranchSha
};
var options = createOptions("post", "application/json", content);
try {
UrlFetchApp.fetch(refUrl, options);
}
catch (err) {
Logger.log("Branch already exists with name " + branchName);
}
}
function createFile(fileName, fileNameWithoutSpaces, fileNameWithExtNoSpaces, fileContent, githubFolderName, errors) {
if (githubFolderName != "") {var path = githubFolderName + "/" + fileNameWithExtNoSpaces;}
else {var path = fileNameWithExtNoSpaces;}
var fileDataUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/contents/" + path + "?access_token=" + personalAccessToken;
var commitMessage = "Create file " + fileNameWithoutSpaces;
var fileContentEncoded = encodeBase64Str(fileContent);
var content = {
"path" : path,
"message" : commitMessage,
"content" : fileContentEncoded,
"branch" : branchName
};
var options = createOptions("put", "application/json", content);
try {
UrlFetchApp.fetch(fileDataUrl, options); //This is where I get the error when the file already exists
}
catch (err) {
Logger.log("File " + fileNameWithoutSpaces + " could not be created. " + err);
errors.push("The script found an error with this file: " + githubFolderName + "/" + fileName + ". This is likely due to a duplicate file.");
}
}
function createCommit(fileName, fileNameWithoutSpaces, fileNameWithExtNoSpaces, fileContent, githubFolderName) {
//Create data:
var commitMessage = "Update " + fileNameWithoutSpaces;
if (githubFolderName != "") {var path = githubFolderName + "/" + fileNameWithExtNoSpaces;}
else {var path = fileNameWithExtNoSpaces;}
var fileContent = encodeBase64Str(fileContent);
var commitUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/contents/" + path +"?ref=" + branchName + "&access_token=" + personalAccessToken;
var fileData = UrlFetchApp.fetch(commitUrl);
fileData = JSON.parse(fileData);
var sha = fileData.sha;
var content = {
"message" : commitMessage,
"content" : fileContent,
"sha" : sha,
"branch" : branchName
};
var options = createOptions("put", "application/json", content);
try {
UrlFetchApp.fetch(commitUrl, options);
}
catch (err) {
Logger.log("Nothing to commit to the file " + fileName); //I don't think this would cause an error actually?
}
}
function createPullRequest(errors) {
//This function will take all the commits that have been made to a branch and create a pr to merge them to master
var title = "Update Google Drive Competency documents";
var base = "master";
var body = "The Competency Sync Google script would like to update files in this repo. \n\n"; //body is a comment/message left in the pr
if (errors.length > 0) {
body += "The script encountered the following errors: \n";
for (var i=0; i<errors.length; i++) {
body += errors[i] + "\n";
}
}
var url = "https://api.github.com/repos/sendwithus/" + repoName + "/pulls?access_token=" + personalAccessToken;
var content = {
"title" : title,
"head" : branchName,
"base" : base,
"body" : body
};
var options = createOptions("post", "application/json", content);
try {
UrlFetchApp.fetch(url, options);
sendPREmail();
}
catch (err) {
Logger.log("Pull request already open for this branch or nothing to merge.");
var pullsUrl = "https://github.com/sendwithus/competencies/pulls";
sendErrorEmail("Competency Sync would like to commit changes in GitHub but a pull request is already open. Please merge the pull request in order to sync latest changes. " + pullsUrl);
}
}
function branchExists(branchNameToCheck) {
//This function checks if a branch exists in a GitHub repo
branchNameToCheck = branchNameToCheck.toString();
var url = "https://api.github.com/repos/sendwithus/" + repoName + "/branches?access_token=" + personalAccessToken;
var branchData = UrlFetchApp.fetch(url);
var branches = JSON.parse(branchData);
for (branchIndex=0; branchIndex<branches.length; branchIndex++) {
if (branches[branchIndex].name == branchNameToCheck) {
return true;
}
}
return false;
}
function createOptions(method, contentType, content) {
//This function returns an object based on params that customizes an HTTPS request
var options = {
"method" : method,
"contentType" : contentType,
"payload" : JSON.stringify(content)
};
return options;
}
function sendPREmail() {
//This function sends an email to the config address with a link to the pr
//get data on pr's for repo:
var pullDataUrl = "https://api.github.com/repos/sendwithus/" + repoName + "/pulls?access_token=" + personalAccessToken;
var pullData = UrlFetchApp.fetch(pullDataUrl);
pullData = JSON.parse(pullData);
var prNumber = pullData[0].number; //get number for latest pr
var prUrl = "https://github.com/sendwithus/" + repoName + "/pull/" + prNumber;
MailApp.sendEmail(emailAddress, "Your pull request is ready to merge Competency Files!", "View pull request on GitHub: " + prUrl);
}
function sendErrorEmail(err) {
MailApp.sendEmail(emailAddress, "Competency Sync Error", "The Competency Sync Google script received the following error: " + err);
}
function getTitleElementOfDoc(file) {
var doc = DocumentApp.openById(file.getId());
var numChildren = doc.getBody().getNumChildren();
for (var i=0; i<numChildren; i++) {
var child = doc.getBody().getChild(i);
if (child.getType() == DocumentApp.ElementType.PARAGRAPH && child.getHeading() == DocumentApp.ParagraphHeading.TITLE) {
return child.asText().getText();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//Sub-functions:
function decodeBase64Str(encodedStr) {
//This function decodes a string that is encoded in Base 64
var decodedStrAsByteArray = Utilities.base64Decode(encodedStr); //returns decoded string as a byte array
var decodedStr = Utilities.newBlob(decodedStrAsByteArray).getDataAsString(); //converts byte array to string
return decodedStr;
}
function encodeBase64Str(str) {
//This function takes a string and encodes it in base 64
var encodedStr = Utilities.base64Encode(str);
return encodedStr;
}
function readFromCell(sheet, row, col) {
//This function gets the value of a specified cell
var range = sheet.getRange(row, col);
var cellValue = range.getDisplayValue(); //gets value of cell as string always, not object if value exists as object
return cellValue;
}
function removeSpacesFromStr(str) {
var strWithoutSpaces = "";
for (i=0; i<str.length; i++) {
if (str[i] == ' ') {
if (str[i+1] != '-' && str[i-1] != '-') {
if (i != 0 && i != str.length) {
strWithoutSpaces += '-';
}
}
}
else if (str[i] == "/") {
strWithoutSpaces += '-'
}
else {
strWithoutSpaces += str[i];
}
}
return strWithoutSpaces.toString();
}
function getRowContents(row, col) {
var rowContents = [], colIndex=0;
while (readFromCell(sheet, row, col) != "") {
rowContents[colIndex] = readFromCell(sheet, row, col);
colIndex ++;
col++;
}
return rowContents;
}
function isInRow(value, rowContents) {
return (rowContents.indexOf(value) !== -1);
}
function isLinkToInternalDoc(url) {
var internalUrls = ["docs.google.com/document", "github.com/sendwithus", "github.com/techdroplabs", "https://youtube.com"];
for (internalUrlsIndex=0; internalUrlsIndex<internalUrls.length; internalUrlsIndex++) {
if (url.indexOf(internalUrls[internalUrlsIndex]) != -1) {
return true;
}
}
return false;
}
function getPath(folderName) {
var folder = DriveApp.getFoldersByName(folderName);
var pathPoints = [], pathPointsIndex = 0, path = "";
while (folderParentName != "My Drive") {
var folderParentName = folder.next().getParents().next().getName();
pathPoints[pathPointsIndex] = folderParentName;
folder = DriveApp.getFoldersByName(folderParentName);
pathPointsIndex ++;
}
for (pathIndex=(pathPoints.length-1); pathIndex>=0; pathIndex--) {
path += "/" + pathPoints[pathIndex].toString();
}
return path;
}
function substrFoundCaseInsensitive(substr, str) {
return (str.toLowerCase().indexOf(substr.toLowerCase()) !== -1);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//Triggers:
function triggers() {
//Trigger to update Competency Docs every week
ScriptApp.newTrigger('main')
.timeBased()
.everyWeeks(1)
.onWeekDay(ScriptApp.WeekDay.FRIDAY)
.create();
}
//////////////////////////////////////////////////////////////////////////////////////////////
//Markdown:
function convertToMarkdown(doc) {
var numChildren = doc.getActiveSection().getNumChildren();
var text = "";
var inSrc = false;
var inClass = false;
var globalImageCounter = 0;
var globalListCounters = {};
// edbacher: added a variable for indent in src <pre> block. Let style sheet do margin.
var srcIndent = "";
var attachments = [];
// Walk through all the child elements of the doc.
for (var i = 0; i < numChildren; i++) {
var child = doc.getActiveSection().getChild(i);
//Remove any links to internal documents:
var childLinkUrl = child.asText().getLinkUrl();
if (childLinkUrl != null) {
if (isLinkToInternalDoc(childLinkUrl)) {
//Remove link
child.asText().editAsText().insertText(0, "").setLinkUrl("");
}
}
var result = processParagraph(i, child, inSrc, globalImageCounter, globalListCounters);
globalImageCounter += (result && result.images) ? result.images.length : 0;
if (result!==null) {
if (result.sourcePretty==="start" && !inSrc) {
inSrc=true;
text+="<pre class=\"prettyprint\">\n";
} else if (result.sourcePretty==="end" && inSrc) {
inSrc=false;
text+="</pre>\n\n";
} else if (result.source==="start" && !inSrc) {
inSrc=true;
text+="<pre>\n";
} else if (result.source==="end" && inSrc) {
inSrc=false;
text+="</pre>\n\n";
} else if (result.inClass==="start" && !inClass) {
inClass=true;
text+="<div class=\""+result.className+"\">\n";
} else if (result.inClass==="end" && inClass) {
inClass=false;
text+="</div>\n\n";
} else if (inClass) {
text+=result.text+"\n\n";
} else if (inSrc) {
text+=(srcIndent+escapeHTML(result.text)+"\n");
} else if (result.text && result.text.length>0) {
text+=result.text+"\n\n";
}
if (result.images && result.images.length>0) {
for (var j=0; j<result.images.length; j++) {
attachments.push( {
"fileName": result.images[j].name,
"mimeType": result.images[j].type,
"content": result.images[j].bytes } );
}
}
} else if (inSrc) { // support empty lines inside source code
text+='\n';
}
}
text = text.replace(new RegExp("’", 'g'), "'");
return text;
//return str.replace(new RegExp(find, 'g'), replace);
}
function escapeHTML(text) {
return text.replace(/</g, '<').replace(/>/g, '>');
}
// Process each child element (not just paragraphs).
function processParagraph(index, element, inSrc, imageCounter, listCounters) {
// First, check for things that require no processing.
if (element.getNumChildren()==0) {
return null;
}
// Punt on TOC.
if (element.getType() === DocumentApp.ElementType.TABLE_OF_CONTENTS) {
return {"text": "[[TOC]]"};
}
// Set up for real results.
var result = {};
var pOut = "";
var textElements = [];
var imagePrefix = "image_";
// Handle Table elements. Pretty simple-minded now, but works for simple tables.
// Note that Markdown does not process within block-level HTML, so it probably
// doesn't make sense to add markup within tables.
if (element.getType() === DocumentApp.ElementType.TABLE) {
textElements.push("<table>\n");
var nCols = element.getChild(0).getNumCells();
for (var i = 0; i < element.getNumChildren(); i++) {
textElements.push(" <tr>\n");
// process this row
for (var j = 0; j < nCols; j++) {
textElements.push(" <td>" + element.getChild(i).getChild(j).getText() + "</td>\n");
}
textElements.push(" </tr>\n");
}
textElements.push("</table>\n");
}
// Process various types (ElementType).
for (var i = 0; i < element.getNumChildren(); i++) {
var t=element.getChild(i).getType();
if (t === DocumentApp.ElementType.TABLE_ROW) {
// do nothing: already handled TABLE_ROW
} else if (t === DocumentApp.ElementType.TEXT) {
var txt=element.getChild(i);
pOut += txt.getText();
textElements.push(txt);
} else if (t === DocumentApp.ElementType.INLINE_IMAGE) {
//We deleted this!
} else if (t === DocumentApp.ElementType.PAGE_BREAK) {
// ignore
} else if (t === DocumentApp.ElementType.HORIZONTAL_RULE) {
textElements.push('* * *\n');
} else if (t === DocumentApp.ElementType.FOOTNOTE) {
textElements.push(' (NOTE: '+element.getChild(i).getFootnoteContents().getText()+')');
} else {
throw "Paragraph "+index+" of type "+element.getType()+" has an unsupported child: "
+t+" "+(element.getChild(i)["getText"] ? element.getChild(i).getText():'')+" index="+index;
}
}
if (textElements.length==0) {
// Isn't result empty now?
return result;
}
// evb: Add source pretty too. (And abbreviations: src and srcp.)
// process source code block:
if (/^\s*---\s+srcp\s*$/.test(pOut) || /^\s*---\s+source pretty\s*$/.test(pOut)) {
result.sourcePretty = "start";
} else if (/^\s*---\s+src\s*$/.test(pOut) || /^\s*---\s+source code\s*$/.test(pOut)) {
result.source = "start";
} else if (/^\s*---\s+class\s+([^ ]+)\s*$/.test(pOut)) {
result.inClass = "start";
result.className = RegExp.$1;
} else if (/^\s*---\s*$/.test(pOut)) {
result.source = "end";
result.sourcePretty = "end";
result.inClass = "end";
} else {
prefix = findPrefix(inSrc, element, listCounters);
var pOut = "";
for (var i=0; i<textElements.length; i++) {
pOut += processTextElement(inSrc, textElements[i]);
}
// replace Unicode quotation marks
pOut = pOut.replace('\u201d', '"').replace('\u201c', '"');
result.text = prefix+pOut;
}
return result;
}
// Add correct prefix to list items.
function findPrefix(inSrc, element, listCounters) {
var prefix="";
if (!inSrc) {
if (element.getType()===DocumentApp.ElementType.PARAGRAPH) {
var paragraphObj = element;
switch (paragraphObj.getHeading()) {
// Add a # for each heading level. No break, so we accumulate the right number.
case DocumentApp.ParagraphHeading.HEADING6: prefix+="#";
case DocumentApp.ParagraphHeading.HEADING5: prefix+="#";
case DocumentApp.ParagraphHeading.HEADING4: prefix+="#";
case DocumentApp.ParagraphHeading.HEADING3: prefix+="#";
case DocumentApp.ParagraphHeading.HEADING2: prefix+="#";
case DocumentApp.ParagraphHeading.HEADING1: prefix+="#";
case DocumentApp.ParagraphHeading.TITLE: prefix+="# ";
default:
}
} else if (element.getType()===DocumentApp.ElementType.LIST_ITEM) {
var listItem = element;
var nesting = listItem.getNestingLevel()
for (var i=0; i<nesting; i++) {
prefix += " ";
}
var gt = listItem.getGlyphType();
// Bullet list (<ul>):
if (gt === DocumentApp.GlyphType.BULLET
|| gt === DocumentApp.GlyphType.HOLLOW_BULLET
|| gt === DocumentApp.GlyphType.SQUARE_BULLET) {
prefix += "* ";
} else {
// Ordered list (<ol>):
var key = listItem.getListId() + '.' + listItem.getNestingLevel();
var counter = listCounters[key] || 0;
counter++;
listCounters[key] = counter;
prefix += counter+". ";
}
}
}
return prefix;
}
function processTextElement(inSrc, txt) {
if (typeof(txt) === 'string') {
return txt;
}
var pOut = txt.getText();
if (! txt.getTextAttributeIndices) {
return pOut;
}
var attrs=txt.getTextAttributeIndices();
var lastOff=pOut.length;
for (var i=attrs.length-1; i>=0; i--) {
var off=attrs[i];
var url=txt.getLinkUrl(off);
var font=txt.getFontFamily(off);
if (url) { // start of link
if (i>=1 && attrs[i-1]==off-1 && txt.getLinkUrl(attrs[i-1])===url) {
// detect links that are in multiple pieces because of errors on formatting:
i-=1;
off=attrs[i];
url=txt.getLinkUrl(off);
}
pOut=pOut.substring(0, off)+'['+pOut.substring(off, lastOff)+']('+url+')'+pOut.substring(lastOff);
} else if (font) {
if (!inSrc && font===font.COURIER_NEW) {
while (i>=1 && txt.getFontFamily(attrs[i-1]) && txt.getFontFamily(attrs[i-1])===font.COURIER_NEW) {
// detect fonts that are in multiple pieces because of errors on formatting:
i-=1;
off=attrs[i];
}
pOut=pOut.substring(0, off)+'`'+pOut.substring(off, lastOff)+'`'+pOut.substring(lastOff);
}
}
if (txt.isBold(off)) {
var d1 = d2 = "**";
if (txt.isItalic(off)) {
// edbacher: changed this to handle bold italic properly.
d1 = "**_"; d2 = "_**";
}
pOut=pOut.substring(0, off)+d1+pOut.substring(off, lastOff)+d2+pOut.substring(lastOff);
} else if (txt.isItalic(off)) {
pOut=pOut.substring(0, off)+'*'+pOut.substring(off, lastOff)+'*'+pOut.substring(lastOff);
}
lastOff=off;
}
return pOut;
}