literature.utils#

literature.utils holds two helper functions that no model owns.

  • parse_date_parts turns one CSL JSON date-parts entry, such as [2019, 8], into a PartialDate with the same precision. It returns None for a value that is not a number, and raises ValidationError for numbers that name no date, such as month 13.

  • doi_url turns a DOI stored without its resolver, such as 10.1002/abc, into a https://doi.org/ address, or returns None for a value not shaped like a DOI. The scheme and host are fixed, so the stored text can only ever become the path of the link. Item.doi_url and the reference page both use it.

Helper functions for CSL JSON dates and DOIs.

doi_url(value: str | None) → str | None[source]#

Return the doi.org address for a bare DOI, or None if it is not one.

The scheme and host are fixed here, so the stored text can only ever become the path of an https link. Anything not shaped like a DOI (10. followed by a registrant and a /) stays plain text. It lives in the core because Item.doi_url gives every host project the same link.

Parameters:

value – The stored DOI text, or None.

Returns:

The https://doi.org/ link, or None if value is not a DOI.

parse_date_parts(date_parts: list) → PartialDate | None[source]#

Convert a single CSL JSON date-parts entry to a PartialDate.

A date-parts array holds one or two date arrays of 1-3 integers: [year], [year, month] or [year, month, day]. Month is 1-based.

Parameters:

date_parts – A single date array from CSL JSON date-parts, e.g. [2019], [2019, 8], or [2019, 8, 16].

Returns:

A date with precision matching the number of components provided, or None if parsing fails (empty list, values that are not numbers, etc.).

Raises:

ValidationError – The numbers name no date, such as month 13.

Examples

>>> str(parse_date_parts([2019]))
'2019'
>>> str(parse_date_parts([2019, 8]))
'2019-08'
>>> str(parse_date_parts([2019, 8, 16]))
'2019-08-16'
>>> parse_date_parts([]) is None
True