CSL-JSON Writer

This notebook shows how to generate a metadata in CSL-JSON format. CSL-JSON is used by the Citation Styles Language to generate formatted citations and bibliographies.

As with all commonmeta-py Writer notebooks, we start by fetching metadata, in this example a journal article via its Crossref DOI, and convert them to the internal commonmeta format.

from commonmeta import Metadata

# Fetch metadata from a DOI
string = '10.7554/elife.01567'
metadata = Metadata(string)

# Check that metadata was fetched successfully
print(metadata.state)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/Users/mfenner/Documents/commonmeta-py/docs/writers/csl_writer.ipynb Zelle 3 line 1
----> <a href='vscode-notebook-cell:/Users/mfenner/Documents/commonmeta-py/docs/writers/csl_writer.ipynb#W2sZmlsZQ%3D%3D?line=0'>1</a> from commonmeta import Metadata
      <a href='vscode-notebook-cell:/Users/mfenner/Documents/commonmeta-py/docs/writers/csl_writer.ipynb#W2sZmlsZQ%3D%3D?line=2'>3</a> # Fetch metadata from a DOI
      <a href='vscode-notebook-cell:/Users/mfenner/Documents/commonmeta-py/docs/writers/csl_writer.ipynb#W2sZmlsZQ%3D%3D?line=3'>4</a> string = '10.7554/elife.01567'

File ~/Library/Python/3.11/lib/python/site-packages/commonmeta_py-0.6.0-py3.11.egg/commonmeta/__init__.py:17
     14 __author__ = "Martin Fenner"
     15 __license__ = "MIT"
---> 17 from .metadata import Metadata

File ~/Library/Python/3.11/lib/python/site-packages/commonmeta_py-0.6.0-py3.11.egg/commonmeta/metadata/__init__.py:3
      1 # -*- coding: utf-8 -*-
----> 3 from .metadata import Metadata

File ~/Library/Python/3.11/lib/python/site-packages/commonmeta_py-0.6.0-py3.11.egg/commonmeta/metadata/metadata.py:8
      5 import yaml
      6 import xmltodict
----> 8 from ..readers import (
      9     get_crossref,
     10     read_crossref,
     11     get_datacite,
     12     read_datacite,
     13     read_datacite_xml,
     14     get_crossref_xml,
     15     read_crossref_xml,
     16     get_schema_org,
     17     read_schema_org,
     18     get_codemeta,
     19     read_citeproc,
     20     read_codemeta,
     21     get_cff,
     22     read_cff
     23 )
     24 from ..writers import (
     25     write_datacite,
     26     write_bibtex,
   (...)
     31     write_commonmeta
     32 )
     33 from ..utils import normalize_id, find_from_format

File ~/Library/Python/3.11/lib/python/site-packages/commonmeta_py-0.6.0-py3.11.egg/commonmeta/readers/__init__.py:2
      1 """Readers for different metadata formats"""
----> 2 from .crossref_reader import get_crossref, read_crossref
      3 from .datacite_reader import get_datacite, read_datacite
      4 from .schema_org_reader import get_schema_org, read_schema_org

File ~/Library/Python/3.11/lib/python/site-packages/commonmeta_py-0.6.0-py3.11.egg/commonmeta/readers/crossref_reader.py:16
     14 from ..base_utils import wrap, compact, presence, sanitize
     15 from ..author_utils import get_authors
---> 16 from ..date_utils import get_date_from_date_parts
     17 from ..doi_utils import doi_as_url, doi_from_url, get_doi_ra, crossref_api_url
     18 from ..constants import (
     19     CR_TO_BIB_TRANSLATIONS,
     20     CR_TO_CP_TRANSLATIONS,
   (...)
     24     Commonmeta,
     25 )

File ~/Library/Python/3.11/lib/python/site-packages/commonmeta_py-0.6.0-py3.11.egg/commonmeta/date_utils.py:5
      3 from datetime import datetime as dt
      4 from typing import Optional, Union
----> 5 import dateparser
      6 import pydash as py_
      7 from .utils import wrap

File ~/.pyenv/versions/3.9.16/lib/python3.9/site-packages/dateparser/__init__.py:3
      1 __version__ = '1.1.8'
