diff --git a/README.md b/README.md index 39e8406..1b7a984 100755 --- a/README.md +++ b/README.md @@ -10,9 +10,15 @@ Uses the [bibtexparser](https://github.com/sciunto/python-bibtexparser) library You must specify the location of your bibtex file or files in preferences. Multiple files can be added as a list. -Optionally you can define the bibtex fields to search in when using Citer: Search, the default citation format, and the list of scopes to limit the operation of the plugin (by default, Citer will only suggest citations within plain text scopes and is disabled in source code). +Optionally you can define +- `search_fields` the bibtex fields to search in when using Citer: Search +- `citation_format` the citation format +- `completions_scopes` the list of scopes to limit the operation of the plugin (by default, Citer will only suggest citations within plain text scopes and is disabled in source code) +- `enable_completions` enable/disable citation completions (when you hit @) +- `quickview_format` customise the format when listing library entries in the quickvew panel (e.g. with the Citer: Show All command). Place variables beteen `{}` braces. Available variables are `citekey`, `title`, `author`, `year`. +- `auto_merge_citations` Whether to automatically merge citations that are inserted next to each other. `[@Fred2000][@Mary2001]` becomes `[@Fred2000; @Mary2001]`. Equivalent to running `Citer: Combine adjacent citations` on every insert -See below for example configuration +See below for example (default) configuration ```js @@ -33,7 +39,10 @@ See below for example configuration //list of scopes. Could be top level "text" or "source", or limit to // e.g "text.html.markdown" "completions_scopes": ["text"], - "enable_completions": true + "enable_completions": true, + //Customise the quickview of you library, using python format syntax + "quickview_format": "{citekey} - {title}", + "auto_merge_citations": false } ``` @@ -46,6 +55,9 @@ See below for example configuration **Citer: Insert Title** - show all the entries in your bibtex in a searchable quick view, inserts the title +**Citer: Combine adjacent citations** - Combines neighboring citations i.e. `[@Fred2000][@Mary2001]` becomes `[@Fred2000; @Mary2001]` + + # Completions Citer provides autocompletions for your citekeys, these are enabled by default and can be disabled in the config. diff --git a/citer.py b/citer.py index f6c9f84..3bfb86f 100755 --- a/citer.py +++ b/citer.py @@ -4,6 +4,8 @@ import sys import os.path +import re +import string # ST3 loads each package as a module, so it needs an extra prefix @@ -29,9 +31,10 @@ SEARCH_IN = None CITATION_FORMAT = None LST_MOD_TIME = None -QUICKVIEW_FORMAT = "{citekey} - {title}" # this could be configurable +QUICKVIEW_FORMAT = None ENABLE_COMPLETIONS = None COMPLETIONS_SCOPES = None +PANDOC_FIX = None _EXCLUDE = None @@ -50,6 +53,7 @@ def plugin_loaded(): 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') @@ -59,7 +63,8 @@ def plugin_loaded(): CITATION_FORMAT = settings.get('citation_format', "@%s") COMPLETIONS_SCOPES = settings.get('completions_scopes', ['text.html.markdown']) ENABLE_COMPLETIONS = settings.get('enable_completions', True) - PANDOC_FIX = settings.get('pandoc_cite_fix', False) + 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() @@ -83,34 +88,64 @@ def refresh_caches(): with open(single_path, 'r', encoding="utf-8") as bibfile: bp = BibTexParser(bibfile.read(), customization=convert_to_unicode) _DOCUMENTS += list(bp.get_entry_list()) - _MENU = _make_citekey_menu_list(_DOCUMENTS) - _CITEKEYS = [doc.get('id') for doc in _DOCUMENTS] else: last_modified_time = os.path.getmtime(BIBFILE_PATH) + if LST_MOD_TIME is None or last_modified_time != LST_MOD_TIME: + LST_MOD_TIME = last_modified_time + with open(BIBFILE_PATH, 'r', encoding="utf-8") as bibfile: + bp = BibTexParser(bibfile.read(), customization=convert_to_unicode) + _DOCUMENTS = list(bp.get_entry_list()) + _CITEKEYS = [doc.get('id') for doc in _DOCUMENTS] + _MENU = _make_citekey_menu_list(_DOCUMENTS) - if LST_MOD_TIME is None or last_modified_time != LST_MOD_TIME: - LST_MOD_TIME = last_modified_time - with open(BIBFILE_PATH, 'r', encoding="utf-8") as bibfile: - bp = BibTexParser(bibfile.read(), customization=convert_to_unicode) - _DOCUMENTS = list(bp.get_entry_list()) - _MENU = _make_citekey_menu_list(_DOCUMENTS) - _CITEKEYS = [doc.get('id') for doc in _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 len(doc.get('title')) > 90: - # title = doc.get('id') + ' - ' + doc.get('title')[0:90] - # menu_entry.append(title) - # menu_entry.append(' ' + doc.get('title')[90:]) - # else: - title = QUICKVIEW_FORMAT.format( - citekey=doc.get('id'), title=doc.get('title')) + + 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) @@ -173,7 +208,7 @@ def _paste(self, item): citekey = CITATION_FORMAT % ent if PANDOC_FIX: self.view.run_command('insert', {'characters': citekey}) - self.view.run_command('find_replace_bracket') + self.view.run_command('citer_combine_citations') else: self.view.run_command('insert', {'characters': citekey}) @@ -206,7 +241,7 @@ def _paste(self, item): citekey = CITATION_FORMAT % ent if PANDOC_FIX: self.view.run_command('insert', {'characters': citekey}) - self.view.run_command('find_replace_bracket') + self.view.run_command('citer_combine_citations') else: self.view.run_command('insert', {'characters': citekey}) @@ -262,4 +297,3 @@ def run(self, edit): lstpos = self.view.find_all(r'\]\[') for i, pos in reversed(list(enumerate(lstpos))): self.view.replace(edit, pos, r'; ') -