-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevobib.py
More file actions
225 lines (198 loc) · 5.99 KB
/
evobib.py
File metadata and controls
225 lines (198 loc) · 5.99 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/python2.6
import cgitb
cgitb.enable()
import cgi
import sqlite3
import datetime
from docutils.core import publish_parts
import codecs
import re
import os
# get the args of the url, convert nasty field storage to plain dictionary,
# there is probably a better solution, but this works for the moment
xargs = dict(
key = '',
author = '',
year = '',
title = '',
fulltext = '',
display = '',
keys = '',
search = '',
)
args = {}
tmp_args = cgi.FieldStorage()
for arg in xargs:
tmp = tmp_args.getvalue(arg)
if tmp:
args[arg] = tmp
db = sqlite3.connect('evobib.sqlite3')
cursor = db.cursor()
display = 'bibtex'
if args.get('display', ''):
display = args.get('display', '')
if display == 'js':
print "Content-type: application/javascript; charset=utf-8"
else:
print "Content-type: text/html; charset=utf-8"
print
print '<head>'
print '<link rel="stylesheet" href="../cgi-bin/table.css">'
print '<link rel="stylesheet" href="table.css">'
print '<title>EvoBib BibTex</title>'
print """<script type="text/javascript">
function showBibTex(node){
var falert = document.createElement('div');
falert.id='fake';
falert.class='fake_alert';
var text = '<div class="message"><p>Entry in BibTex</p><p><pre style="overflow-x:scroll;text-align:left;margin:10px;padding:10px;background-color:white;">'+node.dataset['bibtex']+'</pre></p>';
text += '<div style="background-color:Crimson;color:white;cursor:pointer;border:2px solid white;" onclick="closeBibTex();">CLOSE</div></div>';
document.body.appendChild(falert);
falert.innerHTML=text;
}
function closeBibTex(){
var fake = document.getElementById('fake');
document.body.removeChild(fake);
}
</script>
<style>
.fake_alert {
position: fixed;
background-color: rgba(0,0,0,0.5);
z-index: 100;
top:0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
float: left;
text-align:center;
}
.message {
position: absolute;
margin-left: auto;
margin-right: auto;
top:30%;
left:0;
right:0;
background-color: #2d6ca2;
border: 2px solid LightYellow;
text-align:center;
min-width:200px;
max-width:50%;
width: auto;
float: left;
border-radius:5px;
}
</style>
"""
print '</head>'
if args.get('search', ''):
search = args.get('search', '')
search = search.split(',')
query = 'select key, author, editor, title, booktitle, year, url, doi, bibtex from bibliography where bibtex like "%'+search[0].strip()+'%"'
for term in search[1:]:
query += 'and bibtex like "%'+term.strip()+'%"'
query += ' order by year;'
elif args.get('fulltext', ''):
fulltext = args.get('fulltext', '')
query = 'select key, author, editor, title, booktitle, year, url, doi, bibtex from bibliography where bibtex like "%'+fulltext+'%" order by year;'
elif args.get('key', ''):
query = 'select key, author, editor, title, booktitle, year, url, doi, bibtex from bibliography where key="'+args.get('key', '')+'" order by year;'
elif args.get('keys', ''):
query = 'select key, author, editor, title, booktitle, year, url, doi, bibtex from bibliography where key like "'+args.get('keys', '')+'%" order by year;'
else:
query = ''
if display == 'keys':
keys = []
for line in cursor.execute('select key from bibliography;'):
keys += [line[0]]
keys = ', '.join(keys)
print '<span type="hidden" id="keys" data-list="'+keys+'"></span>';
elif display == 'table':
text = '<table class="refs">'
text += """
<tr>
<th>Key</th>
<th>Author</th>
<th>Title</th>
<th>Year</th>
<th>URL</th>
<th>Reference</th>
</tr>
"""
hits = 0
for line in cursor.execute(query):
bibtex = line[-1].replace('"', "''")
doi, url = line[7], line[6]
if not url: url = ''
if not doi: doi = ''
if doi:
doi = '<a target="_blank" href="'+doi+'">DOI</a>'
elif url.strip():
doi = '<a target="_blank" href="'+url+'">URL</a>'
else:
doi = ''
author, editor = line[1], line[2]
if not author:
author = editor
editor = True
else:
editor = False
author = author.split(' and ')
if len(author) == 1:
author = author[0]
elif len(author) == 2:
author = ' and '.join(author)
else:
author = author[0]+' et al.'
if editor:
author += ' (ed.)'
year = line[5]
if not year: year=''
title, booktitle = line[3], line[4]
if not title:
title = booktitle
td = """
<tr>
<td><span class="ebkey" data-eb="{0}">{0}</a></td>
<td>{1}</td>
<td>{2}</td>
<td>{3}</td>
<td>{4}</td>
<td ><span class="eb" style="cursor:pointer;" data-eb="{0}" data-bibtex="{5}" onclick="showBibTex(this);">BibTex</span></td>
</tr>
""".format(
line[0],
author.encode('utf-8'),
title.encode('utf-8'),
year,
doi,
bibtex.encode('utf-8')
)
text += td
hits += 1
text += '</table>'
if hits: print(text)
else:
print 'No values selected.'
elif display == 'js':
print
print 'var EvoBib = {'
text = ''
for key, bibtex in cursor.execute('select key, bibtex from bibliography;'):
textp = '"'+key+'": "'+bibtex.replace('"', '')+'",'
textp = textp.replace('<', '<')
textp = textp.replace('>', '<')
textp = textp.replace('\n', r'\n')
text += textp.encode('utf-8')
text = text[:-1]
print text
print '};'
else:
text = '<pre>'
for line in cursor.execute(query):
bibtex = line[-1].replace('"', "''")
text += '\n'+bibtex.encode('utf-8')+'\n'
print(text)