-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathzotero.lua
147 lines (132 loc) · 4.23 KB
/
zotero.lua
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
-- read from the Zotero databaes to one day generate citations
-- and populate the local requirements.bib file
local sql = {}
-- Open db file at path or environment variable, otherwise open in memory.
local db = require('sqlite.db'):open('~/Zotero/zotero.sqlite', { open_mode = 'ro' })
local resp_collection_names = db:eval [[
SELECT collections.collectionName
FROM collections
]]
M = {}
M._creators = {
'editor',
'seriesEditor',
'translator',
'reviewedAuthor',
'artist',
'performer',
'composer',
'director',
'podcaster',
'cartographer',
'programmer',
'presenter',
'interviewee',
'interviewer',
'recipient',
'sponsor',
'inventor',
}
local collections = {}
for _, c in ipairs(resp_collection_names) do
collections[c['collectionName']] = {}
end
local resp_collections = db:eval [[
SELECT items.itemID, collections.collectionName
FROM items, collections, collectionItems
WHERE
items.itemID = collectionItems.itemID
and collections.collectionID = collectionItems.collectionID
ORDER by collections.collectionName
]]
for _, item in ipairs(resp_collections) do
table.insert(collections[item['collectionName']], item['itemID'])
end
local resp_fields = db:eval [[
SELECT items.itemID, items.key, fields.fieldName, itemDataValues.value
FROM items, itemData, fields, itemDataValues
WHERE
items.itemID = itemData.itemID
and itemData.fieldID = fields.fieldID
and itemData.valueID = itemDataValues.valueID
]]
local items = {}
for _, item in ipairs(resp_fields) do
if not items[item['itemID']] then
items[item['itemID']] = { zotkey = item['key'], alastnm = '' }
end
items[item['itemID']][item['fieldName']] = item['value']
end
local resp_authors = db:eval [[
SELECT items.itemID, creatorTypes.creatorType, creators.lastName, creators.firstName
FROM items, itemCreators, creators, creatorTypes
WHERE
items.itemID = itemCreators.itemID
and itemCreators.creatorID = creators.creatorID
and creators.creatorID = creators.creatorID
and itemCreators.creatorTypeID = creatorTypes.creatorTypeID
ORDER by itemCreators.ORDERIndex
]]
for _, author in ipairs(resp_authors) do
if items[author['itemID']] then
if items[author['itemID']][author['creatorType']] then
table.insert(items[author['itemID']][author['creatorType']], { author['lastName'], author['firstName'] })
else
items[author['itemID']][author['creatorType']] = { { author['lastName'], author['firstName'] } }
end
if author['creatorType'] == 'author' then
items[author['itemID']]['alastnm'] = items[author['itemID']]['alastnm'] .. ', ' .. author['lastName']
else
local sought = { 'author' }
for _, c in ipairs(M._creators) do
if author['creatorType'] == c then
local flag = false
for _, s in ipairs(sought) do
if items[author['itemID']][s] then
flag = true
break
end
end
if not flag then
items[author['itemID']]['alastnm'] = items[author['itemID']]['alastnm'] .. ', ' .. author['lastName']
end
end
table.insert(sought, c)
end
end
end
end
-- generate citation keys
for _, item in ipairs(items) do
local authors = item['author']
local author = ''
if authors ~= nil then
author = authors[1][1]
end
local year = ''
local date = item['date']
if date ~= nil then
year = string.match(date, '%d%d%d%d')
end
local title = item['title']
title = string.sub(title, 1, 10)
local key = author .. title .. year
key = string.gsub(key, '[^%w]', '')
item['citekey'] = key
end
local resp_types = db:eval [[
SELECT items.itemID, itemTypes.typeName
FROM items, itemTypes
WHERE
items.itemTypeID = itemTypes.itemTypeID
]]
for _, item in ipairs(resp_types) do
if items[item['itemID']] then
if item['typeName'] == 'attachment' then
items[item['itemID']] = nil
else
items[item['itemID']]['etype'] = item['typeName']
end
end
end
vim.print(items)