|
| 1 | +# yml front matter for paper from citaton.cff file |
| 2 | + |
| 3 | +import ruamel.yaml |
| 4 | +from pathlib import Path |
| 5 | +from rich import print |
| 6 | + |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +yaml = ruamel.yaml.YAML() |
| 10 | +yaml.indent(mapping=2, sequence=4, offset=2) |
| 11 | + |
| 12 | +CITATION_CFF = Path(__file__).parent.parent.parent.joinpath("CITATION.cff") |
| 13 | + |
| 14 | +first_author = "Gau" |
| 15 | +sort_authors_alphabetically = True |
| 16 | + |
| 17 | + |
| 18 | +def return_author_order(author_list, first_author): |
| 19 | + """Return the order of authors in the paper.""" |
| 20 | + author_order = [x["family-names"].strip() for x in author_list] |
| 21 | + if sort_authors_alphabetically: |
| 22 | + author_order = sorted(author_order) |
| 23 | + author_order.pop(author_order.index(first_author)) |
| 24 | + author_order.insert(0, first_author) |
| 25 | + print(author_order) |
| 26 | + return author_order |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + with open(CITATION_CFF, encoding="utf8") as f: |
| 31 | + citation = yaml.load(f) |
| 32 | + |
| 33 | + author_order = return_author_order(citation["authors"], first_author) |
| 34 | + author_names = [x["family-names"].strip() for x in citation["authors"]] |
| 35 | + |
| 36 | + author_list = [] |
| 37 | + affiliation_list = [] |
| 38 | + |
| 39 | + for this_author_name in author_order: |
| 40 | + |
| 41 | + author = citation["authors"][author_names.index(this_author_name)] |
| 42 | + |
| 43 | + this_author = { |
| 44 | + "name": f"{author['given-names']} {author.get('family-names', '')}".strip() |
| 45 | + } |
| 46 | + |
| 47 | + if author.get("orcid", None) is not None: |
| 48 | + this_author["orcid"] = author.get("orcid").replace("https://orcid.org/", "") |
| 49 | + |
| 50 | + if author.get("affiliation") is not None: |
| 51 | + |
| 52 | + this_affiliation = author.get("affiliation") |
| 53 | + affiliation_list_names = [x["name"] for x in affiliation_list] |
| 54 | + |
| 55 | + if this_affiliation not in affiliation_list_names: |
| 56 | + affiliation_list.append( |
| 57 | + {"name": this_affiliation, "index": len(affiliation_list) + 1} |
| 58 | + ) |
| 59 | + |
| 60 | + affiliation_list_names = [x["name"] for x in affiliation_list] |
| 61 | + this_author["affiliation"] = ( |
| 62 | + affiliation_list_names.index(this_affiliation) + 1 |
| 63 | + ) |
| 64 | + |
| 65 | + author_list.append(this_author) |
| 66 | + |
| 67 | + content = { |
| 68 | + "title": "", |
| 69 | + "tags": citation["keywords"], |
| 70 | + "authors": author_list, |
| 71 | + "affiliations": affiliation_list, |
| 72 | + "date": datetime.now().strftime("%Y-%m-%d"), |
| 73 | + "bibliography": "paper.bib", |
| 74 | + } |
| 75 | + |
| 76 | + with open("metadata.yml", "w", encoding="utf8") as output_file: |
| 77 | + return yaml.dump(content, output_file) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments