Skip to content

bib.bibtexparser.middlewares.library.generating_entrykeys

Classes

GenerateEntriesCiteKey

GenerateEntriesCiteKey(
    abbr_article_pattern_dict,
    abbr_inproceedings_pattern_dict,
    allow_inplace_modification=True,
)

Bases: LibraryMiddleware

Generate entries key of a library.

The entry.key is also Cite Key.

Source code in pybibtexer/bib/bibtexparser/middlewares/library/generating_entrykeys.py
def __init__(
    self,
    abbr_article_pattern_dict: dict,
    abbr_inproceedings_pattern_dict: dict,
    allow_inplace_modification: bool = True,
):
    super().__init__(allow_inplace_modification=allow_inplace_modification)

    self.abbr_article_pattern_dict = abbr_article_pattern_dict
    self.abbr_inproceedings_pattern_dict = abbr_inproceedings_pattern_dict

Functions

generate_cite_key
generate_cite_key(entry)

Generate user citation key.

Source code in pybibtexer/bib/bibtexparser/middlewares/library/generating_entrykeys.py
def generate_cite_key(self, entry: Entry) -> str:
    """Generate user citation key."""
    prefix = generate_cite_key_prefix(
        entry,
        self.abbr_article_pattern_dict,
        self.abbr_inproceedings_pattern_dict,
    )

    cite_key = self.generate_google_cite_key(entry)
    if prefix != "":
        cite_key = prefix + "_" + cite_key
    return cite_key
generate_google_cite_key
generate_google_cite_key(entry)

Generate google citation key.

Source code in pybibtexer/bib/bibtexparser/middlewares/library/generating_entrykeys.py
def generate_google_cite_key(self, entry: Entry) -> str:
    """Generate google citation key."""
    author = entry["author"] if "author" in entry else ""
    family_name = self._obtain_family_name(author).lower()

    year = entry["year"] if "year" in entry else ""

    first_word_of_title = ""
    title = entry["title"] if "title" in entry else ""
    regex = re.compile(r"\\href{(.*)}{(.*)}")
    if mch := regex.search(title):
        title = mch.group(2)

    word_list = [w.lower() for w in re.split(r"\s+", title) if w.strip()]
    word_list = [re.sub(r"[^a-zA-Z0-9]", "", w) for w in word_list]
    for w in word_list:
        if w not in SKIP_WORD_IN_CITATION_KEY:
            first_word_of_title = w
            break

    citation_key = family_name + year + first_word_of_title
    return re.sub(r"[^a-zA-Z0-9]", "", citation_key)