forked from retorquere/zotero-file-hierarchy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile Hierarchy.ts
More file actions
85 lines (66 loc) · 2.42 KB
/
File Hierarchy.ts
File metadata and controls
85 lines (66 loc) · 2.42 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
declare const Zotero: any
class Collections {
private collections = {}
private saved = {}
constructor() {
let coll
while (coll = Zotero.nextCollection()) {
const key = (coll.primary ? coll.primary : coll).key
this.collections[key] = {
parent: coll.fields.parentKey,
name: coll.name,
}
}
for (const key in this.collections) {
const coll = this.collections[key]
if (coll.parent && !this.collections[coll.parent]) {
coll.parent = false
}
coll.path = this.path(coll)
}
Zotero.debug('collections: ' + JSON.stringify(this.collections))
}
clean(filename) {
return filename.replace(/[#%&{}\\<>\*\?\/\$!'":@]/g, '_')
}
path(coll) {
return (this.collections[coll.parent] ? this.path(this.collections[coll.parent]) : '') + this.clean(coll.name) + '/'
}
save(item) {
const attachments = (item.itemType === 'attachment') ? [ item ] : (item.attachments || [])
const collections = (item.collections || []).map(key => this.collections[key]).filter(coll => coll)
for (const att of attachments) {
if (!att.defaultPath) continue
const subdir = [
(item.itemType !== 'attachment' ? this.clean(item.title) : null),
/* assume text/html is snapshot */
(att.contentType === 'text/html' ? this.clean(att.filename.replace(/\.html?$/, '')) : null),
].filter(p => p).join('/')
Zotero.write(`// subdir=${subdir}`)
for (const coll of (collections.length ? collections : [{ path: '' }])) {
const path = `${coll.path}${subdir}`
this.saved[path] = this.saved[path] || {}
const parts = att.filename.split('.')
const ext = this.clean(parts.length > 1 ? ('.' + parts.pop()) : '')
const basename = this.clean(parts.join('.'))
let postfix = 0
let filename = basename + ext
while (this.saved[filename]) {
filename = `${basename}_${++postfix}${ext}`
}
this.saved[path][filename] = true
Zotero.debug(`saving to ${path}/${filename}\n`)
att.saveFile(`${path}/${filename}`, true)
Zotero.write(`${path}/${filename}\n`)
}
}
}
}
function doExport() {
if (!Zotero.getOption('exportFileData')) throw new Error('File Hierarchy needs "Export File Data" to be on')
const collections = new Collections
let item, attachments
while ((item = Zotero.nextItem())) {
collections.save(item)
}
}