Skip to content

Commit

Permalink
pandoc-citeproc: fix multiple references in brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel authored and bdo311 committed Apr 7, 2017
1 parent fd510a6 commit 1f00cac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 15 additions & 1 deletion build/citations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ def validate_reference(ref):
return None


bracketed_reference_pattern = re.compile(r'\[(@.+?)\]', flags=re.DOTALL)


def get_references_from_text(text):
"""
Extract the set of references in a text
"""
refs = set()
for ref_text in re.findall(r'\[(@.+?)\]', text, flags=re.DOTALL):
for ref_text in bracketed_reference_pattern.findall(text):
for ref in ref_text.split():
if not ref:
continue
refs.add(ref)
return refs


def semicolon_separate_references(text):
"""
Separate multiple references inside the same brackets with a space and
semicolon for pandoc-citeproc.
"""
return bracketed_reference_pattern.sub(
repl=lambda x: '; '.join(x.group().split()),
string=text
)


def get_brackets_without_reference(text):
"""
Find bracketed text that does not start with @. Does not match
Expand Down
3 changes: 3 additions & 0 deletions build/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_brackets_without_reference,
get_references_from_text,
get_text,
semicolon_separate_references,
validate_reference,
)

Expand Down Expand Up @@ -120,6 +121,8 @@ def get_standard_citatation(citation, cache):
old = re.escape(old)
new = f'@{new}'
converted_text = re.sub(old + '(?=[\s\]])', new, converted_text)
# Semicolon separate multiple refernces for pandoc-citeproc
converted_text = semicolon_separate_references(converted_text)

# Write manuscript for pandoc
with gen_dir.joinpath('all-sections.md').open('wt') as write_file:
Expand Down

0 comments on commit 1f00cac

Please sign in to comment.