Skip to content

bib.bibtexparser.middlewares.block.entry_field_values_normalize

Classes

AddUrlToFieldValueInEntry

AddUrlToFieldValueInEntry(
    field_key, allow_inplace_modification=True
)

Bases: BlockMiddleware

Add url link to title.

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def __init__(self, field_key: str, allow_inplace_modification: bool = True):
    super().__init__(allow_inplace_modification=allow_inplace_modification, allow_parallel_execution=True)

    self.field_key = field_key

NormalizeEntryFieldValues

NormalizeEntryFieldValues(
    field_keys=["journal", "booktitle"],
    title_lower_upper="upper",
    allow_inplace_modification=True,
)

Bases: BlockMiddleware

Normalize some field values (journal and booktitle) to upper case.

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def __init__(
    self,
    field_keys: list[str] = ["journal", "booktitle"],
    title_lower_upper: str = "upper",
    allow_inplace_modification: bool = True,
):
    super().__init__(allow_inplace_modification=allow_inplace_modification, allow_parallel_execution=True)

    self._field_keys = field_keys
    self.title_lower_upper = title_lower_upper

NormalizeFieldValuesInEntry

NormalizeFieldValuesInEntry(
    field_key,
    sentence_title,
    allow_inplace_modification=True,
)

Bases: BlockMiddleware

Sentence field values.

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def __init__(self, field_key: str, sentence_title: str, allow_inplace_modification: bool = True):
    super().__init__(allow_inplace_modification=allow_inplace_modification, allow_parallel_execution=True)

    self.field_key = field_key
    self.sentence_title = sentence_title

Functions

__generate_new_case_title
__generate_new_case_title(old_title, flag)

Generate new title.

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def __generate_new_case_title(self, old_title: str, flag: str) -> str:
    """Generate new title."""
    old_list, new_list = re.split(r"\s+", old_title), []
    for i in range(len(old_list)):
        old_str = old_list[i]
        if re.search(r"-", old_str):
            temp_list, new_temp_list = re.split("-", old_str), []
            if i == 0:  # for the first element
                new_temp_list = [self.__upper_or_lower_first_letter(temp_list[0], "upper")]
                temp_list = temp_list[1:]
            for t in temp_list:
                if len(t.strip()) == 1:
                    new_temp_list.append(t)  # not change
                else:
                    if flag == "sentence":
                        new_temp_list.append(self.__lower_first_letter_and_others_not_contain_uppers(t))
                    elif flag == "title":
                        new_temp_list.append(self.__upper_first_letter_and_others_not_contain_uppers(t))
                    else:
                        pass
            new_list.append("-".join(new_temp_list))
        else:
            if i == 0:
                new_list.append(self.__upper_or_lower_first_letter(old_str, "upper"))
            else:
                if flag == "sentence":
                    new_list.append(self.__lower_first_letter_and_others_not_contain_uppers(old_str))
                elif flag == "title":
                    new_list.append(self.__upper_first_letter_and_others_not_contain_uppers(old_str))
                else:
                    pass
    return " ".join(new_list)
__lower_first_letter_and_others_not_contain_uppers
__lower_first_letter_and_others_not_contain_uppers(
    input_str,
)

Lower.

Input: About; A; $about; ABOUT; aBOUT Output: about; a; $about; ABOUT; aBOUT

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def __lower_first_letter_and_others_not_contain_uppers(self, input_str: str) -> str:
    """Lower.

    Input: About; A; $about; ABOUT; aBOUT
    Output: about; a; $about; ABOUT; aBOUT
    """
    new_input_str = input_str.strip()
    if new_input_str and (not re.search(r"[A-Z]", new_input_str[1:])):  # Others not contain upper letter
        input_str = self.__upper_or_lower_first_letter(input_str, "lower")  # Lower
    return input_str
__upper_first_letter_and_others_not_contain_uppers
__upper_first_letter_and_others_not_contain_uppers(
    input_str,
)

Upper.

Input: about; a; $about; ABOUT; abOUT Output: About; A; $about; ABOUT; abOUT

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def __upper_first_letter_and_others_not_contain_uppers(self, input_str: str) -> str:
    """Upper.

    Input: about; a; $about; ABOUT; abOUT
    Output: About; A; $about; ABOUT; abOUT
    """
    new_input_str = input_str.strip()
    if new_input_str.lower() in SKIP_WORD_IN_CITATION_KEY:
        return new_input_str.lower()

    if new_input_str and (not re.search(r"[A-Z]", new_input_str[1:])):  # Others not contain upper letter
        input_str = self.__upper_or_lower_first_letter(input_str, "upper")  # upper
    return input_str
__upper_or_lower_first_letter staticmethod
__upper_or_lower_first_letter(input_str, flag)

Upper or lower first letter.

Check whether the first is in the a-zA-Z and then UPPER or LOWER it. flag = upper Input: about; $food; About; aBout Output: About; $food; About; ABout flag = lower Input: About; $food; about; ABout Output: about; $food; about; aBout

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
@staticmethod
def __upper_or_lower_first_letter(input_str: str, flag: str) -> str:
    """Upper or lower first letter.

    Check whether the first is in the a-zA-Z and then UPPER or LOWER it.
    flag = upper
    Input: about; $food; About; aBout
    Output: About; $food; About; ABout
    flag = lower
    Input: About; $food; about; ABout
    Output: about; $food; about; aBout
    """
    new_input_str = input_str.strip()
    if new_input_str and re.search(r"[a-zA-Z]", new_input_str[0]):
        if flag == "lower":
            new_input_str = new_input_str[0].lower() + new_input_str[1:]
        elif flag == "upper":
            new_input_str = new_input_str[0].upper() + new_input_str[1:]
        else:
            new_input_str = input_str
    return new_input_str
generate_standard_sentence_case
generate_standard_sentence_case(title_content)

Generate standard title.

"Hello, world".upper() # HELLO WORLD "HELLO, WORLD".lower() # hello world "hello, world".capitalize() # Hello, world "hello, world".title() # Hello, World

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def generate_standard_sentence_case(self, title_content: str) -> str:
    """Generate standard title.

    "Hello, world".upper() # HELLO WORLD
    "HELLO, WORLD".lower() # hello world
    "hello, world".capitalize() # Hello, world
    "hello, world".title() # Hello, World
    """
    return self._generate_standard_title(title_content, "sentence")
generate_standard_title_case
generate_standard_title_case(title_content)

Generate standard title.

"Hello, world".upper() # HELLO WORLD "HELLO, WORLD".lower() # hello world "hello, world".capitalize() # Hello, world "hello, world".title() # Hello, World

Source code in pybibtexer/bib/bibtexparser/middlewares/block/entry_field_values_normalize.py
def generate_standard_title_case(self, title_content: str) -> str:
    """Generate standard title.

    "Hello, world".upper() # HELLO WORLD
    "HELLO, WORLD".lower() # hello world
    "hello, world".capitalize() # Hello, world
    "hello, world".title() # Hello, World
    """
    return self._generate_standard_title(title_content, "title")