├── README.md ├── customize_journal.json ├── journal_abbrev.py └── journals.json /README.md: -------------------------------------------------------------------------------- 1 | # bib-journal-abbreviation 2 | There are some bibtex tools out there to help abbreviate journal names. However, I found that those packages are often over-complicated with many functions and require extra dependencies which sometimes break in my early trial due to lack of maintenance. For the sole purpose of abbreviating journal names, this script provides a lightweight way to transform your latex bib file without extra dependencies. This script abbreviates the journal names according to the built-in data file `journals.json` from [betterbib](https://github.com/nschloe/betterbib). This data file should satisfiy the requirements in most cases but users can customize the abbreviations in their own json file as well. 3 | 4 | 5 | 6 | ### Usage 7 | 8 | ```bash 9 | cat /path/to/old/bib/file | python journal_abbrev.py > /path/to/new/bib/file 10 | ``` 11 | 12 | Users can specify their own extra abbreviation rules through customized json file with additionl argument to the python script as `--user-json` (refer to `customize_journal.json` for an example). The customized abbreviation rules would override the default one in the case of conflict. 13 | 14 | ### Acknowledgement 15 | This is motivated by Wanzhen He. 16 | -------------------------------------------------------------------------------- /customize_journal.json: -------------------------------------------------------------------------------- 1 | { 2 | "The Journal of Physical Chemistry C": "J. Phys. Chem. C", 3 | "Nuclear Instruments and Methods in Physics Research Section B: Beam Interactions with Materials and Atoms": "Nucl. Instrum. Methods Phys. Res., B" 4 | } -------------------------------------------------------------------------------- /journal_abbrev.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import json 3 | import re 4 | import argparse 5 | 6 | def abbreviate(line, journal_to_abbr): 7 | if re.search('".*"', line) is not None: 8 | journal_name_template = '"{}"' 9 | journal_str = re.search('".*"', line).group(0) 10 | elif re.search('{.*}', line) is not None: 11 | journal_name_template = '{{{}}}' 12 | journal_str = re.search('{.*}', line).group(0) 13 | else: 14 | raise ValueError('the format "{}" is not valid'.format(line)) 15 | journal_name_strip = journal_str[1:-1] 16 | journal_name = journal_name_strip.replace('{','').replace('}','') 17 | journal_name = journal_to_abbr.get(journal_name.lower(), journal_name_strip) 18 | journal_name = journal_name_template.format(journal_name) 19 | 20 | return line.replace(journal_str, journal_name) 21 | 22 | 23 | 24 | 25 | def main(journal_to_abbr): 26 | for line in sys.stdin: 27 | line_strip = line.strip() 28 | if line_strip.startswith('journal'): 29 | new_line = abbreviate(line, journal_to_abbr) 30 | print(new_line.rstrip()) 31 | else: 32 | print(line.rstrip()) 33 | 34 | 35 | if __name__ == '__main__': 36 | parser = argparse.ArgumentParser(description="Journal abbreviation") 37 | parser.add_argument('--user-json', type=str, default=None, help="customized json file") 38 | args = parser.parse_args() 39 | 40 | with open('journals.json') as fin: 41 | journal_to_abbr = json.load(fin) 42 | 43 | if args.user_json is not None: 44 | with open(args.user_json) as fin: 45 | customize_json = json.load(fin) 46 | journal_to_abbr.update(customize_json) 47 | 48 | journal_to_abbr = {k.lower(): v for k, v in journal_to_abbr.items()} 49 | 50 | main(journal_to_abbr) 51 | --------------------------------------------------------------------------------