|
| 1 | +# Author: axel@cern.ch, 2021-07-08 |
| 2 | +# License: LGPL2.1+, see file `LICENSE` in this folder. |
| 3 | + |
| 4 | +import pypandoc |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import datetime |
| 8 | +import os, sys |
| 9 | + |
| 10 | +import ROOT # for ROOT.__version__ |
| 11 | + |
| 12 | +""" |
| 13 | +Generate a CITATION.cff-style YAML block for the ROOT project based on a Markdown input file. |
| 14 | +
|
| 15 | +This script reads a Markdown file (e.g. `makeCITATION_example_input.md`, but |
| 16 | +typically the ROOT release notes) containing a bullet list of authors and |
| 17 | +optional ORCIDs, converts it to a Pandoc JSON AST using pypandoc, extracts |
| 18 | +author information. |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +def printNames(nameBlock): |
| 23 | + # print(json.dumps(nameBlock)) |
| 24 | + nameParts = [c for c in nameBlock] |
| 25 | + firstLastNames = [n["c"].replace("\u00a0", " ") for n in nameParts if n["t"] == "Str"] |
| 26 | + firstName = firstLastNames[0] |
| 27 | + lastName = firstLastNames[1] |
| 28 | + if lastName[-1] == ",": |
| 29 | + lastName = lastName[0:-1] |
| 30 | + print(" - family-names:", lastName) |
| 31 | + print(" given-names:", firstName) |
| 32 | + |
| 33 | + |
| 34 | +parser = argparse.ArgumentParser() |
| 35 | +parser.add_argument("releasenotes") |
| 36 | +parser.add_argument("--dump-ast", action="store_true") |
| 37 | +args = parser.parse_args() |
| 38 | + |
| 39 | +jsonRN = json.loads(pypandoc.convert_file(args.releasenotes, "json")) |
| 40 | +if args.dump_ast: |
| 41 | + print(json.dumps(jsonRN, indent=1)) |
| 42 | + sys.exit(0) |
| 43 | + |
| 44 | + |
| 45 | +print( |
| 46 | + """cff-version: 1.1.0 |
| 47 | +message: "If you use ROOT, please cite it as below." |
| 48 | +authors:""" |
| 49 | +) |
| 50 | + |
| 51 | +for block in jsonRN["blocks"]: |
| 52 | + if "t" in block and block["t"] == "BulletList": |
| 53 | + for item in block["c"]: |
| 54 | + itemC = item[0]["c"] |
| 55 | + if itemC[0]["t"] == "Link": |
| 56 | + # 0th element is ["", [], []] ? |
| 57 | + printNames(itemC[0]["c"][1]) |
| 58 | + orcid = itemC[0]["c"][2][0] |
| 59 | + print(" orcid:", orcid) |
| 60 | + elif itemC[0]["t"] == "Str": |
| 61 | + printNames(itemC) |
| 62 | + else: |
| 63 | + print('ERROR: expected "Link" or "Str" in', json.dumps(itemC[0], indent=1)) |
| 64 | + sys.exit(1) |
| 65 | + break |
| 66 | + |
| 67 | +print( |
| 68 | + f"""title: "ROOT: analyzing, storing and visualizing big data, scientifically" |
| 69 | +version: {ROOT.__version__} |
| 70 | +doi: 10.5281/zenodo.848818 |
| 71 | +date-released: {datetime.datetime.now().strftime("%Y-%m-%d")} |
| 72 | +""" |
| 73 | +) |
0 commit comments