-
Notifications
You must be signed in to change notification settings - Fork 12
Fixing autocomplete and adding citation extraction #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhufe
wants to merge
3
commits into
mangecoeur:master
Choose a base branch
from
dhufe:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """ | ||
| Functions to extract bibliographic keys from a BibTeX file, using the | ||
| keys used in a Markdown or LaTeX file. | ||
|
|
||
| Modified from md2bib.py [1], which is (c) Copyright 2011-2012 by | ||
| Joseph Reagle and licensed under the GPLv3. | ||
|
|
||
| [1] https://github.com/reagle/pandoc-wrappers/ | ||
|
|
||
| """ | ||
|
|
||
| from collections import OrderedDict | ||
| import logging | ||
| import re | ||
|
|
||
|
|
||
| BIBKEY_PAT = '([.:;,\-\w]+)' | ||
|
|
||
|
|
||
| def parse_bibtex(text): | ||
| """Return a dictionary of entry dictionaries, each with a field/value. | ||
| The parser is simple/fast *and* inflexible, unlike the proper but | ||
| slow parsers bibstuff and pyparsing-based parsers. | ||
|
|
||
| """ | ||
| entries = OrderedDict() | ||
| key_pat = re.compile('@' + BIBKEY_PAT + '\{(.*),') | ||
| value_pat = re.compile('[\s]*(\w+)[\s]*=[\s]*{(.*)},?') | ||
| for line in text: | ||
| key_match = key_pat.match(line) | ||
| if key_match: | ||
| entry_type = key_match.group(1) | ||
| key = key_match.group(2) | ||
| entries[key] = OrderedDict({'entry_type': entry_type}) | ||
| continue | ||
| value_match = value_pat.match(line) | ||
| if value_match: | ||
| field, value = value_match.groups() | ||
| entries[key][field] = value | ||
| return entries | ||
|
|
||
|
|
||
| def emit_entry(identifier, values, outfd): | ||
| """Emit a single bibtex entry.""" | ||
| outfd.write('@%s{%s,\n' % (values['entry_type'], identifier)) | ||
| for field, value in values.items(): | ||
| if field != 'entry_type': | ||
| outfd.write(' %s = {%s},\n' % (field, value)) | ||
| outfd.write("}\n\n") | ||
|
|
||
|
|
||
| def emit_bibliography(entries, outfd): | ||
| """Emit a bibtex file.""" | ||
| for identifier, values in entries.items(): | ||
| emit_entry(identifier, values, outfd) | ||
|
|
||
|
|
||
| def subset_bibliography(entries, keys): | ||
| """Emit a subset of a bibtex file based on bibtex keys.""" | ||
| subset = OrderedDict() | ||
| for key in sorted(keys): | ||
| if key in entries: | ||
| subset[key] = entries[key] | ||
| else: | ||
| logging.critical("%s not in entries" % key) | ||
| pass | ||
| return subset | ||
|
|
||
|
|
||
| def get_keys_from_document(filename): | ||
| """Return a list of keys used in filename by looking for citations | ||
| like `@citekey`. | ||
|
|
||
| Also look for citations in the | ||
| `\cite*{key}` style, where `*` can be any character or none. | ||
|
|
||
| """ | ||
| k_md = '\[@' + BIBKEY_PAT + '\]|(?<!\[)@' + BIBKEY_PAT | ||
| k_latex = '\\cite.?\[?(?:.+?)?\]?\{' + BIBKEY_PAT + '\}' | ||
|
|
||
| text = open(filename, 'r', encoding='utf-8').read() | ||
| md = re.findall(k_md, text) | ||
| md_brackets, md_intext = list(zip(*md)) | ||
| md_brackets, md_intext = list(md_brackets), list(md_intext) | ||
|
|
||
| matches = [] | ||
| # Split up finds if necessary to deal with [@key0; @key1] | ||
| for f in md_brackets: | ||
| if '@' in f: | ||
| sub_f = f.replace(' ', '').replace('@', '').split(';') | ||
| matches.extend(sub_f) | ||
| elif f != '': | ||
| matches.append(f) | ||
|
|
||
| matches.extend([i for i in md_intext if i != '']) | ||
|
|
||
| latex = re.findall(k_latex, text) | ||
| for f in latex: | ||
| if ',' in f: | ||
| sub_f = [i.strip() for i in f.split(',')] | ||
| matches.extend(sub_f) | ||
| elif f != '': | ||
| matches.append(f) | ||
|
|
||
| logging.debug('Found keys in document: ' + ', '.join(matches)) | ||
| return matches | ||
|
|
||
|
|
||
| def extract_bibliography(source_doc, source_bib, target_bib): | ||
| # Extract citation keys from source file | ||
| keys = get_keys_from_document(source_doc) | ||
| # Read source bibliography and generate subset | ||
| with open(source_bib, 'r', encoding='utf-8') as f: | ||
| entries = parse_bibtex(f.readlines()) | ||
| subset = subset_bibliography(entries, keys) | ||
| # Write extracted subset to new bibliography file | ||
| with open(target_bib, 'w', encoding='utf-8') as f: | ||
| emit_bibliography(subset, f) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will fail on windows - need to use
os.path.join(or work with pathlib but that's probably overkill)