diff --git a/converttomarkdown.gapps b/converttomarkdown.gapps index c553460..b6344de 100644 --- a/converttomarkdown.gapps +++ b/converttomarkdown.gapps @@ -1,3 +1,15 @@ +/* +Usage: + Adding this script to your doc: + - Tools > Script Manager > New + - Select "Blank Project", then paste this code in and save. + Running the script: + - Tools > Script Manager + - Select "ConvertToMarkdown" function. + - Click Run button. + - Converted doc will be mailed to you. Subject will be "[MARKDOWN_MAKER]...". +*/ + function ConvertToMarkdown() { var numChildren = DocumentApp.getActiveDocument().getActiveSection().getNumChildren(); var text = ""; @@ -5,15 +17,24 @@ function ConvertToMarkdown() { var inClass = false; var globalImageCounter = 0; var globalListCounters = {}; + // edbacher: added a variable for indent in src
block. Let style sheet do margin. + var srcIndent = ""; var attachments = []; - for (var i=0; i\n\n"; + } else if (result.source==="start" && !inSrc) { inSrc=true; text+="\n"; + } else if (result.sourcePretty==="end" && inSrc) { + inSrc=false; + text+="
\n"; } else if (result.source==="end" && inSrc) { @@ -28,7 +49,7 @@ function ConvertToMarkdown() { } else if (inClass) { text+=result.text+"\n\n"; } else if (inSrc) { - text+=(" "+escapeHTML(result.text)+"\n"); + text+=(srcIndent+escapeHTML(result.text)+"\n"); } else if (result.text && result.text.length>0) { text+=result.text+"\n\n"; } @@ -52,7 +73,7 @@ function ConvertToMarkdown() { MailApp.sendEmail(Session.getActiveUser().getEmail(), "[MARKDOWN_MAKER] "+DocumentApp.getActiveDocument().getName(), "Your converted markdown document is attached (converted from "+DocumentApp.getActiveDocument().getUrl()+")"+ - "\n\nDon't know how to use the format options? See http://github.com/mangini/gdocs2md#readme\n", + "\n\nDon't know how to use the format options? See http://github.com/mangini/gdocs2md\n", { "attachments": attachments }); } @@ -60,25 +81,47 @@ function escapeHTML(text) { return text.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_"; - for (var i=0; i\n"); + var nCols = element.getChild(0).getNumCells(); + for (var i = 0; i < element.getNumChildren(); i++) { + textElements.push(" \n"); + // process this row + for (var j = 0; j < nCols; j++) { + textElements.push(" \n"); + } + textElements.push("\n"); + } + + // Process various types (ElementType). + for (var i = 0; i < element.getNumChildren(); i++) { var t=element.getChild(i).getType(); - if (t === DocumentApp.ElementType.TEXT || t === DocumentApp.ElementType.TABLE_ROW) { - // TODO: proper handling of table_row + + 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); @@ -109,23 +152,28 @@ function processParagraph(index, element, inSrc, imageCounter, listCounters) { } 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; + 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+source code\s*$/.test(pOut)) { + 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 = '" + element.getChild(i).getChild(j).getText() + " \n"); + } + textElements.push("