-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc2html.gs
More file actions
173 lines (158 loc) · 5.6 KB
/
doc2html.gs
File metadata and controls
173 lines (158 loc) · 5.6 KB
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
var sourceDocID = '1KhP-Kqrzcg0jNJr1m7c7qj1DvpsVJniZ42z2AKwUCf4';
var targetDocID = '1H0I4lRaAYcEGiwt_I4tIEGInruNmb_XTKSqJ5Ln2l6s';
var folderImages = '0BySmMtYQO1OLV1JCcV9TZmctaVk';
var baseUrl = "http://drive.google.com/uc?export=view&id=";
function doc2html() {
Logger.clear();
var source_doc = DocumentApp.openById(sourceDocID);
var target_doc = DocumentApp.openById(targetDocID);
var docName = 'img_' + source_doc.getName().split(' ').join('');
var rootFolder = DriveApp.getFolderById(folderImages);
rootFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
var imgFolder = rootFolder.createFolder(docName);
var folderID = imgFolder.getId();
var content = '';
var body = source_doc.getBody();
for (var i = 0; i < body.getNumChildren(); i++) {
var child = body.getChild(i);
content += parseChild(child, folderID, docName);
}
target_doc.getBody().editAsText().appendText(content);
//Logger.log(content);
var payload = {
title: docName,
content: content,
author: 'Pepe',
date: Date.now()
}
}
function parseChild(child, folderID, docName) {
switch (child.getType()) {
case DocumentApp.ElementType.BODY_SECTION:
Logger.log('body');
break;
case DocumentApp.ElementType.COMMENT_SECTION:
Logger.log('Comment');
break;
case DocumentApp.ElementType.HEADER_SECTION:
Logger.log('Header section');
break;
case DocumentApp.ElementType.INLINE_IMAGE:
return parseImage(child, folderID, docName);
case DocumentApp.ElementType.LIST_ITEM:
Logger.log('List');
break;
case DocumentApp.ElementType.PARAGRAPH:
return parseParagraph(child, folderID, docName);
case DocumentApp.ElementType.TABLE:
Logger.log('table');
break;
case DocumentApp.ElementType.TEXT:
return parseText(child);
default:
Logger.log('Unsupported: ' + child.getType());
}
}
function parseParagraph(child, folderID, docName) {
var content = '';
for (var i = 0; i < child.getNumChildren(); i++) {
content += parseChild(child.getChild(i), folderID, docName);
}
return content;
}
function parseText(child) {
var text = child.asText();
var content = '<p>';
var bold = {
active: false,
startsAt: 0,
clousure: '</strong>'
};
var italic = {
active: false,
startsAt: 0,
clousure: '</i>'
};
var underline = {
active: false,
startsAt: 0,
clousure: '</u>'
};
var link = {
active: false,
startsAt: 0,
clousure: '</a>'
};
var parsedText = text.getText();
for (var i = 0; i < parsedText.length; i++) {
if (text.isBold(i) && !bold.active) { //Starts bold
bold.startsAt = i;
bold.active = true;
content += '<strong>';
} else if (!text.isBold(i) && bold.active) { //ends bold
bold.active = !bold.active;
content += '</strong>';
} else if (text.isItalic(i) && !italic.active) { //Starts italic
italic.startsAt = i;
italic.active = true;
content += '<i>';
} else if (!text.isItalic(i) && italic.active) { //ends italic
italic.active = !italic.active;
content += '</i>';
} else if (text.isUnderline(i) && !underline.active) { //Starts underline
underline.startsAt = i;
underline.active = true;
content += '<u>';
} else if (!text.isUnderline(i) && underline.active) { //ends underline
underline.active = !underline.active;
content += '</u>';
} else if (isURL(text, i) && !link.active) {
link.active = true;
link.startsAt = i;
//Aquí va una chapuza pero es demasiado tarde para pensar algo mejor
//que se joda al que le toque leerlo, será a mi yo descansado, da igual,
//jodete Joseba
var prevChar = parsedText.charAt(i - 1);
content = content.slice(0, content.length - 1) + '<a href="' + text.getLinkUrl(i) + '">' + prevChar;
} else if (!isURL(text, i) && link.active) {
link.active = !link.active;
content += '</a>';
}
content += parsedText.charAt(i);
}
var htmlTags = [bold, italic, underline, link];
content += closeHTMLTags(htmlTags);
return content + '</p>';
}
function parseImage(child, folderID, docName) {
var blob = child.getBlob();
var content_type = blob.getContentType();
var suffix = content_type.split("/")[1];
var img_name = "img_" + Date.now() + '.' + suffix;
blob.setName(img_name);
try {
var folder = DriveApp.getFolderById(folderID);
var imageStored = folder.createFile(blob);
var pubUrl = baseUrl + imageStored.getId(); // the public URL
var height = child.getHeight();
var width = child.getWidth();
imageStored.setName(img_name);
return '<img src="' + pubUrl + '" height="' + height + 'px" width="' + width + 'px"/>';
} catch (e) {
Logger.log('Problem storing the image');
Logger.log(e);
}
}
function closeHTMLTags(list) {
list.sort(function(a, b) {
return b.startsAt - a.startsAt;
});
var res = "";
for (var i = 0; i < list.length; i++) {
if (list[i].active) res += list[i].clousure;
}
return res;
}
function isURL(text, i) {
return text.getText().length > i && text.getLinkUrl(i) !== null;
}