----> 3 from .date import DateDataParser
      4 from .conf import apply_settings
      6 _default_parser = DateDataParser()

File ~/.pyenv/versions/3.9.16/lib/python3.9/site-packages/dateparser/date.py:6
      3 from collections.abc import Set
      4 from datetime import datetime, timedelta
----> 6 import regex as re
      7 from tzlocal import get_localzone
      8 from dateutil.relativedelta import relativedelta

File ~/.pyenv/versions/3.9.16/lib/python3.9/site-packages/regex/__init__.py:1
----> 1 from .regex import *
      2 from . import regex
      3 __all__ = regex.__all__

File ~/.pyenv/versions/3.9.16/lib/python3.9/site-packages/regex/regex.py:421
    416     return r
    418 # --------------------------------------------------------------------
    419 # Internals.
--> 421 import regex._regex_core as _regex_core
    422 import regex._regex as _regex
    423 from threading import RLock as _RLock

File ~/.pyenv/versions/3.9.16/lib/python3.9/site-packages/regex/_regex_core.py:21
     18 import unicodedata
     19 from collections import defaultdict
---> 21 import regex._regex as _regex
     23 __all__ = ["A", "ASCII", "B", "BESTMATCH", "D", "DEBUG", "E", "ENHANCEMATCH",
     24   "F", "FULLCASE", "I", "IGNORECASE", "L", "LOCALE", "M", "MULTILINE", "P",
     25   "POSIX", "R", "REVERSE", "S", "DOTALL", "T", "TEMPLATE", "U", "UNICODE",
     26   "V0", "VERSION0", "V1", "VERSION1", "W", "WORD", "X", "VERBOSE", "error",
     27   "Scanner", "RegexFlag"]
     29 # The regex exception.

ModuleNotFoundError: No module named 'regex._regex'

Generate CSL-JSON

We can now generate CSL-JSON from the metadata in commonmeta format.

csl = metadata.write(to="csl")
print(csl)
{
    "type": "article-journal",
    "id": "https://doi.org/10.7554/elife.01567",
    "DOI": "10.7554/elife.01567",
    "URL": "https://elifesciences.org/articles/01567",
    "categories": [
        "General Immunology and Microbiology",
        "General Biochemistry, Genetics and Molecular Biology",
        "General Medicine",
        "General Neuroscience"
    ],
    "language": "en",
    "author": [
        {
            "family": "Sankar",
            "given": "Martial"
        },
        {
            "family": "Nieminen",
            "given": "Kaisa"
        },
        {
            "family": "Ragni",
            "given": "Laura"
        },
        {
            "family": "Xenarios",
            "given": "Ioannis"
        },
        {
            "family": "Hardtke",
            "given": "Christian S"
        }
    ],
    "contributor": [],
    "issued": {
        "date-parts": [
            [
                2014,
                2,
                11
            ]
        ]
    },
    "abstract": "Among various advantages, their small size makes model organisms preferred subjects of investigation. Yet, even in model systems detailed analysis of numerous developmental processes at cellular level is severely hampered by their scale. For instance, secondary growth of Arabidopsis hypocotyls creates a radial pattern of highly specialized tissues that comprises several thousand cells starting from a few dozen. This dynamic process is difficult to follow because of its scale and because it can only be investigated invasively, precluding comprehensive understanding of the cell proliferation, differentiation, and patterning events involved. To overcome such limitation, we established an automated quantitative histology approach. We acquired hypocotyl cross-sections from tiled high-resolution images and extracted their information content using custom high-throughput image processing and segmentation. Coupled with automated cell type recognition through machine learning, we could establish a cellular resolution atlas that reveals vascular morphodynamics during secondary growth, for example equidistant phloem pole formation.",
    "container-title": "eLife",
    "volume": "3",
    "publisher": "eLife Sciences Publications, Ltd",
    "title": "Automated quantitative histology reveals vascular morphodynamics during Arabidopsis hypocotyl secondary growth",
    "copyright": "CC-BY-3.0"
}

CSL-JSON is similar to the JSON – e.g. in using date-parts for dates. However, it has some differences, e.g. in the type field. commonmeta therefore supports specific types for CSL-JSON in addition to the ResourceType used for Crossref metadata.