summaryrefslogtreecommitdiffstats
path: root/python/fatcat_tools/transforms/csl.py
blob: 03410ffbfaee57ac7407eefba23193a1666f005d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import json
from typing import Any, Dict, List

from citeproc import (
    Citation,
    CitationItem,
    CitationStylesBibliography,
    CitationStylesStyle,
    formatter,
)
from citeproc.source.json import CiteProcJSON
from citeproc_styles import get_style_filepath
from fatcat_openapi_client import ReleaseContrib, ReleaseEntity


def contribs_by_role(contribs: List[ReleaseContrib], role: str) -> List[ReleaseContrib]:
    ret = [c.copy() for c in contribs if c["role"] == role]
    [c.pop("role") for c in ret]
    # TODO: some note to self here
    [c.pop("literal") for c in ret if "literal" in c]
    if not ret:
        return []
    else:
        return ret


def release_to_csl(entity: ReleaseEntity) -> Dict[str, Any]:
    """
    Returns a python dict which can be json.dumps() to get a CSL-JSON (aka,
    citeproc-JSON, aka Citation Style Language JSON)

    This function will likely become an API method/endpoint

    Follows, but not enforced by: https://github.com/citation-style-language/schema/blob/master/csl-data.json
    """
    contribs = []
    for contrib in entity.contribs or []:
        if contrib.creator:
            # Default to "local" (publication-specific) metadata; fall back to
            # creator-level
            family = (
                contrib.creator.surname
                or contrib.surname
                or (contrib.raw_name and contrib.raw_name.split()[-1])
            )
            if not family:
                # CSL requires some surname (family name)
                continue
            c = dict(
                family=family,
                given=contrib.creator.given_name or contrib.given_name,
                # dropping-particle
                # non-dropping-particle
                # suffix
                # comma-suffix
                # static-ordering
                literal=contrib.creator.display_name or contrib.raw_name,
                # parse-names,
                # role must be defined; default to author
                role=contrib.role or "author",
            )
        else:
            family = contrib.surname or (contrib.raw_name and contrib.raw_name.split()[-1])
            if not family:
                # CSL requires some surname (family name)
                continue
            c = dict(
                family=family,
                given=contrib.given_name,
                literal=contrib.raw_name,
                # role must be defined; default to author
                role=contrib.role or "author",
            )
        for k in list(c.keys()):
            if not c[k]:
                c.pop(k)
        contribs.append(c)
    if not contribs:
        raise ValueError("citeproc requires at least one author with a surname")
    abstract = None
    if entity.abstracts:
        abstract = entity.abstracts[0].content

    issued_date = None
    if entity.release_date:
        issued_date = {
            "date-parts": [
                [
                    entity.release_date.year,
                    entity.release_date.month,
                    entity.release_date.day,
                ]
            ]
        }
    elif entity.release_year:
        issued_date = {"date-parts": [[entity.release_year]]}

    csl = dict(
        # id,
        # categories
        type=entity.release_type or "article",  # can't be blank
        language=entity.language,
        # journalAbbreviation
        # shortTitle
        ## see below for all contrib roles
        # accessed
        # container
        # event-date
        issued=issued_date,
        # original-date
        # submitted
        abstract=abstract,
        # annote
        # archive
        # archive_location
        # archive-place
        # authority
        # call-number
        # chapter-number
        # citation-number
        # citation-label
        # collection-number
        # collection-title
        container_title=entity.container and entity.container.name,
        # container-title-short
        # dimensions
        DOI=entity.ext_ids.doi,
        # edition
        # event
        # event-place
        # first-reference-note-number
        # genre
        ISBN=entity.ext_ids.isbn13,
        ISSN=entity.container and entity.container.issnl,
        issue=entity.issue,
        # jurisdiction
        # keyword
        # locator
        # medium
        # note
        # number
        # number-of-pages
        # number-of-volumes
        # original-publisher
        # original-publisher-place
        # original-title
        # TODO: page=entity.pages,
        page_first=entity.pages and entity.pages.split("-")[0],
        PMCID=entity.ext_ids.pmcid,
        PMID=entity.ext_ids.pmid,
        publisher=(entity.container and entity.container.publisher) or entity.publisher,
        # publisher-place
        # references
        # reviewed-title
        # scale
        # section
        # source
        # status
        title=entity.title,
        # title-short
        # URL
        # version
        volume=entity.volume,
        # year-suffix
    )
    for role in [
        "author",
        "collection-editor",
        "composer",
        "container-author",
        "director",
        "editor",
        "editorial-director",
        "interviewer",
        "illustrator",
        "original-author",
        "recipient",
        "reviewed-author",
        "translator",
    ]:
        cbr = contribs_by_role(contribs, role)
        if cbr:
            csl[role] = cbr
    # underline-to-dash
    csl["container-title"] = csl.pop("container_title")
    csl["page-first"] = csl.pop("page_first")
    empty_keys = [k for k, v in csl.items() if not v]
    for k in empty_keys:
        csl.pop(k)
    return csl


def refs_to_csl(entity: ReleaseEntity) -> List[Dict[str, Any]]:
    ret = []
    for ref in entity.refs or []:
        if ref.release_id and False:
            # TODO: fetch full entity from API and convert with release_to_csl
            raise NotImplementedError
        else:
            issued_date = None
            if ref.year:
                issued_date = [[ref.year]]
            csl = dict(
                title=ref.title,
                issued=issued_date,
            )
        csl["id"] = (ref.key or ref.index,)  # zero- or one-indexed?
        ret.append(csl)
    return ret


def citeproc_csl(csl_json: Dict[str, Any], style: str, html: bool = False) -> str:
    """
    Renders a release entity to a styled citation.

    Notable styles include:
    - 'csl-json': special case to JSON encode the structured CSL object (via
      release_to_csl())
    - bibtext: multi-line bibtext format (used with LaTeX)

    Returns a string; if the html flag is set, and the style isn't 'csl-json'
    or 'bibtex', it will be HTML. Otherwise plain text.
    """
    if not csl_json.get("id"):
        csl_json["id"] = "unknown"
    if style == "csl-json":
        return json.dumps(csl_json)
    bib_src = CiteProcJSON([csl_json])
    form = formatter.plain
    if html:
        form = formatter.html
    style_path = get_style_filepath(style)
    bib_style = CitationStylesStyle(style_path, validate=False)
    bib = CitationStylesBibliography(bib_style, bib_src, form)
    bib.register(Citation([CitationItem(csl_json["id"])]))
    lines = bib.bibliography()[0]
    if style == "bibtex":
        out = ""
        for line in lines:
            if line.startswith(" @"):
                out += "@"
            elif line.startswith(" "):
                out += "\n " + line
            else:
                out += line
        return "".join(out)
    else:
        return "".join(lines)