This repository has been archived by the owner on Jan 26, 2025. It is now read-only.
forked from medialab/drive-out
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (145 loc) · 5.67 KB
/
index.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
'use strict';
/*
Example and basiuc usage for drive-out!
settings fil have to be in the same level.
*/
var settings = require('./settings'),
extend = require('util')._extend,
drive = require('./drive-api')(settings),
cmp = require('./custom-markdown-parser'),
fs = require('fs'),
MEDIA_PATH = './app/media',
CONTENTS_PATH = './app/contents';
/*
Return a well filled result object according to file.id
*/
function parseGoogleDocument(result) {
console.log(' parsing', result.slug)
// for GOOGLE DOCUMENTS
var html = drive.files.getHtml({fileId:result.id}),
$ = drive.utils.parse(html);
result.title = $('.title').text();
result.subtitle = $('.subtitle').html();
//console.log(html)
// parse document sections
$('h1').each( function(i, el){
var contents = $(this).nextUntil('h1').get().map(function(e) {return $(e).html()}).join(''), // html specific to this section
section = {
title: $(this).text(),
html: cmp(contents) // guillaume
};
// check it's own h4
var directive = $(this).nextUntil('h1').filter('h4');
//console.log(section.title, directives.length, directives);
if(directive.length) {
//console.log(directive.html())
console.log('n. attachments: ', directive.find('a[href]').length);
section.directive = directive.text().split(' ').shift().trim()
section.type = 'directive'
// check linked data
directive.find('a[href]').each(function (i, e) {
var datahref = e.attribs['href'].match(/id=([^&]*)/); // this is the address on google drive for the linked data
if(datahref) {
// get download urls
var file = drive.files.get({
fileId: datahref.pop()
});
console.log('directive has this file attached', file.title)
if(file.downloadUrl) {
section.datasrc = section.datasrc || []; // add a proper list to hold data urls if it has'nt been done yet
drive.files.download({
downloadUrl: file.downloadUrl,
filepath: MEDIA_PATH + '/' + file.id + '.' + file.fileExtension
})
section.datasrc.push( 'media/' + file.id + '.' + file.fileExtension)
console.log('downloading file', file.downloadUrl);
} else {
console.log('CANNOT DOWNLoAD file', file.title);
}
};
/*
drive.files.download({
downloadUrl: file.downloadUrl,
filepath: options.mediapath + '/' + file.id + '.' + file.fileExtension
})
*/
})
} else {
section.type = 'text'
}
result.sections.push(section);
})
return result;
};
drive.start().then(function logic() {
console.log('custom indexer of drive-out');
var fileId = drive.utils.getFileId(settings.DRIVE_FOLDER_URL),
pages,
narratives = []; // static pages dicts
console.log();
console.log('folder url:', settings.DRIVE_FOLDER_URL);
console.log('folder id: ', fileId);
fs.existsSync(MEDIA_PATH) || fs.mkdirSync(MEDIA_PATH);
fs.existsSync(CONTENTS_PATH) || fs.mkdirSync(CONTENTS_PATH);
// static pages
/*
Left menu...
*/
pages = drive.files.walk({fileId: fileId, mediapath: MEDIA_PATH}, function (file, options, results) {
var result = {
id: file.id,
title: file.title,
slug: drive.utils.slugify(file.title),
mimeType: file.mimeType,
sections: [] // it will bring h1 sections inside
};
console.log('--> ', file.title, file.id, file.mimeType);
// save result to a file named as file.slug
if(file.mimeType == 'application/vnd.google-apps.document') {
result = parseGoogleDocument(result);
drive.utils.write(CONTENTS_PATH + '/' + result.slug + '.json', JSON.stringify(result,null,2));
return result;
};// end if file.mimeType == 'application/vnd.google-apps.document'
if(file.mimeType == 'application/vnd.google-apps.folder') {
result.type = 'folder';
narratives.push(result)
return result;
};
});
drive.utils.write(CONTENTS_PATH + '/index.json', JSON.stringify(pages,null,2));
// cycle through narratives folder to get files (one narrative per google doc)
for(var i=0; i<narratives.length; i++) {
console.log();
if(narratives[i].slug != 'ipcc') {
continue
}
console.log(narratives[i].slug);
fs.existsSync(CONTENTS_PATH + '/' + narratives[i].slug) || fs.mkdirSync(CONTENTS_PATH +'/' + narratives[i].slug);
console.log('-------------------------');
drive.files.walk({fileId: narratives[i].id, mediapath: MEDIA_PATH}, function (file, options, results, items) {
console.log('--> "', file.title,'" ', file.id, file.mimeType);
var result = {
id: file.id,
title: file.title.replace(/[0-9]*[\s.]*/,''),
slug: drive.utils.slugify(file.title.replace(/[0-9]*[\s.]*/,'')),
mimeType: file.mimeType,
sections: [] // it will bring h1 sections inside
};
if(file.mimeType == 'application/vnd.google-apps.document') {
result = parseGoogleDocument(result);
result.menu = items.filter(function(d) {
if(d.mimeType == 'application/vnd.google-apps.document' && d.title.match(/^[0-9]{1,}[\s.]*/))
return d
}).map(function(d) {
return {
title: d.title.replace(/^[0-9]*[\s.]*/,''),
slug: drive.utils.slugify(d.title.replace(/^[0-9]*[\s.]*/,'')),
}
});
drive.utils.write(CONTENTS_PATH + '/' + narratives[i].slug + '/' + result.slug + '.json', JSON.stringify(result,null,2));
};
})
};
}, console.log).catch(function(err) {
console.log(err)
});