literature.utils#
literature.utils holds two helper functions that no model owns.
parse_date_partsturns one CSL JSONdate-partsentry, such as[2019, 8], into aPartialDatewith the same precision. It returnsNonefor a value that is not a number, and raisesValidationErrorfor numbers that name no date, such as month 13.doi_urlturns a DOI stored without its resolver, such as10.1002/abc, into ahttps://doi.org/address, or returnsNonefor 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_urland 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
Noneif 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 becauseItem.doi_urlgives every host project the same link.- Parameters:
value – The stored DOI text, or
None.- Returns:
The
https://doi.org/link, orNoneifvalueis 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-partsarray 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
Noneif 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