forked from mangini/gdocs2md
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverttomarkdown.js
772 lines (644 loc) · 28 KB
/
converttomarkdown.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
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
/*
gdocs2md
========
A simple Google Apps script to convert a properly formatted Google Drive Document to the markdown (.md) format.
## Usage
### Adding to a document
This script must be added to a document to run it - the document doesn't need to be the one you wish to export:
1. Tools > Script Editor > New
2. Select "Blank Project", then paste the code from converttomarkdown.js into it and save.
### Preparing files
Put all documents you wish to convert into category folders within a folder in your docs list, called "DocsToConvertToMarkdown". Each category folder can contain multiple documents. You can use just one category folder, e.g. "default" if you don't want to use categories.
### Running the script
1. Tools > Script Manager
2. Select "convertDefaultFolder()" function.
3. Click Run button.
4. All documents in the folder will be converted to markdown, and resulting files put in a new subfolder called "ExportedMarkdown-TIMESTAMP" where TIMESTAMP is the current ISO GMT timestamp. Note that during conversion, the export folder will be named "ExportedMarkdown-IN-PROGRESS" - see notes below in Miscellaneous section on resuming.
### Settings
There are several optional features, convertDefaultFolder() runs with most features enabled, however most other functions take a settings object. The following fields should be set to true to enable the corresponding features:
* **markdownTables** - Output tables in GitHub-Flavoured Markdown rather than HTML. This also supports bold and italic formatting, and inline code blocks, within table cells.
* **plainTextOutput** - Output markdown to plaintext files (type "text/plain" with .txt extension),allowing for viewing directly in Google Docs. If this is not specified, files will be have type "text/x-markdown" and .md extension.
* **boldItalicIsQuote** - Any lines starting with bold+italic formatting will become block quotes in the markdown output.
* **codeBlocks** - Any lines starting with a tab and then containing at least one courier-new formatted character will be considered as code lines. In addition, any empty or whitespace-only lines following a code line will be considered to be code lines. Runs of code lines will be output with three backticks before and after the run, and with the first tab (if any) removed from each line, to form a standard markdown code block.
### Using different folders
You can call convertFolderByName(folderName, settings) with a different folder name as required
### Miscellaneous
* There are also functions for emailing documents, see source for details.
* You may find that for large sets of documents, the script will time out. In this case, please restart the script and it will attempt to resume the conversion from where it reached. Repeatedly running the script should allow it to complete. Files named "EXPORT-COMPLETE.txt" are used to track progress to allow resuming - these can safely be deleted or ignored after export is complete.
## Interpreted formats
* Text:
* paragraphs are separated by two newlines
* text styled as heading 1, 2, 3, etc is converted to Markdown heading: #, ##, ###, etc
* text formatted with Courier New is backquoted: ``text``
* links are converted to MD format: `[anchortext](url)`
* Lists:
* Numbered lists are converted correctly, including nested lists
* bullet lists are converted to "`*`" Markdown format appropriately, including nested lists
* Images:
* images are correctly extracted and sent as attachments
* Blocks:
* Table of contents is replaced by `[[TOC]]`
* blocks of text delimited by "--- class whateverclassnameyouwant" and "---" are converted to `<div class="whateverclassnameyouwant"></div>`
* Source code:
* **UPDATED**: blocks of text delimited by "--- source code" or "--- src" and "---" are converted to `<pre></pre>`
* **NEW**: blocks of text delimited by "--- source pretty" or "--- srcp" and "---" are converted to `<pre class="prettyprint"></pre>`
* Tables:
* **NEW**: Simple `<table>` processing
* "--- jsperf `<testID>`" is replaced by an iframe that shows an interactive chart of a JSPerf test. The `<testID>` is the last part of the URL of the Browserscope anchor in your JSPerf test. Something like `"agt1YS1wcm9maWxlcnINCxIEVGVzdBjlm_EQDA"` in the URL `http://www.browserscope.org/user/tests/table/agt1YS1wcm9maWxlcnINCxIEVGVzdBjlm_EQDA`
## CONTRIBUTORS
* Renato Mangini - [G+](//google.com/+renatomangini) - [Github](//github.com/mangini)
* Ed Bacher - [G+](//plus.google.com/106923847899206957842) - [Github](//github.com/evbacher)
* Ben Webster - [Github](//github.com/trepidacious)
## LICENSE
Use this script at your will, on any document you want and for any purpose, commercial or not.
The MarkDown files generated by this script are not considered derivative work and
don't require any attribution to the owners of this script.
If you want to modify and redistribute the script (not the converted documents - those are yours),
just keep a reference to this repo or to the license info below:
```
Copyright 2013 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
*/
function convertDefaultFolder() {
var settings = {markdownTables: true, plainTextOutput: false, boldItalicIsQuote: true, codeBlocks: true, codeBlockMinIndent: 12};
convertDefaultFolderWithSettings(settings);
}
function convertDefaultFolderWithSettings(settings) {
convertDownOneLevelByName('DocsToConvertToMarkdown', settings);
}
function convertDownOneLevelByName(folderName, settings) {
var folderIt = DriveApp.getFoldersByName(folderName);
if (folderIt.hasNext()) {
var folder = folderIt.next();
convertDownOneLevel(folder, settings);
} else {
Logger.log("Requested folder '" + folderName + "' not found.")
}
}
function ensureFolder(parentFolder, name) {
var folderIt = parentFolder.getFoldersByName(name);
if (folderIt.hasNext()) {
Logger.log("Requested folder '" + name + "' in '" + parentFolder + "', it already exists so using directly.");
return folderIt.next();
} else {
Logger.log("Requested folder '" + name + "' in '" + parentFolder + "', creating it.");
return parentFolder.createFolder(name);
}
}
function convertDownOneLevel(rootFolder, settings) {
//If in-progress export folder already exists, use it, otherwise create
var mainExportFolder = ensureFolder(rootFolder, exportFolderNameInProgress());
var folders = rootFolder.getFolders();
//Convert all folders that don't start with our export prefix
while (folders.hasNext()) {
var folder = folders.next();
if (folder.getName().substring(0, exportFolderPrefix.length) != exportFolderPrefix) {
var exportFolder = ensureFolder(mainExportFolder, folder.getName())
convertFolder(folder, exportFolder, settings);
}
}
//Convert contents of the root folder as well, mainly to catch any meta data
convertFolder(rootFolder, mainExportFolder, settings);
//Now export is complete, rename it so it is no longer in progress
mainExportFolder.setName(exportFolderName());
}
function convertFolderByName(folderName, settings) {
var folderIt = DriveApp.getFoldersByName(folderName);
if (folderIt.hasNext()) {
var folder = folderIt.next();
var exportFolder = ensureFolder(folder, exportFolderNameInProgress());
convertFolder(folder, exportFolder, settings);
//Now export is complete, rename it so it is no longer in progress
exportFolder.setName(exportFolderName());
}
}
function convertFolder(folder, exportFolder, settings) {
var exportCompleteName = "EXPORT-COMPLETE.txt";
var files = folder.getFilesByType(MimeType.GOOGLE_DOCS);
var convertedDocNames = "";
while (files.hasNext()) {
var file = files.next();
var doc = DocumentApp.openById(file.getId());
var docFolder = ensureFolder(exportFolder, doc.getName());
//If we already have an EXPORT-COMPLETE.txt then a previous
//run of the script has already converted the file, so skip
if (docFolder.getFilesByName(exportCompleteName).hasNext()) {
convertedDocNames += doc.getName() + ": already done - skipping\n";
Logger.log(doc.getName() + ": already done - skipping");
//Convert unconverted file
} else {
Logger.log(doc.getName() + ": converting...");
var converted = convertToMarkdownAttachments(doc, settings);
convertedDocNames += doc.getName() + "\n";
// Write all files to target folder
for(var file in converted.files) {
file = converted.files[file];
var blob = file.blob.copyBlob();
var name = file.name;
blob.setName(name);
docFolder.createFile(blob);
}
// Write markdown file to target folder
if (settings.plainTextOutput) {
docFolder.createFile(doc.getName() + ".txt", converted.markdown, "text/plain");
} else {
docFolder.createFile(doc.getName() + ".md", converted.markdown, "text/x-markdown");
}
//Mark this folder as converted, so we won't redo it if resuming
docFolder.createFile(exportCompleteName, "Export completed at " + Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'"), MimeType.PLAIN_TEXT);
}
}
//FIXME display some other way? Browser only works from a spreadsheet!
//Browser.msgBox("Export complete", "Converted documents:\n" + convertedDocNames, Browser.Buttons.OK)
}
var exportFolderPrefix = "ExportedMarkdown-";
var exportFolderInProgressSuffix = "IN-PROGRESS";
function exportFolderName() {
var timeStamp = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
return exportFolderPrefix + timeStamp;
}
function exportFolderNameInProgress() {
return exportFolderPrefix + exportFolderInProgressSuffix;
}
function mailActiveDocumentAsMarkdown() {
var doc = DocumentApp.getActiveDocument();
var attachments = convertToMarkdownAttachments(doc).attachments;
mailWithAttachments(doc, attachments);
}
function mailWithAttachments(document, attachments) {
MailApp.sendEmail(
Session.getActiveUser().getEmail(),
"[MARKDOWN_MAKER] "+document.getName(),
"Your converted markdown document is attached (converted from " + document.getUrl() + ")" +
"\n\nDon't know how to use the format options? See http://github.com/mangini/gdocs2md\n",
{ "attachments": attachments });
}
function convertToMarkdownAttachments(document, settings) {
var numChildren = document.getActiveSection().getNumChildren();
var text = "";
var inSrc = false;
var inCodeBlock = 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 = [];
var files = [];
// Walk through all the child elements of the doc.
for (var i = 0; i < numChildren; i++) {
var child = document.getActiveSection().getChild(i);
var result = processParagraph(i, child, inSrc, globalImageCounter, globalListCounters, settings, inCodeBlock);
globalImageCounter += (result && result.images) ? result.images.length : 0;
if (result!==null) {
if (inCodeBlock && !result.cbl) {
text+="```\n\n";
inCodeBlock = false;
}
if (result.cbl) {
if (!inCodeBlock) {
text+="\n```\n";
inCodeBlock = true;
}
text+=result.text + "\n";
} else 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 } );
files.push( {
"name" : result.images[j].name,
"blob" : result.images[j].blob
});
}
}
} else if (inSrc) { // support empty lines inside source code
text+='\n';
}
}
if (settings.plainTextOutput) {
attachments.push({"fileName":document.getName()+".txt", "mimeType": "text/plain", "content": text});
} else {
attachments.push({"fileName":document.getName()+".md", "mimeType": "text/x-markdown", "content": text});
}
return {attachments: attachments, files: files, markdown: text};
}
function escapeHTML(text) {
return text.replace(/</g, '<').replace(/>/g, '>');
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
// Process each child element (not just paragraphs).
function processParagraph(index, element, inSrc, imageCounter, listCounters, settings, inCodeBlock) {
// Set up for real results.
var result = {};
var pOut = "";
var textElements = [];
var imagePrefix = "image_";
var indented = false;
// First, check for things that require no processing.
if (element.getNumChildren()==0) {
if (inCodeBlock) {
result.text = "";
result.cbl = true;
return result;
} else {
return null;
}
}
// Punt on TOC.
if (element.getType() === DocumentApp.ElementType.TABLE_OF_CONTENTS) {
return {"text": "[[TOC]]"};
}
//Check for indent that can indicate code block etc.
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
if (isNumber(settings.codeBlockMinIndent)) {
if (element.getIndentStart() > settings.codeBlockMinIndent) {
indented = true;
}
}
}
// 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) {
//Markdown table
if (settings.markdownTables) {
textElements.push("\n");
var nCols = element.getChild(0).getNumCells();
for (var i = 0; i < element.getNumChildren(); i++) {
// process this row
for (var j = 0; j < nCols; j++) {
var cell = element.getChild(i).getChild(j);
//We expect the TableCell to have a single Paragraph child - if
//so we will add all its text elements to our textElements array for
//later processing
if (cell.getNumChildren() > 0) {
var cellPara = cell.getChild(0);
if (cellPara.getType() === DocumentApp.ElementType.PARAGRAPH) {
for (var k = 0; k < cellPara.getNumChildren(); k++) {
var el = cellPara.getChild(k);
if (el.getType() === DocumentApp.ElementType.TEXT) {
textElements.push(el);
}
}
}
}
if (j < nCols - 1) textElements.push(" | ");
}
textElements.push("\n");
//Line of row separators after first row (assumed to be table headings)
if (i == 0) {
for (var j = 0; j < nCols; j++) {
textElements.push("---");
if (j < nCols - 1) textElements.push(" | ");
}
textElements.push("\n");
}
}
textElements.push("\n");
//HTML table
} else {
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) {
result.images = result.images || [];
var contentType = element.getChild(i).getBlob().getContentType();
var extension = "";
if (/\/png$/.test(contentType)) {
extension = ".png";
} else if (/\/gif$/.test(contentType)) {
extension = ".gif";
} else if (/\/jpe?g$/.test(contentType)) {
extension = ".jpg";
} else {
throw "Unsupported image type: "+contentType;
}
var name = imagePrefix + imageCounter + extension;
imageCounter++;
textElements.push('![image alt text]('+name+')');
result.images.push( {
"bytes": element.getChild(i).getBlob().getBytes(),
"blob": element.getChild(i).getBlob(),
"type": contentType,
"name": name});
} 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 if (/^\s*---\s+jsperf\s*([^ ]+)\s*$/.test(pOut)) {
result.text = '<iframe style="width: 100%; height: 340px; overflow: hidden; border: 0;" '+
'src="http://www.html5rocks.com/static/jsperfview/embed.html?id='+RegExp.$1+
'"></iframe>';
//Paragraphs containing any text elements that look like a code block are code block lines
} else if (anyCBL(settings, textElements, indented)) {
//If paragraph is not indented, and is a CBL, then remove the leading tab if there
//is one, since it is there just to indicate a code block.
if (pOut.length > 1 && pOut.charAt(0) === "\t" && !indented) {
pOut = pOut.slice(1);
}
result.text = pOut;
result.cbl = true;
//Blank paragraphs following a code block are part of the code block
} else if (inCodeBlock && pOut.trim().length == 0) {
result.text = "";
result.cbl = true;
} else {
prefix = findPrefix(inSrc, element, listCounters);
var pOut = "";
for (var i=0; i<textElements.length; i++) {
var txt = textElements[i];
var processed = processTextElement(inSrc, textElements[i], settings, inCodeBlock);
pOut += processed;//.markdown;
}
// replace Unicode quotation marks
pOut = pOut.replace('\u201d', '"').replace('\u201c', '"');
result.text = prefix+pOut;
}
return result;
}
function isTextCourier(txt, c) {
return (txt.getFontFamily(c) == DocumentApp.FontFamily.COURIER_NEW);
}
function anyCBL(settings, txts, indented) {
for (var i=0; i<txts.length; i++) {
var txt = txts[i];
if (isCBL(settings, txt, indented)) return true;
}
return false;
}
function isCBL(settings, txt, indented) {
if (typeof(txt) === 'string') {
return false;
}
var cbl = false;
if (settings.codeBlocks && ((txt.getText().length > 1 && txt.getText().charAt(0) === "\t") || indented)) {
for (var c = 0; c < txt.getText().length; c++) {
if (isTextCourier(txt, c)) {
cbl = true;
}
}
}
return cbl;
}
// 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+="# ";
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;
}
//Transfer any leading spaces from inside to end of pre, and trailing spaces from inside to start of post,
//so that formatting symbols are adjacent to the words they apply to, without intervening white space.
function compressInside(sections, blank) {
while (sections.inside.substring(0, 1) == blank) {
sections.pre = sections.pre + blank;
sections.inside = sections.inside.substring(1);
}
while (sections.inside.substring(sections.inside.length - 1) == blank) {
sections.post = blank + sections.post;
sections.inside = sections.inside.substring(0, sections.inside.length - 1);
}
}
//Compress inside using spaces, tabs, carriage returns, newlines and form feeds.
function compressInsideWhitespace(sections) {
compressInside(sections, " ");
compressInside(sections, "\t");
compressInside(sections, "\r");
compressInside(sections, "\n");
compressInside(sections, "\f");
}
function makeSections(pOut, off, lastOff) {
var sections = {
inside: pOut.substring(off, lastOff),
pre: pOut.substring(0, off),
post: pOut.substring(lastOff)
};
return sections;
}
function processTextElement(inSrc, txt, settings, inCodeBlock) {
if (typeof(txt) === 'string') {
return txt;
}
var pOut = txt.getText();
if (! txt.getTextAttributeIndices) {
return pOut;
}
//Get the offsets within the text where text formatting runs start
var attrs=txt.getTextAttributeIndices();
//Previous offset processed, starts at end of text
var lastOff=pOut.length;
//Process the text string in formatting runs, backwards
for (var i=attrs.length-1; i>=0; i--) {
//Start of formatting run
var off=attrs[i];
var url=txt.getLinkUrl(off);
var sections = makeSections(pOut, off, lastOff);
//Process URL
if (url) { // start of link
// detect links that are in multiple pieces because of errors on formatting:
if (i>=1 && attrs[i-1]==off-1 && txt.getLinkUrl(attrs[i-1])===url) {
i-=1;
off=attrs[i];
url=txt.getLinkUrl(off);
sections = makeSections(pOut, off, lastOff);
}
pOut = sections.pre + '[' + sections.inside + '](' + url + ')' + sections.post;
//Process font for inline code
} else if (!inSrc && isTextCourier(txt, off)) {
// detect fonts that are in multiple pieces because of errors on formatting:
while (i>=1 && isTextCourier(txt, attrs[i-1])) {
i-=1;
off=attrs[i];
}
sections = makeSections(pOut, off, lastOff);
//Shrink bold/italic regions to reduce the whitespace they apply to.
compressInsideWhitespace(sections);
//If we have shrunk the formatted region to nothing, ignore it
if (sections.inside.length == 0) {
pOut = sections.pre + sections.post;
//Apply formatting
} else {
pOut = sections.pre + '`' + sections.inside + '`' + sections.post;
}
//Process bold and/or italic. Note that this is only processed if this section is NOT
//an URL or code.
} else if (txt.isBold(off) || txt.isItalic(off)) {
//FIXME can we use the same "multiple pieces" code above to prevent errors in runs of the same
//bold/italic state with redundant attributes in the middle?
var bold = txt.isBold(off);
var italic = txt.isItalic(off);
// Expand run of formatting backwards, if bold/italic state is the same. Not sure why, but Docs occasionally have
// redundant formatting, most notably around colons.
while (i>=1 && (txt.isBold(attrs[i-1]) === bold) && (txt.isItalic(attrs[i-1]) === italic)) {
i-=1;
off=attrs[i];
}
sections = makeSections(pOut, off, lastOff);
var d1 = d2 = "*";
if (bold) {
var d1 = d2 = "**"
if (italic) {
d1 = "**_"; d2 = "_**";
}
}
//Shrink bold/italic regions to reduce the whitespace they apply to.
compressInsideWhitespace(sections);
//If we have shrunk the formatted region to nothing, ignore it
if (sections.inside.length == 0) {
pOut = sections.pre + sections.post;
//Apply formatting
} else {
pOut = sections.pre + d1 + sections.inside + d2 + sections.post;
}
}
lastOff=off;
}
//Format any text that starts with bold italic as a quote
if (settings.boldItalicIsQuote && txt.getText().length > 0 && txt.isBold(0) && txt.isItalic(0)) {
pOut = "> " + pOut;
}
return pOut;
}
function endsWith(s, suffix) {
return s.indexOf(suffix, this.length - suffix.length) !== -1;
};
//Recursive copy GOOGLE_DOCS files in expected gdocs2md layout from one root
//folder to another. Can be used to backup a file layout
function copyFolders(sourceFolder, targetFolder) {
var folderIt = DriveApp.getFoldersByName(sourceFolder);
var folderDest = DriveApp.createFolder(targetFolder)
if (folderIt.hasNext()) {
var folder = folderIt.next();
Logger.log(folder);
var subjectFoldersIt = folder.getFolders();
while (subjectFoldersIt.hasNext()) {
//Source and destination folders for this subject
var subjectFolder = subjectFoldersIt.next();
var subjectFolderDest = folderDest.createFolder(subjectFolder.getName())
Logger.log("- " + subjectFolder);
var subjectFilesIt = subjectFolder.getFilesByType(MimeType.GOOGLE_DOCS);
while (subjectFilesIt.hasNext()) {
var subjectFile = subjectFilesIt.next();
Logger.log(" - " + subjectFile);
var name = subjectFile.getName();
var newName = name;
Logger.log(" '" + name + "'->'" + newName + "'");
var fileDest = subjectFile.makeCopy(newName, subjectFolderDest)
}
}
}
}