\n\n"+DocumentApp.getActiveDocument().getName()+"\n\n"
+ var inSrc = false;
+ 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 = [];
+
+ // Walk through all the child elements of the doc.
+ var inlist= false;
+ var inSublist = false;
+ var inSession=false;
+ var inSection=false;
+ var inSubSection=false;
+ for (var i = 0; i < numChildren; i++) {
+ var child = DocumentApp.getActiveDocument().getActiveSection().getChild(i);
+ var result = processParagraph(i, child, inSrc, globalImageCounter, globalListCounters);
+
+ if (child.getType()===DocumentApp.ElementType.PARAGRAPH) {
+ var heading=child.getHeading();
+ //Crappy default hierarchy to try to organise Session/Section/SubSection
+ //Partial attempt at making elements robust
+ if (heading===DocumentApp.ParagraphHeading.HEADING3) {
+ if (inSubSection) text=text+"\n";
+ if (!(inSession)) {
+ text=text+"\n\n";
+ inSession=true;
+ }
+ if (!(inSection)) {
+ text=text+"\n\n";
+ inSection=true;
+ }
+ text=text+"\n";
+ inSubSection=true;
+ } else if (heading===DocumentApp.ParagraphHeading.HEADING2) {
+ if (inSubSection) {
+ text=text+"\n\n";
+ inSubSection=false;
+ } else if (inSection) text=text+"\n";
+ if (!(inSession)) {
+ text=text+"\n\n";
+ inSession=true;
+ }
+ text=text+"\n";
+ inSection=true;
+ } else if (heading===DocumentApp.ParagraphHeading.HEADING1) {
+ if (inSubSection) {
+ text=text+"\n\n\n";
+ inSubSection=false;
+ inSection=false;
+ } else if (inSection) {
+ text=text+"\n\n";
+ inSection=false;
+ } else if (inSession) text=text+"\n";
+ text=text+"\n";
+ inSession=true;
+ }
+ }
+ if (child.getType()===DocumentApp.ElementType.LIST_ITEM) {
+ //Refactor note: as we hit a list element for the first time,
+ // perhaps we should go into a while loop, incrementing i
+ //until such a time as we aren't still in a LIST_ITEM?
+ //This would allow us to more easily handle the outer list tags?
+
+ inlist=true;
+ var listItem = child;
+ var nesting = listItem.getNestingLevel()
+
+ var key = listItem.getListId()+ '.' + listItem.getNestingLevel();
+ var counter = globalListCounters[key] || 0;
+ counter++;
+ globalListCounters[key] = counter;
+ //counter===1 says we're seeing a list for the first time
+ if (counter===1) {
+ if (nesting===0) {
+ if (inSublist){
+ text=text + '\n\n\n';
+ inSublist=false;
+ }
+ text=text+"\n";
+ } else if (nesting===1) {
+ if (inlist) {
+ text=text.replace(/<\/ListItem>\s*$/, '\n');
+ } else {
+ text=text+"\n\n";
+ inList=true;
+ }
+ text=text+"";
+ inSublist= true;
+ } else throw "Paragraph "+index+" of type "+element.getType()+" has an unsupported nesting level of " +nesting;
+ } else {
+ //We're in a list we know about...
+ if (nesting===0) {
+ if (inSublist){
+ text=text + '\n\n\n';
+ inSublist=false;
+ }
+ } else if (nesting===1){
+ if (!(inSublist)){
+ text=text.replace(/<\/ListItem>\s*$/, '\n');
+ text=text+"";
+ inSublist=true;
+ }
+ }
+ }
+ } else if ( (inlist) || (inSublist) ) {
+ //We've hit a non-list item but we're in a list... So make good...
+ if (inSublist) text=text + '\n\n\n';
+ text=text + '\n\n\n';
+ inlist=false;
+ inSublist=false;
+ }
+
+
+ globalImageCounter += (result && result.images) ? result.images.length : 0;
+ if (result!==null) {
+ if (result.sourcePretty==="start" && !inSrc) {
+ inSrc=true;
+ text+="\n";
+ } else if (result.sourcePretty==="end" && inSrc) {
+ inSrc=false;
+ text+="\n\n";
+ } else if (result.source==="start" && !inSrc) {
+ inSrc=true;
+ text+="\n";
+ } else if (result.source==="end" && inSrc) {
+ inSrc=false;
+ text+="\n\n";
+ } else if (result.inClass==="start" && !inClass) {
+ inClass=true;
+ text+="\n";
+ } else if (result.inClass==="end" && inClass) {
+ inClass=false;
+ text+="
\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\n\n"
+ if (inlist) text+="\n\n"
+ if (inSubSection) text+="";
+ if (inSection) text+="";
+ if (inSession) text+="";
+ text+="\n"+FOOTER
+
+ //Hack fixes - bleurghh
+ text=text.replace(/