-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pre and post text in citation targets. (#316)
- Loading branch information
1 parent
bff8ca9
commit 514231c
Showing
24 changed files
with
555 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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,37 @@ | ||
"""Citation keys.""" | ||
|
||
import re | ||
|
||
from typing import NamedTuple, Iterable | ||
|
||
|
||
class CitationTarget(NamedTuple): | ||
"""Citation key, pre-text, and post-text.""" | ||
|
||
key: str | ||
pre: str | ||
post: str | ||
|
||
|
||
_re_citation_target = re.compile( | ||
r"\s*([{](?P<pre>[^{}]+)[}])?" | ||
r"\s*(?P<key>[^{}\s,]+)" | ||
r"\s*([{](?P<post>[^{}]+)[}])?\s*" | ||
) | ||
|
||
|
||
def parse_citation_targets(targets: str, pos=0) -> Iterable[CitationTarget]: | ||
"""Parse citation target string into a list of citation keys.""" | ||
match = _re_citation_target.match(targets, pos=pos) | ||
if match is None: | ||
raise ValueError(f"malformed citation target: {targets}") | ||
yield CitationTarget( | ||
key=match.group("key") or "", | ||
pre=match.group("pre") or "", | ||
post=match.group("post") or "", | ||
) | ||
end = match.end() | ||
if end < len(targets): | ||
if targets[end] != ",": | ||
raise ValueError(f"malformed citation target: {targets}") | ||
yield from parse_citation_targets(targets, end + 1) |
This file contains 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 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 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 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 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
Oops, something went wrong.