forked from mangecoeur/Citer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
citer.py
374 lines (289 loc) · 10.6 KB
/
citer.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from __future__ import print_function, absolute_import, division
import sublime
import sublime_plugin
import sys
import os.path
import string, re
# ST3 loads each package as a module, so it needs an extra prefix
reloader_name = 'citer.reloader'
reloader_name = 'Citer.' + reloader_name
from imp import reload
# Make sure all dependencies are reloaded on upgrade
if reloader_name in sys.modules:
reload(sys.modules[reloader_name])
if os.path.dirname(__file__) not in sys.path:
sys.path.append(os.path.dirname(__file__))
#sys.path.append(os.path.join(os.path.dirname(__file__), 'python-bibtexparser'))
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import convert_to_unicode
# settings cache globals
BIBFILE_PATH = None
SEARCH_IN = None
CITATION_FORMAT = None
QUICKVIEW_FORMAT = None
ENABLE_COMPLETIONS = None
COMPLETIONS_SCOPES = None
PANDOC_FIX = None
EXCLUDE = None
# Internal Cache globals
_PAPERS = {}
_YAMLBIB_PATH = None
_LST_MOD_TIME = {}
_DOCUMENTS = []
_MENU = None
_CITEKEYS = None
def plugin_loaded():
"""Called directly from sublime on plugin load
"""
global BIBFILE_PATH
global SEARCH_IN
global CITATION_FORMAT
global COMPLETIONS_SCOPES
global ENABLE_COMPLETIONS
global EXCLUDE
global PANDOC_FIX
global QUICKVIEW_FORMAT
settings = sublime.load_settings('Citer.sublime-settings')
BIBFILE_PATH = settings.get('bibtex_file_path')
SEARCH_IN = settings.get('search_fields', ["author", "title", "year", "id"])
CITATION_FORMAT = settings.get('citation_format', "@%s")
COMPLETIONS_SCOPES = settings.get('completions_scopes', ['text.html.markdown'])
ENABLE_COMPLETIONS = settings.get('enable_completions', True)
QUICKVIEW_FORMAT = settings.get('quickview_format', '{citekey} - {title}')
PANDOC_FIX = settings.get('auto_merge_citations', False)
EXCLUDE = settings.get('hide_other_completions', True)
refresh_caches()
def plugin_unloaded():
pass
# Papers
def load_yamlbib_path(view):
global _PAPERS
global _YAMLBIB_PATH
filename = view.file_name()
if filename not in _PAPERS:
_PAPERS[filename] = Paper(view)
_YAMLBIB_PATH = _PAPERS[filename].bibpath()
class Paper:
_filepath = None
_bibpath = None
_modified = None
def __init__(self, view):
self.view = view
self._filepath = view.file_name()
def bibpath(self):
modified = os.path.getmtime(self._filepath)
if self._modified != modified:
self._modified = modified
self._bibpath = None
text = self.view.substr(sublime.Region(0, self.view.size()))
yamlP = re.compile(r'^---$.*?((^---$)|(^\.\.\.$))', re.MULTILINE | re.DOTALL)
yamlMatch = yamlP.search(text)
if yamlMatch:
bibP = re.compile(r'^bibliography:', re.MULTILINE)
bibMatch = bibP.search(yamlMatch.group())
if bibMatch:
text = yamlMatch.group()[bibMatch.end():]
pathP = re.compile(r'\S+')
pathMatch = pathP.search(text)
if pathMatch:
folder = os.path.dirname(os.path.realpath(self._filepath))
self._bibpath = os.path.join(folder, pathMatch.group())
return self._bibpath
# Bibfiles
def bibfile_modifed(bib_path):
global _LST_MOD_TIME
bib_path = bib_path.strip()
last_modified_time = os.path.getmtime(bib_path)
cached_modifed_time = _LST_MOD_TIME.get(bib_path)
if cached_modifed_time is None or last_modified_time != cached_modifed_time:
_LST_MOD_TIME[bib_path] = last_modified_time
return True
else:
return False
def load_bibfile(bib_path):
bib_path = bib_path.strip()
with open(bib_path, 'r', encoding="utf-8") as bibfile:
bp = BibTexParser(bibfile.read(), customization=convert_to_unicode)
return list(bp.get_entry_list())
def refresh_caches():
global _DOCUMENTS
global _MENU
global _CITEKEYS
paths = []
if BIBFILE_PATH is not None:
if isinstance(BIBFILE_PATH, list):
paths += BIBFILE_PATH
else:
paths.append(BIBFILE_PATH)
if _YAMLBIB_PATH is not None:
paths.append(_YAMLBIB_PATH)
if len(paths) == 0:
sublime.status_message("WARNING: No BibTex file configured for Citer")
else:
# To avoid duplicate entries, if any bibfiles modified, reload all of them
modified = False
for single_path in paths:
modified = modified or bibfile_modifed(single_path)
if modified:
_DOCUMENTS = []
for single_path in paths:
_DOCUMENTS += load_bibfile(single_path)
_CITEKEYS = [doc.get('id') for doc in _DOCUMENTS]
_MENU = _make_citekey_menu_list(_DOCUMENTS)
# Do some fancy build to get a sane list in the UI
class SafeDict(dict):
def __missing__(self, key):
return '{' + key + '}'
def _parse_authors(auth):
"""
PARSE AUTHORS. Formats:
Single Author: Lastname
Two Authors: Lastname1 and Lastname2
Three or More Authors: Lastname 1 et al.
"""
try:
authors = auth.split(' and ')
lat = len(authors)
if lat == 1:
authors_abbr = authors[0]
elif lat == 2:
authors_abbr = authors[0] + " and " + authors[1]
else:
authors_abbr = authors[0] + " et. al"
except:
authors_abbr = auth
return authors_abbr
def _make_citekey_menu_list(bibdocs):
citekeys = []
for doc in bibdocs:
menu_entry = []
if doc.get('author') is not None:
auths = _parse_authors(doc.get('author'))
else:
auths = 'Anon'
title = string.Formatter().vformat(QUICKVIEW_FORMAT, (),
SafeDict(
citekey=doc.get('id'),
title=doc.get('title'),
author=auths,
year=doc.get('year')
)
)
# title = QUICKVIEW_FORMAT.format(
# citekey=doc.get('id'), title=doc.get('title'))
menu_entry.append(title)
citekeys.append(menu_entry)
citekeys = sorted(citekeys)
return citekeys
def documents():
refresh_caches()
return _DOCUMENTS
def citekeys_menu():
refresh_caches()
return _MENU
def citekeys_list():
refresh_caches()
return _CITEKEYS
class CiterSearchCommand(sublime_plugin.TextCommand):
"""
"""
current_results_list = []
def search_keyword(self, search_term):
results = {}
for doc in documents():
for section_name in SEARCH_IN:
section_text = doc.get(section_name)
if section_text and search_term.lower() in section_text.lower():
txt = QUICKVIEW_FORMAT.format(
citekey=doc.get('id'), title=doc.get('title'))
# ensure we never have duplicates
results[doc.get('id')] = txt
self.current_results_list = list(results.values())
self.view.window().show_quick_panel(
self.current_results_list, self._paste)
def run(self, edit):
self.view.window().show_input_panel(
"Cite search", "", self.search_keyword, None, None)
def is_enabled(self):
"""Determines if the command is enabled
"""
return True
def _paste(self, item):
"""Paste item into buffer
"""
if item == -1:
return
ent = self.current_results_list[item]
ent = ent.split(' ')[0]
citekey = CITATION_FORMAT % ent
if PANDOC_FIX:
self.view.run_command('insert', {'characters': citekey})
self.view.run_command('citer_combine_citations')
else:
self.view.run_command('insert', {'characters': citekey})
class CiterShowKeysCommand(sublime_plugin.TextCommand):
"""
"""
current_results_list = []
def run(self, edit):
ctk = citekeys_menu()
if len(ctk) > 0:
self.current_results_list = ctk
self.view.window().show_quick_panel(self.current_results_list,
self._paste)
def is_enabled(self):
"""Determines if the command is enabled
"""
return True
def _paste(self, item):
"""Paste item into buffer
"""
if item == -1:
return
ent = self.current_results_list[item][0]
ent = ent.split(' ')[0]
citekey = CITATION_FORMAT % ent
if PANDOC_FIX:
self.view.run_command('insert', {'characters': citekey})
self.view.run_command('citer_combine_citations')
else:
self.view.run_command('insert', {'characters': citekey})
class CiterGetTitleCommand(sublime_plugin.TextCommand):
"""
"""
current_results_list = []
def run(self, edit):
ctk = citekeys_menu()
if len(ctk) > 0:
self.current_results_list = ctk
self.view.window().show_quick_panel(self.current_results_list,
self._paste)
def is_enabled(self):
"""Determines if the command is enabled
"""
return True
def _paste(self, item):
"""Paste item into buffer
"""
if item == -1:
return
ent = self.current_results_list[item][0]
title = ent.split(' - ', 1)[1]
self.view.run_command('insert', {'characters': title})
class CiterCompleteCitationEventListener(sublime_plugin.EventListener):
"""docstring for CiterCompleteCitationEventListener"""
def on_query_completions(self, view, prefix, loc):
if ENABLE_COMPLETIONS and any(view.match_selector(loc[0],
scope) for scope in COMPLETIONS_SCOPES):
load_yamlbib_path(view)
search = prefix.replace('@', '').lower()
results = [[key, key] for key in citekeys_list() if search in key.lower()]
if EXCLUDE and len(results) > 0:
return (results, sublime.INHIBIT_WORD_COMPLETIONS)
else:
return results
class CiterCombineCitationsCommand(sublime_plugin.TextCommand):
def run(self, edit):
lstpos = self.view.find_all(r'\]\[')
for i, pos in reversed(list(enumerate(lstpos))):
self.view.replace(edit, pos, r'; ')